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?
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 setBUILD_STATIC_LIBS
for the sub-project (ParentLibrary
) usingSET(... CACHE BOOL "" FORCE)
it will set the value for all projects.I'm using the following project structure:
Now I have
CMakeLists.txt (dependencies)
which looks like this: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 theParentLibrary
as otherwise when executing CMake configuration initially the value of the variable would first be set by theset
command and later the value would be overwritten by theoption
command because there was no value in the cache. When executing CMake configuration for the second time theoption
command would be ignored because there is already a value in the cache and the value from theset
command would be used. This would lead to some strange behavior that the configuration between two CMake runs would be different.Try setting the variable in the CACHE
Note: You need to specify the variable type and a description so CMake knows how to display this entry in the GUI.