Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New CLI subcommand to create clang-compatible compilation database (compile_commands.json) #14370

Merged
merged 44 commits into from
Sep 16, 2021
Merged
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
47365ae
pulled source from dev branch
Apr 25, 2020
e0bff4d
missed a file from origin
Apr 25, 2020
5a51eaa
formatting
Apr 25, 2020
d817b7d
revised argument names. relaxed matching rules to work for avr too
Apr 25, 2020
30b564f
add docstrings
Apr 25, 2020
849a12f
added docs. tightened up regex
Apr 25, 2020
b671c40
remove unused imports
Apr 25, 2020
f42cadc
cleaning up command file. use existing qmk dir constant
May 9, 2020
e768fac
rename parser library file
May 9, 2020
21c5b63
move lib functions into command file. there are only 2 and they aren'…
May 9, 2020
3fe7817
currently debugging...
May 9, 2020
ec3739b
more robustly find config
May 9, 2020
bf2aef2
updated docs
May 9, 2020
10171f8
remove unused imports
May 9, 2020
697ca67
reuse make executable from the main make command
May 19, 2020
82a8be3
pulled source from dev branch
Apr 25, 2020
3a95195
missed a file from origin
Apr 25, 2020
3ed55aa
formatting
Apr 25, 2020
c9a4d97
revised argument names. relaxed matching rules to work for avr too
Apr 25, 2020
9226ba5
add docstrings
Apr 25, 2020
764f35d
added docs. tightened up regex
Apr 25, 2020
b0a5e1a
remove unused imports
Apr 25, 2020
67c08c6
cleaning up command file. use existing qmk dir constant
May 9, 2020
c0fc66e
rename parser library file
May 9, 2020
2f103f5
move lib functions into command file. there are only 2 and they aren'…
May 9, 2020
c03da56
currently debugging...
May 9, 2020
8db4e13
more robustly find config
May 9, 2020
67ae7d8
updated docs
May 9, 2020
976b0b1
remove unused imports
May 9, 2020
7ab3465
reuse make executable from the main make command
May 19, 2020
16c47fd
Merge branch 'compile_commands' of https://github.com/xton/qmk_firmwa…
Aug 24, 2020
70dfd86
Merge branch 'master' into compile_commands
Sep 7, 2020
c558c5d
remove MAKEFLAGS from environment for better control over process man…
Sep 7, 2020
afb2527
Update .gitignore
xton Sep 27, 2020
f64f64a
add a usage line to docs
Oct 11, 2020
3830823
Merge branch 'compile_commands' of https://github.com/xton/qmk_firmwa…
Oct 11, 2020
6cbcc9c
doc change as suggested
xton Nov 30, 2020
bae3306
Merge remote-tracking branch 'xton/compile_commands' into develop
baodrate Sep 10, 2021
13044e0
rename command
baodrate Sep 10, 2021
ecffaa4
Merge branch 'develop' into compile_commands
baodrate Sep 10, 2021
42014f0
remove debug print statements
baodrate Sep 10, 2021
d8d0f25
generate-compilation-database: fix arg handling
baodrate Sep 10, 2021
7df7b9e
generate-comilation-db: improve error handling
baodrate Sep 10, 2021
66e090d
use cli.run() instead of Popen()
baodrate Sep 10, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
generate-comilation-db: improve error handling
  • Loading branch information
baodrate committed Sep 10, 2021
commit 7df7b9ebe8a9092e66521fc04c7e057432e3d7ef
42 changes: 22 additions & 20 deletions lib/python/qmk/cli/generate/compilation_database.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
"""Creates a compilation database for the given keyboard build.
"""

import itertools
import json
import os
import re
import shlex
import shutil
import subprocess
import os
from functools import lru_cache
from pathlib import Path
from subprocess import check_output
from typing import Dict, List, TextIO
from typing import Dict, Iterator, List

from milc import cli

Expand All @@ -19,21 +20,19 @@


@lru_cache(maxsize=10)
def system_libs(binary: str):
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)
return list(Path(bin_path).resolve().parent.parent.glob("*/include")) if bin_path else []

try:
return list(Path(check_output(['which', binary]).rstrip().decode()).resolve().parent.parent.glob("*/include"))
except Exception:
return []

file_re = re.compile(r'printf "Compiling: ([^"]+)')
cmd_re = re.compile(r'LOG=\$\((.+?)&&')

file_re = re.compile(r"""printf "Compiling: ([^"]+)""")
cmd_re = re.compile(r"""LOG=\$\((.+?)\&\&""")


def parse_make_n(f: TextIO) -> List[Dict[str, str]]:
def parse_make_n(f: Iterator[str]) -> List[Dict[str, str]]:
"""parse the output of `make -n <target>`

This function makes many assumptions about the format of your build log.
Expand All @@ -51,6 +50,7 @@ def parse_make_n(f: TextIO) -> List[Dict[str, str]]:
state = 'cmd'

if state == 'cmd':
assert this_file
m = cmd_re.search(line)
if m:
# we have a hit!
Expand Down Expand Up @@ -101,16 +101,18 @@ def generate_compilation_database(cli):
# re-use same executable as the main make invocation (might be gmake)
clean_command = [command[0], 'clean']
cli.log.info('Making clean with {fg_cyan}%s', ' '.join(clean_command))
subprocess.run(clean_command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, env=env)
subprocess.run(clean_command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, env=env, check=True)

cli.log.info('Gathering build instructions from {fg_cyan}%s', ' '.join(command))
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, env=env)
db = parse_make_n(proc.stdout)
res = proc.wait()
if res != 0:
raise RuntimeError(f"Got error from: {repr(command)}")

cli.log.info(f"Found {len(db)} compile commands")
with subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, env=env) as proc:
baodrate marked this conversation as resolved.
Show resolved Hide resolved
stdout1, stdout2 = itertools.tee(proc.stdout or [])
db = parse_make_n(stdout1)
proc.wait()
if not db:
cli.log.error("Failed to parse output from make output:\n%s", ''.join(stdout2))
return False

cli.log.info("Found %s compile commands", len(db))

dbpath = QMK_FIRMWARE / 'compile_commands.json'

Expand Down