|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import sys |
| 6 | + |
| 7 | +class ModuleNotFoundError(Exception): |
| 8 | + def __init__(self, module_name): |
| 9 | + self.module_name = module_name |
| 10 | + |
| 11 | +class FullDeps: |
| 12 | + def __init__(self): |
| 13 | + self.modules = dict() |
| 14 | + self.translation_units = str() |
| 15 | + |
| 16 | +def getModulePathArgs(modules, full_deps): |
| 17 | + cmd = [] |
| 18 | + for md in modules: |
| 19 | + m = full_deps.modules[md['module-name'] + '-' + md['context-hash']] |
| 20 | + cmd += [u'-fmodule-map-file=' + m['clang-modulemap-file']] |
| 21 | + cmd += [u'-fmodule-file=' + md['module-name'] + '-' + md['context-hash'] + '.pcm'] |
| 22 | + return cmd |
| 23 | + |
| 24 | +def getCommandLineForModule(module_name, full_deps): |
| 25 | + for m in full_deps.modules.values(): |
| 26 | + if m['name'] == module_name: |
| 27 | + module = m |
| 28 | + break |
| 29 | + else: |
| 30 | + raise ModuleNotFoundError(module_name) |
| 31 | + |
| 32 | + cmd = m['command-line'] |
| 33 | + cmd += getModulePathArgs(m['clang-module-deps'], full_deps) |
| 34 | + cmd += [u'-o', m['name'] + '-' + m['context-hash'] + '.pcm'] |
| 35 | + cmd += [m['clang-modulemap-file']] |
| 36 | + |
| 37 | + return cmd |
| 38 | + |
| 39 | +def getCommandLineForTU(tu, full_deps): |
| 40 | + cmd = tu['command-line'] |
| 41 | + cmd = [a for a in cmd if not (a.startswith('-fmodule-map-file=') or a.startswith('-fmodule-file='))] |
| 42 | + cmd += getModulePathArgs(tu['clang-module-deps'], full_deps) |
| 43 | + return cmd |
| 44 | + |
| 45 | +def parseFullDeps(json): |
| 46 | + ret = FullDeps() |
| 47 | + for m in json['modules']: |
| 48 | + ret.modules[m['name'] + '-' + m['context-hash']] = m |
| 49 | + ret.translation_units = json['translation-units'] |
| 50 | + return ret |
| 51 | + |
| 52 | +def main(): |
| 53 | + parser = argparse.ArgumentParser() |
| 54 | + parser.add_argument("full_deps_file", help="Path to the full dependencies json file", |
| 55 | + type=str) |
| 56 | + action = parser.add_mutually_exclusive_group(required=True) |
| 57 | + action.add_argument("--module-name", help="The name of the module to get arguments for", |
| 58 | + type=str) |
| 59 | + action.add_argument("--tu-index", help="The index of the translation unit to get arguments for", |
| 60 | + type=int) |
| 61 | + args = parser.parse_args() |
| 62 | + |
| 63 | + full_deps = parseFullDeps(json.load(open(args.full_deps_file, 'r'))) |
| 64 | + |
| 65 | + try: |
| 66 | + if args.module_name: |
| 67 | + print(" ".join(getCommandLineForModule(args.module_name, full_deps))) |
| 68 | + |
| 69 | + elif args.tu_index != None: |
| 70 | + print(" ".join(getCommandLineForTU(full_deps.translation_units[args.tu_index], full_deps))) |
| 71 | + except: |
| 72 | + print("Unexpected error:", sys.exc_info()[0]) |
| 73 | + raise |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + main() |
0 commit comments