This commit is contained in:
Jalal El Mansouri
2025-09-11 00:32:12 +08:00
committed by GitHub
2 changed files with 23 additions and 9 deletions

View File

@@ -151,11 +151,21 @@ class BuildTarget:
def generate_compilation_database(self, build_target: str = None, skip_clean: bool = False, **env_vars) -> None:
self.prepare_build(build_target=build_target, **env_vars)
command = self.compile_command(build_target=build_target, dry_run=True, **env_vars)
output_path = QMK_FIRMWARE / 'compile_commands.json'
ret = write_compilation_database(command=command, output_path=output_path, skip_clean=skip_clean, **env_vars)
if ret and output_path.exists() and HAS_QMK_USERSPACE:
shutil.copy(str(output_path), str(QMK_USERSPACE / 'compile_commands.json'))
return ret
db = generate_compilation_database(command=command, skip_clean=skip_clean, **env_vars)
if not db:
return False
qmk_firmware_db = QMK_FIRMWARE / 'compile_commands.json'
if HAS_QMK_USERSPACE:
(QMK_USERSPACE / 'compile_commands.json').write_text(db)
try:
qmk_firmware_db.write_text(db)
except PermissionError:
# Write is best-effort; ignore a permission failure
cli.log.warning("Permission error, failed to write to %s", qmk_firmware_db)
pass
else:
qmk_firmware_db.write_text(db)
return True
def compile(self, build_target: str = None, dry_run: bool = False, **env_vars) -> None:
if self._clean or self._compiledb:

View File

@@ -102,7 +102,7 @@ def parse_make_n(f: Iterator[str]) -> List[Dict[str, str]]:
return records
def 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:
def generate_compilation_database(keyboard: str = None, keymap: str = None, skip_clean: bool = False, command: List[str] = None, **env_vars):
# Generate the make command for a specific keyboard/keymap.
if not command:
from qmk.build_targets import KeyboardKeymapBuildTarget # Lazy load due to circular references
@@ -112,7 +112,7 @@ def write_compilation_database(keyboard: str = None, keymap: str = None, output_
if not command:
cli.log.error('You must supply both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
cli.echo('usage: qmk generate-compilation-database [-kb KEYBOARD] [-km KEYMAP]')
return False
return None
# remove any environment variable overrides which could trip us up
env = os.environ.copy()
@@ -130,11 +130,15 @@ def write_compilation_database(keyboard: str = None, keymap: str = None, output_
db = parse_make_n(result.stdout.splitlines())
if not db:
cli.log.error("Failed to parse output from make output:\n%s", result.stdout)
return False
return None
cli.log.info("Found %s compile commands", len(db))
return json.dumps(db, indent=4)
def 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:
if not db:
return False
cli.log.info(f"Writing build database to {output_path}")
output_path.write_text(json.dumps(db, indent=4))
output_path.write_text(db)
return True