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