Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Runtimes] Merge 'compile_commands.json' files from runtimes build #116303

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions llvm/utils/merge-json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need LLVM copyright here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think so, none of the other utility scripts I saw had one.

"""A command line utility to merge two JSON files.

This is a python program that merges two JSON files into a single one. The
intended use for this is to combine generated 'compile_commands.json' files
created by CMake when performing an LLVM runtime build.
"""

import argparse
import json


def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"-o",
type=str,
help="The output file to write JSON data to",
default=None,
nargs="?",
)
parser.add_argument(
"json_files", type=str, nargs="+", help="Input JSON files to merge"
)
args = parser.parse_args()

merged_data = []

for json_file in args.json_files:
try:
with open(json_file, "r") as f:
data = json.load(f)
merged_data.extend(data)
except (IOError, json.JSONDecodeError) as e:
print("Failed to parse {json_file}: {e}", file=sys.stderr)
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably drop a warning or so here


# Deduplicate by converting each entry to a tuple of sorted key-value pairs
unique_data = list({json.dumps(entry, sort_keys=True) for entry in merged_data})
unique_data = [json.loads(entry) for entry in unique_data]

with open(args.o, "w") as f:
json.dump(unique_data, f, indent=2)


if __name__ == "__main__":
main()
15 changes: 15 additions & 0 deletions runtimes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,18 @@ if(SUB_COMPONENTS)
${LLVM_BINARY_DIR}/runtimes/Components.cmake)
endif()
endif()

# If the user requested 'compile_commands.json' we merge the generated JSON from
# the created directories.
if(CMAKE_EXPORT_COMPILE_COMMANDS)
file(TO_NATIVE_PATH "${LLVM_MAIN_SRC_DIR}/utils/merge-json.py" MERGE_JSON_PATH)
add_custom_command(OUTPUT ${LLVM_BINARY_DIR}/compile_commands.json
COMMAND ${CMAKE_COMMAND} -E touch ${LLVM_BINARY_DIR}/compile_commands.json
COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_BINARY_DIR}/compile_commands.json
COMMAND ${Python3_EXECUTABLE} ${MERGE_JSON_PATH}
${LLVM_BINARY_DIR}/compile_commands.json
${CMAKE_BINARY_DIR}/compile_commands.json
-o ${LLVM_BINARY_DIR}/compile_commands.json
DEPENDS ${CMAKE_BINARY_DIR}/compile_commands.json)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC this custom command will just depend on the main compile_commands.json, which is generated after CMake configuration. However, the runtime build is the last stage of build, so I'm not sure if this dependency can push it back to the end.

Copy link
Contributor Author

@jhuber6 jhuber6 Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm not 100% positive on how this will work out. When I test it under normal circumstances (fresh build and modification of files) it works fine. however if I manually delete the main compile_commands.json it will get into a dumb state where it keeps thinking it's not there.

add_custom_target(merge_runtime_commands ALL DEPENDS ${LLVM_BINARY_DIR}/compile_commands.json)
endif()
Loading