summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Magnusson <ulfalizer@gmail.com>2018-08-25 20:07:05 +0200
committerUlf Magnusson <ulfalizer@gmail.com>2018-08-25 20:32:34 +0200
commit932d0f7b8a69bdcac5d945de600ce6be807aeff7 (patch)
treeb28def99ad24db522ee2bf51df9b9cf52d611b11
parent20de53b6a29fcfd2d91e815c5698b9b806a908f4 (diff)
Add a Kconfig.env_vars attribute that lists env. variables
Kconfig.env_vars is a set() with the names of all environment variables referenced in the configuration. Can be used e.g. for custom incremental build implementations, though sync_deps() already indirectly catches any relevant changes to environment variables.
-rw-r--r--kconfiglib.py21
-rw-r--r--tests/Kpreprocess13
-rw-r--r--testsuite.py14
3 files changed, 45 insertions, 3 deletions
diff --git a/kconfiglib.py b/kconfiglib.py
index 5fdd9b7..38bdff2 100644
--- a/kconfiglib.py
+++ b/kconfiglib.py
@@ -479,6 +479,24 @@ class Kconfig(object):
already indirectly catches any file modifications that change the
configuration output.
+ env_vars:
+ A set() with the names of all environment variables referenced in the
+ Kconfig files.
+
+ Only environment variables referenced with the preprocessor $(FOO) syntax
+ will be registered. The older $FOO syntax is only supported for backwards
+ compatibility.
+
+ Also note that $(FOO) won't be registered unless the environment variable
+ $FOO is actually set. If it isn't, $(FOO) is an expansion of an unset
+ preprocessor variable (which gives the empty string).
+
+ Another gotcha is that environment variables referenced in the values of
+ recursively expanded preprocessor variables (those defined with =) will
+ only be registered if the variable is actually used (expanded) somewhere.
+
+ The note from the 'kconfig_filenames' documentation applies here too.
+
n/m/y:
The predefined constant symbols n/m/y. Also available in const_syms.
@@ -583,6 +601,7 @@ class Kconfig(object):
"const_syms",
"defconfig_list",
"defined_syms",
+ "env_vars",
"kconfig_filenames",
"m",
"mainmenu_text",
@@ -773,6 +792,7 @@ class Kconfig(object):
# Not used internally. Provided as a convenience.
self.kconfig_filenames = [filename]
+ self.env_vars = set()
# These implement a single line of "unget" for the parser
self._saved_line = None
@@ -2180,6 +2200,7 @@ class Kconfig(object):
# Environment variables are tried last
if fn in os.environ:
+ self.env_vars.add(fn)
return os.environ[fn]
return ""
diff --git a/tests/Kpreprocess b/tests/Kpreprocess
index 73053fe..2ebf6e6 100644
--- a/tests/Kpreprocess
+++ b/tests/Kpreprocess
@@ -69,7 +69,7 @@ qaz = QAZ
echo = $(1)
config PRINT_ME
- string "$(ENV_VAR)" if ($(echo,FOO) && $(echo,BAR)) || !$(echo,BAZ) || !(($(qaz)))
+ string "$(ENV_1)" if ($(echo,FOO) && $(echo,BAR)) || !$(echo,BAZ) || !(($(qaz)))
default "$(echo,"foo")" if "foo $(echo,"bar") baz" = "$(undefined)"
@@ -128,3 +128,14 @@ error-n-res := $(error-if,n,oops)
# Causes an error when expanded
error-y-res = $(error-if,y,oops)
+
+
+# Environment variables (for testing Kconfig.env_vars). ENV_1 is already
+# referenced above.
+env_ref_1 := xxx $(ENV_2) xxx
+env_ref_2 := $(shell,echo $(ENV_3))
+env_ref_3 :=
+env_ref_3 += $(ENV_4)
+$(warning-if,$(ENV_5),$(ENV_UNDEFINED))
+source "$(ENV_6)"
+env_ref_4 = $(ENV_7) # Never evaluated
diff --git a/testsuite.py b/testsuite.py
index 4faed1f..e22c775 100644
--- a/testsuite.py
+++ b/testsuite.py
@@ -2344,7 +2344,13 @@ config J
print("Testing preprocessor")
- os.environ["ENV_VAR"] = "env"
+ os.environ["ENV_1"] = "env_1"
+ os.environ["ENV_2"] = "env_2"
+ os.environ["ENV_3"] = "env_3"
+ os.environ["ENV_4"] = "env_4"
+ os.environ["ENV_5"] = "n"
+ os.environ["ENV_6"] = "Kconfiglib/tests/empty"
+ os.environ["ENV_7"] = "env_7"
# We verify warnings manually
c = Kconfig("Kconfiglib/tests/Kpreprocess", warn_to_stderr=False)
@@ -2392,7 +2398,7 @@ config J
verify_str(c.syms["PRINT_ME"], r"""
config PRINT_ME
string
- prompt "env" if (FOO && BAR) || !BAZ || !QAZ
+ prompt "env_1" if (FOO && BAR) || !BAZ || !QAZ
default "\"foo\"" if "foo \"bar\" baz" = ""
""")
@@ -2439,6 +2445,10 @@ config PRINT_ME
else:
fail("expanding error-y-res didn't raise an exception")
+ # Check Kconfig.env_vars
+ verify_equal(c.env_vars,
+ set(("ENV_1", "ENV_2", "ENV_3", "ENV_4", "ENV_5", "ENV_6")))
+
# Check that the expected warnings were generated
verify_equal(c.warnings, [
"Kconfiglib/tests/Kpreprocess:116: warning: 'echo message on stderr >&2' wrote to stderr: message on stderr",