Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variables down to subdirectory only

Tags:

cmake

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.txtof my_library.

like image 291
telephone Avatar asked Jun 22 '15 16:06

telephone


1 Answers

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.

like image 159
tamas.kenez Avatar answered Oct 23 '22 14:10

tamas.kenez