How do I pass project specific variables down to subdirectory? I wonder if there is a "official" way of doing 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
should always be buildt with type "Release"
, main project and other subdirectories should be buildt with type defined by configuration. I am not able to modify the CMakeLists.txt
of my_library
.
Answering the revised question in your comment (how can I pass different values), so values other then CMAKE_BUILD_TYPE`:
There's no extra machinery for this. If the variable is project specific, you just set the variable:
set(MYLIB_SOME_OPTION OFF)
add_subdirectory(mylib)
If it's more general, you need to revert 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 into a function and you don't need to revert the variables you changed since functions has a scope.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With