CMake: Fix Windows -debug-and-release builds with CMake 3.21.0

CMake 3.21.0 introduced a regression where per-config sources can't be
specified for object libs when building Qt with Ninja Multi-Config
and cross-config mode on.
We hit that in our Windows resource compiler handling.

To work around the issue, pass the config-specific sources directly to
the target rather than via an object library, when using CMake 3.20 or
later. The original issue of not being able to do that has been fixed
in 3.20.

Amends ba6175eb73
Amends a1ccedeb44
Amends 657fa0462d

Pick-to: 6.2
Fixes: QTBUG-95229
Change-Id: Idea6d5bcc54b3124c66c45538c2e06e92f288f5f
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
Alexandru Croitor 2021-07-16 18:22:35 +02:00
parent 3a19c5b2e6
commit cd89b7b619

View File

@ -1361,10 +1361,24 @@ END
# We would like to do the following:
# target_sources(${target} PRIVATE "$<$<CONFIG:${cfg}>:${output}>")
# However, https://gitlab.kitware.com/cmake/cmake/-/issues/20682 doesn't let us.
# Work-around by compiling the resources in an object lib and linking that.
add_library(${target}_rc OBJECT "${output}")
target_link_libraries(${target} PRIVATE $<TARGET_OBJECTS:${target}_rc>)
#
# However, https://gitlab.kitware.com/cmake/cmake/-/issues/20682 doesn't let us do that
# in CMake 3.19 and earlier.
# We can do it in CMake 3.20 and later.
# And we have to do it with CMake 3.21.0 to avoid a different issue
# https://gitlab.kitware.com/cmake/cmake/-/issues/22436
#
# So use the object lib work around for <= 3.19 and target_sources directly for later
# versions.
set(use_obj_lib FALSE)
set(end_target "${target}")
if(CMAKE_VERSION VERSION_LESS 3.20)
set(use_obj_lib TRUE)
set(end_target "${target}_rc")
add_library(${target}_rc OBJECT "${output}")
target_link_libraries(${target} PRIVATE $<TARGET_OBJECTS:${target}_rc>)
endif()
while(outputs)
list(POP_FRONT cfgs cfg)
list(POP_FRONT outputs output)
@ -1373,7 +1387,7 @@ END
DEPENDS "${input}"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${input}" "${output}"
)
target_sources(${target}_rc PRIVATE "$<$<CONFIG:${cfg}>:${output}>")
target_sources(${end_target} PRIVATE "$<$<CONFIG:${cfg}>:${output}>")
endwhile()
endif()
endfunction()