EXCLUDE_FROM_ALL does not work when the target is included in unused configurations for a build in Visual Studio and Xcode.
When a target is excluded for default build with EXCLUDE_FROM_ALL
but is included in another target's target_link_library
for certain configurations, it is always built through cmake --build
, even when a different configuration is specified.
Below is a CMakeLists.txt
that can cause this behavior, assuming main.cpp
and test.cpp
exist.
cmake_minimum_required(VERSION 3.16)
project(test CXX C)
add_executable(test)
set(CMAKE_CONFIGURATION_TYPES USE_DEPENDENT_TARGET NO_DEPENDENT_TARGET)
target_link_libraries(test
PRIVATE
$<$<CONFIG:USE_DEPENDENT_TARGET>:dependent_target>
)
target_sources(test
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)
add_library(dependent_target STATIC)
target_sources(dependent_target
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/test.cpp
)
set_target_properties(dependent_target
PROPERTIES
EXCLUDE_FROM_ALL TRUE
)
When built with --config NO_DEPENDENT_TARGET
the dependent_target
target is still built, for both Visual Studio and Xcode generators.