diff --git a/llvm/runtimes/CMakeLists.txt b/llvm/runtimes/CMakeLists.txt --- a/llvm/runtimes/CMakeLists.txt +++ b/llvm/runtimes/CMakeLists.txt @@ -530,4 +530,23 @@ set_property(GLOBAL APPEND PROPERTY LLVM_ALL_ADDITIONAL_TEST_TARGETS runtimes ${RUNTIMES_TEST_DEPENDS}) endif() + + # Attempt to merge the runtimes' compile_commands.json file into the main + # file. + if(CMAKE_EXPORT_COMPILE_COMMANDS AND TARGET runtimes AND TARGET runtimes-configure) + set(files_to_concat + ${LLVM_BINARY_DIR}/compile_commands.json + ${LLVM_BINARY_DIR}/runtimes/runtimes-bins/compile_commands.json + ) + set(output_file ${LLVM_BINARY_DIR}/compile_commands.json) + add_custom_command( + OUTPUT ${output_file} + COMMAND "${Python3_EXECUTABLE}" ${LLVM_MAIN_SRC_DIR}/utils/concat-compile-commands.py ${files_to_concat} -o ${output_file} + COMMENT "Merging the LLVM runtimes 'compile_commands.json' file" + DEPENDS runtimes-configure ${LLVM_BINARY_DIR}/runtimes/runtimes-bins/compile_commands.json + VERBATIM + ) + add_custom_target(merge-compile-commands DEPENDS ${output_file}) + add_dependencies(runtimes merge-compile-commands) + endif() endif() diff --git a/llvm/utils/concat-compile-commands.py b/llvm/utils/concat-compile-commands.py new file mode 100755 --- /dev/null +++ b/llvm/utils/concat-compile-commands.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +import argparse +import json + +desc = '''Concatenate JSON files generated by CMake tools.''' + +def main(): + parser = argparse.ArgumentParser(description=desc) + parser.add_argument( + 'json_files', + nargs='+', + help='list of JSON files to concatenate.') + parser.add_argument( + '--output', + '-o', + required=True, + help='Filename to output the merged JSON file to.') + + data = [] + args = parser.parse_args() + for json_file in args.json_files: + with open(json_file) as file: + data.extend(json.load(file)) + + with open(args.output, 'w') as file: + json.dump(data, file, indent=2) + +if __name__ == '__main__': + main()