Overriding a default option(...) value in CMake from a parent CMakeLists.txt

user79758 picture user79758 · Sep 22, 2010 · Viewed 31.5k times · Source

I am trying to include several third-party libraries in my source tree with minimal changes to their build system for ease of upgrading. They all use CMake, as do I, so in my own CMakeLists.txt I can use add_subdirectory(extern/foo) for libfoo.

But the foo CMakeLists.txt compiles a test harness, builds documentation, a shared library which I don't need, and so on. The libfoo authors had the foresight to control these via options - option(FOO_BUILD_SHARED "Build libfoo shared library" ON) for example - which means I can set them via the CMake command line. But I would like to make that off by default and overridable via the command line.

I have tried doing set(FOO_BUILD_SHARED OFF) before add_subdirectory(extern/foo). That has the effect of not trying to build the shared library during the second and subsequent build attempts, but not during the first one, which is the one I really need to speed up.

Is this possible, or do I need to maintain forked CMakeLists.txt for these projects?

Answer

pkit picture pkit · Sep 22, 2010

Try setting the variable in the CACHE

SET(FOO_BUILD_SHARED OFF CACHE BOOL "Build libfoo shared library")

Note: You need to specify the variable type and a description so CMake knows how to display this entry in the GUI.