Skip to content

Instantly share code, notes, and snippets.

@klemens-morgenstern
Created April 5, 2023 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klemens-morgenstern/d999dcb11ab208e43f89b2f687f2574e to your computer and use it in GitHub Desktop.
Save klemens-morgenstern/d999dcb11ab208e43f89b2f687f2574e to your computer and use it in GitHub Desktop.
b2 -> compile_commands.json
import subprocess
import re
import sys
import os
import json
import os.path
cwd = os.getcwd()
ll = subprocess.Popen(
sys.argv[1:] + ['-n'],
stdout=subprocess.PIPE
).stdout.readlines()
rc = re.compile("^([^.]+)\.compile\.c(?:\+\+)? ([^\n]+)$")
tc = re.compile('.*"((?:\\"|[^"])+)"$')
res = []
i = 0
while i < len(ll):
m = rc.match(ll[i].decode())
if m:
output = m[2]
i += 2
command = ll[i].strip().decode()
file = tc.match(command)[1]
res.append({
"directory": cwd,
"command": command,
"file": os.path.abspath(file),
"output": os.path.abspath(output)
})
i += 1
json.dump(res, sys.stdout, indent=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment