summaryrefslogtreecommitdiffstats
path: root/mkspecs
diff options
context:
space:
mode:
authorSimon Hausmann <simon.hausmann@qt.io>2017-05-26 14:36:22 +0200
committerSimon Hausmann <simon.hausmann@qt.io>2018-04-23 13:56:07 +0000
commitbe9a56e5e3ced5d0d668fa24e4c65ae928f2e25a (patch)
tree02df6e52ee13ba89b75275fd46cbf2ecaaf7c5ec /mkspecs
parent72bb1d95fd99ece1c79223ad889fc7dbddcc36c6 (diff)
Make it easier to use resources in plugins when using static linking
RCC generates code that registers resources automatically on program startup via global constructors. When linking statically and nothing references the symbols in the .o file compiled from the RCC generated code, then the linker will discard the embedded resources and they will not get initialized. That is why for static linking it is necessary to explicitly initialize resources using the Q_INIT_RESOURCE macro. We can avoid the need for the explicit initialization in the context of plugins that are statically linked into the application. resources.prf can generate a .cpp file with a helper function that contains all the Q_INIT_RESOURCE calls for all resources in the plugin. That helper function in turn is injected into the plugin entry point, which in turn is guaranteed to be included in the final binary. Change-Id: If1abf9c85ef92935020af073b989c58c1ae6ca63 Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
Diffstat (limited to 'mkspecs')
-rw-r--r--mkspecs/features/resources.prf32
1 files changed, 32 insertions, 0 deletions
diff --git a/mkspecs/features/resources.prf b/mkspecs/features/resources.prf
index ff45446219..7ae78d021e 100644
--- a/mkspecs/features/resources.prf
+++ b/mkspecs/features/resources.prf
@@ -72,6 +72,38 @@ for(resource, RESOURCES) {
RESOURCES += $$resource_file
}
+!isEmpty(RESOURCES):contains(TEMPLATE, .*lib):plugin:static {
+ resource_init_function = $$lower($$basename(TARGET))_plugin_resource_init
+ DEFINES += "QT_PLUGIN_RESOURCE_INIT_FUNCTION=$$resource_init_function"
+
+ RESOURCE_INIT_CPP = $$OUT_PWD/$$lower($$basename(TARGET))_plugin_resources.cpp
+
+ GENERATED_SOURCES += $$RESOURCE_INIT_CPP
+ QMAKE_DISTCLEAN += $$RESOURCE_INIT_CPP
+
+ !build_pass {
+ RESOURCE_INIT_CONT = \
+ "// This file is autogenerated by qmake. It contains a function that" \
+ "// references all resources the plugin includes and the function is" \
+ "// referenced by Qt_(MOC_)EXPORT_PLUGIN to ensure the inclusion in" \
+ "// the statically linked plugin." \
+ "$${LITERAL_HASH}include <QtCore/qglobal.h>" \
+ "void $${resource_init_function}() " \
+ "{" \
+
+ for (resource, RESOURCES) {
+ resource_name = $$section($$list($$basename(resource)), ., 0, 0)
+ resource_name = $$replace(resource_name, [^a-zA-Z0-9_], _)
+ RESOURCE_INIT_CONT += " Q_INIT_RESOURCE($$resource_name);"
+ }
+
+ RESOURCE_INIT_CONT += \
+ "}"
+
+ write_file($$RESOURCE_INIT_CPP, RESOURCE_INIT_CONT)|error()
+ }
+}
+
rcc.input = RESOURCES
rcc.name = RCC ${QMAKE_FILE_IN}
rcc.depend_command = $$QMAKE_RCC_DEP -list $$QMAKE_RESOURCE_FLAGS ${QMAKE_FILE_IN}