I'm currently configuring build of my project and decided to split CMakeLists.txt
into subfiles which are responsible for a single build sub-task (like testing, packaging, compiling, etc...). The problem is the cmake
subfiles needs info about how to do their tasks. So I decided to require some variables to be set before including the module. It looks like
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)
And the root 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)
So I require to define project-specific variables that will be used for creation packages using CPack
. I'm new to CMake
and this just seems natural to do things that way. But I'm not sure if it is sort of common approach when dealing with CMake
.
Does it make sense to define all variable the CMake
submodules needs and provide them in the root CMakeLists.txt
?