summarylogtreecommitdiffstats
path: root/0022-Pull-dependencies-of-static-libraries-in-CMake-modul.patch
blob: 4ab26a2ab3b7d784b83361d3436c52c7de81f4ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
From 71f297d50afe7143f6754a5ea95013cddf25c3ea Mon Sep 17 00:00:00 2001
From: Martchus <martchus@gmx.net>
Date: Sun, 18 Sep 2016 18:32:00 +0200
Subject: [PATCH 22/34] 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.
---
 .../data/cmake/Qt5BasicConfig.cmake.in        | 208 +++++++++++++++---
 .../data/cmake/Qt5PluginTarget.cmake.in       |  11 +-
 qmake/generators/makefile.cpp                 |  16 +-
 3 files changed, 193 insertions(+), 42 deletions(-)

diff --git a/mkspecs/features/data/cmake/Qt5BasicConfig.cmake.in b/mkspecs/features/data/cmake/Qt5BasicConfig.cmake.in
index 3ed6dd5889..9073046205 100644
--- a/mkspecs/features/data/cmake/Qt5BasicConfig.cmake.in
+++ b/mkspecs/features/data/cmake/Qt5BasicConfig.cmake.in
@@ -48,36 +48,111 @@ but not all the files it references.
     endif()
 endmacro()
 
+!!IF !isEmpty(CMAKE_STATIC_TYPE)
+if(NOT COMMAND _qt5_add_dependencies)
+    # add dependencies via imported targets with their absolute file path so CMake does not mess with them
+    macro(_qt5_add_dependencies IMPORTED_TARGET_NAME IMPORTED_TARGET_CONFIG STATIC_DEPENDENCIES)
+        # prefer static libs
+        set(DEFAULT_CMAKE_FIND_LIBRARY_SUFFIXES \"${CMAKE_FIND_LIBRARY_SUFFIXES}\")
+        set(DEFAULT_CMAKE_FIND_LIBRARY_PREFIXES \"${CMAKE_FIND_LIBRARY_PREFIXES}\")
+        set(CMAKE_FIND_LIBRARY_SUFFIXES .a .lib)
+        set(CMAKE_FIND_LIBRARY_PREFIXES lib \"\")
+
+        set(DEPENDENCY_INDEX 0)
+        unset(EXTRA_PATHS)
+        unset(DEPENDENCY_TARGETS)
+
+        foreach(LIBRARY_NAME ${STATIC_DEPENDENCIES})
+            if (TARGET \"${LIBRARY_NAME}\")
+                list(APPEND DEPENDENCY_TARGETS \"${LIBRARY_NAME}\")
+                continue()
+            endif()
+            set(DEPENDENCY_NAME ${IMPORTED_TARGET_NAME}_DEPENDENCY_${DEPENDENCY_INDEX})
+            if (NOT TARGET \"${DEPENDENCY_NAME}\")
+                if(EXISTS \"${LIBRARY_NAME}\")
+                    set(IMPORTED_LOCATION ${LIBRARY_NAME})
+                elseif(\"${LIBRARY_NAME}\" MATCHES \" *-l(.*)\")
+                    set(LIBRARY_NAME \"${CMAKE_MATCH_1}\")
+                    find_library(\"${DEPENDENCY_NAME}_LIBRARY_PATH\" \"${LIBRARY_NAME}\" PATHS \"${EXTRA_PATHS}\")
+                    if (NOT EXISTS "${${DEPENDENCY_NAME}_LIBRARY_PATH}")
+                        message(WARNING \"Unable to find dependency ${LIBRARY_NAME} for static Qt target (was looking in ${EXTRA_PATHS}).\")
+                        continue()
+                    endif()
+                    set(IMPORTED_LOCATION \"${${DEPENDENCY_NAME}_LIBRARY_PATH}\")
+                elseif(\"${LIBRARY_NAME}\" MATCHES \" *-L(.*)\")
+                    list(APPEND EXTRA_PATHS \"${CMAKE_MATCH_1}\")
+                else()
+                    set(IMPORTED_LOCATION ${LIBRARY_NAME})
+                    message(WARNING \"Format of dependency unknown: ${LIBRARY_NAME}\")
+                endif()
+                add_library(${DEPENDENCY_NAME} STATIC IMPORTED)
+                set_property(TARGET ${DEPENDENCY_NAME} PROPERTY IMPORTED_LOCATION ${IMPORTED_LOCATION})
+            endif()
+            if (TARGET ${DEPENDENCY_NAME})
+                list(APPEND DEPENDENCY_TARGETS ${DEPENDENCY_NAME})
+            endif()
+            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()
+
+        # restore previous setup
+        set(CMAKE_FIND_LIBRARY_SUFFIXES \"${DEFAULT_CMAKE_FIND_LIBRARY_SUFFIXES}\")
+        set(CMAKE_FIND_LIBRARY_PREFIXES \"${DEFAULT_CMAKE_FIND_LIBRARY_PREFIXES}\")
+    endmacro()
+endif()
+!!ENDIF
+
 !!IF !equals(TEMPLATE, aux)
-macro(_populate_$${CMAKE_MODULE_NAME}_target_properties Configuration LIB_LOCATION IMPLIB_LOCATION)
-    set_property(TARGET Qt5::$${CMAKE_MODULE_NAME} APPEND PROPERTY IMPORTED_CONFIGURATIONS ${Configuration})
+macro(_populate_$${CMAKE_MODULE_NAME}_target_properties TARGET_CONFIG LIB_LOCATION IMPLIB_LOCATION)
+    set_property(TARGET Qt5::$${CMAKE_MODULE_NAME} APPEND PROPERTY IMPORTED_CONFIGURATIONS ${TARGET_CONFIG})
 
 !!IF isEmpty(CMAKE_DLL_DIR_IS_ABSOLUTE)
     set(imported_location \"${_qt5$${CMAKE_MODULE_NAME}_install_prefix}/$${CMAKE_DLL_DIR}${LIB_LOCATION}\")
 !!ELSE
     set(imported_location \"$${CMAKE_DLL_DIR}${LIB_LOCATION}\")
 !!ENDIF
+
     _qt5_$${CMAKE_MODULE_NAME}_check_file_exists(${imported_location})
+
+!!IF !isEmpty(CMAKE_STATIC_TYPE)
+    if (_Qt5$${CMAKE_MODULE_NAME}_LIB_DEPENDENCIES)
+        set(_list_sep \";\")
+    endif()
+    set_target_properties(Qt5::$${CMAKE_MODULE_NAME} PROPERTIES
+        \"IMPORTED_LOCATION_${TARGET_CONFIG}\" ${imported_location}
+!!IF !isEmpty(CMAKE_LIB_SONAME)
+        \"IMPORTED_SONAME_${TARGET_CONFIG}\" \"$${CMAKE_LIB_SONAME}\"
+!!ENDIF
+    )
+    _qt5_add_dependencies(Qt5::$${CMAKE_MODULE_NAME} \"${TARGET_CONFIG}\" \"${_Qt5$${CMAKE_MODULE_NAME}_LIB_DEPENDENCIES}${_list_sep}${_Qt5$${CMAKE_MODULE_NAME}_STATIC_${TARGET_CONFIG}_LIB_DEPENDENCIES}\")
+!!ELSE
     set_target_properties(Qt5::$${CMAKE_MODULE_NAME} PROPERTIES
-        \"INTERFACE_LINK_LIBRARIES\" \"${_Qt5$${CMAKE_MODULE_NAME}_LIB_DEPENDENCIES}\"
-        \"IMPORTED_LOCATION_${Configuration}\" ${imported_location}
+        \"INTERFACE_LINK_LIBRARIES_${TARGET_CONFIG}\" \"${_Qt5$${CMAKE_MODULE_NAME}_LIB_DEPENDENCIES}\"
+        \"IMPORTED_LOCATION_${TARGET_CONFIG}\" ${imported_location}
 !!IF !isEmpty(CMAKE_LIB_SONAME)
-        \"IMPORTED_SONAME_${Configuration}\" \"$${CMAKE_LIB_SONAME}\"
+        \"IMPORTED_SONAME_${TARGET_CONFIG}\" \"$${CMAKE_LIB_SONAME}\"
 !!ENDIF
         # For backward compatibility with CMake < 2.8.12
-        \"IMPORTED_LINK_INTERFACE_LIBRARIES_${Configuration}\" \"${_Qt5$${CMAKE_MODULE_NAME}_LIB_DEPENDENCIES}\"
+        \"IMPORTED_LINK_INTERFACE_LIBRARIES_${TARGET_CONFIG}\" \"${_Qt5$${CMAKE_MODULE_NAME}_LIB_DEPENDENCIES}\"
     )
+!!ENDIF
 
 !!IF !isEmpty(CMAKE_WINDOWS_BUILD)
 !!IF isEmpty(CMAKE_LIB_DIR_IS_ABSOLUTE)
     set(imported_implib \"${_qt5$${CMAKE_MODULE_NAME}_install_prefix}/$${CMAKE_LIB_DIR}${IMPLIB_LOCATION}\")
 !!ELSE
-    set(imported_implib \"IMPORTED_IMPLIB_${Configuration}\" \"$${CMAKE_LIB_DIR}${IMPLIB_LOCATION}\")
+    set(imported_implib \"IMPORTED_IMPLIB_${TARGET_CONFIG}\" \"$${CMAKE_LIB_DIR}${IMPLIB_LOCATION}\")
 !!ENDIF
     _qt5_$${CMAKE_MODULE_NAME}_check_file_exists(${imported_implib})
     if(NOT \"${IMPLIB_LOCATION}\" STREQUAL \"\")
         set_target_properties(Qt5::$${CMAKE_MODULE_NAME} PROPERTIES
-        \"IMPORTED_IMPLIB_${Configuration}\" ${imported_implib}
+        \"IMPORTED_IMPLIB_${TARGET_CONFIG}\" ${imported_implib}
         )
     endif()
 !!ENDIF
@@ -217,13 +292,45 @@ if (NOT TARGET Qt5::$${CMAKE_MODULE_NAME})
 !!IF !isEmpty(CMAKE_STATIC_TYPE)
     add_library(Qt5::$${CMAKE_MODULE_NAME} STATIC IMPORTED)
     set_property(TARGET Qt5::$${CMAKE_MODULE_NAME} PROPERTY IMPORTED_LINK_INTERFACE_LANGUAGES "CXX")
+    macro(_process_prl_file PRL_FILE_LOCATION CONFIGURATION)
+        if (EXISTS \"${PRL_FILE_LOCATION}\")
+            file(STRINGS \"${PRL_FILE_LOCATION}\" prl_strings REGEX \"QMAKE_PRL_LIBS_FOR_CMAKE\")
+            string(REGEX REPLACE \"QMAKE_PRL_LIBS_FOR_CMAKE *= *([^\\n]*)\" \"\\\\1\" static_depends ${prl_strings})
+            string(REGEX REPLACE \"\\\\$\\\\$\\\\[QT_INSTALL_LIBS\\\\]\" \"${_qt5$${CMAKE_MODULE_NAME}_install_prefix}/lib\" static_depends \"${static_depends}\")
+            string(REPLACE \"-framework;\" \"-framework \" static_depends \"${static_depends}\")
+            if (_Qt5$${CMAKE_MODULE_NAME}_STATIC_${CONFIGURATION}_LIB_DEPENDENCIES)
+                set(_list_sep \";\")
+            endif()
+            set(_Qt5$${CMAKE_MODULE_NAME}_STATIC_${CONFIGURATION}_LIB_DEPENDENCIES \"${_Qt5$${CMAKE_MODULE_NAME}_STATIC_${CONFIGURATION}_LIB_DEPENDENCIES}${_list_sep}${static_depends}\")
+        endif()
+    endmacro()
+
+    macro(_populate_plugin_target_dependencies_from_prl_file PLUGIN_TARGET_NAME PRL_FILE_LOCATION CONFIGURATION LIB_DIRECTORY LIB_LOCATION)
+        file(STRINGS "${PRL_FILE_LOCATION}" prl_strings REGEX "QMAKE_PRL_LIBS_FOR_CMAKE")
+        string(REGEX REPLACE \"QMAKE_PRL_LIBS_FOR_CMAKE *= *([^\n]*)\" \"\\\\1\" static_depends ${prl_strings})
+        string(REGEX REPLACE \"\\\\$\\\\$\\\\[QT_INSTALL_LIBS\\\\]\" \"${_qt5$${CMAKE_MODULE_NAME}_install_prefix}/lib\" static_depends \"${static_depends}\")
+        _qt5_add_dependencies(\"${PLUGIN_TARGET_NAME}\" ALL \"${static_depends}\")
+    endmacro()
+
+!!IF isEmpty(CMAKE_LIB_DIR_IS_ABSOLUTE)
+    _process_prl_file(\"${_qt5$${CMAKE_MODULE_NAME}_install_prefix}/$${CMAKE_LIB_DIR}$${CMAKE_PRL_FILE_LOCATION_DEBUG}\" DEBUG)
 !!ELSE
+    _process_prl_file(\"$${CMAKE_LIB_DIR}$${CMAKE_PRL_FILE_LOCATION_DEBUG}\" DEBUG)
+!!ENDIF
+!!IF isEmpty(CMAKE_LIB_DIR_IS_ABSOLUTE)
+    _process_prl_file(\"${_qt5$${CMAKE_MODULE_NAME}_install_prefix}/$${CMAKE_LIB_DIR}$${CMAKE_PRL_FILE_LOCATION_RELEASE}\" RELEASE)
+!!ELSE
+    _process_prl_file(\"$${CMAKE_LIB_DIR}$${CMAKE_PRL_FILE_LOCATION_RELEASE}\" RELEASE)
+!!ENDIF
+
+!!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)
     set_property(TARGET Qt5::$${CMAKE_MODULE_NAME} PROPERTY FRAMEWORK 1)
@@ -238,6 +345,56 @@ if (NOT TARGET Qt5::$${CMAKE_MODULE_NAME})
     set_property(TARGET Qt5::$${CMAKE_MODULE_NAME} PROPERTY INTERFACE_QT_ENABLED_FEATURES $$join(QT.$${MODULE}.enabled_features, ";"))
     set_property(TARGET Qt5::$${CMAKE_MODULE_NAME} PROPERTY INTERFACE_QT_DISABLED_FEATURES $$join(QT.$${MODULE}.disabled_features, ";"))
 
+    # 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_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})
@@ -256,9 +413,13 @@ 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}
         )
+!!ENDIF // CMAKE_STATIC_TYPE
     endif()
 
 !!IF !equals(TEMPLATE, aux)
@@ -328,9 +489,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}\" )
-!!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}\" )
     endif()
+!!ENDIF // CMAKE_STATIC_TYPE
+
+
 !!ENDIF // CMAKE_RELEASE_TYPE
 !!ENDIF // CMAKE_FIND_OTHER_LIBRARY_BUILD
 
@@ -341,29 +504,6 @@ if (NOT TARGET Qt5::$${CMAKE_MODULE_NAME})
     )
 !!ENDIF // TEMPLATE != aux
 
-    file(GLOB pluginTargets \"${CMAKE_CURRENT_LIST_DIR}/Qt5$${CMAKE_MODULE_NAME}_*Plugin.cmake\")
-
-    macro(_populate_$${CMAKE_MODULE_NAME}_plugin_properties Plugin Configuration PLUGIN_LOCATION)
-        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}
-        )
-    endmacro()
-
-    if (pluginTargets)
-        foreach(pluginTarget ${pluginTargets})
-            include(${pluginTarget})
-        endforeach()
-    endif()
-
-
 !!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 5baf0fdb10..ec5f3cc437 100644
--- a/mkspecs/features/data/cmake/Qt5PluginTarget.cmake.in
+++ b/mkspecs/features/data/cmake/Qt5PluginTarget.cmake.in
@@ -1,11 +1,14 @@
-
-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_RELEASE_TYPE)
-_populate_$${CMAKE_MODULE_NAME}_plugin_properties($$CMAKE_PLUGIN_NAME RELEASE \"$${CMAKE_PLUGIN_LOCATION_RELEASE}\")
+    _populate_$${CMAKE_MODULE_NAME}_plugin_properties(Qt5::$$CMAKE_MODULE_NAME Qt5::$$CMAKE_PLUGIN_NAME $$CMAKE_PLUGIN_NAME RELEASE \"$${CMAKE_PLUGIN_LOCATION_RELEASE}\")
 !!ENDIF
 !!IF !isEmpty(CMAKE_DEBUG_TYPE)
-_populate_$${CMAKE_MODULE_NAME}_plugin_properties($$CMAKE_PLUGIN_NAME DEBUG \"$${CMAKE_PLUGIN_LOCATION_DEBUG}\")
+    _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)
+
+endif()
diff --git a/qmake/generators/makefile.cpp b/qmake/generators/makefile.cpp
index e0c42131f8..94dfed92b3 100644
--- a/qmake/generators/makefile.cpp
+++ b/qmake/generators/makefile.cpp
@@ -1019,10 +1019,18 @@ MakefileGenerator::writePrlFile(QTextStream &t)
             libs << "LIBS" << "QMAKE_LIBS";
         else
             libs << "LIBS" << "LIBS_PRIVATE" << "QMAKE_LIBS" << "QMAKE_LIBS_PRIVATE";
-        t << "QMAKE_PRL_LIBS =";
-        for (ProStringList::Iterator it = libs.begin(); it != libs.end(); ++it)
-            t << qv(project->values((*it).toKey()));
-        t << endl;
+        QStringList libNames;
+        QStringList libNamesCMake;
+        for (const auto &lib : libs) {
+            for (const auto &libName : project->values(lib.toKey())) {
+                libNames << QMakeEvaluator::quoteValue(libName);
+                QString libNameCMake(libName.toQString());
+                libNameCMake.replace(QChar('\\'), QLatin1String("\\\\"));
+                libNamesCMake << libNameCMake;
+            }
+        }
+        t << "QMAKE_PRL_LIBS = " << libNames.join(QChar(' ')) << endl;
+        t << "QMAKE_PRL_LIBS_FOR_CMAKE = " << libNamesCMake.join(QChar(';')) << endl;
     }
 }
 
-- 
2.21.0