xref: /xnu-12377.81.4/tests/unit/tools/merge_cmds_json.py (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1#!/usr/bin/env python3
2import sys
3import os
4import glob
5
6def main():
7    xnu_root = sys.argv[1]
8    xnu_build_dir = sys.argv[2]
9    tests_obj_dir = sys.argv[3]
10
11    xnu_json = os.path.join(xnu_build_dir, "compile_commands.json")
12    if not os.path.exists(xnu_json):
13        print(f"did not find xnu build json: {xnu_json}")
14        return 0
15    root_json = os.path.join(xnu_root, "compile_commands.json")
16
17    if os.path.exists(root_json):
18        if not os.path.islink(root_json):
19            print(f"root json is not a symlink, not removing it: {root_json}")
20            return 0
21
22    add_text = ""
23    for filename in glob.glob(os.path.join(tests_obj_dir, "*.json")):
24        if filename.endswith("compile_commands.json"):
25            continue
26        print(f"found {filename}")
27        text = open(filename).read()
28        add_text += text
29    add_text = add_text.rstrip()
30    if add_text[-1] == ',':
31        add_text = add_text[:-1]
32
33    if len(add_text) == 0:
34        print(f"did not find any json files in {tests_obj_dir}")
35        return 0
36
37    xnu_j = open(xnu_json).read()
38    if xnu_j[-3:] != "\n]\n":
39        print(f"doesn't look like a legit compile_commands.json: {xnu_json}")
40        return 0
41
42    xnu_j_mod = xnu_j[:-3] + ",\n\n" + add_text + "]\n"
43
44    tests_json = os.path.join(tests_obj_dir, "compile_commands.json")
45    open(tests_json, "w").write(xnu_j_mod)
46    print(f"saved {tests_json}")
47
48    if os.path.exists(root_json):
49        print(f"removing old link {root_json}")
50        os.unlink(root_json)
51    os.symlink(tests_json, root_json)
52    print(f"added link {root_json}")
53
54
55if __name__ == "__main__":
56    sys.exit(main())