CMake: Disable static plugin imports for non-executable targets

There is no point in generating cpp files containing Q_IMPORT_PLUGIN()
macro calls for non-executable targets like modules, plugins and object
libraries in a static Qt build.
It causes unnecessary compiling of 10+ files for each of those targets.
In a static Qt build, plugin imports should only be done for executables,
tools and applications.

Pick-to: 6.0
Change-Id: Ied90ef2f6d77a61a093d393cfdf94c400284c4f0
Reviewed-by: Craig Scott <craig.scott@qt.io>
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
Alexandru Croitor 2020-11-30 19:28:00 +01:00
parent 9d83149133
commit b3576164f0
4 changed files with 39 additions and 2 deletions

View File

@ -9,11 +9,14 @@ function(qt_internal_add_cmake_library target)
${ARGN}
)
set(is_static_lib 0)
### Define Targets:
if(${arg_INTERFACE})
add_library("${target}" INTERFACE)
elseif(${arg_STATIC} OR (${arg_MODULE} AND NOT BUILD_SHARED_LIBS))
add_library("${target}" STATIC)
set(is_static_lib 1)
elseif(${arg_SHARED})
add_library("${target}" SHARED)
qt_internal_apply_win_prefix_and_suffix("${target}")
@ -32,6 +35,9 @@ function(qt_internal_add_cmake_library target)
qt_internal_apply_win_prefix_and_suffix("${target}")
else()
add_library("${target}")
if(NOT BUILD_SHARED_LIBS)
set(is_static_lib 1)
endif()
endif()
if (NOT arg_ARCHIVE_INSTALL_DIRECTORY AND arg_INSTALL_DIRECTORY)
@ -43,6 +49,11 @@ function(qt_internal_add_cmake_library target)
endif()
qt_skip_warnings_are_errors_when_repo_unclean("${target}")
# No need to compile Q_IMPORT_PLUGIN-containing files for non-executables.
if(is_static_lib)
_qt_internal_disable_static_default_plugins("${target}")
endif()
if (arg_INSTALL_DIRECTORY)
set(install_arguments
ARCHIVE_INSTALL_DIRECTORY ${arg_ARCHIVE_INSTALL_DIRECTORY}
@ -93,11 +104,14 @@ function(qt_internal_add_3rdparty_library target)
${ARGN}
)
set(is_static_lib 0)
### Define Targets:
if(${arg_INTERFACE})
add_library("${target}" INTERFACE)
elseif(${arg_STATIC} OR (${arg_MODULE} AND NOT BUILD_SHARED_LIBS))
add_library("${target}" STATIC)
set(is_static_lib 1)
elseif(${arg_SHARED})
add_library("${target}" SHARED)
elseif(${arg_MODULE})
@ -114,6 +128,9 @@ function(qt_internal_add_3rdparty_library target)
endif()
else()
add_library("${target}")
if(NOT BUILD_SHARED_LIBS)
set(is_static_lib 1)
endif()
endif()
if(NOT arg_INTERFACE)
@ -128,6 +145,11 @@ function(qt_internal_add_3rdparty_library target)
qt_internal_add_target_aliases(${target})
_qt_internal_apply_strict_cpp(${target})
# No need to compile Q_IMPORT_PLUGIN-containing files for non-executables.
if(is_static_lib)
_qt_internal_disable_static_default_plugins("${target}")
endif()
if (ANDROID)
qt_android_apply_arch_suffix("${target}")
endif()

View File

@ -31,16 +31,19 @@ function(qt_internal_add_module target)
### Define Targets:
set(is_interface_lib 0)
set(is_shared_lib 0)
set(is_static_lib 0)
if(${arg_HEADER_MODULE})
add_library("${target}" INTERFACE)
set(is_interface_lib 1)
elseif(${arg_STATIC})
add_library("${target}" STATIC)
set(is_static_lib 1)
elseif(${QT_BUILD_SHARED_LIBS})
add_library("${target}" SHARED)
set(is_shared_lib 1)
else()
add_library("${target}" STATIC)
set(is_static_lib 1)
endif()
set(property_prefix "INTERFACE_")
@ -86,6 +89,11 @@ function(qt_internal_add_module target)
qt_skip_warnings_are_errors_when_repo_unclean("${target}")
_qt_internal_apply_strict_cpp("${target}")
# No need to compile Q_IMPORT_PLUGIN-containing files for non-executables.
if(is_static_lib)
_qt_internal_disable_static_default_plugins("${target}")
endif()
# Add _private target to link against the private headers:
if(NOT ${arg_NO_PRIVATE_MODULE})
set(target_private "${target}Private")

View File

@ -88,7 +88,7 @@ function(qt_internal_add_plugin target)
# Disable linking of plugins against other plugins during static regular and
# super builds. The latter causes cyclic dependencies otherwise.
set_target_properties(${target} PROPERTIES QT_DEFAULT_PLUGINS 0)
_qt_internal_disable_static_default_plugins("${target}")
set_target_properties("${target}" PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${output_directory}"

View File

@ -494,6 +494,10 @@ macro(_qt_import_plugin target plugin)
endif()
endmacro()
function(_qt_internal_disable_static_default_plugins target)
set_target_properties(${target} PROPERTIES QT_DEFAULT_PLUGINS 0)
endfunction()
# This function is used to indicate which plug-ins are going to be
# used by a given target.
# This allows static linking to a correct set of plugins.
@ -518,7 +522,7 @@ function(qt6_import_plugins target)
# Handle NO_DEFAULT
if(${arg_NO_DEFAULT})
set_target_properties(${target} PROPERTIES QT_DEFAULT_PLUGINS 0)
_qt_internal_disable_static_default_plugins("${target}")
endif()
# Handle INCLUDE
@ -1088,6 +1092,9 @@ function(__qt_propagate_generated_resource target resource_name generated_source
target_link_libraries(${target} INTERFACE
"$<TARGET_OBJECTS:$<TARGET_NAME:${resource_target}>>")
set(${output_generated_target} "${resource_target}" PARENT_SCOPE)
# No need to compile Q_IMPORT_PLUGIN-containing files for non-executables.
_qt_internal_disable_static_default_plugins("${resource_target}")
else()
set(${output_generated_target} "" PARENT_SCOPE)
target_sources(${target} PRIVATE ${generated_source_code})