Passing variables to subdirectory only
How do I pass specific project variables to a subdirectory? I wonder if there is an "official" way to do this:
# (CMAKE_BUILD_TYPE is one of None, Debug, Release, RelWithDebInfo)
# ...
# set specific build type for 'my_library'
set( CMAKE_BUILD_TYPE_COPY "${CMAKE_BUILD_TYPE}" )
set( CMAKE_BUILD_TYPE "Release" )
add_subdirectory( "my_library" )
set( CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE_COPY} )
# continue with original build type
# ...
The library / subdirectory my_library
must always be buildt with the type "Release"
, the main project and other subdirectories must be buildt with the type defined by the config. I cannot change CMakeLists.txt
my_library
.
source to share
Answering the revised question in your comment (how can I pass different values), so values ββother than CMAKE_BUILD_TYPE`:
There are no additional mechanisms for this. If the variable is project specific, you simply set the variable:
set(MYLIB_SOME_OPTION OFF)
add_subdirectory(mylib)
If it's more general you need to return it:
set(BUILD_SHARED_LIBS_SAVED "${BUILD_SHARED_LIBS}")
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(mylib)
set(BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS_SAVED}")
Or, you can put it in a function and you don't need to return the variables that you changed, since functions are scoped.
source to share