Skip to content

Commit 84883d3

Browse files
Eroviafauxpark
andauthored
CLI/c2json: Print 'cpp' error when executed in verbose mode (#12869)
Co-authored-by: Ryan <[email protected]>
1 parent 147cf8a commit 84883d3

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

lib/python/qmk/cli/c2json.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import qmk.path
1010
from qmk.json_encoders import InfoJSONEncoder
1111
from qmk.keyboard import keyboard_completer, keyboard_folder
12+
from qmk.errors import CppError
1213

1314

1415
@cli.argument('--no-cpp', arg_only=True, action='store_false', help='Do not use \'cpp\' on keymap.c')
@@ -37,7 +38,13 @@ def c2json(cli):
3738
cli.args.output = None
3839

3940
# Parse the keymap.c
40-
keymap_json = qmk.keymap.c2json(cli.args.keyboard, cli.args.keymap, cli.args.filename, use_cpp=cli.args.no_cpp)
41+
try:
42+
keymap_json = qmk.keymap.c2json(cli.args.keyboard, cli.args.keymap, cli.args.filename, use_cpp=cli.args.no_cpp)
43+
except CppError as e:
44+
if cli.config.general.verbose:
45+
cli.log.debug('The C pre-processor ran into a fatal error: %s', e)
46+
cli.log.error('Something went wrong. Try to use --no-cpp.\nUse the CLI in verbose mode to find out more.')
47+
return False
4148

4249
# Generate the keymap.json
4350
try:

lib/python/qmk/errors.py

+7
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ class NoSuchKeyboardError(Exception):
33
"""
44
def __init__(self, message):
55
self.message = message
6+
7+
8+
class CppError(Exception):
9+
"""Raised when 'cpp' cannot process a file.
10+
"""
11+
def __init__(self, message):
12+
self.message = message

lib/python/qmk/keymap.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import qmk.path
1515
from qmk.keyboard import find_keyboard_from_dir, rules_mk
16+
from qmk.errors import CppError
1617

1718
# The `keymap.c` template to use when a keyboard doesn't have its own
1819
DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H
@@ -372,7 +373,10 @@ def _c_preprocess(path, stdin=DEVNULL):
372373
"""
373374
cmd = ['cpp', str(path)] if path else ['cpp']
374375
pre_processed_keymap = cli.run(cmd, stdin=stdin)
375-
376+
if 'fatal error' in pre_processed_keymap.stderr:
377+
for line in pre_processed_keymap.stderr.split('\n'):
378+
if 'fatal error' in line:
379+
raise (CppError(line))
376380
return pre_processed_keymap.stdout
377381

378382

0 commit comments

Comments
 (0)