From be9a56e5e3ced5d0d668fa24e4c65ae928f2e25a Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Fri, 26 May 2017 14:36:22 +0200 Subject: 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) --- mkspecs/features/resources.prf | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'mkspecs') 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 " \ + "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} -- cgit v1.2.3