How to disable a CMAKE option

Issue

On ${CMAKE_CURRENT_SOURCE_DIR}/JUCE, there is:

option(JUCE_BUILD_EXTRAS "Add build targets for the Projucer and other tools" OFF)

if(JUCE_BUILD_EXTRAS)
    add_subdirectory(extras)
endif()

link: https://github.com/juce-framework/JUCE/blob/master/CMakeLists.txt#L57

So this is what I did:

set(JUCE_BUILD_EXTRAS OFF)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/JUCE)

However it still tries to build extras. I’m on Android Studio.

What should I do?

I also tried set(JUCE_BUILD_EXTRAS OFF)

The default value is OFF and even then it tried to build.

I always get these errors when trying to disable something on Android CMAKE but it always fails

Solution

The easiest way to guarantee that this will be overridden correctly from a parent CMakeLists.txt is to set it as a CACHE variable with a FORCEd value:

set(JUCE_BUILD_EXTRAS OFF CACHE BOOL "" FORCE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/JUCE)

This will ensure that any state that may already exist from a previous configuration of the project that defines the option will be overridden and picked up first.


Technically, the documentation for option states:

… If is already set as a normal or cache variable, then the command does nothing.

However, this is only true if there have been no prior configurations — so this only works if the variable was set before the first configuration

In practice I’ve found that the FORCE-approach is much more reliable.

Answered By – Human-Compiler

Leave a Comment