summaryrefslogtreecommitdiff
path: root/kconfiglib.py
AgeCommit message (Collapse)Author
2018-12-08_save_old() code nitsUlf Magnusson
2018-12-08Simplify 'verbose' check in write_config()Ulf Magnusson
2018-12-08Preserve symlinks when writing .config.old filesUlf Magnusson
if .config is a symlink, then the intention is probably to overwrite the target, but rename()ing the symlink to .config.old interferes with that. Use the shutil.copyfile() fallback instead if .config is a symlink.
2018-12-08Make {load,write}_config(filename=None) implement the standard behaviorUlf Magnusson
Make the previously obligatory 'filename' argument to load_config() and write_config() default to None, and have that implement the behavior you'd usually want: read/write either KCONFIG_CONFIG or ".config" if unset, and read the 'option defconfig_list' configuration file if KCONFIG_CONFIG/".config" doesn't exist. For load_config(), filename=None also allows the configuration file to be missing without raising an error. load_config() returns True if a local configuration file was loaded, which is useful to check in the menuconfig (if no local configuration file exists, we always want to prompt for saving the configuration when exiting). Also add a 'verbose' argument (default True) to load_config() and read_config() that makes them print which files were read/written in filename=None mode. Also generalize olddefconfig.py and oldconfig.py to not require there to already be a local configuration file. This was a bit silly for olddefconfig.py in particular. Remove the examples/defconfig.py script. It's a duplicate of olddefconfig.py.
2018-12-08Save existing configuration to .<filename>.old in write_config()Ulf Magnusson
Add a default-True 'save_old' flag to write_config(). If 'save_old' is True and an existing configuration file is being overwritten, a copy of the old configuration file is saved to .<filename>.old (e.g. .config.old) in the same directory. Errors are ignored, as the old configuration would usually just be a nice-to-have, and not essential. The same functionality could be added for minimal configuration files and headers, but it's probably most useful for configuration files.
2018-12-07Rename 'val_sym' to 'sym' in str_value()Ulf Magnusson
Consistent with elsewhere.
2018-12-07Remove _internal_error()Ulf Magnusson
These type sanity checks have never hit once during development over the years, and were inconsistently applied too. Remove them to simplify the code. Keep the InternalError exception for backwards compatibility, in case something catches it.
2018-12-01Flag removed symbols as changed in sync_deps()Ulf Magnusson
If a symbol is removed from a Kconfig file, it makes sense to flag it as changed, so that things that still (probably accidentally) depend on it get rebuilt. Saw a patch for the C tools with the same effect floating around, so might as well add it already. The C tools had other brokeness as well though...
2018-12-01Tighten up help text parsingUlf Magnusson
Get rid of _indentation() and inline it into _parse_help(), adding some simplifications and optimizations along the way. Saves a few % of parsing of time. Help text parsing is surprisingly hot.
2018-11-26Reorder some tuples to put y firstUlf Magnusson
CONFIG_FOO=y and 'default y' and the like are more common than the n versions, so test for them first. Turning the tuples into sets would be even better on Python 3, as it optimizes sets with constant keys into a LOAD_CONST, but it has a performance penalty on Python 2.
2018-11-26Speed up more token membership testsUlf Magnusson
Similar change to commit 4b23936 ("Speed up some token tests"), just for more tests. This especially helps for the "not a property line" case in _parse_properties(), which checks against all the cases. Saves 2-3% of parsing time together with the earlier change.
2018-11-25Whitespace nitUlf Magnusson
2018-11-25Speed up some token testsUlf Magnusson
Turn some of the hotter membership test tuples into global sets, like was already done for _TYPE_TOKENS. That saves some global lookups for the tuple members and avoids repeatedly recreating tuples. It's 30%-50% faster in a microbenchmark, even for two-element tuples (with global lookups for the members).
2018-11-25Move _TOKEN_TO_TYPE conversion into _set_type()Ulf Magnusson
Factors out some code. Also use a quick 'is not UNKNOWN' test first inside it, which will usually fail, since single-def symbols are more common. That avoids building a tuple too.
2018-11-24Use constants when _tokens_i is knownUlf Magnusson
Same deal as for the initial token, except we sometimes know that we're dealing with the second token as well. Inline _expect_nonconst_sym_and_eol() and _expect_str(), which are single-use. That allows more specific error messages to be used as well. Also tweak an outdated comment in _tokenize() re. None-termination. Token fetching is more manual now.
2018-11-24Simplify eval_string() a bitUlf Magnusson
2018-11-24Optimize fetching of initial token on lineUlf Magnusson
Another possible optimization was missed in commit ab89ef6 ("Get rid of _next_token() and _peek_token()"): The index of the initial token on a line is known to be 0, so there's no need to check _tokens_i. Also reads a bit clearer.
2018-11-23Clarify that select/imply is a no-op for choice symbolsUlf Magnusson
The warning for selecting/implying a choice symbol could be misunderstood as saying that select/imply has no effect on choice symbols in a particular case. Select/imply never has an effect on choice symbols though. Rephrase the warning to make it clearer.
2018-11-23Flag extra trailing tokens in all contextsUlf Magnusson
The following cases were let through without a parse error (with the extra tokens just being ignored): - endif/endmenu/enchoice <extra tokens> - default FOO <extra tokens> (though 'default FOO if' flagged an error) Make them generate an error.
2018-11-23Get rid of _next_token() and _peek_token()Ulf Magnusson
These are pretty hot. Inline them to save a few % of parsing time. They're pretty simple anyway. _tokens_i was initialized to -1 to simplify the _next_token() implementation. With _next_token() gone, initialize it to 0 instead, which simplifies some other code.
2018-11-21Make quotes consistent in parse error messagesUlf Magnusson
2018-11-21Remove message re. Kconfiglib 10 backwards compat. breakUlf Magnusson
The message has been in for three months now. Hopefully that was enough for it to get noticed.
2018-11-21Use 'foo.__class__ is Bar' instead of 'isinstance(foo, Bar)'Ulf Magnusson
This is 30%-60% faster for both the matching and non-matching case, as measured with timeit on Python 2.7 and 3.6, and saves at least a few percent of total parsing time (and probably some evaluation time too). isinstance(foo, tuple) is particularly slow. Symbol and Choice instances are always created by us, so potential subclassing shouldn't be a problem.
2018-11-18sync_deps() docstring format nitUlf Magnusson
2018-11-17Add support for KCONFIG_ALLCONFIGUlf Magnusson
This allows some symbol values to be forced while running all{def,no,yes,mod}config.py. See Documentation/kbuild/kconfig.txt in the Linux kernel. Add a helper function load_allconfig() to Kconfiglib to avoid code duplication in the tools. Also add functions for enabling/disabling the warning that's generated when a symbol is assigned multiple times in a (set of) .config files and the values differ. It should be disabled when merging the KCONFIG_ALLCONFIG configuration file. Previously, only the warning generated when the assigned values are identical could be disabled. Disable all warnings related to assigning a symbol multiple times in examples/merge_config.py as well.
2018-11-17Kconfig formatting nit in docstringUlf Magnusson
2018-11-17Remove outdated comment in eval_string()Ulf Magnusson
2018-11-07Add a fast path for string parsingUlf Magnusson
For strings with no $ or \ in them (99.86% of all strings in the Linux x86 Kconfigs), we can just find() the matching quote directly. Saves a few % of tokenization time.
2018-11-06Comment nitUlf Magnusson
2018-11-06Always strip trailing whitespace in 'MenuNode.help' and __str__()Ulf Magnusson
Previously, you could get either one or two newlines at the end of MenuNode.help and the various __str__() methods, though this wasn't documented. Always stripping trailing whitespace is cleaner e.g. when using print(), which automatically appends a trailing newline, and makes things consistent. Hopefully nothing relied on the old undocumented behavior. It's fine for genrest.py at least.
2018-11-06Whitespace nitUlf Magnusson
2018-11-03Make UNKNOWN falsyUlf Magnusson
Set UNKNOWN (representing 'no type') to 0, which is falsy, to simplify some checks. Also reorder some dictionary keys for consistency.
2018-11-02Fix removal of multiple consecutive 'if' nodesUlf Magnusson
Despite 'if' nodes being flattened before 'if' removal, consecutive 'if' nodes can still show up for code like the following: ... if X endif if X endif ... _remove_ifs() failed to remove the second 'if' node, leading to a crash e.g. when turning on show-all mode in the menuconfig in a menu with such code (due to the unexpected 'if' node). Stuff like the above could potentially result from 'osource's with no matches, though I just spotted the error while looking over the code. Fix the 'if' removal logic to properly handle consecutive 'if' nodes.
2018-11-01Don't show backtraces for expected exceptions in toolsUlf Magnusson
KconfigError and IOError are part of normal operation and don't indicate a problem with the library itself. Catch and print them in standard_kconfig() and sys.exit(), to avoid spammy backtraces from e.g. menuconfig.py when Kconfig files don't exist or have errors.
2018-11-01Refactor _remove_ifs()Ulf Magnusson
Can get away with a single variable by assigning node.list earlier, and save a tiny bit of work with a chained assignment. Also clarify what the tricky Python chained assignments correspond to, where it matters.
2018-10-31Make errno/strerror/filename available on IOErrorUlf Magnusson
An error reporting flaw was that most raised IOErrors got their errno/strerror/filename fields stripped, due to wanting to show a custom messages. The problem was that adding back 'errno' and 'strerror' made IOError.__str__() always return a fixed string ("[Errno <errno>] <strerror>"), ignoring any custom message. This is friendly to users, but unfriendly to scripts (the menuconfig had a workaround). Make things friendly to both by raising an internal subclass of IOError instead, that preserves errno/strerror/filename but prints a custom message. The exception can then still be caught as IOError/OSError by scripts.
2018-10-30Simplify _INIT_SRCTREE_NOTE definitionUlf Magnusson
2018-10-30Remove no longer needed 'node.item is None' testsUlf Magnusson
After commit e0256b6 ("Have MENU and COMMENT match _T_MENU and _T_COMMENT"), the only falsy value for node.item is None, since all token constants are truthy (since they're never 0).
2018-10-28Replace 'indent == 0' with 'not indent'Ulf Magnusson
Tiny bit faster and smaller. Clear in context.
2018-10-28Have MENU and COMMENT match _T_MENU and _T_COMMENTUlf Magnusson
Same approach as for the expression type symbolic constants. Removes a tiny bit of conversion and makes things a bit more consistent.
2018-10-28Shorten exception class definitions a bitUlf Magnusson
2018-10-28Remove leftover comment in _add_choice_deps()Ulf Magnusson
Made more sense when the code was part of _build_dep(). The same thing is explained in the function docstring now.
2018-10-28Micro-optimize dependency loop checkingUlf Magnusson
Turn the _check_dep_loop_sym lookup into a LOAD_FAST inside the loop.
2018-10-26Support enabling the assignment-to-undef. symbol warning via the environmentUlf Magnusson
This makes it possible to enable it for the bundled tools, by setting KCONFIG_WARN_UNDEF_ASSIGN=y. Previously, the code had to be modified to call Kconfig.enable_undef_warnings(). Also rename KCONFIG_STRICT to KCONFIG_WARN_UNDEF, for consistency. Keep supporting KCONFIG_STRICT as an alias for backwards compatibility.
2018-10-25Move _is_num() into _check_undef_syms()Ulf Magnusson
_check_undef_syms() is the only caller.
2018-10-22Use standard_sc_expr_str() in _name_and_loc()Ulf Magnusson
Makes choices show up as <choice (name, if any)>, which is nice. Previously, just the name was shown for named choices.
2018-10-22Move _check_sym/choice_sanity() into the Kconfig classUlf Magnusson
This makes the calls to Kconfig._warn() a bit less awkward (self._warn() instead of sym/choice.kconfig._warn()). Also move the loops over the symbols/choices into the functions, as a small optimization.
2018-10-22Micro-optimize _make_depend_on() callsUlf Magnusson
Store the function in a local variable outside the loop in _build_dep().
2018-10-21Allow programmatically expanding preprocessor functions with argsUlf Magnusson
Add a Variable.expanded_value_w_args() function for expanding a preprocessor function with particular arguments. This is what expanded_value should have been, because expanded_value_w_args() is more general (expanded_value corresponds to zero arguments). Keep expanded_value for backwards compatibility though. Also add a simple __repr__() to Variable. It could show the expanded value, but that might mean calling shell functions or user-defined functions as a side effect (possibly with missing arguments). Not sure that's a good idea, so just show the unexpanded value.
2018-10-21Test _parse_block() blank line case earlierUlf Magnusson
Can be treated as just another case, and moved later so that the cases become sorted by frequency.