From 6156041560c75831e003202b790ab3f435bbb388 Mon Sep 17 00:00:00 2001 From: Ulf Magnusson Date: Thu, 20 Sep 2018 13:33:59 +0200 Subject: Add support for user-defined Python preprocessor functions Allow preprocessor functions to be defined in Python by putting a module called 'kconfigfunctions' into sys.path. Internally, this simply adds the functions to the predefined functions in Kconfig._functions. User-defined Python functions make it simple to integrate information from existing Python tools into Kconfig, e.g. to have Kconfig symbols depend on hardware information stored in some other format. This might be used to get device tree information into Kconfig in Zephyr. Piggyback module docstring documentation for some extensions that were previously only mentioned in the README. --- testsuite.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'testsuite.py') diff --git a/testsuite.py b/testsuite.py index b5f0388..802234e 100644 --- a/testsuite.py +++ b/testsuite.py @@ -2459,6 +2459,39 @@ config PRINT_ME_TOO ]) + print("Testing user-defined preprocessor functions") + + # Make Kconfiglib/tests/kconfigfunctions.py importable + sys.path.insert(0, "Kconfiglib/tests") + + c = Kconfig("Kconfiglib/tests/Kuserfunctions") + + verify_variable("add-zero", "$(add)", "0", True) + verify_variable("add-one", "$(add,1)", "1", True) + verify_variable("add-three", "$(add,1,-1,2,1)", "3", True) + + verify_variable("one-one", "$(one,foo bar)", "onefoo barfoo bar", True) + + verify_variable("one-or-more-one", "$(one-or-more,foo)", "foo + ", True) + verify_variable("one-or-more-three", "$(one-or-more,foo,bar,baz)", + "foo + bar,baz", True) + + def verify_bad_argno(name): + try: + c.variables[name].expanded_value + except KconfigError: + pass + else: + fail("Expected '{}' expansion to flag wrong number of arguments, " + "didn't".format(name)) + + verify_bad_argno("one-zero") + verify_bad_argno("one-two") + verify_bad_argno("one-or-more-zero") + + sys.path.pop(0) + + print("Testing KCONFIG_STRICT") os.environ["KCONFIG_STRICT"] = "y" -- cgit v1.2.3