Debian Codegraph @

dev-scripts/generate_test_compile_commands.py

raw
1 #!/usr/bin/env python3
2
3 """
4 Generates tests/compile_commands.json file. This file is specific to the respective setup and
5 allows a LSP to provide proper autocompletion for the testfiles.
6
7 The generated file should NOT be versioned!
8 """
9
10 import os
11 import glob
12 import json
13 import shutil
14 import subprocess
15 import re
16
17 SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
18 ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, ".."))
19 CLANG = shutil.which("clang")
20
21 qt_headers = subprocess.check_output(["qmake", "-query", "QT_INSTALL_HEADERS"], stderr=subprocess.STDOUT, text=True).strip()
22
23 result = subprocess.run(['clang', '-v'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True)
24 match = re.search(r'Selected .* installation: (.*)', result.stderr)
25 c_headerpath = match.group(1).strip()
26
27 COMMON_FLAGS = f"-Wno-unused-value -Qunused-arguments -std=c++17 -isystem {c_headerpath} -isystem {qt_headers}"
28
29 tests_dir = os.path.join(ROOT, "tests")
30 output_path = os.path.join(tests_dir, "compile_commands.json")
31
32 pattern1 = os.path.join(tests_dir, "**", "*.cpp")
33 pattern2 = os.path.join(tests_dir, "*.cpp")
34
35 files = glob.glob(pattern1, recursive=True) + glob.glob(pattern2)
36 files = [f for f in files if os.path.isfile(f)]
37
38 entries = []
39
40 for f in files:
41 entry = {
42 "directory": ROOT,
43 "command": f"{CLANG} {COMMON_FLAGS} -c {f}",
44 "file": f
45 }
46 entries.append(entry)
47
48 with open(output_path, "w") as outfile:
49 json.dump(entries, outfile, indent=2)