From 4ee89f7cb286a1b85f9a7834bc245529d5bb610e Mon Sep 17 00:00:00 2001
From: Martchus <martchus@gmx.net>
Date: Sun, 18 Sep 2016 18:32:00 +0200
Subject: [PATCH 20/32] Pull dependencies of static libraries in CMake modules

When doing a static build of Qt, the dependencies of the Qt
libraries and plugins itself must be specified when linking
the final application.

With Qt 5.13.0 the support for doing that has been re-added.
However, some pieces are still missing and that's what this
patch provides:

1. Take dependencies of static plugins into account as well
2. Create imported targets for the dependencies to prevent
   CMake from adding wrong `-Wl,-Bstatic` and `-Wl,-Bdynamic`
   linker flags

This patch has been harmonized with official changes, e.g. to
use QMAKE_PRL_LIBS instead of QMAKE_PRL_LIBS_FOR_CMAKE.

See https://github.com/Martchus/PKGBUILDs/issues/86#issuecomment-476840055
for further details on 2.

With Qt 5.14.0 the support for using static libraries seems
to be further improved. I had not time to test whether it
actually works so I keep using my own implemention for now.
That's why this patch now removes a lot of the 5.14.0 and 5.15.0
additions in favour of 'how it used to work'.

Change-Id: I9a1a384aeb39f9185342e3581ab98feb8ad103a0
---
 .../data/cmake/Qt5BasicConfig.cmake.in        | 242 +++++++++---------
 .../data/cmake/Qt5PluginTarget.cmake.in       |  92 +------
 2 files changed, 133 insertions(+), 201 deletions(-)

diff --git a/mkspecs/features/data/cmake/Qt5BasicConfig.cmake.in b/mkspecs/features/data/cmake/Qt5BasicConfig.cmake.in
index db18dbece6..5a5bc4d61d 100644
--- a/mkspecs/features/data/cmake/Qt5BasicConfig.cmake.in
+++ b/mkspecs/features/data/cmake/Qt5BasicConfig.cmake.in
@@ -52,6 +52,10 @@ function(_qt5_$${CMAKE_MODULE_NAME}_process_prl_file prl_file_location Configura
     set(_lib_deps)
     set(_link_flags)
 
+    # prefer static libs
+    set(CMAKE_FIND_LIBRARY_SUFFIXES .a .lib)
+    set(CMAKE_FIND_LIBRARY_PREFIXES lib \"\")
+
 !!IF isEmpty(CMAKE_LIB_DIR_IS_ABSOLUTE)
     set(_qt5_install_libs \"${_qt5$${CMAKE_MODULE_NAME}_install_prefix}/$${CMAKE_LIB_DIR}\")
 !!ELSE
@@ -60,6 +64,10 @@ function(_qt5_$${CMAKE_MODULE_NAME}_process_prl_file prl_file_location Configura
 
     if(EXISTS \"${prl_file_location}\")
         file(STRINGS \"${prl_file_location}\" _prl_strings REGEX \"QMAKE_PRL_LIBS_FOR_CMAKE[ \\t]*=\")
+        if(NOT _prl_strings)
+            message(WARNING \"Prl file for Qt 5 module $${CMAKE_MODULE_NAME} does not contain QMAKE_PRL_LIBS_FOR_CMAKE. Was looking at \\\"${prl_file_location}\\\".\")
+            return()
+        endif()
 
         # file(STRINGS) replaces all semicolons read from the file with backslash semicolons.
         # We need to do a reverse transformation in CMake. For that we replace all backslash
@@ -91,7 +99,7 @@ function(_qt5_$${CMAKE_MODULE_NAME}_process_prl_file prl_file_location Configura
                     # Handle normal libraries passed as -lfoo
                     set(_lib \"${CMAKE_MATCH_1}\")
                     foreach(_standard_library ${_standard_libraries})
-                        if(_standard_library MATCHES \"^${_lib}(\\\\.lib)?$\")
+                        if(_standard_library MATCHES \"^${_lib}(\\\\.lib|\\\\.a)?$\")
                             set(_lib_is_default_linked TRUE)
                             break()
                         endif()
@@ -138,6 +146,37 @@ function(_qt5_$${CMAKE_MODULE_NAME}_process_prl_file prl_file_location Configura
     set(${lib_deps} ${_lib_deps} PARENT_SCOPE)
     set(${link_flags} \"SHELL:${_link_flags}\" PARENT_SCOPE)
 endfunction()
+
+if(NOT COMMAND _qt5_add_dependencies)
+    # add dependencies via imported targets so CMake does not mess with their (absolute) file path
+    # which can be problematic with unwanted -Wl,-Bstatic or -Wl,-Bdynamic in the linker line
+    function(_qt5_add_dependencies IMPORTED_TARGET_NAME IMPORTED_TARGET_CONFIG STATIC_DEPENDENCIES)
+        set(DEPENDENCY_INDEX 0)
+        unset(EXTRA_PATHS)
+        unset(DEPENDENCY_TARGETS)
+
+        foreach(LIBRARY_LOCATION ${STATIC_DEPENDENCIES})
+            if (TARGET \"${LIBRARY_LOCATION}\")
+                list(APPEND DEPENDENCY_TARGETS \"${LIBRARY_LOCATION}\")
+                continue()
+            endif()
+            set(DEPENDENCY_NAME ${IMPORTED_TARGET_NAME}_DEPENDENCY_${DEPENDENCY_INDEX})
+            if (NOT TARGET \"${DEPENDENCY_NAME}\")
+                add_library(${DEPENDENCY_NAME} STATIC IMPORTED)
+                set_property(TARGET ${DEPENDENCY_NAME} PROPERTY IMPORTED_LOCATION ${LIBRARY_LOCATION})
+            endif()
+            list(APPEND DEPENDENCY_TARGETS ${DEPENDENCY_NAME})
+            math(EXPR DEPENDENCY_INDEX \"${DEPENDENCY_INDEX} + 1\")
+        endforeach()
+
+        if (\"${IMPORTED_TARGET_CONFIG}\" STREQUAL \"ALL\" OR \"${IMPORTED_TARGET_CONFIG}\" STREQUAL \"RELEASE\")
+            set_property(TARGET ${IMPORTED_TARGET_NAME} PROPERTY INTERFACE_LINK_LIBRARIES ${DEPENDENCY_TARGETS})
+        endif()
+        if (NOT \"${IMPORTED_TARGET_CONFIG}\" STREQUAL \"ALL\")
+            set_property(TARGET ${IMPORTED_TARGET_NAME} PROPERTY INTERFACE_LINK_LIBRARIES_${IMPORTED_TARGET_CONFIG} ${DEPENDENCY_TARGETS})
+        endif()
+    endfunction()
+endif()
 !!ENDIF
 
 !!IF !equals(TEMPLATE, aux)
@@ -153,59 +192,36 @@ macro(_populate_$${CMAKE_MODULE_NAME}_target_properties Configuration LIB_LOCATI
     _qt5_$${CMAKE_MODULE_NAME}_check_file_exists(${imported_location})
     set(_deps
         ${_Qt5$${CMAKE_MODULE_NAME}_LIB_DEPENDENCIES}
-    )
-    set(_static_deps
 !!IF !isEmpty(CMAKE_STATIC_TYPE)
         ${_Qt5$${CMAKE_MODULE_NAME}_STATIC_${Configuration}_LIB_DEPENDENCIES}
 !!ENDIF
     )
 
+    # FIXME: handle IsDebugAndRelease like in official version
+
+!!IF !isEmpty(CMAKE_STATIC_TYPE)
     set_target_properties(Qt5::$${CMAKE_MODULE_NAME} PROPERTIES
         \"IMPORTED_LOCATION_${Configuration}\" ${imported_location}
 !!IF !isEmpty(CMAKE_LIB_SONAME)
         \"IMPORTED_SONAME_${Configuration}\" \"$${CMAKE_LIB_SONAME}\"
 !!ENDIF
-        # For backward compatibility with CMake < 2.8.12
-        \"IMPORTED_LINK_INTERFACE_LIBRARIES_${Configuration}\" \"${_deps};${_static_deps}\"
     )
-    set_property(TARGET Qt5::$${CMAKE_MODULE_NAME} APPEND PROPERTY INTERFACE_LINK_LIBRARIES
-                 \"${_deps}\"
-    )
-
-!!IF !isEmpty(CMAKE_STATIC_TYPE)
-    if(NOT "${IsDebugAndRelease}")
-        set(_genex_condition \"1\")
-    else()
-        if("${Configuration}" STREQUAL "DEBUG")
-            set(_genex_condition \"$<CONFIG:Debug>\")
-        else()
-            set(_genex_condition \"$<NOT:$<CONFIG:Debug>>\")
-        endif()
-    endif()
-
-    if(_static_deps)
-        set(_static_deps_genex \"$<${_genex_condition}:${_static_deps}>\")
-        set_property(TARGET Qt5::$${CMAKE_MODULE_NAME} APPEND PROPERTY INTERFACE_LINK_LIBRARIES
-                     \"${_static_deps_genex}\"
+    _qt5_add_dependencies(Qt5::$${CMAKE_MODULE_NAME} \"${Configuration}\" \"${_deps}\")
+    if(NOT CMAKE_VERSION VERSION_LESS \"3.13\")
+        set_target_properties(Qt5::$${CMAKE_MODULE_NAME} PROPERTIES
+            \"INTERFACE_LINK_OPTIONS\" \"${_Qt5$${CMAKE_MODULE_NAME}_STATIC_${Configuration}_LINK_FLAGS}\"
         )
     endif()
-
-    set(_static_link_flags \"${_Qt5$${CMAKE_MODULE_NAME}_STATIC_${Configuration}_LINK_FLAGS}\")
-    if(_static_link_flags)
-        set(_static_link_flags_genex \"$<${_genex_condition}:${_static_link_flags}>\")
-        if(NOT CMAKE_VERSION VERSION_LESS \"3.13\")
-            set_property(TARGET Qt5::$${CMAKE_MODULE_NAME} APPEND PROPERTY INTERFACE_LINK_OPTIONS
-                \"${_static_link_flags_genex}\"
-            )
-        else()
-            # Abuse INTERFACE_LINK_LIBRARIES to add link flags when CMake version is too low.
-            # Strip out SHELL:, because it is not supported in this property. And hope for the best.
-            string(REPLACE \"SHELL:\" \"\" _static_link_flags_genex \"${_static_link_flags_genex}\")
-            set_property(TARGET Qt5::$${CMAKE_MODULE_NAME} APPEND PROPERTY INTERFACE_LINK_LIBRARIES
-                \"${_static_link_flags_genex}\"
-            )
-        endif()
-    endif()
+!!ELSE
+    set_target_properties(Qt5::$${CMAKE_MODULE_NAME} PROPERTIES
+        \"INTERFACE_LINK_LIBRARIES_${Configuration}\" \"${_deps}\"
+        \"IMPORTED_LOCATION_${Configuration}\" ${imported_location}
+!!IF !isEmpty(CMAKE_LIB_SONAME)
+        \"IMPORTED_SONAME_${Configuration}\" \"$${CMAKE_LIB_SONAME}\"
+!!ENDIF
+        # For backward compatibility with CMake < 2.8.12
+        \"IMPORTED_LINK_INTERFACE_LIBRARIES_${Configuration}\" \"${_deps}\"
+    )
 !!ENDIF
 
 !!IF !isEmpty(CMAKE_WINDOWS_BUILD)
@@ -222,6 +238,15 @@ macro(_populate_$${CMAKE_MODULE_NAME}_target_properties Configuration LIB_LOCATI
     endif()
 !!ENDIF
 endmacro()
+
+macro(_populate_$${CMAKE_MODULE_NAME}_plugin_target_dependencies_from_prl_file PLUGIN_TARGET_NAME PRL_FILE_LOCATION CONFIGURATION LIB_DIRECTORY LIB_LOCATION)
+    _qt5_$${CMAKE_MODULE_NAME}_process_prl_file(
+        \"${PRL_FILE_LOCATION}\"
+        \"${CONFIGURATION}\"
+        _Qt5$${CMAKE_MODULE_NAME}_STATIC_PLUGIN_${CONFIGURATION}_LIB_DEPENDENCIES
+        _Qt5$${CMAKE_MODULE_NAME}_STATIC_PLUGIN_${CONFIGURATION}_LINK_FLAGS)
+    _qt5_add_dependencies(\"${PLUGIN_TARGET_NAME}\" ALL \"${_Qt5$${CMAKE_MODULE_NAME}_STATIC_PLUGIN_${CONFIGURATION}_LIB_DEPENDENCIES}\")
+endmacro()
 !!ENDIF
 
 if (NOT TARGET Qt5::$${CMAKE_MODULE_NAME})
@@ -411,13 +436,14 @@ if (NOT TARGET Qt5::$${CMAKE_MODULE_NAME})
     add_library(Qt5::$${CMAKE_MODULE_NAME} STATIC IMPORTED)
     set_property(TARGET Qt5::$${CMAKE_MODULE_NAME} PROPERTY IMPORTED_LINK_INTERFACE_LANGUAGES "CXX")
 !!ENDIF
-!!ELSE
+
+!!ELSE // !isEmpty(CMAKE_STATIC_TYPE)
 !!IF equals(TEMPLATE, aux)
     add_library(Qt5::$${CMAKE_MODULE_NAME} INTERFACE IMPORTED)
 !!ELSE
     add_library(Qt5::$${CMAKE_MODULE_NAME} SHARED IMPORTED)
 !!ENDIF
-!!ENDIF
+!!ENDIF // !isEmpty(CMAKE_STATIC_TYPE)
 
 !!IF !equals(TEMPLATE, aux)
 !!IF !isEmpty(CMAKE_BUILD_IS_FRAMEWORK)
@@ -449,6 +475,56 @@ if (NOT TARGET Qt5::$${CMAKE_MODULE_NAME})
 
     set_property(TARGET Qt5::$${CMAKE_MODULE_NAME} PROPERTY INTERFACE_QT_PLUGIN_TYPES \"$${CMAKE_MODULE_PLUGIN_TYPES}\")
 
+    # Find plugin targets
+    file(GLOB pluginTargetsMaybe \"${CMAKE_CURRENT_LIST_DIR}/*Qt5$${CMAKE_MODULE_NAME}_*.cmake\")
+    unset(pluginTargets)
+    if(pluginTargetsMaybe)
+        foreach(pluginTarget ${pluginTargetsMaybe})
+            file(STRINGS ${pluginTarget} matched REGEX Qt5$${CMAKE_MODULE_NAME}_PLUGINS)
+            if(matched)
+                list(APPEND pluginTargets ${pluginTarget})
+            endif()
+        endforeach()
+    endif()
+
+    macro(_populate_$${CMAKE_MODULE_NAME}_plugin_properties TARGET_NAME PLUGIN_TARGET_NAME PLUGIN_NAME CONFIG PLUGIN_LOCATION)
+        set_property(TARGET ${PLUGIN_TARGET_NAME} APPEND PROPERTY IMPORTED_CONFIGURATIONS ${CONFIG})
+
+!!IF isEmpty(CMAKE_PLUGIN_DIR_IS_ABSOLUTE)
+        set(imported_location \"${_qt5$${CMAKE_MODULE_NAME}_install_prefix}/$${CMAKE_PLUGIN_DIR}${PLUGIN_LOCATION}\")
+!!ELSE
+        set(imported_location \"$${CMAKE_PLUGIN_DIR}${PLUGIN_LOCATION}\")
+!!ENDIF
+        _qt5_$${CMAKE_MODULE_NAME}_check_file_exists(${imported_location})
+        set_target_properties(${PLUGIN_TARGET_NAME} PROPERTIES
+            \"IMPORTED_LOCATION_${CONFIG}\" ${imported_location}
+        )
+!!IF !isEmpty(CMAKE_STATIC_TYPE)
+        set_property(TARGET ${TARGET_NAME} APPEND PROPERTY STATIC_PLUGINS ${PLUGIN_NAME})
+        get_filename_component(_PLUGIN_DIR ${PLUGIN_LOCATION} PATH)
+        get_filename_component(_PLUGIN_DIR_NAME ${_PLUGIN_DIR} NAME)
+        get_filename_component(_PLUGIN_NAME ${PLUGIN_LOCATION} NAME)
+        string(REGEX REPLACE \"^lib(.*)\\\\.a\" \"${_qt5$${CMAKE_MODULE_NAME}_install_prefix}/lib/qt/plugins/${_PLUGIN_DIR_NAME}/\\\\1.static.prl\" PLUGIN_PRL_FILE_LOCATION ${_PLUGIN_NAME})
+        if(NOT EXISTS ${PLUGIN_PRL_FILE_LOCATION})
+            string(REGEX REPLACE \"^lib(.*)\\\\.a\" \"${_qt5$${CMAKE_MODULE_NAME}_install_prefix}/lib/qt/plugins/${_PLUGIN_DIR_NAME}/\\\\1.prl\" PLUGIN_PRL_FILE_LOCATION ${_PLUGIN_NAME})
+        endif()
+        if(NOT EXISTS ${PLUGIN_PRL_FILE_LOCATION})
+            string(REGEX REPLACE \"^lib(.*)\\\\.a\" \"${_qt5$${CMAKE_MODULE_NAME}_install_prefix}/lib/qt/plugins/${_PLUGIN_DIR_NAME}/lib\\\\1.prl\" PLUGIN_PRL_FILE_LOCATION ${_PLUGIN_NAME})
+        endif()
+        if(NOT EXISTS ${PLUGIN_PRL_FILE_LOCATION})
+            message(FATAL_ERROR "The prl file containing dependencies of static plugin ${PLUGIN_TARGET_NAME} of ${TARGET_NAME} could not be found.")
+        endif()
+        _populate_$${CMAKE_MODULE_NAME}_plugin_target_dependencies_from_prl_file(${PLUGIN_TARGET_NAME} ${PLUGIN_PRL_FILE_LOCATION} ${CONFIG} \"lib/qt/plugins\" ${PLUGIN_LOCATION})
+!!ENDIF
+
+    endmacro()
+
+    if(pluginTargets)
+        foreach(pluginTarget ${pluginTargets})
+            include(${pluginTarget})
+        endforeach()
+    endif()
+
     set(_Qt5$${CMAKE_MODULE_NAME}_PRIVATE_DIRS_EXIST TRUE)
     foreach (_Qt5$${CMAKE_MODULE_NAME}_PRIVATE_DIR ${Qt5$${CMAKE_MODULE_NAME}_OWN_PRIVATE_INCLUDE_DIRS})
         if (NOT EXISTS ${_Qt5$${CMAKE_MODULE_NAME}_PRIVATE_DIR})
@@ -467,6 +543,9 @@ if (NOT TARGET Qt5::$${CMAKE_MODULE_NAME})
                 list(APPEND _Qt5$${CMAKE_MODULE_NAME}_PRIVATEDEPS ${dep}Private)
             endif()
         endforeach()
+!!IF !isEmpty(CMAKE_STATIC_TYPE)
+        _qt5_add_dependencies(Qt5::$${CMAKE_MODULE_NAME}Private ALL \"Qt5::$${CMAKE_MODULE_NAME};${_Qt5$${CMAKE_MODULE_NAME}_PRIVATEDEPS}\")
+!!ELSE
         set_property(TARGET Qt5::$${CMAKE_MODULE_NAME}Private PROPERTY
             INTERFACE_LINK_LIBRARIES Qt5::$${CMAKE_MODULE_NAME} ${_Qt5$${CMAKE_MODULE_NAME}_PRIVATEDEPS}
         )
@@ -478,6 +557,7 @@ if (NOT TARGET Qt5::$${CMAKE_MODULE_NAME})
                 INTERFACE_LINK_LIBRARIES \"Qt5::$${CMAKE_MODULE_NAME}Private\"
             )
         endif()
+!!ENDIF // CMAKE_STATIC_TYPE
     endif()
 
 !!IF !equals(TEMPLATE, aux)
@@ -547,9 +627,11 @@ if (NOT TARGET Qt5::$${CMAKE_MODULE_NAME})
 !!ELSE
         \"$${CMAKE_IMPLIB_FILE_LOCATION_RELEASE}\" )
 !!ENDIF
-        _populate_$${CMAKE_MODULE_NAME}_target_properties(RELEASE \"$${CMAKE_LIB_FILE_LOCATION_RELEASE}\" \"$${CMAKE_IMPLIB_FILE_LOCATION_RELEASE}\" $${CMAKE_DEBUG_AND_RELEASE})
-!!ENDIF // CMAKE_STATIC_WINDOWS_BUILD
+        _populate_$${CMAKE_MODULE_NAME}_target_properties(Qt5::$${CMAKE_MODULE_NAME} RELEASE \"$${CMAKE_LIB_FILE_LOCATION_RELEASE}\" \"$${CMAKE_IMPLIB_FILE_LOCATION_RELEASE}\" $${CMAKE_DEBUG_AND_RELEASE})
     endif()
+!!ENDIF // CMAKE_STATIC_TYPE
+
+
 !!ENDIF // CMAKE_RELEASE_TYPE
 !!ENDIF // CMAKE_FIND_OTHER_LIBRARY_BUILD
 
@@ -560,76 +642,6 @@ if (NOT TARGET Qt5::$${CMAKE_MODULE_NAME})
     )
 !!ENDIF // TEMPLATE != aux
 
-!!IF isEmpty(CMAKE_INTERNAL_MODULE)
-    # In Qt 5.15 the glob pattern was relaxed to also catch plugins not literally named "Plugin".
-    # Define QT5_STRICT_PLUGIN_GLOB or ModuleName_STRICT_PLUGIN_GLOB to revert to old behavior.
-    if (QT5_STRICT_PLUGIN_GLOB OR Qt5$${CMAKE_MODULE_NAME}_STRICT_PLUGIN_GLOB)
-        file(GLOB pluginTargets \"${CMAKE_CURRENT_LIST_DIR}/Qt5$${CMAKE_MODULE_NAME}_*Plugin.cmake\")
-    else()
-        file(GLOB pluginTargets \"${CMAKE_CURRENT_LIST_DIR}/Qt5$${CMAKE_MODULE_NAME}_*.cmake\")
-    endif()
-
-    macro(_populate_$${CMAKE_MODULE_NAME}_plugin_properties Plugin Configuration PLUGIN_LOCATION
-          IsDebugAndRelease)
-        set_property(TARGET Qt5::${Plugin} APPEND PROPERTY IMPORTED_CONFIGURATIONS ${Configuration})
-
-!!IF isEmpty(CMAKE_PLUGIN_DIR_IS_ABSOLUTE)
-        set(imported_location \"${_qt5$${CMAKE_MODULE_NAME}_install_prefix}/$${CMAKE_PLUGIN_DIR}${PLUGIN_LOCATION}\")
-!!ELSE
-        set(imported_location \"$${CMAKE_PLUGIN_DIR}${PLUGIN_LOCATION}\")
-!!ENDIF
-        _qt5_$${CMAKE_MODULE_NAME}_check_file_exists(${imported_location})
-        set_target_properties(Qt5::${Plugin} PROPERTIES
-            \"IMPORTED_LOCATION_${Configuration}\" ${imported_location}
-        )
-
-!!IF !isEmpty(CMAKE_STATIC_TYPE)
-        set(_static_deps
-            ${_Qt5${Plugin}_STATIC_${Configuration}_LIB_DEPENDENCIES}
-        )
-
-        if(NOT "${IsDebugAndRelease}")
-            set(_genex_condition \"1\")
-        else()
-            if("${Configuration}" STREQUAL "DEBUG")
-                set(_genex_condition \"$<CONFIG:Debug>\")
-            else()
-                set(_genex_condition \"$<NOT:$<CONFIG:Debug>>\")
-            endif()
-        endif()
-        if(_static_deps)
-            set(_static_deps_genex \"$<${_genex_condition}:${_static_deps}>\")
-            set_property(TARGET Qt5::${Plugin} APPEND PROPERTY INTERFACE_LINK_LIBRARIES
-                         \"${_static_deps_genex}\"
-            )
-        endif()
-
-        set(_static_link_flags \"${_Qt5${Plugin}_STATIC_${Configuration}_LINK_FLAGS}\")
-        if(_static_link_flags)
-            set(_static_link_flags_genex \"$<${_genex_condition}:${_static_link_flags}>\")
-            if(NOT CMAKE_VERSION VERSION_LESS \"3.13\")
-                set_property(TARGET Qt5::${Plugin} APPEND PROPERTY INTERFACE_LINK_OPTIONS
-                    \"${_static_link_flags_genex}\"
-                )
-            else()
-                # Abuse INTERFACE_LINK_LIBRARIES to add link flags when CMake version is too low.
-                # Strip out SHELL:, because it is not supported in this property. And hope for the best.
-                string(REPLACE \"SHELL:\" \"\" _static_link_flags_genex \"${_static_link_flags_genex}\")
-                set_property(TARGET Qt5::${Plugin} APPEND PROPERTY INTERFACE_LINK_LIBRARIES
-                    \"${_static_link_flags_genex}\"
-                )
-            endif()
-        endif()
-!!ENDIF
-    endmacro()
-
-    if (pluginTargets)
-        foreach(pluginTarget ${pluginTargets})
-            include(${pluginTarget})
-        endforeach()
-    endif()
-!!ENDIF // isEmpty(CMAKE_INTERNAL_MODULE)
-
 !!IF !isEmpty(CMAKE_MODULE_EXTRAS)
     include(\"${CMAKE_CURRENT_LIST_DIR}/Qt5$${CMAKE_MODULE_NAME}ConfigExtras.cmake\")
 !!ENDIF
diff --git a/mkspecs/features/data/cmake/Qt5PluginTarget.cmake.in b/mkspecs/features/data/cmake/Qt5PluginTarget.cmake.in
index b465a7fa89..eaf9011c3d 100644
--- a/mkspecs/features/data/cmake/Qt5PluginTarget.cmake.in
+++ b/mkspecs/features/data/cmake/Qt5PluginTarget.cmake.in
@@ -1,5 +1,6 @@
-
-add_library(Qt5::$$CMAKE_PLUGIN_NAME MODULE IMPORTED)
+# Some Qt modules also load plugin target in extra config, so check whether the target already exists
+if(NOT TARGET Qt5::$$CMAKE_PLUGIN_NAME)
+    add_library(Qt5::$$CMAKE_PLUGIN_NAME MODULE IMPORTED)
 
 !!IF !isEmpty(CMAKE_STATIC_TYPE)
 set(_Qt5$${CMAKE_PLUGIN_NAME}_MODULE_DEPENDENCIES \"$${CMAKE_PLUGIN_MODULE_DEPS}\")
@@ -16,98 +17,17 @@ foreach(_module_dep ${_Qt5$${CMAKE_PLUGIN_NAME}_MODULE_DEPENDENCIES})
 endforeach()
 
 !!IF !isEmpty(CMAKE_RELEASE_TYPE)
-!!IF isEmpty(CMAKE_LIB_DIR_IS_ABSOLUTE)
-_qt5_$${CMAKE_MODULE_NAME}_process_prl_file(
-    \"${_qt5$${CMAKE_MODULE_NAME}_install_prefix}/$${CMAKE_PLUGIN_DIR}$${CMAKE_PRL_FILE_LOCATION_RELEASE}\" RELEASE
-    _Qt5$${CMAKE_PLUGIN_NAME}_STATIC_RELEASE_LIB_DEPENDENCIES
-    _Qt5$${CMAKE_PLUGIN_NAME}_STATIC_RELEASE_LINK_FLAGS
-)
-!!ELSE
-_qt5_$${CMAKE_MODULE_NAME}_process_prl_file(
-    \"$${CMAKE_PLUGIN_DIR}$${CMAKE_PRL_FILE_LOCATION_RELEASE}\" RELEASE
-    _Qt5$${CMAKE_PLUGIN_NAME}_STATIC_RELEASE_LIB_DEPENDENCIES
-    _Qt5$${CMAKE_PLUGIN_NAME}_STATIC_RELEASE_LINK_FLAGS
-)
+    _populate_$${CMAKE_MODULE_NAME}_plugin_properties(Qt5::$$CMAKE_MODULE_NAME Qt5::$$CMAKE_PLUGIN_NAME $$CMAKE_PLUGIN_NAME RELEASE \"$${CMAKE_PLUGIN_LOCATION_RELEASE}\")
 !!ENDIF
 !!ENDIF
 
 !!IF !isEmpty(CMAKE_DEBUG_TYPE)
-!!IF isEmpty(CMAKE_LIB_DIR_IS_ABSOLUTE)
-_qt5_$${CMAKE_MODULE_NAME}_process_prl_file(
-    \"${_qt5$${CMAKE_MODULE_NAME}_install_prefix}/$${CMAKE_PLUGIN_DIR}$${CMAKE_PRL_FILE_LOCATION_DEBUG}\" DEBUG
-    _Qt5$${CMAKE_PLUGIN_NAME}_STATIC_DEBUG_LIB_DEPENDENCIES
-    _Qt5$${CMAKE_PLUGIN_NAME}_STATIC_DEBUG_LINK_FLAGS
-)
-!!ELSE
-_qt5_$${CMAKE_MODULE_NAME}_process_prl_file(
-    \"$${CMAKE_PLUGIN_DIR}$${CMAKE_PRL_FILE_LOCATION_DEBUG}\" DEBUG
-    _Qt5$${CMAKE_PLUGIN_NAME}_STATIC_DEBUG_LIB_DEPENDENCIES
-    _Qt5$${CMAKE_PLUGIN_NAME}_STATIC_DEBUG_LINK_FLAGS
-)
-!!ENDIF
-!!ENDIF
-
-set_property(TARGET Qt5::$$CMAKE_PLUGIN_NAME PROPERTY INTERFACE_SOURCES
-    \"${CMAKE_CURRENT_LIST_DIR}/Qt5$${CMAKE_MODULE_NAME}_$${CMAKE_PLUGIN_NAME}_Import.cpp\"
-)
-!!ENDIF
-
-!!IF !isEmpty(CMAKE_RELEASE_TYPE)
-_populate_$${CMAKE_MODULE_NAME}_plugin_properties($$CMAKE_PLUGIN_NAME RELEASE \"$${CMAKE_PLUGIN_LOCATION_RELEASE}\" $${CMAKE_DEBUG_AND_RELEASE})
-!!ENDIF
-!!IF !isEmpty(CMAKE_DEBUG_TYPE)
-_populate_$${CMAKE_MODULE_NAME}_plugin_properties($$CMAKE_PLUGIN_NAME DEBUG \"$${CMAKE_PLUGIN_LOCATION_DEBUG}\" $${CMAKE_DEBUG_AND_RELEASE})
+    _populate_$${CMAKE_MODULE_NAME}_plugin_properties(Qt5::$$CMAKE_MODULE_NAME Qt5::$$CMAKE_PLUGIN_NAME $$CMAKE_PLUGIN_NAME DEBUG \"$${CMAKE_PLUGIN_LOCATION_DEBUG}\")
 !!ENDIF
 
 list(APPEND Qt5$${CMAKE_MODULE_NAME}_PLUGINS Qt5::$$CMAKE_PLUGIN_NAME)
 set_property(TARGET Qt5::$${CMAKE_MODULE_NAME} APPEND PROPERTY QT_ALL_PLUGINS_$${CMAKE_PLUGIN_TYPE_ESCAPED} Qt5::$${CMAKE_PLUGIN_NAME})
-!!IF !isEmpty(CMAKE_STATIC_TYPE)
-# $<GENEX_EVAL:...> wasn\'t added until CMake 3.12, so put a version guard around it
-if(CMAKE_VERSION VERSION_LESS \"3.12\")
-    set(_manual_plugins_genex \"$<TARGET_PROPERTY:QT_PLUGINS>\")
-    set(_plugin_type_genex \"$<TARGET_PROPERTY:QT_PLUGINS_$${CMAKE_PLUGIN_TYPE_ESCAPED}>\")
-    set(_no_plugins_genex \"$<TARGET_PROPERTY:QT_NO_PLUGINS>\")
-else()
-    set(_manual_plugins_genex \"$<GENEX_EVAL:$<TARGET_PROPERTY:QT_PLUGINS>>\")
-    set(_plugin_type_genex \"$<GENEX_EVAL:$<TARGET_PROPERTY:QT_PLUGINS_$${CMAKE_PLUGIN_TYPE_ESCAPED}>>\")
-    set(_no_plugins_genex \"$<GENEX_EVAL:$<TARGET_PROPERTY:QT_NO_PLUGINS>>\")
-endif()
-set(_user_specified_genex
-    \"$<IN_LIST:Qt5::$$CMAKE_PLUGIN_NAME,${_manual_plugins_genex};${_plugin_type_genex}>\"
-)
-set(_user_specified_genex_versionless
-    \"$<IN_LIST:Qt::$$CMAKE_PLUGIN_NAME,${_manual_plugins_genex};${_plugin_type_genex}>\"
-)
-string(CONCAT _plugin_genex
-    \"$<$<OR:\"
-        # Add this plugin if it\'s in the list of manually specified plugins or in the list of
-        # explicitly included plugin types.
-        \"${_user_specified_genex},\"
-        \"${_user_specified_genex_versionless},\"
-        # Add this plugin if all of the following are true:
-        # 1) the list of explicitly included plugin types is empty
-        # 2) the QT_PLUGIN_EXTENDS property for the plugin is empty or equal to one of the modules
-        #    listed in the plugin\'s PLUGIN_EXTEND qmake variable
-        # 3) the user hasn\'t explicitly excluded the plugin.
-        # TODO: Note that the current implementation of (2) is not entirely correct QTBUG-93501
-        \"$<AND:\"
-            \"$<STREQUAL:${_plugin_type_genex},>,\"
-            \"$<OR:\"
-                $${CMAKE_PLUGIN_EXTENDS_GENEX_CHECK}
-                \"$<STREQUAL:$<TARGET_PROPERTY:Qt5::$${CMAKE_PLUGIN_NAME},QT_PLUGIN_EXTENDS>,>\"
-            \">,\"
-            \"$<NOT:$<IN_LIST:Qt5::$${CMAKE_PLUGIN_NAME},${_no_plugins_genex}>>,\"
-            \"$<NOT:$<IN_LIST:Qt::$${CMAKE_PLUGIN_NAME},${_no_plugins_genex}>>\"
-        \">\"
-    \">:Qt5::$$CMAKE_PLUGIN_NAME>\"
-)
-set_property(TARGET Qt5::$${CMAKE_MODULE_NAME} APPEND PROPERTY INTERFACE_LINK_LIBRARIES
-    ${_plugin_genex}
-)
-set_property(TARGET Qt5::$${CMAKE_PLUGIN_NAME} APPEND PROPERTY INTERFACE_LINK_LIBRARIES
-    \"$${CMAKE_PLUGIN_QT5_MODULE_DEPS}\"
-)
-!!ENDIF
 set_property(TARGET Qt5::$${CMAKE_PLUGIN_NAME} PROPERTY QT_PLUGIN_TYPE \"$$CMAKE_PLUGIN_TYPE\")
 set_property(TARGET Qt5::$${CMAKE_PLUGIN_NAME} PROPERTY QT_PLUGIN_EXTENDS \"$$CMAKE_PLUGIN_EXTENDS\")
 set_property(TARGET Qt5::$${CMAKE_PLUGIN_NAME} PROPERTY QT_PLUGIN_CLASS_NAME \"$$CMAKE_PLUGIN_NAME\")
+endif()
-- 
2.41.0

