Skip to content

Commit

Permalink
kconfig: kconfiglib.py: Add an option to allow empty macros
Browse files Browse the repository at this point in the history
When parsing Kconfig which include macros it is currently necessary to
provide a value for all macros in advance. This may not be possible in
some cases, e.g. when the caller is performing checks on the Kconfig
options but is not running a full build of the project.

Add an option to support this. This allows parsing of Zephyr Kconfig
files without specifying a particular board, etc.

This corresponds to upstream PR:

   ulfalizer/Kconfiglib#112

but it looks like the project might be dead as there is no activity in
18 months.

Signed-off-by: Simon Glass <[email protected]>
Change-Id: Icf36da1e47fb7293f3d8bc3569affd7cd7598100
  • Loading branch information
sjg20 committed Jan 28, 2022
1 parent a26af4b commit 26c5579
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions scripts/kconfig/kconfiglib.py
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ class Kconfig(object):
"_srctree_prefix",
"_unset_match",
"_warn_assign_no_prompt",
"allow_empty_macros",
"choices",
"comments",
"config_header",
Expand Down Expand Up @@ -866,7 +867,8 @@ class Kconfig(object):
#

def __init__(self, filename="Kconfig", warn=True, warn_to_stderr=True,
encoding="utf-8", suppress_traceback=False, search_paths=None):
encoding="utf-8", suppress_traceback=False, search_paths=None,
allow_empty_macros=False):
"""
Creates a new Kconfig object by parsing Kconfig files.
Note that Kconfig files are not the same as .config files (which store
Expand Down Expand Up @@ -957,9 +959,21 @@ def __init__(self, filename="Kconfig", warn=True, warn_to_stderr=True,
Each search path is prepended to the relative filename to assist in
finding the file. The proeect directories should have distinct
filenames and/or subdirectory structures, so avoid ambiguity.
allow_empty_macros (default: False):
Normally when macros expand to empty it means that the macro is not
defined. This is considered an error and parsing of the Kconfig files
aborts with an exception. In some cases it is useful to continue
parsing, to obtain what information is available.
An example is where the value of various macros is not known but the
caller simply wants to get a list of the available Kconfig options.
Pass True here to allow empty / undefined macros.
"""
try:
self._init(filename, warn, warn_to_stderr, encoding, search_paths)
self._init(filename, warn, warn_to_stderr, encoding, search_paths,
allow_empty_macros)
except (EnvironmentError, KconfigError) as e:
if suppress_traceback:
cmd = sys.argv[0] # Empty string if missing
Expand All @@ -971,7 +985,8 @@ def __init__(self, filename="Kconfig", warn=True, warn_to_stderr=True,
sys.exit(cmd + str(e).strip())
raise

def _init(self, filename, warn, warn_to_stderr, encoding, search_paths):
def _init(self, filename, warn, warn_to_stderr, encoding, search_paths,
allow_empty_macros):
# See __init__()

self._encoding = encoding
Expand All @@ -982,6 +997,7 @@ def _init(self, filename, warn, warn_to_stderr, encoding, search_paths):
# because it assumes symlink/../foo is the same as foo/.
self._srctree_prefix = realpath(self.srctree) + os.sep
self.search_paths = search_paths
self.allow_empty_macros = allow_empty_macros

self.warn = warn
self.warn_to_stderr = warn_to_stderr
Expand Down Expand Up @@ -2700,7 +2716,8 @@ def _expand_name(self, s, i):
if not name.strip():
# Avoid creating a Kconfig symbol with a blank name. It's almost
# guaranteed to be an error.
self._parse_error("macro expanded to blank string")
if not self.allow_empty_macros:
self._parse_error("macro expanded to blank string")

# Skip trailing whitespace
while end_i < len(s) and s[end_i].isspace():
Expand Down

0 comments on commit 26c5579

Please sign in to comment.