1*61046927SAndroid Build Coastguard Worker#!/usr/bin/env python3 2*61046927SAndroid Build Coastguard Worker# Copyright © 2019 Intel Corporation 3*61046927SAndroid Build Coastguard Worker 4*61046927SAndroid Build Coastguard Worker# Permission is hereby granted, free of charge, to any person obtaining a copy 5*61046927SAndroid Build Coastguard Worker# of this software and associated documentation files (the "Software"), to deal 6*61046927SAndroid Build Coastguard Worker# in the Software without restriction, including without limitation the rights 7*61046927SAndroid Build Coastguard Worker# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8*61046927SAndroid Build Coastguard Worker# copies of the Software, and to permit persons to whom the Software is 9*61046927SAndroid Build Coastguard Worker# furnished to do so, subject to the following conditions: 10*61046927SAndroid Build Coastguard Worker 11*61046927SAndroid Build Coastguard Worker# The above copyright notice and this permission notice shall be included in 12*61046927SAndroid Build Coastguard Worker# all copies or substantial portions of the Software. 13*61046927SAndroid Build Coastguard Worker 14*61046927SAndroid Build Coastguard Worker# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15*61046927SAndroid Build Coastguard Worker# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16*61046927SAndroid Build Coastguard Worker# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17*61046927SAndroid Build Coastguard Worker# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18*61046927SAndroid Build Coastguard Worker# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19*61046927SAndroid Build Coastguard Worker# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20*61046927SAndroid Build Coastguard Worker# SOFTWARE. 21*61046927SAndroid Build Coastguard Worker 22*61046927SAndroid Build Coastguard Worker"""This script reads a meson build directory and gives back the command line it 23*61046927SAndroid Build Coastguard Workerwas configured with. 24*61046927SAndroid Build Coastguard Worker 25*61046927SAndroid Build Coastguard WorkerThis only works for meson 0.49.0 and newer. 26*61046927SAndroid Build Coastguard Worker""" 27*61046927SAndroid Build Coastguard Worker 28*61046927SAndroid Build Coastguard Workerimport argparse 29*61046927SAndroid Build Coastguard Workerimport ast 30*61046927SAndroid Build Coastguard Workerimport configparser 31*61046927SAndroid Build Coastguard Workerimport pathlib 32*61046927SAndroid Build Coastguard Workerimport sys 33*61046927SAndroid Build Coastguard Worker 34*61046927SAndroid Build Coastguard Worker 35*61046927SAndroid Build Coastguard Workerdef parse_args() -> argparse.Namespace: 36*61046927SAndroid Build Coastguard Worker """Parse arguments.""" 37*61046927SAndroid Build Coastguard Worker parser = argparse.ArgumentParser() 38*61046927SAndroid Build Coastguard Worker parser.add_argument( 39*61046927SAndroid Build Coastguard Worker 'build_dir', 40*61046927SAndroid Build Coastguard Worker help='Path the meson build directory') 41*61046927SAndroid Build Coastguard Worker args = parser.parse_args() 42*61046927SAndroid Build Coastguard Worker return args 43*61046927SAndroid Build Coastguard Worker 44*61046927SAndroid Build Coastguard Worker 45*61046927SAndroid Build Coastguard Workerdef load_config(path: pathlib.Path) -> configparser.ConfigParser: 46*61046927SAndroid Build Coastguard Worker """Load config file.""" 47*61046927SAndroid Build Coastguard Worker conf = configparser.ConfigParser() 48*61046927SAndroid Build Coastguard Worker with path.open() as f: 49*61046927SAndroid Build Coastguard Worker conf.read_file(f) 50*61046927SAndroid Build Coastguard Worker return conf 51*61046927SAndroid Build Coastguard Worker 52*61046927SAndroid Build Coastguard Worker 53*61046927SAndroid Build Coastguard Workerdef build_cmd(conf: configparser.ConfigParser) -> str: 54*61046927SAndroid Build Coastguard Worker """Rebuild the command line.""" 55*61046927SAndroid Build Coastguard Worker args = [] 56*61046927SAndroid Build Coastguard Worker for k, v in conf['options'].items(): 57*61046927SAndroid Build Coastguard Worker if ' ' in v: 58*61046927SAndroid Build Coastguard Worker args.append(f'-D{k}="{v}"') 59*61046927SAndroid Build Coastguard Worker else: 60*61046927SAndroid Build Coastguard Worker args.append(f'-D{k}={v}') 61*61046927SAndroid Build Coastguard Worker 62*61046927SAndroid Build Coastguard Worker cf = conf['properties'].get('cross_file') 63*61046927SAndroid Build Coastguard Worker if cf: 64*61046927SAndroid Build Coastguard Worker args.append('--cross-file={}'.format(cf)) 65*61046927SAndroid Build Coastguard Worker nf = conf['properties'].get('native_file') 66*61046927SAndroid Build Coastguard Worker if nf: 67*61046927SAndroid Build Coastguard Worker # this will be in the form "['str', 'str']", so use ast.literal_eval to 68*61046927SAndroid Build Coastguard Worker # convert it to a list of strings. 69*61046927SAndroid Build Coastguard Worker nf = ast.literal_eval(nf) 70*61046927SAndroid Build Coastguard Worker args.extend(['--native-file={}'.format(f) for f in nf]) 71*61046927SAndroid Build Coastguard Worker return ' '.join(args) 72*61046927SAndroid Build Coastguard Worker 73*61046927SAndroid Build Coastguard Worker 74*61046927SAndroid Build Coastguard Workerdef main(): 75*61046927SAndroid Build Coastguard Worker args = parse_args() 76*61046927SAndroid Build Coastguard Worker path = pathlib.Path(args.build_dir, 'meson-private', 'cmd_line.txt') 77*61046927SAndroid Build Coastguard Worker if not path.exists(): 78*61046927SAndroid Build Coastguard Worker print('Cannot find the necessary file to rebuild command line. ' 79*61046927SAndroid Build Coastguard Worker 'Is your meson version >= 0.49.0?', file=sys.stderr) 80*61046927SAndroid Build Coastguard Worker sys.exit(1) 81*61046927SAndroid Build Coastguard Worker 82*61046927SAndroid Build Coastguard Worker conf = load_config(path) 83*61046927SAndroid Build Coastguard Worker cmd = build_cmd(conf) 84*61046927SAndroid Build Coastguard Worker print(cmd) 85*61046927SAndroid Build Coastguard Worker 86*61046927SAndroid Build Coastguard Worker 87*61046927SAndroid Build Coastguard Workerif __name__ == '__main__': 88*61046927SAndroid Build Coastguard Worker main() 89