Skip to content

Commit

Permalink
Add support for rust cfg response file
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kloenk committed Aug 24, 2024
1 parent 061e71f commit b437f13
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
44 changes: 44 additions & 0 deletions kconfiglib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,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
Expand Down

0 comments on commit b437f13

Please sign in to comment.