From 8da56e7edf43bf64db3db7357830f1f17ed65629 Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Tue, 19 Mar 2024 13:54:17 +1100 Subject: [PATCH 1/2] clangd fixes. - Adds new ignored arguments, which should fix up start-of-file errors complaining about `-mcpu=`, `-mfpu=` etc. - Parses the removed `-mcpu=`, `-mfpu=` etc. args when building the compilation database, ensuring any `#define`s are correctly propagated depending on the MCU in use, as well as its settings. --- .clangd | 2 +- .../qmk/cli/generate/compilation_database.py | 34 ++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/.clangd b/.clangd index 2be2d817fc7c..6133ae7229d8 100644 --- a/.clangd +++ b/.clangd @@ -1,4 +1,4 @@ CompileFlags: Add: [-Wno-unknown-attributes, -Wno-maybe-uninitialized, -Wno-unknown-warning-option] - Remove: [-W*, -mcall-prologues] + Remove: [-W*, -mmcu=*, -mcpu=*, -mfpu=*, -mfloat-abi=*, -mno-unaligned-access, -mno-thumb-interwork, -mcall-prologues] Compiler: clang diff --git a/lib/python/qmk/cli/generate/compilation_database.py b/lib/python/qmk/cli/generate/compilation_database.py index a2190fee66c7..4c593ca0c678 100755 --- a/lib/python/qmk/cli/generate/compilation_database.py +++ b/lib/python/qmk/cli/generate/compilation_database.py @@ -25,7 +25,6 @@ def system_libs(binary: str) -> List[Path]: """Find the system include directory that the given build tool uses. """ cli.log.debug("searching for system library directory for binary: %s", binary) - bin_path = shutil.which(binary) # Actually query xxxxxx-gcc to find its include paths. if binary.endswith("gcc") or binary.endswith("g++"): @@ -37,7 +36,31 @@ def system_libs(binary: str) -> List[Path]: paths.append(Path(line.strip()).resolve()) return paths - return list(Path(bin_path).resolve().parent.parent.glob("*/include")) if bin_path else [] + return list(Path(binary).resolve().parent.parent.glob("*/include")) if binary else [] + + +@lru_cache(maxsize=10) +def cpu_defines(binary: str, compiler_args: str) -> List[str]: + cli.log.debug("gathering definitions for compilation: %s %s", binary, compiler_args) + if binary.endswith("gcc") or binary.endswith("g++"): + invocation = [binary, '-dM', '-E'] + if binary.endswith("gcc"): + invocation.extend(['-x', 'c']) + elif binary.endswith("g++"): + invocation.extend(['-x', 'c++']) + compiler_args = shlex.split(compiler_args) + invocation.extend(compiler_args) + invocation.append('-') + result = cli.run(invocation, capture_output=True, check=True, stdin=None, input='\n') + define_args = [] + for line in result.stdout.splitlines(): + line_args = line.split(' ', 2) + if len(line_args) == 3 and line_args[0] == '#define': + define_args.append(f'-D{line_args[1]}={line_args[2]}') + elif len(line_args) == 2 and line_args[0] == '#define': + define_args.append(f'-D{line_args[1]}') + return list(set(define_args)) + return [] file_re = re.compile(r'printf "Compiling: ([^"]+)') @@ -68,9 +91,12 @@ def parse_make_n(f: Iterator[str]) -> List[Dict[str, str]]: # we have a hit! this_cmd = m.group(1) args = shlex.split(this_cmd) - for s in system_libs(args[0]): + binary = shutil.which(args[0]) + compiler_args = set(filter(lambda x: x.startswith('-m') or x.startswith('-f'), args)) + for s in system_libs(binary): args += ['-isystem', '%s' % s] - new_cmd = ' '.join(shlex.quote(s) for s in args if s != '-mno-thumb-interwork') + args.extend(cpu_defines(binary, ' '.join(shlex.quote(s) for s in compiler_args))) + new_cmd = ' '.join(shlex.quote(s) for s in args) records.append({"directory": str(QMK_FIRMWARE.resolve()), "command": new_cmd, "file": this_file}) state = 'start' From 14c6cb17746dfd01bdb642c6e3c6226d4da7af3d Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Tue, 19 Mar 2024 14:16:12 +1100 Subject: [PATCH 2/2] May as well sort. --- lib/python/qmk/cli/generate/compilation_database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/python/qmk/cli/generate/compilation_database.py b/lib/python/qmk/cli/generate/compilation_database.py index 4c593ca0c678..b9c716bf0c52 100755 --- a/lib/python/qmk/cli/generate/compilation_database.py +++ b/lib/python/qmk/cli/generate/compilation_database.py @@ -59,7 +59,7 @@ def cpu_defines(binary: str, compiler_args: str) -> List[str]: define_args.append(f'-D{line_args[1]}={line_args[2]}') elif len(line_args) == 2 and line_args[0] == '#define': define_args.append(f'-D{line_args[1]}') - return list(set(define_args)) + return list(sorted(set(define_args))) return []