Skip to content

Instantly share code, notes, and snippets.

@OlivierLi
Created September 28, 2018 17:33
Show Gist options
  • Save OlivierLi/2c8f2a5e85bc1f3884b696e6fcf3bc59 to your computer and use it in GitHub Desktop.
Save OlivierLi/2c8f2a5e85bc1f3884b696e6fcf3bc59 to your computer and use it in GitHub Desktop.
import json
import sys
import os.path
from shutil import copyfile
from tempfile import mkstemp
import re
import subprocess
# These are simply removed from the overall command.
banned_sub_commands = ['\/nologo ', '\/showIncludes ', 'C:\\\\src\\\\goma\\\\goma-win64\/gomacc.exe ']
# Banned sub_dirs
# banned_sub_dirs = ["third_party"]
# These are the projects we want
_poi = [".*components.*"]
projects_of_interest = [re.compile(p) for p in _poi]
def bail(message):
print(message)
sys.exit(-1)
def main():
# First of all lets generate the compile_commands file
proc = subprocess.Popen('ninja -C ./out/Default build_all -t compdb cxx cc', stdout=subprocess.PIPE)
lines = proc.stdout
values = []
new_values = []
try:
values=json.load(lines)
except ValueError:
bail("Problem loading JSON!")
# Go over all values.
for value in values:
# Remove banned sub commands
command = value["command"]
for banned_sub_command in banned_sub_commands:
command = re.sub(banned_sub_command,'',command)
value["command"] = command
# Correct the slashes in the main part of the command
for key in ["command", "file"]:
# Separate the string
parts = value[key].split()
parts[0] = re.sub("\/",'\\\\', parts[0])
value[key] = " ".join(parts)
# Only keep values of interest.
if any(pattern.match(value["file"]) for pattern in projects_of_interest):
new_values.append(value)
with open("compile_commands.json", 'w+') as out_file:
json.dump(new_values, out_file, indent=4, separators=(',', ': '))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment