Kconfiglib
Kconfiglib is a Python library (v14.1.0) for scripting and extracting information from Kconfig configuration systems. It can load, analyze, and output kernel-style configurations, programmatically get and set symbol values, read and write .config and defconfig files, and generate C headers. Highly compatible with the C Kconfig tools, it is a single-file library primarily used in projects like Zephyr and ACRN, running on Python 2.7 and 3.2+ with releases following semantic versioning.
Warnings
- breaking Kconfiglib 2.x is not backwards-compatible with Kconfiglib 1.x. The API changed significantly, replacing functions with properties for accessing symbol values and menu structures. Code written for Kconfiglib 1.x will break.
- breaking Starting with Kconfiglib 13.0.0, the `windows-curses` package is no longer automatically installed on Windows. This dependency is required for the terminal `menuconfig` interface to function.
- deprecated The old syntax for referencing environment variables as `$FOO` is deprecated. The new syntax is `$(FOO)`. While the old syntax is currently supported for compatibility with older Linux kernels, it might be removed in a future major version.
- gotcha Assignments to hidden (promptless) symbols in configuration files are ignored. These symbols derive their values indirectly from other symbols via `default` or `select`. It's a common mistake to assume such assignments in a `.config` file will be respected when read back by Kconfiglib.
- gotcha Overusing `select` statements in Kconfig can lead to complex and hard-to-debug dependency issues. Changes to a selected symbol's dependencies often require propagating those changes to all symbols that select it, which is easily overlooked.
- gotcha Kconfiglib may fail to correctly parse `Kconfig.include` files from Linux kernel versions 5.12.1 and newer, leading to `ValueError: invalid literal for int() with base 10: 'error-if'` during parsing.
- gotcha Within Kconfig files, all hexadecimal literals must be prefixed with `0x` or `0X` to be correctly distinguished from symbol references by the parser.
Install
-
pip install kconfiglib
Imports
- Kconfig
from kconfiglib import Kconfig
- Symbol
from kconfiglib import Symbol
- Kconfiglib 1.x API
Quickstart
import os
from kconfiglib import Kconfig
# Create a dummy Kconfig file for demonstration
kconfig_content = """
menu "My Application Configuration"
config MY_FEATURE_ENABLED
bool "Enable My Feature"
default y
config MY_STRING_SETTING
string "My String Setting"
default "default_value"
depends on MY_FEATURE_ENABLED
endmenu
"""
with open("Kconfig_example", "w") as f:
f.write(kconfig_content)
# Load the Kconfig file
kconf = Kconfig("Kconfig_example")
# Access a symbol by its name
feature_symbol = kconf.syms["MY_FEATURE_ENABLED"]
string_symbol = kconf.syms["MY_STRING_SETTING"]
# Print symbol information
print(f"Symbol: {feature_symbol.name}, Prompt: '{feature_symbol.prompt}', Value: {feature_symbol.str_value}")
print(f"Symbol: {string_symbol.name}, Prompt: '{string_symbol.prompt}', Value: {string_symbol.str_value}")
# Set a symbol value
feature_symbol.set_value(0) # Set to 'n'
print(f"Updated {feature_symbol.name} value: {feature_symbol.str_value}")
# Try to set string_symbol, which now depends on MY_FEATURE_ENABLED being 'y'
# This will fail silently or yield an 'n' if the dependency is not met
string_symbol.set_value("new_value")
print(f"Updated {string_symbol.name} value: {string_symbol.str_value} (expected empty if feature disabled)")
# Re-enable MY_FEATURE_ENABLED and set string_symbol
feature_symbol.set_value(2) # Set to 'y'
string_symbol.set_value("new_value_2")
print(f"Updated {string_symbol.name} value: {string_symbol.str_value} (expected 'new_value_2')")
# Clean up the dummy Kconfig file
os.remove("Kconfig_example")