From c15f9789c32c38268a851cd1f1c58cc2d06254de Mon Sep 17 00:00:00 2001 From: Fiona Behrens Date: Mon, 4 Nov 2024 12:08:20 +0100 Subject: [PATCH] Add support for rust cfg response file Add support to write the rustc_cfg response file via write_rustcfg. This is equivalent to the rust response file used for rust support in the kernel as introduced in 2f7ab1267dc9b2d1f29695aff3211c87483480f3 in the kernel tree. Link: https://lore.kernel.org/rust-for-linux/20220212130410.6901-17-ojeda@kernel.org/#Z31scripts:kconfig:confdata.c --- kconfiglib.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ testsuite.py | 12 ++++++++++++ 2 files changed, 56 insertions(+) diff --git a/kconfiglib.py b/kconfiglib.py index a50312e..de8853d 100644 --- a/kconfiglib.py +++ b/kconfiglib.py @@ -1729,6 +1729,50 @@ def _min_config_contents(self, header): return "".join(chunks) + def write_rustcfg(self, filename=None): + r""" + Write out rustcfg flags as a response file, matching the format used + by include/generated/rustc_cfg in the kernel. + + filename (default: None): + Path to write respone file to. + + If None (the default), the path in the environment variable + KCONFIG_RUSTCCFG is used if set, and "include/generated/rustc_cfg" + otherwise. This is compatible with the C tools. + """ + if filename is None: + filename = os.getenv("KCONFIG_RUSTCCFG", + "include/generated/rustc_cfg") + + if self._write_if_changed(filename, self._rustcfg_contents()): + return "Kconfig cfg saved to '{}".format(filename) + return "No changes to Kconfig cfg in '{}'".format(filename) + + def _rustcfg_contents(self): + chunks = [] + add = chunks.append + + for sym in self.unique_defined_syms: + if not sym.choice and \ + sym.visibility <= expr_value(sym.rev_dep): + continue + + val = sym.str_value + if sym.orig_type in _BOOL_TRISTATE: + # We do not care about disabled ones, would be a comment + if val == "n": + continue + add("--cfg={}{}\n" + .format(self.config_prefix, sym.name)) + elif sym.orig_type == HEX: + if not val.lower().startswith("0x"): + val = "0x{}".format(val); + add("--cfg={}{}={}\n" + .format(self.config_prefix, sym.name, escape(val))) + + return "".join(chunks) + def sync_deps(self, path): """ Creates or updates a directory structure that can be used to avoid diff --git a/testsuite.py b/testsuite.py index a4ea556..9744bde 100644 --- a/testsuite.py +++ b/testsuite.py @@ -1986,6 +1986,18 @@ def verify_file_contents(fname, contents): #define CONFIG_I 5 #define CONFIG_N 6 #define CONFIG_G 7 +"""[1:]) + + c.write_rustcfg(config_test_file) + verify_file_contents(config_test_file, """ +--cfg=CONFIG_O=0 +--cfg=CONFIG_R=1 +--cfg=CONFIG_D=2 +--cfg=CONFIG_E=3 +--cfg=CONFIG_R2=4 +--cfg=CONFIG_I=5 +--cfg=CONFIG_N=6 +--cfg=CONFIG_G=7 """[1:]) # Differs from defaults