|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | +import shlex |
| 7 | +import shutil |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | + |
| 11 | + |
| 12 | +def split_command(value): |
| 13 | + if not value: |
| 14 | + return None |
| 15 | + value = value.strip() |
| 16 | + if not value: |
| 17 | + return None |
| 18 | + return shlex.split(value, posix=(os.name != 'nt')) |
| 19 | + |
| 20 | + |
| 21 | +def find_compiler(): |
| 22 | + for var in ('CC', 'CXX'): |
| 23 | + command = split_command(os.environ.get(var)) |
| 24 | + if command and shutil.which(command[0]): |
| 25 | + return command |
| 26 | + |
| 27 | + for name in ('cl.exe', 'cl', 'clang-cl.exe', 'clang-cl', 'cc', 'clang', 'gcc'): |
| 28 | + path = shutil.which(name) |
| 29 | + if path: |
| 30 | + return [path] |
| 31 | + |
| 32 | + raise RuntimeError('Unable to locate a compiler for preprocessing assembly') |
| 33 | + |
| 34 | + |
| 35 | +def normalize_path(value): |
| 36 | + return str(value).strip().strip('"') |
| 37 | + |
| 38 | + |
| 39 | +def unique_paths(paths): |
| 40 | + seen = set() |
| 41 | + result = [] |
| 42 | + for path in paths: |
| 43 | + if path in seen: |
| 44 | + continue |
| 45 | + seen.add(path) |
| 46 | + result.append(path) |
| 47 | + return result |
| 48 | + |
| 49 | + |
| 50 | +def preprocess(args): |
| 51 | + compiler = find_compiler() |
| 52 | + input_path = normalize_path(args.input) |
| 53 | + output = Path(normalize_path(args.output)) |
| 54 | + include_dirs = [normalize_path(include_dir) for include_dir in args.include_dir] |
| 55 | + include_dirs.append(str(output.parent)) |
| 56 | + include_dirs = unique_paths(include_dirs) |
| 57 | + output.parent.mkdir(parents=True, exist_ok=True) |
| 58 | + |
| 59 | + if os.name == 'nt' and Path(compiler[0]).name.lower() in ('cl.exe', 'cl', 'clang-cl.exe', 'clang-cl'): |
| 60 | + command = compiler + ['/nologo', '/EP', '/TC'] |
| 61 | + command += [f'/I{include_dir}' for include_dir in include_dirs] |
| 62 | + command += [f'/D{define}' for define in args.define] |
| 63 | + command += [input_path] |
| 64 | + else: |
| 65 | + command = compiler + ['-E', '-P', '-x', 'c'] |
| 66 | + command += [f'-I{include_dir}' for include_dir in include_dirs] |
| 67 | + command += [f'-D{define}' for define in args.define] |
| 68 | + command += [input_path] |
| 69 | + |
| 70 | + result = subprocess.run(command, capture_output=True, text=True) |
| 71 | + if result.returncode != 0: |
| 72 | + sys.stderr.write(result.stderr) |
| 73 | + raise RuntimeError(f'Preprocessing failed: {" ".join(command)}') |
| 74 | + |
| 75 | + output.write_text(result.stdout, encoding='utf-8') |
| 76 | + |
| 77 | + |
| 78 | +def main(argv=None): |
| 79 | + parser = argparse.ArgumentParser(description='Preprocess libffi assembly source') |
| 80 | + parser.add_argument('--input', required=True) |
| 81 | + parser.add_argument('--output', required=True) |
| 82 | + parser.add_argument('--include-dir', action='append', default=[]) |
| 83 | + parser.add_argument('--define', action='append', default=[]) |
| 84 | + args = parser.parse_args(argv) |
| 85 | + |
| 86 | + preprocess(args) |
| 87 | + return 0 |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == '__main__': |
| 91 | + raise SystemExit(main()) |
0 commit comments