Skip to content

Commit

Permalink
CLI/c2json: Print 'cpp' error when executed in verbose mode (qmk#12869)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan <fauxpark@gmail.com>
  • Loading branch information
2 people authored and HokieGeek committed Jul 11, 2021
1 parent 30a093f commit fa06402
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lib/python/qmk/cli/c2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import qmk.path
from qmk.json_encoders import InfoJSONEncoder
from qmk.keyboard import keyboard_completer, keyboard_folder
from qmk.errors import CppError


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

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

# Generate the keymap.json
try:
Expand Down
7 changes: 7 additions & 0 deletions lib/python/qmk/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ class NoSuchKeyboardError(Exception):
"""
def __init__(self, message):
self.message = message


class CppError(Exception):
"""Raised when 'cpp' cannot process a file.
"""
def __init__(self, message):
self.message = message
6 changes: 5 additions & 1 deletion lib/python/qmk/keymap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import qmk.path
from qmk.keyboard import find_keyboard_from_dir, rules_mk
from qmk.errors import CppError

# The `keymap.c` template to use when a keyboard doesn't have its own
DEFAULT_KEYMAP_C = """#include QMK_KEYBOARD_H
Expand Down Expand Up @@ -372,7 +373,10 @@ def _c_preprocess(path, stdin=DEVNULL):
"""
cmd = ['cpp', str(path)] if path else ['cpp']
pre_processed_keymap = cli.run(cmd, stdin=stdin)

if 'fatal error' in pre_processed_keymap.stderr:
for line in pre_processed_keymap.stderr.split('\n'):
if 'fatal error' in line:
raise (CppError(line))
return pre_processed_keymap.stdout


Expand Down

0 comments on commit fa06402

Please sign in to comment.