|
17 | 17 | """Generates a compile_commands.json file at $(bazel info workspace) for |
18 | 18 | libclang based tools. |
19 | 19 |
|
20 | | -This is modified from |
| 20 | +Derived from |
21 | 21 | https://github.com/grailbio/bazel-compilation-database/blob/08d706d3cf7daf3d529a26ca76d75da1a3eae6c0/generate.py |
22 | 22 | """ |
23 | 23 |
|
|
29 | 29 | import tempfile |
30 | 30 |
|
31 | 31 |
|
32 | | -# _BAZEL = os.getenv("BAZEL_COMPDB_BAZEL_PATH") or "bazel" |
33 | | - |
34 | | -# def bazel_info(): |
35 | | -# """Returns a dict containing key values from bazel info.""" |
36 | | - |
37 | | -# bazel_info_dict = dict() |
38 | | -# try: |
39 | | -# out = subprocess.check_output([_BAZEL, 'info', 'execution_root', 'workspace', 'bazel-bin']).decode('utf-8').strip().split('\n') |
40 | | -# except subprocess.CalledProcessError as err: |
41 | | -# # This exit code is returned when this command is run outside of a bazel workspace. |
42 | | -# if err.returncode == 2: |
43 | | -# sys.exit(0) |
44 | | -# sys.exit(err.returncode) |
45 | | - |
46 | | -# for line in out: |
47 | | -# key_val = line.strip().partition(": ") |
48 | | -# bazel_info_dict[key_val[0]] = key_val[2] |
49 | | - |
50 | | -# return bazel_info_dict |
51 | | - |
52 | 32 | if __name__ == "__main__": |
53 | 33 | ## |
54 | 34 | ## Setup Args |
|
60 | 40 | help="build events json file from the compilation aspect") |
61 | 41 | args = parser.parse_args() |
62 | 42 |
|
63 | | - ## |
64 | | - ## Setup Bazel Metadata |
65 | | - ## |
66 | | - # print("Gathering bazel info...") |
67 | | - # bazel_info_dict = bazel_info() |
68 | | - # bazel_exec_root = bazel_info_dict['execution_root'] |
69 | | - # bazel_workspace = bazel_info_dict['workspace'] |
70 | | - |
71 | | - # want 'bazel-out/darwin-fastbuild/bin' |
72 | | - # bazel_bin = bazel_info_dict['bazel-bin'] |
73 | | - # if bazel_bin.startswith(bazel_exec_root): |
74 | | - # bazel_bin = bazel_bin[len(bazel_exec_root)+1:] |
75 | | - |
76 | 43 | ## |
77 | 44 | ## Parse Build Events |
78 | 45 | ## |
79 | 46 | print("Gathering output files...") |
| 47 | + |
80 | 48 | local_exec_root = '__EXEC_ROOT__' |
81 | 49 | workspace_directory = '__WORKSPACE__' |
82 | | - build_events = [] |
83 | 50 | bazel_stderr = [] |
84 | 51 | with open(args.build_events_json_file, 'r') as f: |
85 | 52 | for line in f: |
86 | | - # print(line) |
87 | 53 | event = json.loads(line) |
88 | 54 | if 'started' in event: |
89 | 55 | workspace_directory = event['started']['workspaceDirectory'] |
90 | 56 | print("Workspace Directory:", workspace_directory) |
91 | 57 | elif 'progress' in event: |
92 | 58 | if 'stderr' in event['progress']: |
93 | | - # print(event['progress']['stderr']) |
94 | 59 | bazel_stderr.extend(event['progress']['stderr'].splitlines()) |
95 | | - |
96 | 60 | elif 'workspaceInfo' in event: |
97 | 61 | local_exec_root = event['workspaceInfo']['localExecRoot'] |
98 | 62 | print('Execution Root:', local_exec_root) |
99 | 63 |
|
100 | | - build_events.append(event) |
101 | | - |
102 | 64 | compile_command_json_db_files = [] |
103 | 65 | for line in bazel_stderr: |
104 | 66 | if line.endswith('.compile_commands.json'): |
|
107 | 69 | ## |
108 | 70 | ## Collect/Fix/Merge Compilation Databases |
109 | 71 | ## |
110 | | - print("Collecting target databases...") |
| 72 | + print("Preparing compilation database...") |
| 73 | + |
111 | 74 | db_entries = [] |
112 | 75 | for db in compile_command_json_db_files: |
113 | 76 | with open(db, 'r') as f: |
114 | 77 | db_entries.extend(json.load(f)) |
115 | 78 |
|
116 | | - print("Fixing up commands...") |
117 | 79 | def fix_db_entry(db_entry): |
118 | 80 | if 'directory' in db_entry and db_entry['directory'] == '__EXEC_ROOT__': |
119 | 81 | db_entry['directory'] = bazel_workspace if args.source_dir else local_exec_root |
| 82 | + # TODO: research better if this is advantageous |
120 | 83 | # if 'file' in db_entry and db_entry['file'].startswith(bazel_bin): |
121 | 84 | # db_entry['file'] = db_entry['file'][len(bazel_bin)+1:] |
122 | 85 | if 'command' in db_entry: |
123 | 86 | command = db_entry['command'] |
124 | 87 | if command: |
125 | 88 | command = command.replace('-isysroot __BAZEL_XCODE_SDKROOT__', '') |
| 89 | + # -iquote seems to misbehave with vscode |
126 | 90 | command = command.replace('-iquote', '-I') |
127 | 91 | db_entry['command'] = command |
128 | 92 | return db_entry |
129 | 93 | db_entries = list(map(fix_db_entry, db_entries)) |
130 | 94 |
|
131 | 95 | compdb_file = os.path.join(workspace_directory, "compile_commands.json") |
132 | 96 |
|
133 | | - # os.chdir(bazel_workspace) |
134 | | - |
135 | 97 | with open(compdb_file, 'w') as outdb: |
136 | 98 | json.dump(db_entries, outdb, indent=2) |
137 | 99 |
|
|
0 commit comments