Use kconfiglib in sdkconfig update script

This commit is contained in:
Scott Shawcroft 2023-08-16 10:57:25 -07:00
parent e90c07ed87
commit feabf7b6ce
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA

View File

@ -4,6 +4,8 @@
import pathlib
import click
import copy
import kconfiglib
import os
OPT_SETTINGS = [
"CONFIG_ESP_ERR_TO_NAME_LOOKUP",
@ -21,17 +23,14 @@ OPT_SETTINGS = [
"CONFIG_OPTIMIZATION_ASSERTION_LEVEL",
"CONFIG_OPTIMIZATION_ASSERTIONS_",
"CONFIG_HAL_DEFAULT_ASSERTION_LEVEL",
"CONFIG_BOOTLOADER_LOG_LEVEL",
"LOG_DEFAULT_LEVEL",
]
TARGET_SETTINGS = [
"CONFIG_IDF_TARGET",
"CONFIG_IDF_FIRMWARE_CHIP_ID",
"CONFIG_BOOTLOADER_OFFSET_IN_FLASH",
"CONFIG_ESP32_",
"CONFIG_ESP32C3_",
"CONFIG_ESP32S2_",
"CONFIG_ESP32S3_",
"CONFIG_ESP32H2_",
"CONFIG_ESP_SLEEP_POWER_DOWN_FLASH",
"CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE",
"CONFIG_ESP_SYSTEM_MEMPROT_",
@ -57,7 +56,6 @@ TARGET_SETTINGS = [
]
BOARD_SETTINGS = [
"PSRAM clock and cs IO for ESP32S3",
"CONFIG_SPIRAM",
"CONFIG_DEFAULT_PSRAM_",
"_SPIRAM_SUPPORT",
@ -102,6 +100,33 @@ def add_group(lines, last_group, current_group):
return last_group
def sym_default(sym):
# Skip symbols that cannot be changed. Only check
# non-choice symbols, as selects don't affect choice
# symbols.
if not sym.choice and sym.visibility <= kconfiglib.expr_value(sym.rev_dep):
return True
# Skip symbols whose value matches their default
if sym.str_value == sym._str_default():
return True
# Skip symbols that would be selected by default in a
# choice, unless the choice is optional or the symbol type
# isn't bool (it might be possible to set the choice mode
# to n or the symbol to m in those cases).
if (
sym.choice
and not sym.choice.is_optional
and sym.choice._selection_from_defaults() is sym
and sym.orig_type is kconfiglib.BOOL
and sym.tri_value == 2
):
return True
return False
@click.command()
@click.option("--debug")
@click.option("--board")
@ -122,7 +147,20 @@ def update(debug, board, update_all):
elif line.startswith("CIRCUITPY_ESP_FLASH_SIZE"):
flash = line.split("=")[1].strip()
os.environ["IDF_TARGET"] = target
os.environ[
"COMPONENT_KCONFIGS_PROJBUILD_SOURCE_FILE"
] = f"build-{board}/esp-idf/kconfigs_projbuild.in"
os.environ["COMPONENT_KCONFIGS_SOURCE_FILE"] = f"build-{board}/esp-idf/kconfigs.in"
kconfig_path = pathlib.Path(f"build-{board}/esp-idf/kconfigs.in")
kconfig_path = pathlib.Path(f"esp-idf/Kconfig")
kconfig = kconfiglib.Kconfig(kconfig_path)
input_config = pathlib.Path(f"build-{board}/esp-idf/sdkconfig")
kconfig.load_config(input_config)
default_config = pathlib.Path("esp-idf-config/sdkconfig.defaults")
if debug:
opt_config = pathlib.Path("esp-idf-config/sdkconfig-debug.defaults")
@ -133,11 +171,9 @@ def update(debug, board, update_all):
ble_config = pathlib.Path(f"esp-idf-config/sdkconfig-ble.defaults")
board_config = pathlib.Path(f"boards/{board}/sdkconfig")
defaults = default_config.read_text().split("\n")
defaults.extend(opt_config.read_text().split("\n"))
defaults.extend(flash_config.read_text().split("\n"))
defaults.extend(target_config.read_text().split("\n"))
defaults.extend(ble_config.read_text().split("\n"))
cp_kconfig_defaults = kconfiglib.Kconfig(kconfig_path)
for default_file in (default_config, opt_config, flash_config, target_config, ble_config):
cp_kconfig_defaults.load_config(default_file, replace=False)
board_settings = []
last_board_group = None
@ -151,43 +187,107 @@ def update(debug, board, update_all):
last_ble_group = None
default_settings = []
last_default_group = None
current_group = []
for line in input_config.read_text().split("\n"):
# Normalize the deprecated section labels.
if line == "# End of deprecated options":
line = "# end of Deprecated options for backward compatibility"
if (
line.startswith("# ")
and "CONFIG_" not in line
and "DO NOT EDIT" not in line
and "Project Configuration" not in line
and len(line) > 3
):
if line.startswith("# end of"):
current_group.pop()
for sym in kconfig.unique_defined_syms:
sym._visited = False
# This merges the normal `write_config`, `write_min_config` and CP settings to split into
# different files.
pending_nodes = [kconfig.top_node]
i = 0
while pending_nodes:
node = pending_nodes.pop()
if node is None:
current_group.pop()
continue
if node.item is kconfiglib.MENU:
if node.prompt:
print(" " * len(current_group), i, node.prompt[0])
i += 1
if node.next:
pending_nodes.append(node.next)
# if i > 300:
# break
# We have a configuration item.
item = node.item
if isinstance(item, kconfiglib.Symbol):
if item._visited:
continue
item._visited = True
config_string = item.config_string.strip()
if not config_string:
continue
if node.list:
pending_nodes.append(node.list)
matches_cp_default = cp_kconfig_defaults.syms[item.name].str_value == item.str_value
matches_esp_default = sym_default(item)
if not matches_esp_default:
print(" " * len(current_group), i, config_string.strip())
target_reference = False
for referenced in item.referenced:
if referenced.name.startswith("IDF_TARGET"):
# print(item.name, "references", referenced.name)
target_reference = True
break
if (not update_all and not matches_cp_default) or (
update_all
and matches_group(config_string, BOARD_SETTINGS)
and not matches_esp_default
):
print(" " * (len(current_group) + 1), "board")
last_board_group = add_group(board_settings, last_board_group, current_group)
board_settings.append(config_string)
elif update_all and not matches_esp_default:
if matches_group(config_string, OPT_SETTINGS):
print(" " * (len(current_group) + 1), "opt")
last_opt_group = add_group(opt_settings, last_opt_group, current_group)
opt_settings.append(config_string)
elif matches_group(config_string, FLASH_SETTINGS):
print(" " * (len(current_group) + 1), "flash")
last_flash_group = add_group(flash_settings, last_flash_group, current_group)
flash_settings.append(config_string)
elif target_reference or matches_group(config_string, TARGET_SETTINGS):
print(" " * (len(current_group) + 1), "target")
last_target_group = add_group(
target_settings, last_target_group, current_group
)
target_settings.append(config_string)
elif matches_group(config_string, BLE_SETTINGS):
print(" " * (len(current_group) + 1), "ble")
last_ble_group = add_group(ble_settings, last_ble_group, current_group)
ble_settings.append(config_string)
else:
print(" " * (len(current_group) + 1), "all")
last_default_group = add_group(
default_settings, last_default_group, current_group
)
default_settings.append(config_string)
elif kconfiglib.expr_value(node.dep):
if item is kconfiglib.COMMENT:
print("comment", repr(item))
elif item is kconfiglib.MENU:
# This menu isn't visible so skip to the next node.
if kconfiglib.expr_value(node.visibility) and node.list:
current_group.append(node.prompt[0])
pending_nodes.append(None)
pending_nodes.append(node.list)
elif isinstance(item, kconfiglib.Choice):
# Choices are made up of individual symbols that we need to check.
pending_nodes.append(node.list)
else:
current_group.append(line[2:])
elif (not update_all and line not in defaults) or (
update_all and matches_group(line, BOARD_SETTINGS)
):
last_board_group = add_group(board_settings, last_board_group, current_group)
board_settings.append(line)
elif update_all:
if matches_group(line, OPT_SETTINGS):
last_opt_group = add_group(opt_settings, last_opt_group, current_group)
opt_settings.append(line)
elif matches_group(line, FLASH_SETTINGS):
last_flash_group = add_group(flash_settings, last_flash_group, current_group)
flash_settings.append(line)
elif matches_group(line, TARGET_SETTINGS):
last_target_group = add_group(target_settings, last_target_group, current_group)
target_settings.append(line)
elif matches_group(line, BLE_SETTINGS):
last_ble_group = add_group(ble_settings, last_ble_group, current_group)
ble_settings.append(line)
elif "CONFIG_" in line:
last_default_group = add_group(default_settings, last_default_group, current_group)
default_settings.append(line)
print("unknown", repr(item))
add_group(board_settings, last_board_group, current_group)
add_group(opt_settings, last_opt_group, current_group)