xref: /llvm-project/llvm/utils/merge-json.py (revision 2072ec1ff957cb08a054e5ce7a1e916232d3bc6b)
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            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
46if __name__ == "__main__":
47    main()
48