Skip to content

Commit 666774d

Browse files
committed
kconfig: Add an option to allow empty macros
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. Signed-off-by: Simon Glass <[email protected]>
1 parent 16ceb81 commit 666774d

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

tools/kconfig/kconfiglib.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ class Kconfig(object):
817817
"_srctree_prefix",
818818
"_unset_match",
819819
"_warn_assign_no_prompt",
820+
"allow_empty_macros",
820821
"choices",
821822
"comments",
822823
"config_header",
@@ -866,7 +867,8 @@ class Kconfig(object):
866867
#
867868

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

974-
def _init(self, filename, warn, warn_to_stderr, encoding, search_paths):
988+
def _init(self, filename, warn, warn_to_stderr, encoding, search_paths,
989+
allow_empty_macros):
975990
# See __init__()
976991

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

9861002
self.warn = warn
9871003
self.warn_to_stderr = warn_to_stderr
@@ -2712,7 +2728,8 @@ def _expand_name(self, s, i):
27122728
if not name.strip():
27132729
# Avoid creating a Kconfig symbol with a blank name. It's almost
27142730
# guaranteed to be an error.
2715-
self._parse_error("macro expanded to blank string")
2731+
if not self.allow_empty_macros:
2732+
self._parse_error("macro expanded to blank string")
27162733

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

0 commit comments

Comments
 (0)