85

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?

3 Answers 3

97

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.

6
  • 13
    Put it before the add_subdirectory command, so it will set the default value for the variable.
    – tibur
    Commented Sep 23, 2010 at 11:15
  • 12
    Sometimes you need to use the FORCE
    – Rajish
    Commented Apr 22, 2014 at 15:40
  • 8
    Pro tip (to myself): FORCE goes after the doc string, not randomly in the middle...
    – Claudiu
    Commented Dec 15, 2015 at 18:06
  • 10
    @Rajish If the variable is already cached due to a previous OPTION or SET( CACHE ) call or a CMakeCache.txt from a previous configure, you have to use SET(FOO_BUILD_SHARED OFF CACHE BOOL "Build libfoo shared library" FORCE). source
    – Abai
    Commented Mar 14, 2016 at 15:22
  • Won't INTERNAL do?
    – Zingam
    Commented Jan 23, 2018 at 22:48
12

This question is rather old but Google brought me here.

The problem with SET(<variable name> <value> CACHE BOOL "" FORCE) is that it will set the option project wide. If you want to use a sub-project, which is a library, and you want to set BUILD_STATIC_LIBS for the sub-project (ParentLibrary) using SET(... CACHE BOOL "" FORCE) it will set the value for all projects.

I'm using the following project structure:

|CMakeLists.txt (root)
|- dependencies
   | CMakeLists.txt (dependencies)
   |- ParentLibrary
      | CMakeLists.txt (parent)
|- lib
   | CMakeLists.txt (lib)

Now I have CMakeLists.txt (dependencies) which looks like this:

# Copy the option you want to change from ParentLibrary here
option (BUILD_SHARED_LIBS "Build shared libraries" ON)
set(BUILD_SHARED_LIBS OFF)
add_subdirectory(ParentLibrary)

Advantage is that I don't have to modify ParentLibrary and that I can set the option only for that project.

It is necessary to explicitly copy the option command from the ParentLibrary as otherwise when executing CMake configuration initially the value of the variable would first be set by the set command and later the value would be overwritten by the option command because there was no value in the cache. When executing CMake configuration for the second time the option command would be ignored because there is already a value in the cache and the value from the set command would be used. This would lead to some strange behavior that the configuration between two CMake runs would be different.

1
  • Good idea, but to be precise, the option will be valid for all sub-projects under dependencies, not only for one. Commented Jul 6, 2020 at 16:02
8

To give a more current answer, since newer projects use newer CMake versions (probably with libraries which use old CMake versions).

As of CMake 3.13 there is an option to simply set a variable in the parent project, which sets the variable in all sub projects. This is basically what you've tried, but now it is possible. Version 3.13 introduced CMP0077:

The option() command is typically used to create a cache entry to allow users to set the option. However, there are cases in which a normal (non-cached) variable of the same name as the option may be defined by the project prior to calling the option() command. For example, a project that embeds another project as a subdirectory may want to hard-code options of the subproject to build the way it needs.
...
In CMake 3.13 and above the option() command prefers to do nothing when a normal variable of the given name already exists. It does not create or update a cache entry or remove the normal variable. The new behavior is consistent between the first and later runs in a build tree. This policy provides compatibility with projects that have not been updated to expect the new behavior.

So you can simply use set to hardcode an option inside the parent project, by adding the following before add_subdirectory:

set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
set(FOO_BUILD_SHARED OFF)

...

add_subdirectory(extern/foo)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.