Skip to content

Add support for rust cfg response file #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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
Expand Down