Propagate library/plugin resources automatically when linking statically

The rcc generated code relies on global constructors to register the resources.
The object file of the generated code is included by default in shared libraries
and executables. However when the object file ends up in a static library, the
linker will discard the object file when nothing references any of the symbols
in that object file, when linking the static library into the executable/shared library.
The solution is to link the object file straight into the final target, by means of a
cmake object library.

Change-Id: I02ad1173e4f7e8f907022c906640dc9086233676
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
This commit is contained in:
Simon Hausmann 2019-08-02 12:01:00 +02:00
parent 5d88ba001e
commit e343affd63
1 changed files with 14 additions and 1 deletions

View File

@ -2570,7 +2570,20 @@ function(add_qt_resource target resourceName)
DEPENDS ${files}
COMMENT "RCC ${resourceName}"
VERBATIM)
get_target_property(type "${target}" TYPE)
if(type STREQUAL STATIC_LIBRARY)
set(resourceTarget "${target}_resources_${resourceName}")
add_library("${resourceTarget}" OBJECT "${generatedSourceCode}")
qt_internal_add_target_aliases("${resourceTarget}")
qt_install(TARGETS "${resourceTarget}"
EXPORT "${INSTALL_CMAKE_NAMESPACE}${target}Targets"
DESTINATION ${INSTALL_LIBDIR}
)
target_link_libraries(${target} INTERFACE "$<TARGET_OBJECTS:${INSTALL_CMAKE_NAMESPACE}::${resourceTarget}>")
else()
target_sources(${target} PRIVATE "${generatedSourceCode}")
endif()
endfunction()