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