13

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.

telephone
  • 1,131
  • 1
  • 10
  • 29
  • 2
    About `set(CMAKE_BUILD_TYPE Release)`: http://stackoverflow.com/a/24470998/2288008 –  Jun 22 '15 at 20:39
  • 1
    Your question is to pass different flags to different directories ? – coincoin Jun 23 '15 at 14:02
  • 1
    For most details see @ruslo comment/link. Just wanted to add that - if we are talking about VS - what you see in the "Configuration Manager" when opening the generated VS solution is hard-coded into the VS generator. See [cmVisualStudio10TargetGenerator.cxx](https://github.com/Kitware/CMake/blob/master/Source/cmVisualStudio10TargetGenerator.cxx#L628). The project's config will always match the solution's config. I recommend to generate `my_library` as an [external project](http://stackoverflow.com/questions/26440988/how-can-i-generate-visual-studio-projects-for-a-specific-configuration). – Florian Jun 23 '15 at 19:39
  • 2
    @coincoin: No, my project uses external libraries, which are git submodules. These have their own `CMakeLists.txt`, so I am not able to modify these. I want one of these libraries to be built as `Release`, since this is a pretty heavy OpenGL library. But I want debug symbols in my own code, i.e. `Debug`. – telephone Jun 25 '15 at 09:39
  • 1
    @ruslo: Thanks for the link. This means that I should not pass `CMAKE_BUILD_TYPE` down to other cmake directories. My question is still open; how can I pass different values down without changing the value of current `CMakeLists.txt`, in a nice way. – telephone Jun 25 '15 at 09:46
  • 1
    @telephone From my experience there is no such option, however you can always write your own wrapper –  Jun 25 '15 at 11:04

1 Answers1

21

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.

tamas.kenez
  • 7,301
  • 4
  • 24
  • 34
  • Well, it will be visible in the function which can in turn forward it to the calling scope. But you're right, if the subdirectory sets a variable for PARENT_SCOPE it's simpler no to use a function but the 'more general' example above – tamas.kenez Jul 01 '15 at 08:07
  • 1
    @tamas.kenez: Given that "There's no extra machinery for this" is true, your answer is accepted. – telephone Jul 01 '15 at 21:36
  • Hi how Is it possible to use options defined in main CMakeLists.txt and use the variable name in the subdirectories CMakeList.txt? – Hossein Dec 10 '20 at 10:44
  • so you can call "add_subdirectory" out from a function and set variables there? – Johannes Schaub - litb Jul 20 '21 at 15:43