xref: /llvm-project/llvm/utils/merge-json.py (revision 054f914741421ca9dd1eaa58ea74a20f8695bae6)
1#!/usr/bin/env python
2"""A command line utility to merge two JSON files.
3
4This is a python program that merges two JSON files into a single one. The
5intended use for this is to combine generated 'compile_commands.json' files
6created by CMake when performing an LLVM runtime build.
7"""
8
9import argparse
10import json
11import sys
12
13
14def 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
47if __name__ == "__main__":
48    main()
49