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