Skip to content

Commit 125aa69

Browse files
authored
Merge pull request #8 from sysprog21/allow-empty-macro
Add an option to allow empty macros
2 parents 42eaecf + 25e2ccd commit 125aa69

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

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
@@ -2701,7 +2717,8 @@ def _expand_name(self, s, i):
27012717
if not name.strip():
27022718
# Avoid creating a Kconfig symbol with a blank name. It's almost
27032719
# guaranteed to be an error.
2704-
self._parse_error("macro expanded to blank string")
2720+
if not self.allow_empty_macros:
2721+
self._parse_error("macro expanded to blank string")
27052722

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

0 commit comments

Comments
 (0)