Home > database >  Cmake reconfiguration with sanitizers added doesn't trigger ninja to recompile
Cmake reconfiguration with sanitizers added doesn't trigger ninja to recompile

Time:07-06

Let's assume a minimal top level CMakeLists.txt like this:

  1 cmake_minimum_required(VERSION 3.22)
  2 set(CMAKE_CXX_STANDARD 20)
  3 
  4 project(stackoverflow LANGUAGES CXX C)
  5 
  6 add_executable(prog src/main.cpp)
  7 
  8 option(ENABLE_SANITIZER "Enables sanitizer" OFF)
  9 if(ENABLE_SANITIZER)
 10   target_compile_options(prog PUBLIC -fsanitize=address)
 11   target_link_options(prog PUBLIC -fsanitize=address)
 12 endif()

Where the option ENABLE_SANITIZER adds an address sanitizer to the build.

When I configure with the sanitizer with

cmake -S . -B ./build -G "Ninja Multi-Config" -DENABLE_SANITIZER=ON

and build with

cmake --build ./build/ --target prog 

everything compiles as it should, but when I reconfigure with

cmake -S . -B ./build -G "Ninja Multi-Config"

and build it again, ninja tells me that there is nothing to do:

ninja: no work to do.

Why does this happen when I clearly removed a compile option and link option?

CodePudding user response:

When you set a variable, it is set inside cache CMakeCache.txt. When you don't reset it when reconfiguring, it preserves its previous value. The option.... OFF, only set's the option to OFF if it is unset. Even set(ENABLE_SANITIZER OFF) will not set the variable if it is in cache, only set(.... CACHE "" "" FORCE), refer to documentation.

  • Related