Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set cmake variables with add_subdirectory?

Tags:

cmake

I am currently using add_subdirectory(${CMAKE_SOURCE_DIR}/ext/glfw) to add glfw to my project.

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

In the cmake file from glfw there is

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

Is it possible to set this variable to OFF from my CMakeLists.txt?

like image 347
Maik Klein Avatar asked Dec 19 '14 16:12

Maik Klein


People also ask

What does add_subdirectory do in CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

How do you set a variable value in CMake?

Options and variables are defined on the CMake command line like this: $ cmake -DVARIABLE=value path/to/source You can set a variable after the initial `CMake` invocation to change its value. You can also undefine a variable: $ cmake -UVARIABLE path/to/source Variables are stored in the `CMake` cache.

What are CMake cache variables?

The CMake cache may be thought of as a configuration file. The first time CMake is run on a project, it produces a CMakeCache. txt file in the top directory of the build tree. CMake uses this file to store a set of global cache variables, whose values persist across multiple runs within a project build tree.


1 Answers

Simply do either

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

before the add_subdirectory command or

cmake -DGLFW_BUILD_EXAMPLES:BOOL=OFF ..

on the command line

For example given the following 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 see the following (when run in the build directory

With no command line options, the option() command sets the variable

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

Turning on the over-ride, the set() command overrides the option() command

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

You can also over-ride the option() directly on the command line.

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

Note that I do an rm -rf * in the build directory each time. The value of GLFW_BUILD_EXAMPLES is cached in a file called CMakeCache.txt. CMake will always use the values it finds there before anything set in a set or option command.

like image 156
kdopen Avatar answered Oct 02 '22 18:10

kdopen