如何像在 cmake 中一样创建 linux 内核的 .config

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

我正在进行的项目将具有一些可以根据客户打开/关闭的功能。我知道 cmake 有 "option" 语法,但假设我有几个不同的项目可能具有不同的功能,我如何为每个项目创建一个预定义的配置,这类似于 linux kernel[= 中的默认配置11=]

项目A: 功能 1 开启 功能 2 关闭 功能 3 关闭

项目 B: 功能 1 关闭 功能 1 关闭 功能 1 关闭

当然会有数百个这样的功能....据我所知,我可以在调用 cmake 时将其添加为参数,但如果我有数百个并且不容易阅读,那就不实用了

谢谢

CMake 有关于 initial-cache - 脚本的概念,它在 main CMakeLists.txt 之前执行并且可能填充缓存的参数(和选项)。 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.

因此,您可以为项目 A 创建配置文件,例如:

conf/projectA.cmake:

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

并使用它

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

注意,与 Linux 内核的配置文件不同,这样你可以只存储 initial (default)配置。用户执行的所有缓存修改仅存储在 CMakeCache.txt 中,不会反映回配置文件。