From 26c55796c431f20205f6e5e06f4e291d212b5c69 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 14 Jan 2022 16:30:24 -0700 Subject: [PATCH] kconfig: kconfiglib.py: 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. This corresponds to upstream PR: https://github.com/ulfalizer/Kconfiglib/pull/112 but it looks like the project might be dead as there is no activity in 18 months. Signed-off-by: Simon Glass Change-Id: Icf36da1e47fb7293f3d8bc3569affd7cd7598100 --- scripts/kconfig/kconfiglib.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/scripts/kconfig/kconfiglib.py b/scripts/kconfig/kconfiglib.py index 3dab9914ed7cc9..26abe756282123 100644 --- a/scripts/kconfig/kconfiglib.py +++ b/scripts/kconfig/kconfiglib.py @@ -817,6 +817,7 @@ class Kconfig(object): "_srctree_prefix", "_unset_match", "_warn_assign_no_prompt", + "allow_empty_macros", "choices", "comments", "config_header", @@ -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 @@ -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 @@ -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 @@ -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 @@ -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():