File tree Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Expand file tree Collapse file tree 2 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 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+ def main ():
14+ parser = argparse .ArgumentParser (description = __doc__ )
15+ parser .add_argument (
16+ "-o" ,
17+ type = str ,
18+ help = "The output file to write JSON data to" ,
19+ default = None ,
20+ nargs = "?" ,
21+ )
22+ parser .add_argument (
23+ "json_files" , type = str , nargs = "+" , help = "Input JSON files to merge"
24+ )
25+ args = parser .parse_args ()
26+
27+ merged_data = []
28+
29+ for json_file in args .json_files :
30+ try :
31+ with open (json_file , "r" ) as f :
32+ data = json .load (f )
33+ merged_data .extend (data )
34+ except (IOError , json .JSONDecodeError ) as e :
35+ print ("Failed to parse {json_file}: {e}" , file = sys .stderr )
36+ continue
37+
38+ # Deduplicate by converting each entry to a tuple of sorted key-value pairs
39+ unique_data = list ({json .dumps (entry , sort_keys = True ) for entry in merged_data })
40+ unique_data = [json .loads (entry ) for entry in unique_data ]
41+
42+ with open (args .o , "w" ) as f :
43+ json .dump (unique_data , f , indent = 2 )
44+
45+
46+ if __name__ == "__main__" :
47+ main ()
Original file line number Diff line number Diff line change @@ -313,3 +313,21 @@ if(SUB_COMPONENTS)
313313 ${LLVM_BINARY_DIR} /runtimes/Components.cmake)
314314 endif ()
315315endif ()
316+
317+ # If the user requested 'compile_commands.json' we merge the generated JSON from
318+ # the created directories.
319+ if (CMAKE_EXPORT_COMPILE_COMMANDS)
320+ # Make a dependency so that we don't error if the file gets deleted somehow.
321+ add_custom_command (OUTPUT ${CMAKE_BINARY_DIR} /compile_commands.json
322+ COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_BINARY_DIR} /compile_commands.json)
323+
324+ file (TO_NATIVE_PATH "${LLVM_MAIN_SRC_DIR} /utils/merge-json.py" MERGE_JSON_PATH)
325+ add_custom_command (OUTPUT ${LLVM_BINARY_DIR} /compile_commands.json
326+ COMMAND ${CMAKE_COMMAND} -E touch ${LLVM_BINARY_DIR} /compile_commands.json
327+ COMMAND ${Python3_EXECUTABLE} ${MERGE_JSON_PATH}
328+ ${LLVM_BINARY_DIR} /compile_commands.json
329+ ${CMAKE_BINARY_DIR} /compile_commands.json
330+ -o ${LLVM_BINARY_DIR} /compile_commands.json
331+ DEPENDS ${CMAKE_BINARY_DIR} /compile_commands.json)
332+ add_custom_target (merge_runtime_commands ALL DEPENDS ${LLVM_BINARY_DIR} /compile_commands.json)
333+ endif ()
You can’t perform that action at this time.
0 commit comments