[PATCH] D147441: [CMake] Attempt to merge runtimes and projects compilation databases

Joseph Huber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 3 06:33:45 PDT 2023


jhuber6 created this revision.
jhuber6 added reviewers: jdoerfert, tianshilei1992, ye-luo, MaskRay, fhahn, aaron.ballman, phosek.
Herald added subscribers: ekilmer, StephenFan.
Herald added a project: All.
jhuber6 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147441

Files:
  llvm/runtimes/CMakeLists.txt
  llvm/utils/concat-compile-commands.py


Index: llvm/utils/concat-compile-commands.py
===================================================================
--- /dev/null
+++ llvm/utils/concat-compile-commands.py
@@ -0,0 +1,35 @@
+#!/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='Path to a directory where generated HTML files will be output. '
+             'If the directory does not already exist, it will be created. '
+             '"%(default)s" by default.')
+    
+    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)
+
+    print("\nI have made the file here\n")
+    print(args.output)
+
+if __name__ == '__main__':
+    main()
Index: llvm/runtimes/CMakeLists.txt
===================================================================
--- llvm/runtimes/CMakeLists.txt
+++ 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()


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147441.510481.patch
Type: text/x-patch
Size: 2385 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230403/ea5794da/attachment.bin>


More information about the llvm-commits mailing list