| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import json |
| 4 |
import os |
| 5 |
import subprocess |
| 6 |
import sys |
| 7 |
|
| 8 |
DATA_DIRNAME = ".lsp.data" |
| 9 |
SUFFIXES = [".c", ".cpp", ".cxx"] |
| 10 |
|
| 11 |
|
| 12 |
def get_topdir() -> str: |
| 13 |
for name in ["TOPDIR", "KBUILD_ABS_SRCTREE"]: |
| 14 |
if top_dir := os.getenv(name): |
| 15 |
return os.path.realpath(top_dir) |
| 16 |
|
| 17 |
dirname = os.getcwd() |
| 18 |
|
| 19 |
while dirname != "/": |
| 20 |
if os.path.isdir(os.path.join(dirname, ".git")): |
| 21 |
return dirname |
| 22 |
dirname = os.path.dirname(dirname) |
| 23 |
|
| 24 |
return "/tmp" |
| 25 |
|
| 26 |
|
| 27 |
def main(): |
| 28 |
args = sys.argv[1:] |
| 29 |
cwd = os.getcwd() |
| 30 |
real_compiler = os.getenv("REAL_CC") |
| 31 |
|
| 32 |
command = [real_compiler, *args] |
| 33 |
file = None |
| 34 |
|
| 35 |
for arg in reversed(args): |
| 36 |
try: |
| 37 |
for suffix in SUFFIXES: |
| 38 |
if arg.endswith(suffix): |
| 39 |
file = os.path.realpath(arg, strict=True) |
| 40 |
break |
| 41 |
if file is not None: |
| 42 |
break |
| 43 |
except FileNotFoundError: |
| 44 |
pass |
| 45 |
|
| 46 |
if file is not None: |
| 47 |
data_dir = os.path.join(get_topdir(), DATA_DIRNAME) |
| 48 |
|
| 49 |
if not os.path.isdir(data_dir): |
| 50 |
os.mkdir(data_dir, 0o700) |
| 51 |
|
| 52 |
entry = { |
| 53 |
"arguments": command, |
| 54 |
"directory": cwd, |
| 55 |
"file": os.path.realpath(file), |
| 56 |
} |
| 57 |
|
| 58 |
cmd_file = os.path.join(data_dir, file.replace("/", "_") + ".json") |
| 59 |
|
| 60 |
with open(cmd_file, "w") as fh: |
| 61 |
fh.write(json.dumps(entry, indent=4)) |
| 62 |
|
| 63 |
ret = subprocess.run(command) |
| 64 |
sys.exit(ret.returncode) |
| 65 |
|
| 66 |
|
| 67 |
def main_join(): |
| 68 |
top_dir = get_topdir() |
| 69 |
out_file = os.path.join(top_dir, "compile_commands.json") |
| 70 |
|
| 71 |
try: |
| 72 |
outfh = open(out_file, "x+") |
| 73 |
except FileExistsError: |
| 74 |
outfh = open(out_file, "r+") |
| 75 |
|
| 76 |
res = {} |
| 77 |
|
| 78 |
if os.path.getsize(out_file) > 0: |
| 79 |
try: |
| 80 |
json_list = json.load(outfh) |
| 81 |
|
| 82 |
if isinstance(json_list, list): |
| 83 |
for ent in json_list: |
| 84 |
res[ent["file"]] = ent |
| 85 |
|
| 86 |
except json.decoder.JSONDecodeError: |
| 87 |
pass |
| 88 |
|
| 89 |
try: |
| 90 |
data_dir = os.path.join(top_dir, DATA_DIRNAME) |
| 91 |
|
| 92 |
for name in os.listdir(data_dir): |
| 93 |
with open(os.path.join(data_dir, name), "r") as fh: |
| 94 |
try: |
| 95 |
data = json.load(fh) |
| 96 |
res[data["file"]] = data |
| 97 |
|
| 98 |
except json.decoder.JSONDecodeError: |
| 99 |
pass |
| 100 |
|
| 101 |
except FileNotFoundError: |
| 102 |
outfh.close() |
| 103 |
return |
| 104 |
|
| 105 |
if res: |
| 106 |
json_list = sorted(res.values(), key=lambda x: x["file"]) |
| 107 |
|
| 108 |
outfh.seek(0, os.SEEK_SET) |
| 109 |
outfh.truncate(0) |
| 110 |
outfh.write(json.dumps(json_list, indent=4)) |
| 111 |
|
| 112 |
outfh.close() |
| 113 |
return |
| 114 |
|
| 115 |
|
| 116 |
if __name__ == '__main__': |
| 117 |
if sys.argv[0].endswith("_join") or os.getenv("EXPORT_COMPILE_COMMANDS"): |
| 118 |
main_join() |
| 119 |
elif len(sys.argv) > 1: |
| 120 |
main() |
| 121 |
else: |
| 122 |
prog = os.path.basename(sys.argv[0]) |
| 123 |
print( |
| 124 |
f"Usage: {prog} [COMPILER OPTIONS]... [INPUT FILES]...", |
| 125 |
"", |
| 126 |
"Compiler wrapper to save compiler options before compiling", |
| 127 |
"and after create compile_commands.json.", |
| 128 |
"", |
| 129 |
sep="\n" |
| 130 |
) |