Skip to content

Commit 054f914

Browse files
authored
[Runtimes] Merge 'compile_commands.json' files from runtimes build (llvm#116303)
Summary: When building a project in a runtime mode, the compilation database is a separate CMake invocation. So its `compile_commands.json` file will be placed elsewhere in the `runtimes/runtime-bins` directory. This is somewhat annoying for ongoing development when a runtimes build is necessary. This patch adds some CMake magic to merge the two files.
1 parent a24aa7d commit 054f914

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

llvm/utils/merge-json.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
"""A command line utility to merge two JSON files.
3+
4+
This is a python program that merges two JSON files into a single one. The
5+
intended use for this is to combine generated 'compile_commands.json' files
6+
created by CMake when performing an LLVM runtime build.
7+
"""
8+
9+
import argparse
10+
import json
11+
import sys
12+
13+
14+
def main():
15+
parser = argparse.ArgumentParser(description=__doc__)
16+
parser.add_argument(
17+
"-o",
18+
type=str,
19+
help="The output file to write JSON data to",
20+
default=None,
21+
nargs="?",
22+
)
23+
parser.add_argument(
24+
"json_files", type=str, nargs="+", help="Input JSON files to merge"
25+
)
26+
args = parser.parse_args()
27+
28+
merged_data = []
29+
30+
for json_file in args.json_files:
31+
try:
32+
with open(json_file, "r") as f:
33+
data = json.load(f)
34+
merged_data.extend(data)
35+
except (IOError, json.JSONDecodeError) as e:
36+
print("Failed to parse {json_file}: {e}", file=sys.stderr)
37+
continue
38+
39+
# Deduplicate by converting each entry to a tuple of sorted key-value pairs
40+
unique_data = list({json.dumps(entry, sort_keys=True) for entry in merged_data})
41+
unique_data = [json.loads(entry) for entry in unique_data]
42+
43+
with open(args.o, "w") as f:
44+
json.dump(unique_data, f, indent=2)
45+
46+
47+
if __name__ == "__main__":
48+
main()

runtimes/CMakeLists.txt

+18
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,21 @@ if(SUB_COMPONENTS)
346346
${CMAKE_CURRENT_BINARY_DIR}/runtimes/Components.cmake)
347347
endif()
348348
endif()
349+
350+
# If the user requested 'compile_commands.json' we merge the generated JSON from
351+
# the created directories.
352+
if(CMAKE_EXPORT_COMPILE_COMMANDS)
353+
# Make a dependency so that we don't error if the file gets deleted somehow.
354+
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/compile_commands.json
355+
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_BINARY_DIR}/compile_commands.json)
356+
357+
file(TO_NATIVE_PATH "${LLVM_MAIN_SRC_DIR}/utils/merge-json.py" MERGE_JSON_PATH)
358+
add_custom_command(OUTPUT ${LLVM_BINARY_DIR}/compile_commands.json
359+
COMMAND ${CMAKE_COMMAND} -E touch ${LLVM_BINARY_DIR}/compile_commands.json
360+
COMMAND ${Python3_EXECUTABLE} ${MERGE_JSON_PATH}
361+
${LLVM_BINARY_DIR}/compile_commands.json
362+
${CMAKE_BINARY_DIR}/compile_commands.json
363+
-o ${LLVM_BINARY_DIR}/compile_commands.json
364+
DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json)
365+
add_custom_target(merge_runtime_commands ALL DEPENDS ${LLVM_BINARY_DIR}/compile_commands.json)
366+
endif()

0 commit comments

Comments
 (0)