kconfig 实例1: 基于 python 的 kconfig 系统

kconfig 实例1: 基于 python 的 kconfig 系统

安装

pip install kconfiglib

使用

  • kconfig.py

    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    
    import os, sys
    from kconfiglib import Kconfig
    from menuconfig import menuconfig
    
    def mconf_set_env():
        """
        Set Kconfig Env
        """
        os.environ["MENUCONFIG_STYLE"] = "default selection=fg:white,bg:blue"
        os.environ["KCONFIG_CONFIG"] = os.path.join(".config")
        os.environ["KCONFIG_CONFIG_HEADER"] = "# Generated by My Kconfig Demo"
        os.environ["KCONFIG_AUTOHEADER"] = os.path.join("config.h")
        os.environ["CONFIG_"] = ""
    
    def mconfig(argv):
        mconf_set_env()
        kconf = Kconfig(filename="./config.in")
        menuconfig(kconf)
        kconf.write_autoconf()
    
    if __name__ == "__main__":
        mconfig(sys.argv)
    
    
  • config.in

    mainmenu "My Kconfig Demo"
    
    menu "Debug"
    config CONFIG_DEBUG
        bool "Enable debug"
        default n
        help
            Will print debug information if enable.
    
    config CONFIG_DEBUG_UART
        bool "Enable UART debug"
        default n
        depends on CONFIG_DEBUG
    
    endmenu
    
  • Makefile

    menuconfig:
        python kconfig.py
    
    

演示

$ ls -a
Makefile config.in  kconfig.py
$ make menuconfig
python kconfig.py
Loaded configuration '.config'
Configuration saved to '.config'
$ ls -a
Makefile config.in  kconfig.py .config config.h

执行 make menuconfig 后多出 .config config.h

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。