#!/usr/bin/env python31# Copyright © 2019 Intel Corporation23# Permission is hereby granted, free of charge, to any person obtaining a copy4# of this software and associated documentation files (the "Software"), to deal5# in the Software without restriction, including without limitation the rights6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell7# copies of the Software, and to permit persons to whom the Software is8# furnished to do so, subject to the following conditions:910# The above copyright notice and this permission notice shall be included in11# all copies or substantial portions of the Software.1213# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE19# SOFTWARE.2021"""This script reads a meson build directory and gives back the command line it22was configured with.2324This only works for meson 0.49.0 and newer.25"""2627import argparse28import ast29import configparser30import pathlib31import sys323334def parse_args() -> argparse.Namespace:35"""Parse arguments."""36parser = argparse.ArgumentParser()37parser.add_argument(38'build_dir',39help='Path the meson build directory')40args = parser.parse_args()41return args424344def load_config(path: pathlib.Path) -> configparser.ConfigParser:45"""Load config file."""46conf = configparser.ConfigParser()47with path.open() as f:48conf.read_file(f)49return conf505152def build_cmd(conf: configparser.ConfigParser) -> str:53"""Rebuild the command line."""54args = []55for k, v in conf['options'].items():56if ' ' in v:57args.append(f'-D{k}="{v}"')58else:59args.append(f'-D{k}={v}')6061cf = conf['properties'].get('cross_file')62if cf:63args.append('--cross-file={}'.format(cf))64nf = conf['properties'].get('native_file')65if nf:66# this will be in the form "['str', 'str']", so use ast.literal_eval to67# convert it to a list of strings.68nf = ast.literal_eval(nf)69args.extend(['--native-file={}'.format(f) for f in nf])70return ' '.join(args)717273def main():74args = parse_args()75path = pathlib.Path(args.build_dir, 'meson-private', 'cmd_line.txt')76if not path.exists():77print('Cannot find the necessary file to rebuild command line. '78'Is your meson version >= 0.49.0?', file=sys.stderr)79sys.exit(1)8081conf = load_config(path)82cmd = build_cmd(conf)83print(cmd)848586if __name__ == '__main__':87main()888990