Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features #112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions 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 All @@ -834,6 +835,7 @@ class Kconfig(object):
"n",
"named_choices",
"srctree",
"search_paths",
"syms",
"top_node",
"unique_choices",
Expand Down Expand Up @@ -865,7 +867,8 @@ class Kconfig(object):
#

def __init__(self, filename="Kconfig", warn=True, warn_to_stderr=True,
encoding="utf-8", suppress_traceback=False):
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 @@ -942,9 +945,35 @@ def __init__(self, filename="Kconfig", warn=True, warn_to_stderr=True,

Other exceptions besides EnvironmentError and KconfigError are still
propagated when suppress_traceback is True.

search_paths (default: None):
List of paths to search for Kconfig files. This is needed when the
files are split between two project directories, as is done with
Zephyr OS, for example. It allows files in one project to reference
files in another.

This argument affects the operation of commands which include other
Kconfig files, such as `source` and `rsource`.

When not None, it should be a list of paths to directories to search.
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)
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 @@ -956,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):
def _init(self, filename, warn, warn_to_stderr, encoding, search_paths,
allow_empty_macros):
# See __init__()

self._encoding = encoding
Expand All @@ -966,6 +996,8 @@ def _init(self, filename, warn, warn_to_stderr, encoding):
# relative to $srctree. relpath() can cause issues for symlinks,
# 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 @@ -2685,7 +2717,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 Expand Up @@ -2972,6 +3005,9 @@ def _parse_block(self, end_token, parent, prev):
# Kconfig symbols, which indirectly ensures a consistent
# ordering in e.g. .config files
filenames = sorted(iglob(join(self._srctree_prefix, pattern)))
if self.search_paths:
for prefix in self.search_paths:
filenames += sorted(iglob(join(prefix, pattern)))

if not filenames and t0 in _OBL_SOURCE_TOKENS:
raise KconfigError(
Expand Down