Kconfiglib

14.1.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to load a Kconfig file, access symbols, query their properties, and programmatically set their values. It showcases how symbol values are affected by their dependencies.

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")

view raw JSON →