File: generate_test_compile_commands.py

package info (click to toggle)
clazy 1.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,248 kB
  • sloc: cpp: 23,552; python: 1,450; xml: 450; sh: 237; makefile: 46
file content (49 lines) | stat: -rwxr-xr-x 1,486 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3

"""
Generates tests/compile_commands.json file. This file is specific to the respective setup and
allows a LSP to provide proper autocompletion for the testfiles.

The generated file should NOT be versioned!
"""

import os
import glob
import json
import shutil
import subprocess
import re

SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, ".."))
CLANG = shutil.which("clang")

qt_headers = subprocess.check_output(["qmake", "-query", "QT_INSTALL_HEADERS"], stderr=subprocess.STDOUT, text=True).strip()

result = subprocess.run(['clang', '-v'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True)
match = re.search(r'Selected .* installation: (.*)', result.stderr)
c_headerpath = match.group(1).strip()

COMMON_FLAGS = f"-Wno-unused-value -Qunused-arguments -std=c++17 -isystem {c_headerpath} -isystem {qt_headers}"

tests_dir = os.path.join(ROOT, "tests")
output_path = os.path.join(tests_dir, "compile_commands.json")

pattern1 = os.path.join(tests_dir, "**", "*.cpp")
pattern2 = os.path.join(tests_dir, "*.cpp")

files = glob.glob(pattern1, recursive=True) + glob.glob(pattern2)
files = [f for f in files if os.path.isfile(f)]

entries = []

for f in files:
    entry = {
        "directory": ROOT,
        "command": f"{CLANG} {COMMON_FLAGS} -c {f}",
        "file": f
    }
    entries.append(entry)

with open(output_path, "w") as outfile:
    json.dump(entries, outfile, indent=2)