Skip to content

Instantly share code, notes, and snippets.

@vmiheer
Created September 23, 2020 20:18
Show Gist options
  • Save vmiheer/81e07c4df75177d0cfaa12696bc317b9 to your computer and use it in GitHub Desktop.
Save vmiheer/81e07c4df75177d0cfaa12696bc317b9 to your computer and use it in GitHub Desktop.
# fix clang compilation database in the cases where one compiler uses another host compiler
# e.g. nvcc/icc using gcc headers
from subprocess import Popen, PIPE
from typing import List
import re, json
startRe = re.compile(r"#include [\"<][.]{3}[\">] search starts here:")
endRe = re.compile(r"^End of search list.$")
def getDefaultIncludes():
p1 = Popen(["echo", ""], stdout=PIPE)
p2 = Popen(["/uufs/chpc.utah.edu/sys/installdir/gcc/9.2.0/bin/g++", "-", "-E", "-v"], stdin=p1.stdout, stdout=PIPE, stderr=PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
out = p2.communicate()[1]
start = False
includes = []
for line in out.decode().split('\n'):
if endRe.match(line):
break
if startRe.match(line):
start = True
continue
if start:
includes.append(line.strip())
return (includes)
commands = json.load(open('compile_commands.json'))
additionToCommandLine = "".join([" -I " + x for x in getDefaultIncludes()])
for target in commands:
target['command'] = target['command'] + additionToCommandLine
with open('compile_commands.json', 'w') as json_file:
json.dump(commands, json_file, indent=' ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment