CMAKE将变量传递到包含的cmake子文件中

时间:2019-04-27 09:05:24

标签: c++ cmake cmake-modules

我当前正在配置项目的构建,并决定将CMakeLists.txt拆分为多个子文件,这些子文件负责单个构建子任务(如测试,打包,编译等)。问题是cmake子文件需要有关如何执行其任务的信息。因此,我决定在包含模块之前要求设置一些变量。看起来像

CpackMylib.cmake

if(NOT DEFINED MYLIB_PACKAGE_NAME)
    message(FATAL_ERROR "MYLIB_PACKAGE_NAME variable must be set")
endif()

if(NOT DEFINED MYLIB_VERSION_MAJOR)
    message(FATAL_ERROR "MYLIB_VERSION_MAJOR variable must be set")
endif()

if(NOT DEFINED MYLIB_VERSION_MINOR)
    message(FATAL_ERROR "MYLIB_VERSION_MINOR variable must be set")
endif()

if(NOT DEFINED MYLIB_VERSION_PATCH)
    message(FATAL_ERROR "MYLIB_VERSION_PATCH variable must be set")
endif()

set(CPACK_PACKAGE_VERSION_MAJOR ${MYLIB_VERSION_MAJOR})

set(CPACK_PACKAGE_VERSION_MINOR ${MYLIB_VERSION_MINOR})

set(CPACK_PACKAGE_VERSION_PATCH ${MYLIB_VERSION_PATCH})

set(CPACK_DEBIAN_PACKAGE_NAME "${MYLIB_PACKAGE_NAME}-${MYLIB_VERSION_MAJOR}")

set(CPACK_DEBIAN_PACKAGE_FILE_NAME ${MYLIB_PACKAGE_NAME}-${MYLIB_VERSION_MAJOR})

#other settings...

include(CPack)

还有根CMakeLists.txt

set(MYLIB_VERSION_MAJOR 0)
set(MYLIB_VERSION_MINOR 0)
set(MYLIB_VERSION_PATCH 1)
set(MYLIB_PACKAGE_NAME ${PROJECT_NAME})
include(cmake/CpackMylib.cmake)

因此,我需要定义特定于项目的变量,这些变量将使用CPack用于创建程序包。我是CMake的新手,这样做很自然。但是我不确定在处理CMake时这是否是一种常见方法。

定义CMake子模块所需的所有变量并在根CMakeLists.txt中提供它们是否有意义?

0 个答案:

没有答案