Skip to content

Commit b437f13

Browse files
committed
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/[email protected]/#Z31scripts:kconfig:confdata.c
1 parent 061e71f commit b437f13

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

kconfiglib.py

+44
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,50 @@ def _min_config_contents(self, header):
17291729

17301730
return "".join(chunks)
17311731

1732+
def write_rustcfg(self, filename=None):
1733+
r"""
1734+
Write out rustcfg flags as a response file, matching the format used
1735+
by include/generated/rustc_cfg in the kernel.
1736+
1737+
filename (default: None):
1738+
Path to write respone file to.
1739+
1740+
If None (the default), the path in the environment variable
1741+
KCONFIG_RUSTCCFG is used if set, and "include/generated/rustc_cfg"
1742+
otherwise. This is compatible with the C tools.
1743+
"""
1744+
if filename is None:
1745+
filename = os.getenv("KCONFIG_RUSTCCFG",
1746+
"include/generated/rustc_cfg")
1747+
1748+
if self._write_if_changed(filename, self._rustcfg_contents()):
1749+
return "Kconfig cfg saved to '{}".format(filename)
1750+
return "No changes to Kconfig cfg in '{}'".format(filename)
1751+
1752+
def _rustcfg_contents(self):
1753+
chunks = []
1754+
add = chunks.append
1755+
1756+
for sym in self.unique_defined_syms:
1757+
if not sym.choice and \
1758+
sym.visibility <= expr_value(sym.rev_dep):
1759+
continue
1760+
1761+
val = sym.str_value
1762+
if sym.orig_type in _BOOL_TRISTATE:
1763+
# We do not care about disabled ones, would be a comment
1764+
if val == "n":
1765+
continue
1766+
add("--cfg={}{}\n"
1767+
.format(self.config_prefix, sym.name))
1768+
elif sym.orig_type == HEX:
1769+
if not val.lower().startswith("0x"):
1770+
val = "0x{}".format(val);
1771+
add("--cfg={}{}={}\n"
1772+
.format(self.config_prefix, sym.name, escape(val)))
1773+
1774+
return "".join(chunks)
1775+
17321776
def sync_deps(self, path):
17331777
"""
17341778
Creates or updates a directory structure that can be used to avoid

testsuite.py

+12
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,18 @@ def verify_file_contents(fname, contents):
19871987
#define CONFIG_I 5
19881988
#define CONFIG_N 6
19891989
#define CONFIG_G 7
1990+
"""[1:])
1991+
1992+
c.write_rustcfg(config_test_file)
1993+
verify_file_contents(config_test_file, """
1994+
--cfg=CONFIG_O=0
1995+
--cfg=CONFIG_R=1
1996+
--cfg=CONFIG_D=2
1997+
--cfg=CONFIG_E=3
1998+
--cfg=CONFIG_R2=4
1999+
--cfg=CONFIG_I=5
2000+
--cfg=CONFIG_N=6
2001+
--cfg=CONFIG_G=7
19902002
"""[1:])
19912003

19922004
# Differs from defaults

0 commit comments

Comments
 (0)