From c100249b70124c5640fb2cf1e5713f1deba5f11d Mon Sep 17 00:00:00 2001 From: David Hampton Date: Thu, 14 Nov 2019 15:26:06 -0500 Subject: [PATCH] Rework code to build the compdb for clang-tidy. --- mythtv/Makefile | 7 +--- mythtv/programs/scripts/build_compdb.py | 56 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) create mode 100755 mythtv/programs/scripts/build_compdb.py diff --git a/mythtv/Makefile b/mythtv/Makefile index 0189d72d45c..d6fef68cecf 100644 --- a/mythtv/Makefile +++ b/mythtv/Makefile @@ -121,9 +121,4 @@ cleanall: clean compdb: @echo "Building ../compile_commands.json" - @JFILES="$(shell find .. -name *.o.json \ - | grep -v /external/ \ - | grep -v /moc_ \ - | grep -v dummy.o.json)"; \ - sed -e '1s/^/[\n/' -e '$$s/,$$/\n]/' $$JFILES > ../compile_commands.json - @echo "If the output looks wrong, did you remember to clear ccache?" + @programs/scripts/build_compdb.py diff --git a/mythtv/programs/scripts/build_compdb.py b/mythtv/programs/scripts/build_compdb.py new file mode 100755 index 00000000000..2a59500fdd7 --- /dev/null +++ b/mythtv/programs/scripts/build_compdb.py @@ -0,0 +1,56 @@ +#! /usr/bin/python3 + +from pathlib import Path +import json +from optparse import OptionParser + +parser = OptionParser() +parser.add_option("-v", "--verbose", + action="store_true", dest="verbose", + help="Print progress messages") +(options, args) = parser.parse_args() + +paths = list(Path("..").glob("**/*.o.json")) +if len(paths) < 600: + print("Not enough *.o.json files present. Did you remember to") + print("clean ccache and do a full build with clang?") + exit(1) + +outlines = [] +for path in paths: + filename = path.as_posix() + badnames = ["/external/", "moc_", "dummy.o.json"] + if any(bad in filename for bad in badnames): + if options.verbose: + print("Skipping input file: {0:s}".format(path.as_posix())) + continue + in_file = open(path, "r") + if not in_file: + if options.verbose: + print("Cannot open input file: {0:s}".format(filename)) + continue + + # Read and strip trailing comma + rawcontent = in_file.read() + if len(rawcontent) < 2: + print("Bad file contents: {0:s}".format(filename)) + exit(1) + content = json.loads(rawcontent[:-2]) + in_file.close() + + # Manipulate file names + if content["file"].startswith("../") and not content["file"].endswith("Json.cpp"): + if options.verbose: + print("Skipping input file2: {0:s}".format(filename)) + continue + content["file"] = content["directory"] + "/" + content["file"] + + # Save the output for this file + if options.verbose: + print("Including input file: {0:s}".format(path.as_posix())) + outlines.append(json.dumps(content, indent=4)) + +# Dump out the compdb file +out_file = open("../compile_commands.json", "w") +print("[\n{0:s}\n]".format(",\n".join(outlines)), file=out_file) +out_file.close()