CMake:如何指定不同的CMakeFileList?

时间:2016-02-18 09:36:33

标签: c++ cmake

我们为同一个项目中的不同构建选项创建了3个不同的CMakeFileList文件(CMakeFileList.engine,CMakeFileList.data和CMakeFileList.fep)。 CMake是否支持将CMakeFileList文件指定为参数?如果没有,通过利用cmake完成任务的最佳方法是什么?任何建议都表示赞赏。

2 个答案:

答案 0 :(得分:2)

一般情况下,我通过使用option() cmake命令完成了这样的事情,只提供了一个CMakeLists.txt文件(根据选项在内部决定构建什么,即你可以还可以在单​​个cmake / make运行中构建所有内容。

示例:

# the defaults (can be overridden on the command line)
option(BUILD_ENGINE "Build the Engine" ON)
option(BUILD_DATA "Build the Data" ON)
option(BUILD_FEP "Build the Fep" OFF)

if (BUILD_ENGINE)
    # commands to build the Engine target or include(CMakeFileList.engine)
endif()

if (BUILD_DATA)
    # commands to build the Data target or include(CMakeFileList.data)
endif()

if (BUILD_FEP)
    # commands to build the Fep target or include(CMakeFileList.fep)
endif()

然后,您可以将所有内容放在一个CMakeLists.txt中,并每次构建所需的内容,可能是多个包(如果不同于默认值,则可以在cmake命令行上打开/关闭)。或者也包括单独的cmake列表(并且只是为了确保在需要构建所有内容时它们将一起工作)。

答案 1 :(得分:0)

创建主CMakeLists.txt文件,有条件地使用命令include以使用特定于组件的部分:

set(BUILD_TARGET "" CACHE STRING "Target to build")

if(BUILD_TARGET STREQUAL "engine")
    include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeFileList.engine)
elseif(BUILD_TARGET STREQUAL "data")
    include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeFileList.data)
elseif(BUILD_TARGET STREQUAL "fep")
    include(${CMAKE_CURRENT_SOURCE_DIR}/CMakeFileList.fep)
else()
    message(FATAL_ERROR "Incorrect BUILD_TARGET")
endif()