summaryrefslogtreecommitdiff
path: root/lib/python
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python')
-rw-r--r--lib/python/qmk/build_targets.py2
-rw-r--r--[-rwxr-xr-x]lib/python/qmk/cli/generate/compilation_database.py174
-rwxr-xr-xlib/python/qmk/compilation_database.py137
3 files changed, 145 insertions, 168 deletions
diff --git a/lib/python/qmk/build_targets.py b/lib/python/qmk/build_targets.py
index df5a5ffb42..35a5f89f91 100644
--- a/lib/python/qmk/build_targets.py
+++ b/lib/python/qmk/build_targets.py
@@ -12,6 +12,7 @@ from qmk.keyboard import keyboard_folder
12from qmk.info import keymap_json 12from qmk.info import keymap_json
13from qmk.keymap import locate_keymap 13from qmk.keymap import locate_keymap
14from qmk.path import is_under_qmk_firmware, is_under_qmk_userspace, unix_style_path 14from qmk.path import is_under_qmk_firmware, is_under_qmk_userspace, unix_style_path
15from qmk.compilation_database import write_compilation_database
15 16
16# These must be kept in the order in which they're applied to $(TARGET) in the makefiles in order to ensure consistency. 17# These must be kept in the order in which they're applied to $(TARGET) in the makefiles in order to ensure consistency.
17TARGET_FILENAME_MODIFIERS = ['FORCE_LAYOUT', 'CONVERT_TO'] 18TARGET_FILENAME_MODIFIERS = ['FORCE_LAYOUT', 'CONVERT_TO']
@@ -150,7 +151,6 @@ class BuildTarget:
150 def generate_compilation_database(self, build_target: str = None, skip_clean: bool = False, **env_vars) -> None: 151 def generate_compilation_database(self, build_target: str = None, skip_clean: bool = False, **env_vars) -> None:
151 self.prepare_build(build_target=build_target, **env_vars) 152 self.prepare_build(build_target=build_target, **env_vars)
152 command = self.compile_command(build_target=build_target, dry_run=True, **env_vars) 153 command = self.compile_command(build_target=build_target, dry_run=True, **env_vars)
153 from qmk.cli.generate.compilation_database import write_compilation_database # Lazy load due to circular references
154 output_path = QMK_FIRMWARE / 'compile_commands.json' 154 output_path = QMK_FIRMWARE / 'compile_commands.json'
155 ret = write_compilation_database(command=command, output_path=output_path, skip_clean=skip_clean, **env_vars) 155 ret = write_compilation_database(command=command, output_path=output_path, skip_clean=skip_clean, **env_vars)
156 if ret and output_path.exists() and HAS_QMK_USERSPACE: 156 if ret and output_path.exists() and HAS_QMK_USERSPACE:
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()
diff --git a/lib/python/qmk/compilation_database.py b/lib/python/qmk/compilation_database.py
new file mode 100755
index 0000000000..4c88dadbdd
--- /dev/null
+++ b/lib/python/qmk/compilation_database.py
@@ -0,0 +1,137 @@
1"""Creates a compilation database for the given keyboard build.
2"""
3
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
12
13from milc import cli
14
15from qmk.commands import find_make
16from qmk.constants import QMK_FIRMWARE
17
18
19@lru_cache(maxsize=10)
20def system_libs(binary: str) -> List[Path]:
21 """Find the system include directory that the given build tool uses.
22 """
23 cli.log.debug("searching for system library directory for binary: %s", binary)
24
25 # Actually query xxxxxx-gcc to find its include paths.
26 if binary.endswith("gcc") or binary.endswith("g++"):
27 # (TODO): Remove 'stdin' once 'input' no longer causes issues under MSYS
28 result = cli.run([binary, '-E', '-Wp,-v', '-'], capture_output=True, check=True, stdin=None, input='\n')
29 paths = []
30 for line in result.stderr.splitlines():
31 if line.startswith(" "):
32 paths.append(Path(line.strip()).resolve())
33 return paths
34
35 return list(Path(binary).resolve().parent.parent.glob("*/include")) if binary else []
36
37
38@lru_cache(maxsize=10)
39def cpu_defines(binary: str, compiler_args: str) -> List[str]:
40 cli.log.debug("gathering definitions for compilation: %s %s", binary, compiler_args)
41 if binary.endswith("gcc") or binary.endswith("g++"):
42 invocation = [binary, '-dM', '-E']
43 if binary.endswith("gcc"):
44 invocation.extend(['-x', 'c'])
45 elif binary.endswith("g++"):
46 invocation.extend(['-x', 'c++'])
47 compiler_args = shlex.split(compiler_args)
48 invocation.extend(compiler_args)
49 invocation.append('-')
50 result = cli.run(invocation, capture_output=True, check=True, stdin=None, input='\n')
51 define_args = []
52 for line in result.stdout.splitlines():
53 line_args = line.split(' ', 2)
54 if len(line_args) == 3 and line_args[0] == '#define':
55 define_args.append(f'-D{line_args[1]}={line_args[2]}')
56 elif len(line_args) == 2 and line_args[0] == '#define':
57 define_args.append(f'-D{line_args[1]}')
58 return list(sorted(set(define_args)))
59 return []
60
61
62file_re = re.compile(r'printf "Compiling: ([^"]+)')
63cmd_re = re.compile(r'LOG=\$\((.+?)&&')
64
65
66def parse_make_n(f: Iterator[str]) -> List[Dict[str, str]]:
67 """parse the output of `make -n <target>`
68
69 This function makes many assumptions about the format of your build log.
70 This happens to work right now for qmk.
71 """
72
73 state = 'start'
74 this_file = None
75 records = []
76 for line in f:
77 if state == 'start':
78 m = file_re.search(line)
79 if m:
80 this_file = m.group(1)
81 state = 'cmd'
82
83 if state == 'cmd':
84 assert this_file
85 m = cmd_re.search(line)
86 if m:
87 # we have a hit!
88 this_cmd = m.group(1)
89 args = shlex.split(this_cmd)
90 binary = shutil.which(args[0])
91 compiler_args = set(filter(lambda x: x.startswith('-m') or x.startswith('-f'), args))
92 for s in system_libs(binary):
93 args += ['-isystem', '%s' % s]
94 args.extend(cpu_defines(binary, ' '.join(shlex.quote(s) for s in compiler_args)))
95 new_cmd = ' '.join(shlex.quote(s) for s in args)
96 records.append({"directory": str(QMK_FIRMWARE.resolve()), "command": new_cmd, "file": this_file})
97 state = 'start'
98
99 return records
100
101
102def 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:
103 # Generate the make command for a specific keyboard/keymap.
104 if not command:
105 from qmk.build_targets import KeyboardKeymapBuildTarget # Lazy load due to circular references
106 target = KeyboardKeymapBuildTarget(keyboard, keymap)
107 command = target.compile_command(dry_run=True, **env_vars)
108
109 if not command:
110 cli.log.error('You must supply both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
111 cli.echo('usage: qmk generate-compilation-database [-kb KEYBOARD] [-km KEYMAP]')
112 return False
113
114 # remove any environment variable overrides which could trip us up
115 env = os.environ.copy()
116 env.pop("MAKEFLAGS", None)
117
118 # re-use same executable as the main make invocation (might be gmake)
119 if not skip_clean:
120 clean_command = [find_make(), "clean"]
121 cli.log.info('Making clean with {fg_cyan}%s', ' '.join(clean_command))
122 cli.run(clean_command, capture_output=False, check=True, env=env)
123
124 cli.log.info('Gathering build instructions from {fg_cyan}%s', ' '.join(command))
125
126 result = cli.run(command, capture_output=True, check=True, env=env)
127 db = parse_make_n(result.stdout.splitlines())
128 if not db:
129 cli.log.error("Failed to parse output from make output:\n%s", result.stdout)
130 return False
131
132 cli.log.info("Found %s compile commands", len(db))
133
134 cli.log.info(f"Writing build database to {output_path}")
135 output_path.write_text(json.dumps(db, indent=4))
136
137 return True