Skip to content

Commit

Permalink
Add rustc_cfg file to kconfig
Browse files Browse the repository at this point in the history
Updat kconfiglib.py and add rustc_cfg as output to kconfig.py

Signed-off-by: Finn Behrens <me@kloenk.de>
  • Loading branch information
kloenk committed Aug 24, 2024
1 parent 376085c commit 2fe4b33
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 67 deletions.
12 changes: 12 additions & 0 deletions cmake/modules/kconfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ set_ifndef(AUTOCONF_H ${PROJECT_BINARY_DIR}/include/generated/zephyr/autoconf.h)
# Re-configure (Re-execute all CMakeLists.txt code) when autoconf.h changes
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${AUTOCONF_H})

# rustc_cfg is generated by Kconfig and placed in
# <build>/zephyr/include/generated/rustc_cfg.
# A project may request a custom location by setting RUSTC_CFG explicitly before
# calling 'find_package(Zephyr)' or loading this module.
set_ifndef(RUSTC_CFG ${PROJECT_BINARY_DIR}/include/generated/zephyr/rustc_cfg)

# Folders needed for conf/mconf files (kconfig has no method of redirecting all output files).
# conf/mconf needs to be run from a different directory because of: GH-3408
file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/kconfig/include/generated)
Expand Down Expand Up @@ -367,6 +373,11 @@ if(NOT EXISTS ${autoconf_h_path})
file(MAKE_DIRECTORY ${autoconf_h_path})
endif()

cmake_path(GET RUSTC_CFG PARENT_PATH rustc_cfg_path)
if(NOT EXISTS ${rustc_cfg_path})
file(MAKE_DIRECTORY ${rustc_cfg_path})
endif()

execute_process(
COMMAND ${CMAKE_COMMAND} -E env
${COMMON_KCONFIG_ENV_SETTINGS}
Expand All @@ -378,6 +389,7 @@ execute_process(
${KCONFIG_ROOT}
${DOTCONFIG}
${AUTOCONF_H}
${RUSTC_CFG}
${PARSED_KCONFIG_SOURCES_TXT}
${input_configs}
WORKING_DIRECTORY ${APPLICATION_SOURCE_DIR}
Expand Down
3 changes: 3 additions & 0 deletions scripts/kconfig/kconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def main():
# Write the merged configuration and the C header
print(kconf.write_config(args.config_out))
print(kconf.write_autoconf(args.header_out))
print(kconf.write_rustcfg(args.rustcfg_out))

# Write the list of parsed Kconfig files to a file
write_kconfig_filenames(kconf, args.kconfig_list_out)
Expand Down Expand Up @@ -302,6 +303,8 @@ def parse_args():
help="Output configuration file")
parser.add_argument("header_out",
help="Output header file")
parser.add_argument("rustcfg_out",
help="Output rustcfg file")
parser.add_argument("kconfig_list_out",
help="Output file for list of parsed Kconfig files")
parser.add_argument("configs_in",
Expand Down
127 changes: 60 additions & 67 deletions scripts/kconfig/kconfiglib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1096,8 +1096,6 @@ def _init(self, filename, warn, warn_to_stderr, encoding):

# Do various menu tree post-processing
self._finalize_node(self.top_node, self.y)
for s in self.syms.values():
self._finalize_sym(s)

self.unique_defined_syms = _ordered_unique(self.defined_syms)
self.unique_choices = _ordered_unique(self.choices)
Expand Down Expand Up @@ -1419,7 +1417,7 @@ def load_allconfig(self, filename):
def write_autoconf(self, filename=None, header=None):
r"""
Writes out symbol values as a C header file, matching the format used
by include/generated/zephyr/autoconf.h in the kernel.
by include/generated/autoconf.h in the kernel.
The ordering of the #defines matches the one generated by
write_config(). The order in the C implementation depends on the hash
Expand All @@ -1434,7 +1432,7 @@ def write_autoconf(self, filename=None, header=None):
Path to write header to.
If None (the default), the path in the environment variable
KCONFIG_AUTOHEADER is used if set, and "include/generated/zephyr/autoconf.h"
KCONFIG_AUTOHEADER is used if set, and "include/generated/autoconf.h"
otherwise. This is compatible with the C tools.
header (default: None):
Expand All @@ -1453,7 +1451,7 @@ def write_autoconf(self, filename=None, header=None):
"""
if filename is None:
filename = os.getenv("KCONFIG_AUTOHEADER",
"include/generated/zephyr/autoconf.h")
"include/generated/autoconf.h")

if self._write_if_changed(filename, self._autoconf_contents(header)):
return "Kconfig header saved to '{}'".format(filename)
Expand Down Expand Up @@ -1547,7 +1545,8 @@ def write_config(self, filename=None, header=None, save_old=True,
written.
Errors are silently ignored if <filename>.old cannot be written (e.g.
due to permissions errors).
due to being a directory, or <filename> being something like
/dev/null).
verbose (default: None):
Limited backwards compatibility to prevent crashes. A warning is
Expand Down Expand Up @@ -1730,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 Expand Up @@ -2307,7 +2350,6 @@ def _lookup_sym(self, name):
sym.kconfig = self
sym.name = name
sym.is_constant = False
sym.configdefaults = []
sym.rev_dep = sym.weak_rev_dep = sym.direct_dep = self.n

if self._parsing_kconfigs:
Expand All @@ -2327,7 +2369,6 @@ def _lookup_const_sym(self, name):
sym.kconfig = self
sym.name = name
sym.is_constant = True
sym.configdefaults = []
sym.rev_dep = sym.weak_rev_dep = sym.direct_dep = self.n

if self._parsing_kconfigs:
Expand Down Expand Up @@ -2917,7 +2958,7 @@ def _parse_block(self, end_token, parent, prev):
while self._next_line():
t0 = self._tokens[0]

if t0 in [_T_CONFIG, _T_MENUCONFIG, _T_CONFIGDEFAULT]:
if t0 is _T_CONFIG or t0 is _T_MENUCONFIG:
# The tokenizer allocates Symbol objects for us
sym = self._tokens[1]

Expand All @@ -2932,8 +2973,7 @@ def _parse_block(self, end_token, parent, prev):
node = MenuNode()
node.kconfig = self
node.item = sym
node.is_menuconfig = t0 is _T_MENUCONFIG
node.is_configdefault = t0 is _T_CONFIGDEFAULT
node.is_menuconfig = (t0 is _T_MENUCONFIG)
node.prompt = node.help = node.list = None
node.parent = parent
node.filename = self.filename
Expand All @@ -2944,14 +2984,6 @@ def _parse_block(self, end_token, parent, prev):

self._parse_props(node)

if node.is_configdefault:
if (node.prompt or
node.dep != self.y or
len(node.ranges) > 0 or
len(node.selects) > 0 or
len(node.implies) > 0):
self._parse_error("configdefault can only contain `default`")

if node.is_menuconfig and not node.prompt:
self._warn("the menuconfig symbol {} has no prompt"
.format(sym.name_and_loc))
Expand Down Expand Up @@ -3564,23 +3596,6 @@ def _invalidate_all(self):
# Post-parsing menu tree processing, including dependency propagation and
# implicit submenu creation
#
def _finalize_sym(self, sym):
# Finalizes symbol definitions
#
# - Applies configdefault node defaults to final symbols
#
# sym:
# The symbol to finalize.

inserted = 0
for (idx, defaults) in sym.configdefaults:
for d in defaults:
# Add the defaults to the node, with the requirement that
# direct dependencies are respected. The original order
# of the default statements between nodes is preserved.
default = (d[0], self._make_and(sym.direct_dep, d[1]))
sym.defaults.insert(inserted + idx, default)
inserted += 1

def _finalize_node(self, node, visible_if):
# Finalizes a menu node and its children:
Expand Down Expand Up @@ -3727,14 +3742,6 @@ def _add_props_to_sym(self, node):

sym = node.item

if node.is_configdefault:
# Store any defaults for later application after the complete tree
# is known. The current length of the default array is stored so
# the configdefaults can be inserted in the order they originally
# appeared.
sym.configdefaults.append((len(sym.defaults), node.defaults))
return

# See the Symbol class docstring
sym.direct_dep = self._make_or(sym.direct_dep, node.dep)

Expand Down Expand Up @@ -4287,7 +4294,6 @@ class Symbol(object):
"_write_to_conf",
"choice",
"defaults",
"configdefaults",
"direct_dep",
"env_var",
"implies",
Expand Down Expand Up @@ -4346,7 +4352,7 @@ def str_value(self):
# function call (property magic)
vis = self.visibility

self._write_to_conf = vis != 0
self._write_to_conf = (vis != 0)

if self.orig_type in _INT_HEX:
# The C implementation checks the user value against the range in a
Expand Down Expand Up @@ -4484,7 +4490,7 @@ def tri_value(self):
# Warning: See Symbol._rec_invalidate(), and note that this is a hidden
# function call (property magic)
vis = self.visibility
self._write_to_conf = vis != 0
self._write_to_conf = (vis != 0)

val = 0

Expand Down Expand Up @@ -5652,7 +5658,6 @@ class MenuNode(object):
"help",
"include_path",
"is_menuconfig",
"is_configdefault",
"item",
"kconfig",
"linenr",
Expand Down Expand Up @@ -5854,13 +5859,8 @@ def indent_add_cond(s, cond):
sc = self.item

if sc.__class__ is Symbol:
if self.is_menuconfig:
t = "menuconfig "
elif self.is_configdefault:
t = "configdefault "
else:
t = "config "
lines = [t + sc.name]
lines = [("menuconfig " if self.is_menuconfig else "config ")
+ sc.name]
else:
lines = ["choice " + sc.name if sc.name else "choice"]

Expand Down Expand Up @@ -6247,14 +6247,14 @@ def standard_kconfig(description=None):
Exits with sys.exit() (which raises SystemExit) on errors.
description (default: None):
The 'description' passed to argparse.ArgumentParser(allow_abbrev=False).
The 'description' passed to argparse.ArgumentParser().
argparse.RawDescriptionHelpFormatter is used, so formatting is preserved.
"""
import argparse

parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=description, allow_abbrev=False)
description=description)

parser.add_argument(
"kconfig",
Expand Down Expand Up @@ -6436,12 +6436,6 @@ def _touch_dep_file(path, sym_name):
def _save_old(path):
# See write_config()

if not os.path.isfile(path):
# Backup only files (and symlinks to files). Simplest alternative
# to avoid e.g. (potentially successful attempt to) rename /dev/null
# (and to keep fifos).
return

def copy(src, dst):
# Import as needed, to save some startup time
import shutil
Expand All @@ -6466,7 +6460,8 @@ def copy(src, dst):
except Exception:
# Ignore errors from 'path' missing as well as other errors.
# <filename>.old file is usually more of a nice-to-have, and not worth
# erroring out over e.g. if <filename>.old happens to be a directory.
# erroring out over e.g. if <filename>.old happens to be a directory or
# <filename> is something like /dev/null.
pass


Expand Down Expand Up @@ -6905,7 +6900,6 @@ def _shell_fn(kconf, _, command):
_T_CLOSE_PAREN,
_T_COMMENT,
_T_CONFIG,
_T_CONFIGDEFAULT,
_T_DEFAULT,
_T_DEFCONFIG_LIST,
_T_DEF_BOOL,
Expand Down Expand Up @@ -6949,7 +6943,7 @@ def _shell_fn(kconf, _, command):
_T_TRISTATE,
_T_UNEQUAL,
_T_VISIBLE,
) = range(1, 52)
) = range(1, 51)

# Keyword to token map, with the get() method assigned directly as a small
# optimization
Expand All @@ -6961,7 +6955,6 @@ def _shell_fn(kconf, _, command):
"choice": _T_CHOICE,
"comment": _T_COMMENT,
"config": _T_CONFIG,
"configdefault": _T_CONFIGDEFAULT,
"def_bool": _T_DEF_BOOL,
"def_hex": _T_DEF_HEX,
"def_int": _T_DEF_INT,
Expand Down

0 comments on commit 2fe4b33

Please sign in to comment.