Can cmake variables be set using add_subdirectory?

I am currently using add_subdirectory(${CMAKE_SOURCE_DIR}/ext/glfw)

to add glfw to my project.

The problem is when I do cmake .. && make it always build all examples and I don't want that to happen.

The cmake file from glfw has

option(GLFW_BUILD_EXAMPLES "Build the GLFW example programs" ON)

      

Can I set this variable in OFF

from my CMakeLists.txt?

+3


source to share


1 answer


Just do either

set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "Build the GLFW example programs")

      

before the command add_subdirectory

or

cmake -DGLFW_BUILD_EXAMPLES:BOOL=OFF ..

      

on the command line

For example, for two CMake scripts

cmake_minimum_required(VERSION 2.8)
project(test)

set(OVERRIDE FALSE CACHE BOOL "")

if(OVERRIDE)
    message(STATUS "Overriding option")
    set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "Build the GLFW example programs")
endif()

message(STATUS "OPT BEFORE =${GLFW_BUILD_EXAMPLES}")
add_subdirectory(subdir)
message(STATUS "OPT AFTER=${GLFW_BUILD_EXAMPLES}")

      

and subdir / CMakeLists.txt

option(GLFW_BUILD_EXAMPLES "Build the GLFW example programs" ON)

      



you will see the following (when run in build directory

Without command line parameters, the command option()

sets the variable

$ rm -rf *
$ cmake ..
...
-- OPT BEFORE =
-- OPT AFTER=ON
-- Configuring done
-- Generating done

      

Overload enable, command set()

cancel commandoption()

$ rm -rf *
$ cmake .. -DOVERRIDE=ON
...
-- Overriding option
-- OPT BEFORE =OFF
-- OPT AFTER=OFF
-- Configuring done
-- Generating done

      

You can also overload option()

directly on the command line.

$ rm -rf *
$ cmake .. -DGLFW_BUILD_EXAMPLES=OFF
...
-- OPT BEFORE =OFF
-- OPT AFTER=OFF
-- Configuring done
-- Generating done

      

Please note that every time I do rm -rf *

in the build directory. The value is GLFW_BUILD_EXAMPLES

cached in a file named CMakeCache.txt

. CMake will always use the values ​​it finds there before anything set in the command set

or option

.

+6


source







All Articles