Files
cmake/Tests/RunCMake/CpsMapConfig/TestMappedConfig.cmake
Matthew Woehlke a96b426206 Tests: Test configuration mapping
While evaluating best practices related to CPS configuration use, a
question was raised as to what configuration mapping CMake uses to
choose the configuration of a target linked by another imported target
for which a non-standard configuration was chosen; namely, does CMake
use the `MAP_IMPORTED_CONFIG_<CONFIG>` for the configuration associated
with the most ancestral target / the project's build type, or for the
selected configuration of the direct consumer of the target?

It turns out the answer is "the former", and that this is actually the
more user-beneficial choice, as it allows users to craft configuration
maps that easily allow multi-axis configuration selection on correctly
crafter hierarchies, which are something CPS has "recommended" almost
since its inception (but CMake cannot presently generate).

This has three effects. First, it means that CPS suggesting such
hierarchies as a way to implement multiple configuration axes is not
actually as insane as it would be otherwise. Second, it means that
consumers can specify mappings for transitively included targets without
needing to know or care about what configurations of the intervening
targets exist. Third, it means that the current behavior is desirable,
and having something which verifies that behavior is useful. Thus, this
adds a test to do exactly that.

There are no actual behavior changes here; the test only verifies the
already existing behavior.
2026-01-07 15:02:20 -05:00

26 lines
679 B
CMake

macro(set_config_map CONFIG)
string(TOUPPER "${CONFIG}" CONFIG_UPPER)
set(CMAKE_MAP_IMPORTED_CONFIG_${CONFIG_UPPER} "${ANIMAL_CONFIGS}")
message(STATUS "CMAKE_MAP_IMPORTED_CONFIG_${CONFIG_UPPER} => \"${ANIMAL_CONFIGS}\"")
endmacro()
# Set up configuration maps
if(DEFINED CMAKE_BUILD_TYPE)
set_config_map(${CMAKE_BUILD_TYPE})
else()
foreach(CONFIG IN LISTS CMAKE_CONFIGURATION_TYPES)
set_config_map(${CONFIG})
endforeach()
endif()
# Find the test package
set(animal_DIR ${CMAKE_CURRENT_SOURCE_DIR})
find_package(animal REQUIRED)
# Set up the test executable
set(CMAKE_CXX_STANDARD 11)
add_executable(test test.cpp)
target_link_libraries(test animal::animal)