Skip to content

Instantly share code, notes, and snippets.

@ichizok
Last active April 12, 2026 10:20
Show Gist options
  • Select an option

  • Save ichizok/e567a0094cf625687d560c8600e470e7 to your computer and use it in GitHub Desktop.

Select an option

Save ichizok/e567a0094cf625687d560c8600e470e7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import json
import subprocess
import sys
from argparse import ArgumentParser
from os import path
from tempfile import TemporaryDirectory
def json_dump(obj, filename):
with open(filename, "w") as f:
json.dump(obj, f, indent=2)
f.write("\n")
def compiledb(work_dir, build_log):
compile_commands_json = path.join(work_dir, "compile_commands.json")
with open(build_log) as f:
subprocess.run(["compiledb", "-o", compile_commands_json], stdin=f, check=True)
with open(compile_commands_json) as f:
return json.load(f)
def main():
parser = ArgumentParser()
parser.add_argument("build_log", nargs="?", type=str)
args = parser.parse_args()
with TemporaryDirectory() as work_dir:
if args.build_log is None or args.build_log == "-":
build_log = path.join(work_dir, "build.log")
with open(build_log, "w") as f:
for line in sys.stdin:
f.write(line)
sys.stdout.write(line)
else:
build_log = args.build_log
if not path.isfile(build_log):
raise FileNotFoundError(f"File not found: {build_log}")
r = compiledb(work_dir, build_log)
json_dump(r, "compile_commands.json")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment