Skip to content

Instantly share code, notes, and snippets.

@thekvs
Last active June 13, 2019 17:34
Show Gist options
  • Save thekvs/9bff4e53af70418131059eacef1c35a4 to your computer and use it in GitHub Desktop.
Save thekvs/9bff4e53af70418131059eacef1c35a4 to your computer and use it in GitHub Desktop.
flatten @path/to/file.rsp entries in compile_commands.json file so that it can be used by clangd
import argparse
import json
import copy
import sys
import os.path
import traceback
def parse_args():
parser = argparse.ArgumentParser(
description="Expand compile_commands.json file")
parser.add_argument("--json", type=str, metavar="arg", required=True,
help="path to compile_commands.json file")
parser.add_argument("--result", type=str, metavar="arg", required=True,
help="path to a new compile_commands.json file")
args = parser.parse_args()
return args
def main():
try:
args = parse_args()
base_dir = os.path.split(args.json)[0]
data = open(args.json, "r")
js = json.load(data)
new_js = []
for entry in js:
new_entry = copy.deepcopy(entry)
cmd = entry["command"]
new_cmd = []
items = [entry for entry in cmd.split(" ") if entry]
for item in items:
if item.startswith("@") and item.endswith(".rsp"):
rsp = open(os.path.join(base_dir, item[1:]), "r")
lines = " ".join([l.strip() for l in rsp.readlines()])
new_cmd.append(lines)
else:
new_cmd.append(item)
new_entry["command"] = " ".join(new_cmd)
new_js.append(new_entry)
result = open(args.result, "w")
result.write(json.dumps(new_js, indent=2))
except Exception as exc:
print(exc, file=sys.stderr)
print("===", file=sys.stderr)
print(traceback.format_exc(), file=sys.stderr)
sys.exit(-1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment