Diff
1diff --git a/.clangd b/.clangd
2index 2be2d817fc7cda7b50c9788e7b1559e4bad67a40..6133ae7229d88ab80806282482b7af2ef7ba9c95 100644
3--- a/.clangd
4+++ b/.clangd
5@@ -1,4 +1,4 @@
6 CompileFlags:
7 Add: [-Wno-unknown-attributes, -Wno-maybe-uninitialized, -Wno-unknown-warning-option]
8- Remove: [-W*, -mcall-prologues]
9+ Remove: [-W*, -mmcu=*, -mcpu=*, -mfpu=*, -mfloat-abi=*, -mno-unaligned-access, -mno-thumb-interwork, -mcall-prologues]
10 Compiler: clang
11diff --git a/lib/python/qmk/cli/generate/compilation_database.py b/lib/python/qmk/cli/generate/compilation_database.py
12index a2190fee66c7492d5d2e181d0567367a309703e9..b9c716bf0c5267a9e8d10bb76d076185952d1e4f 100755
13--- a/lib/python/qmk/cli/generate/compilation_database.py
14+++ b/lib/python/qmk/cli/generate/compilation_database.py
15@@ -25,7 +25,6 @@ def system_libs(binary: str) -> List[Path]:
16 """Find the system include directory that the given build tool uses.
17 """
18 cli.log.debug("searching for system library directory for binary: %s", binary)
19- bin_path = shutil.which(binary)
20
21 # Actually query xxxxxx-gcc to find its include paths.
22 if binary.endswith("gcc") or binary.endswith("g++"):
23@@ -37,7 +36,31 @@ def system_libs(binary: str) -> List[Path]:
24 paths.append(Path(line.strip()).resolve())
25 return paths
26
27- return list(Path(bin_path).resolve().parent.parent.glob("*/include")) if bin_path else []
28+ return list(Path(binary).resolve().parent.parent.glob("*/include")) if binary else []
29+
30+
31+@lru_cache(maxsize=10)
32+def cpu_defines(binary: str, compiler_args: str) -> List[str]:
33+ cli.log.debug("gathering definitions for compilation: %s %s", binary, compiler_args)
34+ if binary.endswith("gcc") or binary.endswith("g++"):
35+ invocation = [binary, '-dM', '-E']
36+ if binary.endswith("gcc"):
37+ invocation.extend(['-x', 'c'])
38+ elif binary.endswith("g++"):
39+ invocation.extend(['-x', 'c++'])
40+ compiler_args = shlex.split(compiler_args)
41+ invocation.extend(compiler_args)
42+ invocation.append('-')
43+ result = cli.run(invocation, capture_output=True, check=True, stdin=None, input='\n')
44+ define_args = []
45+ for line in result.stdout.splitlines():
46+ line_args = line.split(' ', 2)
47+ if len(line_args) == 3 and line_args[0] == '#define':
48+ define_args.append(f'-D{line_args[1]}={line_args[2]}')
49+ elif len(line_args) == 2 and line_args[0] == '#define':
50+ define_args.append(f'-D{line_args[1]}')
51+ return list(sorted(set(define_args)))
52+ return []
53
54
55 file_re = re.compile(r'printf "Compiling: ([^"]+)')
56@@ -68,9 +91,12 @@ def parse_make_n(f: Iterator[str]) -> List[Dict[str, str]]:
57 # we have a hit!
58 this_cmd = m.group(1)
59 args = shlex.split(this_cmd)
60- for s in system_libs(args[0]):
61+ binary = shutil.which(args[0])
62+ compiler_args = set(filter(lambda x: x.startswith('-m') or x.startswith('-f'), args))
63+ for s in system_libs(binary):
64 args += ['-isystem', '%s' % s]
65- new_cmd = ' '.join(shlex.quote(s) for s in args if s != '-mno-thumb-interwork')
66+ args.extend(cpu_defines(binary, ' '.join(shlex.quote(s) for s in compiler_args)))
67+ new_cmd = ' '.join(shlex.quote(s) for s in args)
68 records.append({"directory": str(QMK_FIRMWARE.resolve()), "command": new_cmd, "file": this_file})
69 state = 'start'
70