Search code examples
linuxcmakekbuild

how to create a linux kernel's .config like in cmake


I'm working on project that's going to have some feature that can be on / off depending on the customer. I know cmake has "option" syntax, but suppose I have several different projects that might have different features, how can I create a predefined config for each project, this is something like default config in linux kernel

project A: feature1 ON feature2 OFF feature3 OFF

project B: feature1 OFF feature1 OFF feature1 OFF

and of course there will be hundreds of those features.... as I know I can add it as parameter when call cmake, but it's not that practical if I have hundreds of them and not easy to read

Thanks


Solution

  • CMake has notion about initial-cache - script, which is executed before main CMakeLists.txt and which may fill cached parameters (and options). Description of corresponded option in cmake(1) documentation:

    -C <initial-cache>

    Pre-load a script to populate the cache.

    When cmake is first run in an empty build tree, it creates a CMakeCache.txt file and populates it with customizable settings for the project. This option may be used to specify a file from which to load cache entries before the first pass through the project’s cmake listfiles. The loaded entries take priority over the project’s default values. The given file should be a CMake script containing SET commands that use the CACHE option, not a cache-format file.

    So, you may create configuration file for project A like:

    conf/projectA.cmake:

    option(feature1 "Feature 1" ON)
    option(feature2 "Feature 2" OFF)
    option(feature3 "Feature 3" OFF)
    

    and use it

    cmake -C <src-dir>/conf/projectA_conf.cmake <src-dir>
    

    Note, that unlike to Linux kernel's config file, such a way you may store only initial (default) configuration. All cache modifications performed by the user are stored only in CMakeCache.txt and aren't reflected back to the configuration file.