-
Notifications
You must be signed in to change notification settings - Fork 156
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
Glob #40
Glob #40
Conversation
Some projects use wildcards when sourcing a Kconfig file. Add support for globbing the files that match the wildcards and process them one by one. Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no> Signed-off-by: Anas Nashif <anas.nashif@intel.com>
Signed-off-by: Sebastian Bøe <sebastian.boe@nordicsemi.no>
If this could be supported upstream, then we just need to figure out how it is enabled. E.g.; a new source statement, like rsource, (gsource, globbed_source?) or a config constructor argument like 'warn' (like the current patch). NB: rsource has not been patched to support this, but I assume that would be desirable if this is accepted. |
Hello, Would you still be fine with a separate keyword (e.g. One handy thing about Having If you're fine with that approach, I'll implement it for you. I want to keep the "common" paths like |
I'm pretty sure I can sell using gsource to the Zephyr project. It makes sense. Globbed sourcing has different semantics from source, so it should have a different keyword as well. I'll post an RFC this week. |
Do you think this version would be fine? Currently, you always get filenames without the diff --git a/kconfiglib.py b/kconfiglib.py
index 85a0054..ec26517 100644
--- a/kconfiglib.py
+++ b/kconfiglib.py
@@ -354,6 +354,7 @@ Send bug reports, suggestions, and questions to ulfalizer a.t Google's email
service, or open a ticket on the GitHub page.
"""
import errno
+import glob
import os
import platform
import re
@@ -1839,6 +1840,34 @@ class Kconfig(object):
prev_node)
self._leave_file()
+ elif t0 == _T_GSOURCE:
+ # Temporarily cd into $srctree if it is set before globbing.
+ # This setup makes the globbed filenames appear without a
+ # $srctree prefix, which is consistent with how 'source' and
+ # 'rsource' work. The normal $srctree logic will find the files
+ # later.
+ prev_dir = os.getcwd()
+ try:
+ if self.srctree is not None:
+ os.chdir(self.srctree)
+
+ # Sort the glob results to ensure a consistent ordering of
+ # Kconfig symbols, which indirectly ensures a consistent
+ # ordering in e.g. .config files
+ filenames = sorted(glob.glob(
+ self._expand_syms(self._expect_str_and_eol())))
+
+ finally:
+ os.chdir(prev_dir)
+
+ for filename in filenames:
+ self._enter_file(filename)
+ prev_node = self._parse_block(None, # end_token
+ parent,
+ visible_if_deps,
+ prev_node)
+ self._leave_file()
+
elif t0 == end_token:
# We have reached the end of the block. Terminate the final
# node and return it.
@@ -4699,6 +4728,7 @@ STR_TO_TRI = {
_T_EQUAL,
_T_GREATER,
_T_GREATER_EQUAL,
+ _T_GSOURCE,
_T_HELP,
_T_HEX,
_T_IF,
@@ -4725,7 +4755,7 @@ STR_TO_TRI = {
_T_TRISTATE,
_T_UNEQUAL,
_T_VISIBLE,
-) = range(45)
+) = range(46)
# Public integers representing expression types
#
@@ -4759,6 +4789,7 @@ _get_keyword = {
"endif": _T_ENDIF,
"endmenu": _T_ENDMENU,
"env": _T_ENV,
+ "gsource": _T_GSOURCE,
"help": _T_HELP,
"hex": _T_HEX,
"if": _T_IF,
@@ -4792,6 +4823,7 @@ _STRING_LEX = frozenset((
_T_BOOL,
_T_CHOICE,
_T_COMMENT,
+ _T_GSOURCE,
_T_HEX,
_T_INT,
_T_MAINMENU, |
Guess some people might wince at the |
Here's a Mark II without the diff --git a/kconfiglib.py b/kconfiglib.py
index 85a0054..24220d5 100644
--- a/kconfiglib.py
+++ b/kconfiglib.py
@@ -354,6 +354,7 @@ Send bug reports, suggestions, and questions to ulfalizer a.t Google's email
service, or open a ticket on the GitHub page.
"""
import errno
+import glob
import os
import platform
import re
@@ -1839,6 +1840,33 @@ class Kconfig(object):
prev_node)
self._leave_file()
+ elif t0 == _T_GSOURCE:
+ # If $srctree is set, glob relative to it
+ pattern = self._expand_syms(self._expect_str_and_eol())
+ if self.srctree is not None:
+ pattern = os.path.join(self.srctree, pattern)
+
+ # Sort the glob results to ensure a consistent ordering of
+ # Kconfig symbols, which indirectly ensures a consistent
+ # ordering in e.g. .config files
+ for filename in sorted(glob.glob(pattern)):
+ if self.srctree is not None:
+ # Strip the $srctree prefix from the filename and let
+ # the normal $srctree logic find the file. This makes
+ # the globbed filenames appear without a $srctree
+ # prefix in MenuNode.filename, which is consistent with
+ # how 'source' and 'rsource' work. We get the same
+ # behavior as if the files had been 'source'd one by
+ # one.
+ filename = os.path.relpath(filename, self.srctree)
+
+ self._enter_file(filename)
+ prev_node = self._parse_block(None, # end_token
+ parent,
+ visible_if_deps,
+ prev_node)
+ self._leave_file()
+
elif t0 == end_token:
# We have reached the end of the block. Terminate the final
# node and return it.
@@ -4699,6 +4727,7 @@ STR_TO_TRI = {
_T_EQUAL,
_T_GREATER,
_T_GREATER_EQUAL,
+ _T_GSOURCE,
_T_HELP,
_T_HEX,
_T_IF,
@@ -4725,7 +4754,7 @@ STR_TO_TRI = {
_T_TRISTATE,
_T_UNEQUAL,
_T_VISIBLE,
-) = range(45)
+) = range(46)
# Public integers representing expression types
#
@@ -4759,6 +4788,7 @@ _get_keyword = {
"endif": _T_ENDIF,
"endmenu": _T_ENDMENU,
"env": _T_ENV,
+ "gsource": _T_GSOURCE,
"help": _T_HELP,
"hex": _T_HEX,
"if": _T_IF,
@@ -4792,6 +4822,7 @@ _STRING_LEX = frozenset((
_T_BOOL,
_T_CHOICE,
_T_COMMENT,
+ _T_GSOURCE,
_T_HEX,
_T_INT,
_T_MAINMENU, |
Hmm... think that version is broken for absolute paths with |
@ulfalizer @SebastianBoe this version worked fine with absolute paths and |
@carlescufi Here's what I had in mind: When Something like
would then search
You could then still have It's splitting hairs anyway, as I might be the only one who cares about those corner cases. :P |
Heh... thinking about it, the only reason the C tools do that is probably so that people can have a |
'gsource' works like 'source', but takes a glob pattern and sources all matching files. Works as a no-op if no files match, and hence doubles as an include-if-exists function, similar to '-include' in 'make'. Add a 'grsource' statement as well, mirroring 'rsource'. Came up in #40.
I pushed a fixed version of the second version: daac69d. I implemented Tell me if you have any objections. It could be tweaked still, as it's not in any release. |
This is an RFC for changing the Kconfig dialect that Zephyr is using. Zephyr has copied and modified the Linux kernel's configuration language into it's own tree. One modification that Zephyr has done is changed the semantics of the 'source' keyword. Instead of the 'source' argument being treated as a path it is treated as a glob pattern[0]. This is a backwards-incompatible change with the kernel's configuration language, not just an extention of what 'source' accepts. Meaning, running our Kconfig processor on the Linux kernel's sources can produce different results. Specifically, if there is a typo in the 'source' path the linux kernel will trigger an error, but in Zephyr this is silently treated as a pattern that had no matches. Also, when glob special characters are used the two languages will match on different source files. The Linux kernel's literal 'source' keyword is not present in Zephyr, but it is useful, and often desired behaviour. E.g. the intended and expected behaviour of: source "Kconfig.zophyr" would be to trigger an error due to a typo in the file name. But in the Zephyr dialect this is treated as a valid glob pattern that happened to not have any matches. Another issue with changing the semantics of 'source' is that Kconfig code and Kconfig infrastructure can no longer be re-used between the Linux kernel and Zephyr as they are working with different languages. To resolve these issues this RFC proposes to revert 'source' back to the original Linux kernel semantics and introduce the 'gsource' keyword to cover the use-case of wildcard matches. In this solution the linux kernel's language will be a subset of the Zephyr language and if a user wanted to he could write portable Kconfig if he limited himself to this subset. Script to reword wildcard matches to 'gsource': https://gist.github.com/SebastianBoe/8d36b5b1af0491e5abc8fd598ded976c RFC to introduce 'gsource': ulfalizer/Kconfiglib#40 [0] https://en.wikipedia.org/wiki/Glob_(programming) Signed-off-by: Sebastian Bøe <sebastian.boe@nordicsemi.no>
Closing for now. |
Hi @ulfalizer , before I start testing and cleaning up this patch, I would like to request comments on whether the glob extension could become an opt-in feature in Kconfiglib.
The motivation from our side is to avoid forking.