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

41,958

Solution 1

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.

Solution 2

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.

Share:
41,958

Related videos on Youtube

Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    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?

  • tibur
    tibur almost 14 years
    Put it before the add_subdirectory command, so it will set the default value for the variable.
  • asiviero
    asiviero about 10 years
    Sometimes you need to use the FORCE
  • Claudiu
    Claudiu over 8 years
    Pro tip (to myself): FORCE goes after the doc string, not randomly in the middle...
  • Abai
    Abai over 8 years
    @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
  • Zingam
    Zingam over 6 years
    Won't INTERNAL do?
  • stackprotector
    stackprotector almost 4 years
    Good idea, but to be precise, the option will be valid for all sub-projects under dependencies, not only for one.
  • Top-Master
    Top-Master almost 3 years
    Use INTERNAL instead of BOOL if want to hide option (from CMake GUI). Anyway, what about cmake_dependent_option? Does this work for that as well?