scx
scx

Reputation: 3947

How can you force config type when consuming CONFIG packages?

In a cmake build with a custom configuration type, for example RelWithAsserts.

if(CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_CONFIGURATION_TYPES Debug Release RelWithAsserts)
    set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING
        "Reset the configurations to what we need"
        FORCE)
endif()

How can you change the configuration for a package you consume through find_package, and force it to release?

# GTest doesn't have RelWithAsserts config type, so this wont work.
find_package(GTest CONFIG REQUIRED) 

# Somehow force this to use Release config.
target_link_libraries(test PRIVATE GTest::GTest)

ty

Upvotes: 0

Views: 157

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 66348

Configuration of IMPORTED targets is easily adjusted by setting CMAKE_MAP_IMPORTED_CONFIG_<CONFIG> variables:

# When the project is built in RelWithAsserts configuration
# use Release version of IMPORTED targets
set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHASSERTS Release)
# Now `find_package` could be used.
# It will create IMPORTED targets with needed properties
find_package(GTest CONFIG REQUIRED)

target_link_libraries(test PRIVATE GTest::GTest)

You may even use that setting:

# When the project is built in RelWithAsserts configuration
# use Release version of an IMPORTED target if it exists.
# If some target misses Release version, then fallback to Debug.
set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHASSERTS Release Debug)

Upvotes: 1

Related Questions