Skip to content

Commit

Permalink
Look up the top-level Kconfig file relative to $srctree
Browse files Browse the repository at this point in the history
Due to an old design braino, the top-level Kconfig filename passed to
Kconfig.__init__() wasn't looked up relative to $srctree, breaking
out-of-tree usage for e.g. menuconfig. Fixing it required ugliness like

  srctree = os.environ.get("srctree", "")
  kconfiglib.Kconfig(os.path.join(srctree, "Kconfig"))

Change the behavior of Kconfig.__init__() to look up the top-level
Kconfig file relative to $srctree. This means that all Kconfig files
(both the top-level file and any source'd files) now use $srctree, which
makes the vast majority of scripts just work when running out-of-tree.

Also remove the note re. loading a subset of Kconfig files. Saying that
the top-level file and all source'd Kconfig files are looked up relative
to $srctree should make the behavior clear enough.

Print a note about the new behavior whenever the top-level Kconfig file
can't be opened, as this change could be breaking for some scripts.

This is a slight backwards-compatiblity break, so the major version will
be bumped.
  • Loading branch information
ulfalizer committed Aug 18, 2018
1 parent af7f6c3 commit f247ddf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
35 changes: 22 additions & 13 deletions kconfiglib.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,20 +610,14 @@ def __init__(self, filename="Kconfig", warn=True, warn_to_stderr=True,
the right Kconfig is included from there (arch/$SRCARCH/Kconfig as of
writing).
If $srctree is set, 'filename' will be looked up relative to it.
$srctree is also used to look up source'd files within Kconfig files.
See the class documentation.
If you are using Kconfiglib via 'make scriptconfig', the filename of
the base base Kconfig file will be in sys.argv[1]. It's currently
always "Kconfig" in practice.
The $srctree environment variable is used to look up Kconfig files
referenced in Kconfig files if set. See the class documentation.
Note: '(o)source' statements in Kconfig files always work relative to
$srctree (or the current directory if $srctree is unset), even if
'filename' is a path with directories. This allows a subset of
Kconfig files to be loaded without breaking references to other
Kconfig files, e.g. by doing Kconfig("./sub/Kconfig"). sub/Kconfig
might expect to be sourced by ./Kconfig.
warn (default: True):
True if warnings related to this configuration should be generated.
This can be changed later with Kconfig.enable/disable_warnings(). It
Expand Down Expand Up @@ -737,7 +731,7 @@ def __init__(self, filename="Kconfig", warn=True, warn_to_stderr=True,
self.top_node.prompt = ("Main menu", self.y)
self.top_node.parent = None
self.top_node.dep = self.y
self.top_node.filename = os.path.relpath(filename, self.srctree)
self.top_node.filename = filename
self.top_node.linenr = 1

# Parse the Kconfig files
Expand All @@ -751,11 +745,17 @@ def __init__(self, filename="Kconfig", warn=True, warn_to_stderr=True,
self._filestack = []

# The current parsing location
self._filename = os.path.relpath(filename, self.srctree)
self._filename = filename
self._linenr = 0

# Open the top-level Kconfig file
self._file = self._open(filename, "r")
try:
self._file = self._open(os.path.join(self.srctree, filename), "r")
except IOError as e:
if self.srctree:
print(textwrap.fill(
_INIT_SRCTREE_NOTE.format(self.srctree), 80))
raise

try:
# Parse everything
Expand Down Expand Up @@ -6014,3 +6014,12 @@ def _re_search(regex):
GREATER: ">",
GREATER_EQUAL: ">=",
}

_INIT_SRCTREE_NOTE = """
NOTE: Starting with Kconfiglib 10.0.0, the Kconfig filename passed to
Kconfig.__init__() is looked up relative to the $srctree (which is set to '{}')
instead of relative to the working directory. Previously, $srctree only applied
to files being source'd within Kconfig files. This change makes running scripts
out-of-tree work seamlessly, with no special coding required. Sorry for the
backwards compatibility break!
"""[1:]
16 changes: 8 additions & 8 deletions testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,17 +895,17 @@ def verify_repr(item, s):
<configuration with 14 symbols, main menu prompt "Main menu", srctree is current directory, config symbol prefix "CONFIG_", warnings disabled, printing of warnings to stderr enabled, undef. symbol assignment warnings disabled, redundant symbol assignment warnings enabled>
""")

os.environ["srctree"] = "srctree value"
os.environ["srctree"] = "Kconfiglib"
os.environ["CONFIG_"] = "CONFIG_ value"

c = Kconfig("Kconfiglib/tests/Krepr", warn=False)
c = Kconfig("tests/Krepr", warn=False)
c.enable_warnings()
c.disable_stderr_warnings()
c.disable_redun_warnings()
c.enable_undef_warnings()

verify_repr(c, """
<configuration with 14 symbols, main menu prompt "Main menu", srctree "srctree value", config symbol prefix "CONFIG_ value", warnings enabled, printing of warnings to stderr disabled, undef. symbol assignment warnings enabled, redundant symbol assignment warnings disabled>
<configuration with 14 symbols, main menu prompt "Main menu", srctree "Kconfiglib", config symbol prefix "CONFIG_ value", warnings enabled, printing of warnings to stderr disabled, undef. symbol assignment warnings enabled, redundant symbol assignment warnings disabled>
""")

os.environ.pop("srctree", None)
Expand Down Expand Up @@ -987,7 +987,7 @@ def verify_locations(nodes, *expected_locs):
os.environ["srctree"] = srctree

# Has symbol with empty help text, so disable warnings
c = Kconfig("Kconfiglib/tests/Klocation", warn=False)
c = Kconfig("tests/Klocation", warn=False)

verify_locations(c.syms["SINGLE_DEF"].nodes, "tests/Klocation:4")

Expand Down Expand Up @@ -1018,7 +1018,7 @@ def verify_locations(nodes, *expected_locs):
# Test recursive 'source' detection

try:
Kconfig("Kconfiglib/tests/Krecursive1")
Kconfig("tests/Krecursive1")
except KconfigError:
pass
except:
Expand All @@ -1030,7 +1030,7 @@ def verify_locations(nodes, *expected_locs):
# TODO: Make an exception test helper

try:
Kconfig("Kconfiglib/tests/Kmissingsource")
Kconfig("tests/Kmissingsource")
except KconfigError:
pass
except:
Expand All @@ -1039,7 +1039,7 @@ def verify_locations(nodes, *expected_locs):
fail("'source' with missing file did not raise exception")

try:
Kconfig("Kconfiglib/tests/Kmissingrsource")
Kconfig("tests/Kmissingrsource")
except KconfigError:
pass
except:
Expand Down Expand Up @@ -1559,7 +1559,7 @@ def verify_range(sym_name, low, high, default):
"defconfig_filename gave wrong file with $srctree unset")

os.environ["srctree"] = "Kconfiglib/tests"
c = Kconfig("Kconfiglib/tests/Kdefconfig_srctree")
c = Kconfig("Kdefconfig_srctree")
verify(c.defconfig_filename == "Kconfiglib/tests/sub/defconfig_in_sub",
"defconfig_filename gave wrong file with $srctree set")

Expand Down

0 comments on commit f247ddf

Please sign in to comment.