CMake: Fix resource objects story in static prl files

The CMake build of Qt intends to improve the developer experience in
regards to static Qt builds and Qt resource files. Specifically with a
CMake build of Qt, Qt developers don't have to manually call
Q_INIT_RESOURCE anymore.

For details see the following commits
e343affd63
e817ac3d68
4ab5432081

The last commit's implementation is incomplete though.
To ensure successful linking, each target's prl file should contain
not only the resource object files that are assigned to the target,
but also all resource object files of the target's dependencies.

To achieve that, qt_collect_libs will now recursively collect all
resource object files assigned to the QT_RCC_OBJECTS property of each
dependency.

Note this implementation is still incomplete. We do not export rcc
object file information in the CMake Targets files.

That means that when configuring qtdeclarative in a
non-top-level build, the generated Qml prl file will not
contain references to Core's mimetypes resource object file, etc.

So with the current change, only the object files that are part of the
current CMake configuration build are tracked.
Exporting the resource object files locations proves in a format
usable for prl files proves to be difficult (due to CMake not
supporting exporting genexes in random properties) and will have to be
addressed in a separate change.

Amends 4ab5432081

Task-number: QTBUG-88425
Change-Id: I546655bdfdf7aa86a8df9aadfc054fa415130a33
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
Alexandru Croitor 2020-11-16 18:42:23 +01:00
parent 5533eeeb01
commit f9dcade5e7
2 changed files with 70 additions and 26 deletions

View File

@ -9,7 +9,7 @@
#
# Uses qt_internal_walk_libs.
function(qt_find_package_promote_targets_to_global_scope target)
qt_internal_walk_libs("${target}" _discared_out_var
qt_internal_walk_libs("${target}" _discarded_out_var _discarded_out_var_2
"qt_find_package_targets_dict" "promote_global")
endfunction()

View File

@ -10,10 +10,15 @@ function(qt_merge_libs out_libs_var)
endfunction()
# Collects the library dependencies of a target.
# As well as rcc object file dependencies.
# This takes into account transitive usage requirements.
function(qt_collect_libs target out_var)
qt_internal_walk_libs("${target}" "${out_var}" "qt_collect_libs_dict" "collect_libs")
set("${out_var}" "${${out_var}}" PARENT_SCOPE)
function(qt_collect_libs target libs_out_var rcc_objects_out_var)
qt_internal_walk_libs("${target}" "${libs_out_var}"
"${rcc_objects_out_var}" "qt_collect_libs_dict" "collect_libs")
set("${libs_out_var}" "${${libs_out_var}}" PARENT_SCOPE)
set(${rcc_objects_out_var} "${${rcc_objects_out_var}}" PARENT_SCOPE)
endfunction()
# Extracts value from per-target dict key and assigns it to out_var.
@ -64,10 +69,13 @@ endfunction()
#
# out_var is the name of the variable where the result will be assigned. The result is a list of
# libraries, mostly in generator expression form.
# dict_name is used for caching the result, and preventing the same target from being processed
# rcc_objects_out_var is the name of the variable where the collected rcc object files will be
# assigned (for the initial target and its dependencies)
# dict_name is used for caching the results, and preventing the same target from being processed
# twice
# operation is a string to tell the function what to do
function(qt_internal_walk_libs target out_var dict_name operation)
# operation is a string to tell the function what additional behaviors to execute.
function(qt_internal_walk_libs
target out_var rcc_objects_out_var dict_name operation)
set(collected ${ARGN})
if(target IN_LIST collected)
return()
@ -87,9 +95,11 @@ function(qt_internal_walk_libs target out_var dict_name operation)
add_library(${dict_name} INTERFACE IMPORTED GLOBAL)
endif()
qt_internal_get_dict_key_values(libs "${target}" "${dict_name}" "libs")
qt_internal_get_dict_key_values(rcc_objects "${target}" "${dict_name}" "rcc_objects")
if(libs MATCHES "-NOTFOUND$")
unset(libs)
unset(rcc_objects)
get_target_property(target_libs ${target} INTERFACE_LINK_LIBRARIES)
if(NOT target_libs)
unset(target_libs)
@ -101,6 +111,15 @@ function(qt_internal_walk_libs target out_var dict_name operation)
list(APPEND target_libs ${link_libs})
endif()
endif()
# Need to record the rcc object file info not only for dependencies, but also for
# the current target too. Otherwise the saved information is incomplete for prl static
# build purposes.
get_target_property(main_target_rcc_objects ${target} QT_RCC_OBJECTS)
if(main_target_rcc_objects)
qt_merge_libs(rcc_objects ${main_target_rcc_objects})
endif()
foreach(lib ${target_libs})
# Cannot use $<TARGET_POLICY:...> in add_custom_command.
# Check the policy now, and replace the generator expression with the value.
@ -157,18 +176,36 @@ function(qt_internal_walk_libs target out_var dict_name operation)
get_target_property(lib_target_type ${lib_target} TYPE)
if(lib_target_type STREQUAL "INTERFACE_LIBRARY")
qt_internal_walk_libs(
${lib_target} lib_libs_${target} "${dict_name}" "${operation}" ${collected})
${lib_target}
lib_libs_${target}
lib_rcc_objects_${target}
"${dict_name}" "${operation}" ${collected})
if(lib_libs_${target})
qt_merge_libs(libs ${lib_libs_${target}})
set(is_module 0)
endif()
if(lib_rcc_objects_${target})
qt_merge_libs(rcc_objects ${lib_rcc_objects_${target}})
endif()
else()
qt_merge_libs(libs "$<TARGET_FILE:${lib_target}>")
get_target_property(target_rcc_objects "${lib_target}" QT_RCC_OBJECTS)
if(target_rcc_objects)
qt_merge_libs(rcc_objects ${target_rcc_objects})
endif()
qt_internal_walk_libs(
${lib_target} lib_libs_${target} "${dict_name}" "${operation}" ${collected})
${lib_target}
lib_libs_${target}
lib_rcc_objects_${target}
"${dict_name}" "${operation}" ${collected})
if(lib_libs_${target})
qt_merge_libs(libs ${lib_libs_${target}})
endif()
if(lib_rcc_objects_${target})
qt_merge_libs(rcc_objects ${lib_rcc_objects_${target}})
endif()
endif()
if(operation STREQUAL "promote_global")
set(lib_target_unaliased "${lib_target}")
@ -197,9 +234,12 @@ function(qt_internal_walk_libs target out_var dict_name operation)
endif()
endforeach()
qt_internal_memoize_values_in_dict("${target}" "${dict_name}" "libs" "${libs}")
qt_internal_memoize_values_in_dict("${target}" "${dict_name}"
"rcc_objects" "${rcc_objects}")
endif()
set(${out_var} ${libs} PARENT_SCOPE)
set(${rcc_objects_out_var} ${rcc_objects} PARENT_SCOPE)
endfunction()
# Generate a qmake .prl file for the given target.
@ -210,15 +250,6 @@ function(qt_generate_prl_file target install_dir)
return()
endif()
get_target_property(rcc_objects ${target} QT_RCC_OBJECTS)
if(rcc_objects)
if(QT_WILL_INSTALL)
list(TRANSFORM rcc_objects PREPEND "$$[QT_INSTALL_LIBS]/")
endif()
else()
unset(rcc_objects)
endif()
unset(prl_config)
set(is_static FALSE)
if(target_type STREQUAL "STATIC_LIBRARY")
@ -235,6 +266,26 @@ function(qt_generate_prl_file target install_dir)
endif()
list(JOIN prl_config " " prl_config)
set(rcc_objects "")
set(prl_step1_content_libs "")
if(NOT is_static AND WIN32)
# Do nothing. Prl files for shared libraries on Windows shouldn't have the libs listed,
# as per qt_build_config.prf and the conditional CONFIG+=explicitlib assignment.
else()
set(prl_libs "")
qt_collect_libs(${target} prl_libs prl_rcc_objects)
if(prl_libs)
set(prl_step1_content_libs "QMAKE_PRL_LIBS_FOR_CMAKE = ${prl_libs}\n")
endif()
if(prl_rcc_objects)
list(APPEND rcc_objects ${prl_rcc_objects})
endif()
endif()
if(rcc_objects AND QT_WILL_INSTALL)
list(TRANSFORM rcc_objects PREPEND "$$[QT_INSTALL_LIBS]/")
endif()
# Generate a preliminary .prl file that contains absolute paths to all libraries
if(MINGW)
# For MinGW, qmake doesn't have a lib prefix in prl files.
@ -286,15 +337,8 @@ QMAKE_PRL_BUILD_DIR = ${CMAKE_CURRENT_BINARY_DIR}
QMAKE_PRL_TARGET = $<TARGET_FILE_NAME:${target}>
QMAKE_PRL_CONFIG = ${prl_config}
QMAKE_PRL_VERSION = ${PROJECT_VERSION}
${prl_step1_content_libs}
")
if(NOT is_static AND WIN32)
# Do nothing. Prl files for shared libraries on Windows shouldn't have the libs listed,
# as per qt_build_config.prf and the conditional CONFIG+=explicitlib assignment.
else()
set(prl_libs "")
qt_collect_libs(${target} prl_libs)
string(APPEND prl_step1_content "QMAKE_PRL_LIBS_FOR_CMAKE = ${prl_libs}\n")
endif()
file(GENERATE
OUTPUT "${prl_step1_path}"