1

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?

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • 4
    Why do you `set` the version numbers instead of providing it in your [`project`](https://cmake.org/cmake/help/latest/command/project.html) command? And if you don't have a `project` command, then I suggest you add one. – Some programmer dude Apr 27 '19 at 09:28
  • 2
    "Does it make sense to define all variable the `CMake` submodules needs and provide them in the root `CMakeLists.txt`?" - I don't understand the problem. If submodules **needs** that variables, then you **need** to set them. But as the previous commenter notes, in CMake there are many variables with predefined semantic and which sometimes are set automatically. So, if a submodule needs some information from the top-level project, firstly check whether this information is provided by some well-known variable with a predefined semantic. – Tsyvarev Apr 27 '19 at 11:13

0 Answers0