summaryrefslogtreecommitdiff
path: root/lib/python/qmk/cli/generate/compilation_database.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/cli/generate/compilation_database.py')
-rw-r--r--[-rwxr-xr-x]lib/python/qmk/cli/generate/compilation_database.py174
1 files changed, 7 insertions, 167 deletions
diff --git a/lib/python/qmk/cli/generate/compilation_database.py b/lib/python/qmk/cli/generate/compilation_database.py
index b9c716bf0c..339b53c2c2 100755..100644
--- a/lib/python/qmk/cli/generate/compilation_database.py
+++ b/lib/python/qmk/cli/generate/compilation_database.py
@@ -1,169 +1,9 @@
1"""Creates a compilation database for the given keyboard build. 1from milc import cli
2"""
3 2
4import json
5import os
6import re
7import shlex
8import shutil
9from functools import lru_cache
10from pathlib import Path
11from typing import Dict, Iterator, List, Union
12 3
13from milc import cli, MILC 4@cli.argument('-kb', '--keyboard', help='[unused] The keyboard\'s name')
14 5@cli.argument('-km', '--keymap', help='[unused] The keymap\'s name')
15from qmk.commands import find_make 6@cli.subcommand('[deprecated] Create a compilation database.')
16from qmk.constants import QMK_FIRMWARE 7def generate_compilation_database(cli):
17from qmk.decorators import automagic_keyboard, automagic_keymap 8 cli.log.error('This command is deprecated and has effectively been removed. Please use the `--compiledb` flag with `qmk compile` instead.')
18from qmk.keyboard import keyboard_completer, keyboard_folder 9 return False
19from qmk.keymap import keymap_completer
20from qmk.build_targets import KeyboardKeymapBuildTarget
21
22
23@lru_cache(maxsize=10)
24def system_libs(binary: str) -> List[Path]:
25 """Find the system include directory that the given build tool uses.
26 """
27 cli.log.debug("searching for system library directory for binary: %s", binary)
28
29 # Actually query xxxxxx-gcc to find its include paths.
30 if binary.endswith("gcc") or binary.endswith("g++"):
31 # (TODO): Remove 'stdin' once 'input' no longer causes issues under MSYS
32 result = cli.run([binary, '-E', '-Wp,-v', '-'], capture_output=True, check=True, stdin=None, input='\n')
33 paths = []
34 for line in result.stderr.splitlines():
35 if line.startswith(" "):
36 paths.append(Path(line.strip()).resolve())
37 return paths
38
39 return list(Path(binary).resolve().parent.parent.glob("*/include")) if binary else []
40
41
42@lru_cache(maxsize=10)
43def cpu_defines(binary: str, compiler_args: str) -> List[str]:
44 cli.log.debug("gathering definitions for compilation: %s %s", binary, compiler_args)
45 if binary.endswith("gcc") or binary.endswith("g++"):
46 invocation = [binary, '-dM', '-E']
47 if binary.endswith("gcc"):
48 invocation.extend(['-x', 'c'])
49 elif binary.endswith("g++"):
50 invocation.extend(['-x', 'c++'])
51 compiler_args = shlex.split(compiler_args)
52 invocation.extend(compiler_args)
53 invocation.append('-')
54 result = cli.run(invocation, capture_output=True, check=True, stdin=None, input='\n')
55 define_args = []
56 for line in result.stdout.splitlines():
57 line_args = line.split(' ', 2)
58 if len(line_args) == 3 and line_args[0] == '#define':
59 define_args.append(f'-D{line_args[1]}={line_args[2]}')
60 elif len(line_args) == 2 and line_args[0] == '#define':
61 define_args.append(f'-D{line_args[1]}')
62 return list(sorted(set(define_args)))
63 return []
64
65
66file_re = re.compile(r'printf "Compiling: ([^"]+)')
67cmd_re = re.compile(r'LOG=\$\((.+?)&&')
68
69
70def parse_make_n(f: Iterator[str]) -> List[Dict[str, str]]:
71 """parse the output of `make -n <target>`
72
73 This function makes many assumptions about the format of your build log.
74 This happens to work right now for qmk.
75 """
76
77 state = 'start'
78 this_file = None
79 records = []
80 for line in f:
81 if state == 'start':
82 m = file_re.search(line)
83 if m:
84 this_file = m.group(1)
85 state = 'cmd'
86
87 if state == 'cmd':
88 assert this_file
89 m = cmd_re.search(line)
90 if m:
91 # we have a hit!
92 this_cmd = m.group(1)
93 args = shlex.split(this_cmd)
94 binary = shutil.which(args[0])
95 compiler_args = set(filter(lambda x: x.startswith('-m') or x.startswith('-f'), args))
96 for s in system_libs(binary):
97 args += ['-isystem', '%s' % s]
98 args.extend(cpu_defines(binary, ' '.join(shlex.quote(s) for s in compiler_args)))
99 new_cmd = ' '.join(shlex.quote(s) for s in args)
100 records.append({"directory": str(QMK_FIRMWARE.resolve()), "command": new_cmd, "file": this_file})
101 state = 'start'
102
103 return records
104
105
106def write_compilation_database(keyboard: str = None, keymap: str = None, output_path: Path = QMK_FIRMWARE / 'compile_commands.json', skip_clean: bool = False, command: List[str] = None, **env_vars) -> bool:
107 # Generate the make command for a specific keyboard/keymap.
108 if not command:
109 from qmk.build_targets import KeyboardKeymapBuildTarget # Lazy load due to circular references
110 target = KeyboardKeymapBuildTarget(keyboard, keymap)
111 command = target.compile_command(dry_run=True, **env_vars)
112
113 if not command:
114 cli.log.error('You must supply both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
115 cli.echo('usage: qmk generate-compilation-database [-kb KEYBOARD] [-km KEYMAP]')
116 return False
117
118 # remove any environment variable overrides which could trip us up
119 env = os.environ.copy()
120 env.pop("MAKEFLAGS", None)
121
122 # re-use same executable as the main make invocation (might be gmake)
123 if not skip_clean:
124 clean_command = [find_make(), "clean"]
125 cli.log.info('Making clean with {fg_cyan}%s', ' '.join(clean_command))
126 cli.run(clean_command, capture_output=False, check=True, env=env)
127
128 cli.log.info('Gathering build instructions from {fg_cyan}%s', ' '.join(command))
129
130 result = cli.run(command, capture_output=True, check=True, env=env)
131 db = parse_make_n(result.stdout.splitlines())
132 if not db:
133 cli.log.error("Failed to parse output from make output:\n%s", result.stdout)
134 return False
135
136 cli.log.info("Found %s compile commands", len(db))
137
138 cli.log.info(f"Writing build database to {output_path}")
139 output_path.write_text(json.dumps(db, indent=4))
140
141 return True
142
143
144@cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard\'s name')
145@cli.argument('-km', '--keymap', completer=keymap_completer, help='The keymap\'s name')
146@cli.subcommand('Create a compilation database.')
147@automagic_keyboard
148@automagic_keymap
149def generate_compilation_database(cli: MILC) -> Union[bool, int]:
150 """Creates a compilation database for the given keyboard build.
151
152 Does a make clean, then a make -n for this target and uses the dry-run output to create
153 a compilation database (compile_commands.json). This file can help some IDEs and
154 IDE-like editors work better. For more information about this:
155
156 https://clang.llvm.org/docs/JSONCompilationDatabase.html
157 """
158 # check both config domains: the magic decorator fills in `generate_compilation_database` but the user is
159 # more likely to have set `compile` in their config file.
160 current_keyboard = cli.config.generate_compilation_database.keyboard or cli.config.user.keyboard
161 current_keymap = cli.config.generate_compilation_database.keymap or cli.config.user.keymap
162
163 if not current_keyboard:
164 cli.log.error('Could not determine keyboard!')
165 elif not current_keymap:
166 cli.log.error('Could not determine keymap!')
167
168 target = KeyboardKeymapBuildTarget(current_keyboard, current_keymap)
169 return target.generate_compilation_database()