Lines Matching +full:python +full:- +full:magic

1 # Copyright (c) 2011-2019, Ulf Magnusson
2 # SPDX-License-Identifier: ISC
8 Kconfiglib is a Python 2/3 library for scripting and extracting information
9 from Kconfig (https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt)
27 $ wget -qO- https://raw.githubusercontent.com/ulfalizer/Kconfiglib/master/makefile.patch | git am
28 …$ wget -qO- https://raw.githubusercontent.com/ulfalizer/Kconfiglib/master/makefile.patch | patch -
30 Warning: Not passing -p1 to patch will cause the wrong file to be patched.
43 $ git am Kconfiglib/makefile.patch (or 'patch -p1 < Kconfiglib/makefile.patch')
53 ----------------
55 This target runs the curses menuconfig interface with Python 3. As of
56 Kconfiglib 12.2.0, both Python 2 and Python 3 are supported (previously, only
57 Python 3 was supported, so this was a backport).
61 --------------
63 This target runs the Tkinter menuconfig interface. Both Python 2 and Python 3
64 are supported. To change the Python interpreter used, pass
65 PYTHONCMD=<executable> to 'make'. The default is 'python'.
69 --------------------------------
71 This target gives an interactive Python prompt where a Kconfig instance has
72 been preloaded and is available in 'kconf'. To change the Python interpreter
73 used, pass PYTHONCMD=<executable> to 'make'. The default is 'python'.
89 ----------------------------------------------------
91 This target runs the Python script given by the SCRIPT parameter on the
92 configuration. sys.argv[1] holds the name of the top-level Kconfig file
100 -------------------
127 CC_VERSION_TEXT (gcc (Ubuntu 7.3.0-16ubuntu3) 7.3.0)
138 $ srctree=. ARCH=x86 SRCARCH=x86 KERNELVERSION=`make kernelversion` ... python(3)
142 Search the top-level Makefile for "Additional ARCH settings" to see other
193 In this example, A && B && C && D (the prompt condition) needs to be non-n for
209 select*ing* symbol. 'select' does not respect visibility, so non-visible
212 For non-bool/tristate symbols, it only matters whether the visibility is n or
213 non-n: m visibility acts the same as y visibility.
241 sym.visibility is non-0 (non-n) to see whether the user value will have an
250 top-level menu, the title of which is shown at the top in the standard
256 following the 'next' pointer. Usually, a non-None 'list' pointer indicates a
257 menu or Choice, but menu nodes for symbols can sometimes have a non-None 'list'
302 ---------- --------------
329 - The value of A && B is min(A.tri_value, B.tri_value)
331 - The value of A || B is max(A.tri_value, B.tri_value)
333 - The value of !A is 2 - A.tri_value
335 - The value of A = B is 2 (y) if A.str_value == B.str_value, and 0 (n)
358 ---------------------------
367 +--Kconfig
369 +--src
370 +--Kconfig
372 +--SubSystem1
373 +--Kconfig
375 +--ModuleA
376 +--Kconfig
391 'rsource' can be used to create "position-independent" Kconfig trees that can
396 -----------------
413 For example, the following statements will be no-ops if neither "foo" nor any
421 'source' and 'osource' are analogous to 'include' and '-include' in Make.
425 --------------------------
433 -----------------------
437 - KCONFIG_WARN_UNDEF: If set to 'y', warnings will be generated for all
450 - KCONFIG_WARN_UNDEF_ASSIGN: If set to 'y', warnings will be generated for
458 Preprocessor user functions defined in Python
459 ---------------------------------------------
461 Preprocessor functions can be defined in Python, which makes it simple to
462 integrate information from existing Python tools into Kconfig (e.g. to have
465 Putting a Python module named kconfigfunctions(.py) anywhere in sys.path will
474 that maps function names to Python functions, as follows:
481 # Name of the user-defined function ("my-fn"). Think argv[0].
494 "my-fn": (my_fn, <min.args>, <max.args>/None),
495 "my-other-fn": (my_other_fn, <min.args>, <max.args>/None),
520 depends on $(my-fn,arg1,arg2)
531 User-defined preprocessor functions are called as they're encountered at parse
537 Preferably, user-defined functions should be stateless.
583 read-only, and some are implemented through @property magic.
590 Undefined symbols can be recognized by Symbol.nodes being empty -- see
640 'source "/foo/Kconfig"') are kept as-is.
643 top-level Kconfig file. If a file is source'd multiple times, it will
700 Gotcha: scripts/kconfig/Makefile might pass --defconfig=<defconfig> to
706 The menu node (see the MenuNode class) of the implicit top-level menu.
711 Can be changed with the 'mainmenu' statement (see kconfig-language.txt).
721 When 'warn' is False, the values of the other warning-related variables
771 right-hand side of the assignment verbatim.
784 out-of-tree builds. The C tools use this environment variable in the same
810 The current parsing location, for use in Python preprocessor functions.
850 # Parsing-related
868 encoding="utf-8", suppress_traceback=False):
881 Python 3, so it's enough to catch OSError there. If you need Python 2/3
883 common base class of OSError/IOError on Python 2 and an alias for
884 OSError on Python 3.
888 from the top-level directory, as environment variables will make sure
919 encoding (default: "utf-8"):
924 The "utf-8" default avoids exceptions on systems that are configured
927 This parameter has no effect on Python 2, due to implementation
929 distinct in Python 2). Python 2 doesn't decode regular strings
932 Related PEP: https://www.python.org/dev/peps/pep-0538/
940 This hides the Python traceback for "expected" errors like syntax
1011 # Make n/m/y well-formed symbols
1022 "error-if": (_error_if_fn, 2, 2),
1026 "warning-if": (_warning_if_fn, 2, 2),
1029 # Add any user-defined preprocessor functions
1078 # Open the top-level Kconfig file. Store the readline() method directly
1091 # Close the top-level Kconfig file. __self__ fetches the 'file' object
1097 # Do various menu tree post-processing
1118 check_dep_loop_sym = _check_dep_loop_sym # Micro-optimization
1412 Command-specific configuration filename - "allyes.config",
1472 # is a hidden function call due to property magic.
1706 # non-choice symbols, as selects don't affect choice
1801 # is a hidden function call due to property magic.
1914 that holds the top-level items.
1967 To get the string value of non-bool/tristate symbols, use
1969 all non-bool/tristate symbols have the tristate value 0 (n).
1978 # to monkey-patch things a bit here to reuse it.
2080 evaluated on e.g. the interactive Python prompt.
2123 # This is needed for Python 3, because e2 is deleted after
2126 # https://docs.python.org/3/reference/compound_stmts.html#the-try-statement
2157 # (<include path>, <Python 'file' object for Kconfig file>) tuples.
2173 # doing in-place modification
2205 self.filename, self.linenr = self._include_path[-1]
2232 line = line[:-2] + self._readline()
2248 # checked in _next_line(). This special-casing gets rid of it and makes
2253 line = line[:-2] + self._readline()
2266 # used out there to force evaluation-related warnings to be generated.
2335 # Parses 's', returning a None-terminated list of tokens. Registers any
2360 # (accidentally) accepted stuff like "--help--" and "-help---".
2364 if s.strip(" \t\n-") == "help":
2398 # It's a non-const symbol, except we translate n, m, and y
2433 # Neither a keyword nor a non-const symbol
2446 val = s[i + 1:end_i - 1]
2459 val = expandvars(s[i + 1:end_i - 1]
2535 # None-terminating the token list makes token fetching simpler/faster
2543 # More of these could be added, but the single-use cases are inlined as an
2603 # Expand any macros in the left-hand side of the assignment (the
2616 # $(warning-if,$(foo),ops)), provided it expands to a blank string
2669 if i == -1:
2768 nesting -= 1
2805 # args[1..len(args)-1]. Plain variables are treated as functions
2826 var._n_expansions -= 1
2830 # Built-in or user-defined function
2834 if len(args) - 1 < min_arg or \
2835 (max_arg is not None and len(args) - 1 > max_arg):
2842 expected_args = "{}-{}".format(min_arg, max_arg)
2847 expected_args, len(args) - 1))
2951 # due to tricky Python semantics. The order matters.
2965 # - glob() doesn't support globbing relative to a directory, so
2970 # - Sort the glob results to ensure a consistent ordering of
3248 "Note that modules are supported -- "
3272 # Reuse the tokens for the non-property line later
3314 "one help text -- only the last one will be used")
3316 # Micro-optimization. This code is pretty hot.
3319 # Find first non-blank (not all-space) line and get its
3331 len_ = len # Micro-optimization
3337 indent = len_(expline) - len_(expline.lstrip())
3342 # The help text goes on till the first non-blank line with less indent
3347 add_line = lines.append # Micro-optimization
3359 if len_(expline) - len_(expline.lstrip()) < indent:
3377 # simple top-down approach. See the module docstring for the expression
3392 # It helps to think of the 'expr: and_expr' case as a single-operand OR
3393 # (no ||), and of the 'and_expr: factor' case as a single-operand AND
3410 # Return 'and_expr' directly if we have a "single-operand" OR.
3419 # Return 'factor' directly if we have a "single-operand" AND.
3447 return (self._tokens[self._tokens_i - 1], token,
3474 depend_on = _depend_on # Micro-optimization
3528 # y-mode selection of the choice might change if a choice symbol's
3533 # <choice symbol> <-> <choice> dependency loops, but they make loop
3551 # Post-parsing menu tree processing, including dependency propagation and
3558 # - Copies properties from menu nodes up to their contained
3561 # - Propagates dependencies from parent to child nodes
3563 # - Creates implicit menus (see kconfig-language.txt)
3565 # - Removes 'if' nodes
3567 # - Sets 'choice' types and registers choice symbols
3628 # Add the node's non-node-specific properties to the choice, like
3644 # Due to the similar interface, Choice works as a drop-in replacement
3694 # traverses the menu tree roughly breadth-first, meaning properties on
3765 "the {} symbol {} has a malformed default {} -- "
3775 # the quotes were left out if 'foo' isn't all-uppercase
3782 self._warn("the {0} symbol {1} has a non-{0} default {2}"
3808 self._warn("the {0} symbol {1} has a non-{0} "
3826 msg += "\n - " + split_expr(si, AND)[0].name_and_loc
3889 # - Enable universal newlines mode on Python 2 to ease
3891 # default on Python 3.
3893 # The "U" flag would currently work for both Python 2 and 3, but it's
3894 # deprecated on Python 3, so play it future-safe.
3896 # io.open() defaults to universal newlines on Python 2 (and is an
3897 # alias for open() on Python 3), but it returns 'unicode' strings and
3900 # Parsing x86 Kconfigs on Python 2
3915 # "rU" for parsing performance on Python 2.
3917 # - For Python 3, force the encoding. Forcing the encoding on Python 2
3918 # turns strings into Unicode strings, which gets messy. Python 2
3934 # Otherwise, symbols whose names happen to contain only the letters A-F
3951 # - sym.nodes empty means the symbol is undefined (has no
3954 # - Due to Kconfig internals, numbers show up as undefined Kconfig
3957 # - The MODULES symbol always exists
3964 msg += "\n\n- Referenced at {}:{}:\n\n{}" \
3990 The following attributes are available. They should be viewed as read-only,
3991 and some are implemented through @property magic (but are still efficient
4003 UNKNOWN is for undefined symbols, (non-special) constant symbols, and
4009 menuconfig-like functionality.
4012 The type as given in the Kconfig file, without any magic applied. Used
4017 representing n, m, y. Always 0 (n) for non-bool/tristate symbols.
4051 Returns the empty set for non-bool/tristate symbols and for symbols with
4055 visibility. menuconfig represents this as -M- and -*-, respectively.
4057 For string/hex/int symbols, check if Symbol.visibility is non-0 (non-n)
4064 # What's the highest value it can be assigned? [-1] in Python
4066 sym_high = sym.assignable[-1]
4072 if sym.assignable[-1] >= 1:
4089 symbols get written out. This includes all non-n-valued bool/tristate
4097 even for non-.config-like formats. To write just the symbols that would
4104 That function skips all n-valued symbols.
4108 n-valued symbol entries in there.
4125 Holds the parent Choice for choice symbols, and None for non-choice
4307 # function call (property magic)
4314 # separate code path (post-processing after loading a .config).
4337 # or has an out-of-range user value
4346 "being outside the active range ([{}, {}]) -- falling "
4352 # If the user value is well-formed and satisfies range
4446 # function call (property magic)
4453 # Non-choice symbol
4478 # Reverse (select-related) dependencies take precedence
4494 # Visible choice symbol in y-mode choice. The choice mode limits
4500 # Visible choice symbol in m-mode choice, with set non-0 user value
4530 # hidden function call due to property magic.
4595 visibility is non-n.
4622 "the value {} is invalid for {}, which has type {} -- "
4694 interactive Python prompt.
4703 # Only add quotes for non-bool/tristate symbols
4711 # Only add quotes for non-bool/tristate symbols
4773 Symbol constructor -- not intended to be called directly by Kconfiglib
4785 # - UNKNOWN == 0
4786 # - _visited is used during tree iteration and dep. loop detection
4819 # function call (property magic)
4868 # Invalidating MODULES has wide-ranging effects
4876 # of calculating all other (non-constant) cached values.
4959 "currently being {}-selected by the following symbols:" \
4970 # - 'select A if B' turns into A && B
4971 # - 'select A' just turns into A
4976 msg += "\n - {}, with value {}, direct dependencies {} " \
5000 treated as read-only, and some are implemented through @property magic (but
5019 for menuconfig-like functionality.
5022 The type as given in the Kconfig file, without any magic applied. Used
5029 0 (n) - The choice is disabled and no symbols can be selected. For
5031 the 'optional' flag set (see kconfig-language.txt).
5033 1 (m) - Any number of choice symbols can be set to m, the rest will
5036 2 (y) - One symbol will be y, the rest n.
5046 special-casing in many code paths. This is why there is a lot of
5049 lower bound to m for visible non-optional choices (the reverse
5058 and interface less confusing (especially in a user-facing interface).
5194 # non-optional choices, which is how the C implementation does it
5202 # function call (property magic)
5270 "the value {} is invalid for {}, which has type {} -- "
5310 on e.g. the interactive Python prompt.
5374 Choice constructor -- not intended to be called directly by Kconfiglib
5382 # - UNKNOWN == 0
5383 # - _visited is used during dep. loop detection
5398 # to special-case choices.
5408 # function call (property magic)
5427 # function call (property magic)
5471 in e.g. the 'make menuconfig' interface, though non-visible choices, menus,
5475 The top-level menu node, corresponding to the implicit top-level menu, is
5484 be viewed as read-only.
5499 kconfig-language.txt).
5582 - Menus (node.item == MENU)
5584 - Choices
5586 - Symbols defined with the 'menuconfig' keyword. The children come from
5602 in the top-level Kconfig file passed to Kconfig.__init__(), etc.
5719 evaluated on e.g. the interactive Python prompt.
5885 # ... if dep -> ... if y
5889 # (AND, X, dep) -> X
5953 Exception raised for Kconfig-related errors.
5992 'expr' must be an already-parsed expression from a Symbol, Choice, or
6003 # Short-circuit the n case as an optimization (~5% faster
6009 # Short-circuit the y case as an optimization
6013 return 2 - expr_value(expr[1])
6030 comp = _sym_to_num(v1) - _sym_to_num(v2)
6132 Returns a list containing the top-level AND or OR operands in the
6133 expression 'expr', in the same (left-to-right) order as they appear in
6142 (Having this as an operand might be more future-safe than having two
6146 Pseudo-code examples:
6148 split_expr( A , OR ) -> [A]
6149 split_expr( A && B , OR ) -> [A && B]
6150 split_expr( A || B , OR ) -> [A, B]
6151 split_expr( A || B , AND ) -> [A || B]
6152 split_expr( A || B || (C && D) , OR ) -> [A, B, C && D]
6155 split_expr( A || (B && (C || D)) , OR ) -> [A, B && (C || D)]
6158 # encounter any non-'op' nodes)
6159 split_expr( (A || B) || C , OR ) -> [A, B, C]
6160 split_expr( A || (B || C) , OR ) -> [A, B, C]
6219 help="Top-level Kconfig file (default: Kconfig)")
6251 # PEP-3151).
6289 # Symbol or Choice 'sc' -- the logic is nearly identical.
6300 # Non-tristate choice symbols are only visible in y mode
6307 # Promote m to y if we're dealing with a non-tristate (possibly due to
6330 # Non-constant symbol, or choice
6344 # version seems to be a common idiom. It relies on short-circuit evaluation
6361 # strcmp()-alike that returns -1, 0, or 1
6363 return (s1 > s2) - (s1 < s2)
6409 # Python 3 (3.3+) only. Best choice when available, because it
6423 # <filename>.old file is usually more of a nice-to-have, and not worth
6481 # "Flattens" menu nodes without prompts (e.g. 'if' nodes and non-visible
6531 # due to tricky Python semantics. The order matters.
6564 # Detects dependency loops using depth-first search on the dependency graph
6646 # '<sym FOO> -> <choice> -> <sym FOO>' loop otherwise.
6649 # Prevent the choice from being immediately re-entered via the
6709 msg += "(select-related dependencies: {})\n\n" \
6713 msg += "(imply-related dependencies: {})\n\n" \
6738 e.object[max(e.start - 40, 0):e.end + 40],
6806 # newline-to-space conversion.
6808 # On Python 3 versions before 3.6, it's not possible to specify the
6834 # Are we running on Python 2?
6846 # time), even without assuming Python's small integer optimization (which
6911 "---help---": _T_HELP,
6984 # Client code shouldn't rely on it though, as it was non-zero in
7011 # def_bool -> BOOL, etc.
7024 # Identifier-like lexemes ("missing quotes") are also treated as strings after
7067 # Obligatory (non-optional) sources
7116 # Use ASCII regex matching on Python 3. It's already the default on Python 2.
7136 # expansions in the left-hand side.
7137 _command_match = _re_match(r"\s*([A-Za-z0-9_$-]+)\s*")
7141 _id_keyword_match = _re_match(r"([A-Za-z0-9_$/.-]+)\s*")
7143 # A fragment in the left-hand side of a preprocessor variable assignment. These
7146 _assignment_lhs_fragment_match = _re_match("[A-Za-z0-9_-]*")
7148 # The assignment operator and value (right-hand side) in a preprocessor
7159 # end-of-line, in case the macro is the last thing on the line.
7160 _name_special_search = _re_search(r'[^A-Za-z0-9_$/.-]|\$\(|$')
7162 # A valid right-hand side for an assignment to a string symbol in a .config