export: add support for exporting only single configuration from multi-config generators
The export command is a great utility for exporting executables from the host build to the target build in cross-compilation scenarios.
However, if the multi-config CMake generator is used for the host build, the generated script will contain a path to every configuration of the exported binary.
For example, by having export( TARGETS ModelDumper ${CMAKE_BINARY_DIR}/exported-targets.cmake )
, the exported-targets.cmake
will contain code like this (example with Ninja Multi-Config, but similar behavior is also for Xcode and Visual Studio):
# Create imported target ModelDumper
add_executable(ModelDumper IMPORTED)
# Import target "ModelDumper" for configuration "Release"
set_property(TARGET ModelDumper APPEND PROPERTY IMPORTED_CONFIGURATIONS RELEASE)
set_target_properties(ModelDumper PROPERTIES
IMPORTED_LOCATION_RELEASE "/Users/dodo/builds/host/Release/ModelDumper"
)
# Import target "ModelDumper" for configuration "Debug"
set_property(TARGET ModelDumper APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
set_target_properties(ModelDumper PROPERTIES
IMPORTED_LOCATION_DEBUG "/Users/dodo/builds/host/Debug/ModelDumper"
)
Now if I import this script in my cross-compilation build (e.g. Android), and attempt building a Debug
build, the build system will try to use the Debug
version of the ModelDumper
from the host.
This is not always desired. For example, my ModelCompiler
is very slow in Debug build (which is OK, when I want to debug it), and I would like to be able to use the Release
version of it while building the Debug version of my Android app.
My current workaround is to use regex machinery offered by CMake to replace IMPORTED_LOCATION_DEBUG "/Users/dodo/builds/host/Debug/ModelDumper"
with IMPORTED_LOCATION_DEBUG "/Users/dodo/builds/host/Release/ModelDumper"
before including the exported-targets.cmake
on the Android side. I initially tried to "post-process" generated file on the host side, but that didn't work because CMake generates that file after the configuration phase.
I suggest adding an option to the export
command that will enable defining a single configuration that will always be exported, even in multi-config generators. If I'm not mistaken, this is already the case for single-config generators.
Maybe something like export( TARGETS ModelDumper ${CMAKE_BINARY_DIR}/exported-targets.cmake CONFIGURATION Release )