Skip to content

project: Defer argument expansion until after including files pointed to by CMAKE_PROJECT_INCLUDE_BEFORE and CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE_BEFORE

This merge-request relaxes the constraints on the project command by deferring the variable expansion of its arguments until after the files pointed to by CMAKE_PROJECT_INCLUDE_BEFORE and CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE_BEFORE are included.

This allows to provide values for the arguments of the project command through variables, which can even come from the files pointed to by CMAKE_PROJECT_INCLUDE_BEFORE and CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE_BEFORE.

(Of course, the variable providing the name of the project command may come from the file pointed to by CMAKE_PROJECT_INCLUDE_BEFORE but, for obvious reasons, cannot come from the one pointed to by CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE_BEFORE.)


This merge-request enables one to write something like the following:

# In top-level CMakeLists.txt:

...
set( CMAKE_PROJECT_INCLUDE_BEFORE "${CMAKE_CURRENT_LIST_DIR}/common-project-info.cmake" )
set( CMAKE_PROJECT_SomeSubProject_INCLUDE_BEFORE "${CMAKE_CURRENT_LIST_DIR}/generated/SomeSubProject-version-info.cmake" )
...
# In common-project-info.cmake (from top-level directory):

# include project-specific configuration
include( "${CMAKE_CURRENT_SOURCE_DIR}/project-info.cmake" )
# In SomeSubProject-version-info.cmake (from "generated" directory):

# set( project_version "1.2.3" )
# In some sub-level CMakeLists.txt:

project( "${project_name}"
         VERSION     "${project_version}"
         DESCRIPTION "${project_description}" )
...
# In project-info.cmake (from some sub-level directory):

set( project_name "MyFancySubProject" )
set( project_description "An example project" )
Edited by Deniz Bahadir

Merge request reports