VS: cmake build still able to refer to a removed target with msbuild
I'm testing this on Windows with CMake 3.17.3, VS2019 generator:
cmake_minimum_required(VERSION 3.17.3)
project(test-targets)
file(WRITE main.cpp "int main() { return 0; }")
add_executable(test1 main.cpp)
if(NOT SKIP_TEST2)
add_executable(test2 main.cpp)
endif()
If I configure and then build with:
cmake -G "Visual Studio 16 2019" <source folder>
cmake --build . --target test1
cmake --build . --target test2
Both build as expected, but if I configure the same previously configured build folder and then build with:
cmake -G "Visual Studio 16 2019" -DSKIP_TEST2=ON <source folder>
cmake --build . --target test1
cmake --build . --target test2
Both build, which is not expected; I would expect for test2
to not be reachable/non existing for msbuild.
I verified via --trace-expand
that add_executable
for test2
is not called again.
I've also verified that with other build systems like Ninja on Windows, or Ninja/Makefile on Linux, that this doesn't happen.
At most you still have somewhere some folder that contains test2 files, which is "fine", but at least the target cannot be referred from the top directory or via cmake --build
command.
This is an issue because there's no other way I know to block the availability of a (or multiple) target(s) and the behavior is inconsistent compared to the other build systems.