ESP-IDF Kconfig Tooling

3.7.0 · active · verified Sun Apr 12

esp-idf-kconfig is a Python library that provides Kconfig tooling for the Espressif IoT Development Framework (ESP-IDF). It enables project configuration using the Kconfig language, offers IDE support for configuration, and facilitates documentation generation. As of version 3.7.0, it is actively developed and forms a core part of the ESP-IDF 6.x ecosystem. Its release cadence aligns with ESP-IDF updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically interact with Kconfig files using the underlying `kconfiglib` module, which is integrated into `esp-idf-kconfig`. It shows how to load a Kconfig definition, access configuration symbols, and simulate loading a configuration from an `sdkconfig` file. Most users will interact with Kconfig via the `idf.py menuconfig` command-line tool within an ESP-IDF project environment.

import os
from kconfiglib import Kconfig

# Create a dummy Kconfig file for demonstration
with open('Kconfig.example', 'w') as f:
    f.write('config MY_OPTION\n')
    f.write('    bool "My boolean option"\n')
    f.write('    default y\n')
    f.write('\n')
    f.write('config MY_STRING_OPTION\n')
    f.write('    string "My string option"\n')
    f.write('    default "hello"\n')

# Load the Kconfig file
try:
    # Path to the Kconfig file (can be Kconfig or Kconfig.projbuild)
    kconf = Kconfig('Kconfig.example')

    # Access a symbol
    my_option = kconf.syms['MY_OPTION']
    print(f"MY_OPTION: {my_option.str_value}")

    my_string_option = kconf.syms['MY_STRING_OPTION']
    print(f"MY_STRING_OPTION: '{my_string_option.str_value}'")

    # Simulate loading an sdkconfig file (empty for defaults)
    # In a real scenario, this would load a previously saved config
    with open('sdkconfig.temp', 'w') as f:
        f.write('# CONFIG_MY_OPTION is not set\n')
        f.write('CONFIG_MY_STRING_OPTION="world"\n')

    kconf.load_config('sdkconfig.temp')

    print(f"MY_OPTION after loading sdkconfig: {kconf.syms['MY_OPTION'].str_value}")
    print(f"MY_STRING_OPTION after loading sdkconfig: '{kconf.syms['MY_STRING_OPTION'].str_value}'")

finally:
    # Clean up dummy files
    if os.path.exists('Kconfig.example'):
        os.remove('Kconfig.example')
    if os.path.exists('sdkconfig.temp'):
        os.remove('sdkconfig.temp')

view raw JSON →