diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2017-05-26 14:36:22 +0200 |
---|---|---|
committer | Simon Hausmann <simon.hausmann@qt.io> | 2018-04-23 13:56:07 +0000 |
commit | be9a56e5e3ced5d0d668fa24e4c65ae928f2e25a (patch) | |
tree | 02df6e52ee13ba89b75275fd46cbf2ecaaf7c5ec /tests | |
parent | 72bb1d95fd99ece1c79223ad889fc7dbddcc36c6 (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 'tests')
7 files changed, 64 insertions, 23 deletions
diff --git a/tests/auto/corelib/io/qresourceengine/qresourceengine.pro b/tests/auto/corelib/io/qresourceengine/qresourceengine.pro index f937d23fe2..1e12a41dea 100644 --- a/tests/auto/corelib/io/qresourceengine/qresourceengine.pro +++ b/tests/auto/corelib/io/qresourceengine/qresourceengine.pro @@ -1,23 +1,2 @@ -CONFIG += testcase -TARGET = tst_qresourceengine - -QT = core testlib -SOURCES = tst_qresourceengine.cpp -RESOURCES += testqrc/test.qrc - -qtPrepareTool(QMAKE_RCC, rcc, _DEP) -runtime_resource.target = runtime_resource.rcc -runtime_resource.depends = $$PWD/testqrc/test.qrc $$QMAKE_RCC_EXE -runtime_resource.commands = $$QMAKE_RCC -root /runtime_resource/ -binary $$PWD/testqrc/test.qrc -o $${runtime_resource.target} -QMAKE_EXTRA_TARGETS = runtime_resource -PRE_TARGETDEPS += $${runtime_resource.target} -QMAKE_DISTCLEAN += $${runtime_resource.target} - -TESTDATA += \ - parentdir.txt \ - testqrc/* -GENERATED_TESTDATA = $${runtime_resource.target} - -android:!android-embedded { - RESOURCES += android_testdata.qrc -} +TEMPLATE = subdirs +SUBDIRS = staticplugin qresourceengine_test.pro diff --git a/tests/auto/corelib/io/qresourceengine/qresourceengine_test.pro b/tests/auto/corelib/io/qresourceengine/qresourceengine_test.pro new file mode 100644 index 0000000000..9478595df6 --- /dev/null +++ b/tests/auto/corelib/io/qresourceengine/qresourceengine_test.pro @@ -0,0 +1,31 @@ +CONFIG += testcase +TARGET = tst_qresourceengine + +QT = core testlib +SOURCES = tst_qresourceengine.cpp +RESOURCES += testqrc/test.qrc + +qtPrepareTool(QMAKE_RCC, rcc, _DEP) +runtime_resource.target = runtime_resource.rcc +runtime_resource.depends = $$PWD/testqrc/test.qrc $$QMAKE_RCC_EXE +runtime_resource.commands = $$QMAKE_RCC -root /runtime_resource/ -binary $$PWD/testqrc/test.qrc -o $${runtime_resource.target} +QMAKE_EXTRA_TARGETS = runtime_resource +PRE_TARGETDEPS += $${runtime_resource.target} +QMAKE_DISTCLEAN += $${runtime_resource.target} + +TESTDATA += \ + parentdir.txt \ + testqrc/* +GENERATED_TESTDATA = $${runtime_resource.target} + +android:!android-embedded { + RESOURCES += android_testdata.qrc +} + +win32 { + CONFIG(debug, debug|release): LIBS += -Lstaticplugin/debug + else: LIBS += -Lstaticplugin/release +} else { + LIBS += -Lstaticplugin +} +LIBS += -lmoctestplugin diff --git a/tests/auto/corelib/io/qresourceengine/staticplugin/.gitignore b/tests/auto/corelib/io/qresourceengine/staticplugin/.gitignore new file mode 100644 index 0000000000..c397dde6a5 --- /dev/null +++ b/tests/auto/corelib/io/qresourceengine/staticplugin/.gitignore @@ -0,0 +1 @@ +moctestplugin_plugin_resources.cpp diff --git a/tests/auto/corelib/io/qresourceengine/staticplugin/main.cpp b/tests/auto/corelib/io/qresourceengine/staticplugin/main.cpp new file mode 100644 index 0000000000..39a3a1e012 --- /dev/null +++ b/tests/auto/corelib/io/qresourceengine/staticplugin/main.cpp @@ -0,0 +1,9 @@ +#include <QObject> + +class PluginClass : public QObject +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.tests.moc" FILE "staticplugin.json") +}; + +#include "main.moc" diff --git a/tests/auto/corelib/io/qresourceengine/staticplugin/staticplugin.json b/tests/auto/corelib/io/qresourceengine/staticplugin/staticplugin.json new file mode 100644 index 0000000000..4103ecb18c --- /dev/null +++ b/tests/auto/corelib/io/qresourceengine/staticplugin/staticplugin.json @@ -0,0 +1 @@ +{ "Keys": [ "staticplugin" ] } diff --git a/tests/auto/corelib/io/qresourceengine/staticplugin/staticplugin.pro b/tests/auto/corelib/io/qresourceengine/staticplugin/staticplugin.pro new file mode 100644 index 0000000000..e19d884548 --- /dev/null +++ b/tests/auto/corelib/io/qresourceengine/staticplugin/staticplugin.pro @@ -0,0 +1,8 @@ +TEMPLATE = lib +TARGET = moctestplugin +CONFIG += plugin static +SOURCES = main.cpp +plugin_resource.files = main.cpp +plugin_resource.prefix = /staticplugin +RESOURCES += plugin_resource +QT = core diff --git a/tests/auto/corelib/io/qresourceengine/tst_qresourceengine.cpp b/tests/auto/corelib/io/qresourceengine/tst_qresourceengine.cpp index ab1866fb2d..3f0e68c1b8 100644 --- a/tests/auto/corelib/io/qresourceengine/tst_qresourceengine.cpp +++ b/tests/auto/corelib/io/qresourceengine/tst_qresourceengine.cpp @@ -56,6 +56,7 @@ private slots: void doubleSlashInRoot(); void setLocale(); void lastModified(); + void resourcesInStaticPlugins(); private: const QString m_runtimeResourceRcc; @@ -118,6 +119,7 @@ void tst_QResourceEngine::checkStructure_data() << QLatin1String("searchpath1") << QLatin1String("searchpath2") << QLatin1String("secondary_root") + << QLatin1String("staticplugin") << QLatin1String("test") << QLatin1String("withoutslashes"); @@ -504,6 +506,16 @@ void tst_QResourceEngine::lastModified() } } +Q_IMPORT_PLUGIN(PluginClass) +void tst_QResourceEngine::resourcesInStaticPlugins() +{ + // We built a separate static plugin and attempted linking against + // it. That should successfully register the resources linked into + // the plugin via moc generated Q_INIT_RESOURCE calls in a + // Q_CONSTRUCTOR_FUNCTION. + QVERIFY(QFile::exists(":/staticplugin/main.cpp")); +} + QTEST_MAIN(tst_QResourceEngine) #include "tst_qresourceengine.moc" |