summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@nokia.com>2012-02-06 23:36:17 +0100
committerQt by Nokia <qt-info@nokia.com>2012-02-15 17:27:37 +0100
commit7184456f9a26a9c07a689c15b415ddf82eb000b7 (patch)
tree076d42ad3fd6f08a1cbef21ee6811a3fa951850b /src/corelib
parent556dd0228aca07dfe8443fd99e9f2ead4e13ab22 (diff)
Implement new plugin mechanism
moc can now embed meta information about the plugin inside the plugin itself. This information can be queried by Qt without having to load the plugin. Source compatibility with the old plugin loading mechanism is still there, but will be removed before Qt 5.0. Change-Id: I03e4196ddfed07d0fe94acca40d5de8a6ce7f920 Reviewed-by: João Abecasis <joao.abecasis@nokia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/kernel/qobjectdefs.h1
-rw-r--r--src/corelib/plugin/qelfparser_p.cpp4
-rw-r--r--src/corelib/plugin/qelfparser_p.h2
-rw-r--r--src/corelib/plugin/qfactoryloader.cpp157
-rw-r--r--src/corelib/plugin/qfactoryloader_p.h9
-rw-r--r--src/corelib/plugin/qlibrary.cpp159
-rw-r--r--src/corelib/plugin/qlibrary_p.h19
-rw-r--r--src/corelib/plugin/qplugin.h84
-rw-r--r--src/corelib/plugin/qpluginloader.cpp29
-rw-r--r--src/corelib/plugin/qpluginloader.h3
10 files changed, 357 insertions, 110 deletions
diff --git a/src/corelib/kernel/qobjectdefs.h b/src/corelib/kernel/qobjectdefs.h
index 315619761c..0b1fa8839f 100644
--- a/src/corelib/kernel/qobjectdefs.h
+++ b/src/corelib/kernel/qobjectdefs.h
@@ -76,6 +76,7 @@ class QString;
# define emit
#endif
#define Q_CLASSINFO(name, value)
+#define Q_PLUGIN_METADATA(x)
#define Q_INTERFACES(x)
#define Q_PROPERTY(text)
#define Q_PRIVATE_PROPERTY(d, text)
diff --git a/src/corelib/plugin/qelfparser_p.cpp b/src/corelib/plugin/qelfparser_p.cpp
index 0e8d43454c..70abbaf6e1 100644
--- a/src/corelib/plugin/qelfparser_p.cpp
+++ b/src/corelib/plugin/qelfparser_p.cpp
@@ -203,7 +203,7 @@ int QElfParser::parse(const char *dataStart, ulong fdlen, const QString &library
qDebug() << "++++" << i << shnam;
#endif
- if (qstrcmp(shnam, ".qtplugin") == 0 || qstrcmp(shnam, ".rodata") == 0) {
+ if (qstrcmp(shnam, ".qtmetadata") == 0 || qstrcmp(shnam, ".qtplugin") == 0 || qstrcmp(shnam, ".rodata") == 0) {
if (!(sh.type & 0x1)) {
if (shnam[1] == 'r') {
if (lib)
@@ -227,7 +227,7 @@ int QElfParser::parse(const char *dataStart, ulong fdlen, const QString &library
*pos = sh.offset;
*sectionlen = sh.size - 1;
if (shnam[1] == 'q')
- return Ok;
+ return shnam[3] == 'm' ? QtMetaDataSection : QtPluginSection;
}
s += e_shentsize;
}
diff --git a/src/corelib/plugin/qelfparser_p.h b/src/corelib/plugin/qelfparser_p.h
index 96e1eac33f..afd8112138 100644
--- a/src/corelib/plugin/qelfparser_p.h
+++ b/src/corelib/plugin/qelfparser_p.h
@@ -72,7 +72,7 @@ typedef quintptr qelfaddr_t;
class QElfParser
{
public:
- enum {Ok = 0, NotElf = 1, NoQtSection = 2, Corrupt = 3};
+ enum { QtMetaDataSection, QtPluginSection, NoQtSection, NotElf, Corrupt };
enum {ElfLittleEndian = 0, ElfBigEndian = 1};
struct ElfSectionHeader
diff --git a/src/corelib/plugin/qfactoryloader.cpp b/src/corelib/plugin/qfactoryloader.cpp
index fe5aea0e8a..ce988fa2ff 100644
--- a/src/corelib/plugin/qfactoryloader.cpp
+++ b/src/corelib/plugin/qfactoryloader.cpp
@@ -51,6 +51,10 @@
#include "qpluginloader.h"
#include "private/qobject_p.h"
#include "private/qcoreapplication_p.h"
+#include "qjsondocument.h"
+#include "qjsonvalue.h"
+#include "qjsonobject.h"
+#include "qjsonarray.h"
QT_BEGIN_NAMESPACE
@@ -137,29 +141,44 @@ void QFactoryLoader::update()
continue;
}
QStringList keys;
- if (!library->loadPlugin()) {
- if (qt_debug_component()) {
- qDebug() << library->errorString;
- qDebug() << " could not load";
+ if (library->compatPlugin) {
+ qWarning() << "Compat plugin, need to load for accessing meta data";
+ if (!library->loadPlugin()) {
+ if (qt_debug_component()) {
+ qDebug() << library->errorString;
+ qDebug() << " could not load";
+ }
+ library->release();
+ continue;
}
- library->release();
- continue;
- }
- if (!library->inst)
- library->inst = library->instance();
- QObject *instance = library->inst.data();
- if (!instance) {
- library->release();
- // ignore plugins that have a valid signature but cannot be loaded.
- continue;
+ if (!library->inst)
+ library->inst = library->instance();
+ QObject *instance = library->inst.data();
+ if (!instance) {
+ library->release();
+ // ignore plugins that have a valid signature but cannot be loaded.
+ continue;
+ }
+ QFactoryInterface *factory = qobject_cast<QFactoryInterface*>(instance);
+ if (instance && factory && instance->qt_metacast(d->iid))
+ keys = factory->keys();
+ } else {
+ QString iid = library->metaData.value(QLatin1String("IID")).toString();
+ if (iid == QLatin1String(d->iid.constData(), d->iid.size())) {
+ QJsonObject object = library->metaData.value(QLatin1String("MetaData")).toObject();
+ QJsonArray k = object.value(QLatin1String("Keys")).toArray();
+ for (int i = 0; i < k.size(); ++i) {
+ QString s = k.at(i).toString();
+ keys += s;
+ }
+ }
+ if (qt_debug_component())
+ qDebug() << "Got keys from plugin meta data" << keys;
}
- QFactoryInterface *factory = qobject_cast<QFactoryInterface*>(instance);
- if (instance && factory && instance->qt_metacast(d->iid))
- keys = factory->keys();
- if (keys.isEmpty())
- library->unload();
+
if (keys.isEmpty()) {
+ library->unload();
library->release();
continue;
}
@@ -173,7 +192,12 @@ void QFactoryLoader::update()
if (!d->cs)
key = key.toLower();
QLibraryPrivate *previous = d->keyMap.value(key);
- if (!previous || (previous->qt_version > QT_VERSION && library->qt_version <= QT_VERSION)) {
+ int prev_qt_version = 0;
+ if (previous) {
+ prev_qt_version = (int)previous->metaData.value(QLatin1String("version")).toDouble();
+ }
+ int qt_version = (int)library->metaData.value(QLatin1String("version")).toDouble();
+ if (!previous || (prev_qt_version > QT_VERSION && qt_version <= QT_VERSION)) {
d->keyMap[key] = library;
d->keyList += keys.at(k);
}
@@ -200,23 +224,76 @@ QStringList QFactoryLoader::keys() const
Q_D(const QFactoryLoader);
QMutexLocker locker(&d->mutex);
QStringList keys = d->keyList;
- QObjectList instances = QPluginLoader::staticInstances();
- for (int i = 0; i < instances.count(); ++i)
- if (QFactoryInterface *factory = qobject_cast<QFactoryInterface*>(instances.at(i)))
- if (instances.at(i)->qt_metacast(d->iid))
- keys += factory->keys();
+ QVector<QStaticPlugin> staticPlugins = QLibraryPrivate::staticPlugins();
+ for (int i = 0; i < staticPlugins.count(); ++i) {
+ if (staticPlugins.at(i).metaData) {
+ const char *rawMetaData = staticPlugins.at(i).metaData();
+ QJsonObject object = QLibraryPrivate::fromRawMetaData(rawMetaData).object();
+ if (object.value(QLatin1String("IID")) != QLatin1String(d->iid.constData(), d->iid.size()))
+ continue;
+
+ QJsonObject meta = object.value(QLatin1String("MetaData")).toObject();
+ QJsonArray a = meta.value(QLatin1String("Keys")).toArray();
+ for (int i = 0; i < a.size(); ++i) {
+ QString s = a.at(i).toString();
+ if (!s.isEmpty())
+ keys += s;
+ }
+ } else {
+ // compat plugin
+ QObject *instance = staticPlugins.at(i).instance();
+ QFactoryInterface *factory = qobject_cast<QFactoryInterface*>(instance);
+ if (instance && factory && instance->qt_metacast(d->iid))
+ keys = factory->keys();
+ }
+ }
return keys;
}
+QList<QJsonObject> QFactoryLoader::metaData() const
+{
+ Q_D(const QFactoryLoader);
+ QMutexLocker locker(&d->mutex);
+ QList<QJsonObject> metaData;
+ for (int i = 0; i < d->libraryList.size(); ++i)
+ metaData.append(d->libraryList.at(i)->metaData);
+
+ QVector<QStaticPlugin> staticPlugins = QLibraryPrivate::staticPlugins();
+ for (int i = 0; i < staticPlugins.count(); ++i) {
+ if (staticPlugins.at(i).metaData) {
+ const char *rawMetaData = staticPlugins.at(i).metaData();
+ QJsonObject object = QLibraryPrivate::fromRawMetaData(rawMetaData).object();
+ if (object.value(QLatin1String("IID")) != QLatin1String(d->iid.constData(), d->iid.size()))
+ continue;
+
+ QJsonObject meta = object.value(QLatin1String("MetaData")).toObject();
+ metaData.append(meta);
+ } else {
+ // compat plugins
+ QObject *instance = staticPlugins.at(i).instance();
+ QFactoryInterface *factory = qobject_cast<QFactoryInterface*>(instance);
+ if (instance && factory && instance->qt_metacast(d->iid)) {
+ QJsonObject meta;
+ QJsonArray a = QJsonArray::fromStringList(factory->keys());
+ meta.insert(QLatin1String("Keys"), a);
+ metaData.append(meta);
+ }
+ }
+ }
+ return metaData;
+}
+
QObject *QFactoryLoader::instance(const QString &key) const
{
Q_D(const QFactoryLoader);
QMutexLocker locker(&d->mutex);
- QObjectList instances = QPluginLoader::staticInstances();
- for (int i = 0; i < instances.count(); ++i)
- if (QFactoryInterface *factory = qobject_cast<QFactoryInterface*>(instances.at(i)))
- if (instances.at(i)->qt_metacast(d->iid) && factory->keys().contains(key, Qt::CaseInsensitive))
- return instances.at(i);
+ QVector<QStaticPlugin> staticPlugins = QLibraryPrivate::staticPlugins();
+ for (int i = 0; i < staticPlugins.count(); ++i) {
+ QObject *instance = staticPlugins.at(i).instance();
+ if (QFactoryInterface *factory = qobject_cast<QFactoryInterface*>(instance))
+ if (instance->qt_metacast(d->iid) && factory->keys().contains(key, Qt::CaseInsensitive))
+ return instance;
+ }
QString lowered = d->cs ? key : key.toLower();
if (QLibraryPrivate* library = d->keyMap.value(lowered)) {
@@ -234,6 +311,26 @@ QObject *QFactoryLoader::instance(const QString &key) const
return 0;
}
+QObject *QFactoryLoader::instance(int index) const
+{
+ Q_D(const QFactoryLoader);
+ if (index < 0 || index >= d->libraryList.size())
+ return 0;
+
+ QLibraryPrivate *library = d->libraryList.at(index);
+ if (library->instance || library->loadPlugin()) {
+ if (!library->inst)
+ library->inst = library->instance();
+ QObject *obj = library->inst.data();
+ if (obj) {
+ if (!obj->parent())
+ obj->moveToThread(QCoreApplicationPrivate::mainThread());
+ return obj;
+ }
+ }
+ return 0;
+}
+
#if defined(Q_OS_UNIX) && !defined (Q_OS_MAC)
QLibraryPrivate *QFactoryLoader::library(const QString &key) const
{
diff --git a/src/corelib/plugin/qfactoryloader_p.h b/src/corelib/plugin/qfactoryloader_p.h
index 2241a22e5f..ee7a68d6f8 100644
--- a/src/corelib/plugin/qfactoryloader_p.h
+++ b/src/corelib/plugin/qfactoryloader_p.h
@@ -55,8 +55,8 @@
#include "QtCore/qobject.h"
#include "QtCore/qstringlist.h"
+#include "QtCore/qjsonobject.h"
#include "private/qlibrary_p.h"
-
#ifndef QT_NO_LIBRARY
QT_BEGIN_NAMESPACE
@@ -74,8 +74,11 @@ public:
Qt::CaseSensitivity = Qt::CaseSensitive);
~QFactoryLoader();
- QStringList keys() const;
- QObject *instance(const QString &key) const;
+ QT_DEPRECATED QStringList keys() const;
+ QList<QJsonObject> metaData() const;
+
+ QT_DEPRECATED QObject *instance(const QString &key) const;
+ QObject *instance(int index) const;
#if defined(Q_OS_UNIX) && !defined (Q_OS_MAC)
QLibraryPrivate *library(const QString &key) const;
diff --git a/src/corelib/plugin/qlibrary.cpp b/src/corelib/plugin/qlibrary.cpp
index dc08b32db1..b171577184 100644
--- a/src/corelib/plugin/qlibrary.cpp
+++ b/src/corelib/plugin/qlibrary.cpp
@@ -60,6 +60,9 @@
#include <qdebug.h>
#include <qvector.h>
#include <qdir.h>
+#include <qendian.h>
+#include <qjsondocument.h>
+#include <qjsonvalue.h>
#include "qelfparser_p.h"
QT_BEGIN_NAMESPACE
@@ -330,7 +333,7 @@ static long qt_find_pattern(const char *s, ulong s_len,
information could not be read.
Returns true if version information is present and successfully read.
*/
-static bool qt_unix_query(const QString &library, uint *version, bool *debug, QLibraryPrivate *lib = 0)
+static bool qt_unix_query(const QString &library, QLibraryPrivate *lib)
{
QFile file(library);
if (!file.open(QIODevice::ReadOnly)) {
@@ -357,35 +360,71 @@ static bool qt_unix_query(const QString &library, uint *version, bool *debug, QL
/*
ELF binaries on GNU, have .qplugin sections.
*/
+ bool hasMetaData = false;
long pos = 0;
- const char pattern[] = "pattern=QT_PLUGIN_VERIFICATION_DATA";
+ const char oldPattern[] = "pattern=QT_PLUGIN_VERIFICATION_DATA";
+ const ulong oldPlen = qstrlen(oldPattern);
+ const char pattern[] = "QTMETADATA ";
const ulong plen = qstrlen(pattern);
#if defined (Q_OF_ELF) && defined(Q_CC_GNU)
int r = QElfParser().parse(filedata, fdlen, library, lib, &pos, &fdlen);
- if (r == QElfParser::NoQtSection) {
+ if (r == QElfParser::Corrupt || r == QElfParser::NotElf) {
+ if (lib && qt_debug_component()) {
+ qWarning("QElfParser: %s",qPrintable(lib->errorString));
+ }
+ return false;
+ } else if (r == QElfParser::NoQtSection || r == QElfParser::QtPluginSection) {
if (pos > 0) {
// find inside .rodata
- long rel = qt_find_pattern(filedata + pos, fdlen, pattern, plen);
+ long rel = qt_find_pattern(filedata + pos, fdlen, oldPattern, oldPlen);
if (rel < 0) {
pos = -1;
} else {
pos += rel;
}
} else {
- pos = qt_find_pattern(filedata, fdlen, pattern, plen);
- }
- } else if (r != QElfParser::Ok) {
- if (lib && qt_debug_component()) {
- qWarning("QElfParser: %s",qPrintable(lib->errorString));
+ pos = qt_find_pattern(filedata, fdlen, oldPattern, oldPlen);
}
- return false;
+ } else if (r == QElfParser::QtMetaDataSection) {
+ long rel = qt_find_pattern(filedata + pos, fdlen, pattern, plen);
+ if (rel < 0)
+ pos = -1;
+ else
+ pos += rel;
+ hasMetaData = true;
}
#else
pos = qt_find_pattern(filedata, fdlen, pattern, plen);
+ if (pos > 0)
+ hasMetaData = true;
+ else
+ pos = qt_find_pattern(filedata, fdlen, oldPattern, oldPlen);
#endif // defined(Q_OF_ELF) && defined(Q_CC_GNU)
+
bool ret = false;
- if (pos >= 0)
- ret = qt_parse_pattern(filedata + pos, version, debug);
+
+ if (pos >= 0) {
+ if (hasMetaData) {
+ const char *data = filedata + pos;
+ QJsonDocument doc = QLibraryPrivate::fromRawMetaData(data);
+ lib->metaData = doc.object();
+ lib->compatPlugin = false;
+ if (qt_debug_component())
+ qWarning("Found metadata in lib %s, metadata=\n%s\n",
+ library.toLocal8Bit().constData(), doc.toJson().constData());
+ ret = !doc.isNull();
+ } else {
+ qWarning("Old plugin format found in lib %s", library.toLocal8Bit().constData());
+ uint version;
+ bool isDebug;
+ ret = qt_parse_pattern(filedata + pos, &version, &isDebug);
+ if (ret) {
+ lib->metaData.insert(QLatin1String("version"), (int)version);
+ lib->metaData.insert(QLatin1String("debug"), isDebug);
+ lib->compatPlugin = true;
+ }
+ }
+ }
if (!ret && lib)
lib->errorString = QLibrary::tr("Plugin verification data mismatch in '%1'").arg(library);
@@ -446,8 +485,9 @@ static LibraryMap *libraryMap()
}
QLibraryPrivate::QLibraryPrivate(const QString &canonicalFileName, const QString &version)
- :pHnd(0), fileName(canonicalFileName), fullVersion(version), instance(0), qt_version(0),
- libraryRefCount(1), libraryUnloadCount(0), pluginState(MightBeAPlugin)
+ : pHnd(0), fileName(canonicalFileName), fullVersion(version), instance(0),
+ compatPlugin(false), loadHints(0),
+ libraryRefCount(1), libraryUnloadCount(0), pluginState(MightBeAPlugin)
{ libraryMap()->insert(canonicalFileName, this); }
QLibraryPrivate *QLibraryPrivate::findOrCreate(const QString &fileName, const QString &version)
@@ -488,6 +528,8 @@ bool QLibraryPrivate::load()
return false;
bool ret = load_sys();
+ if (qt_debug_component())
+ qDebug() << "loaded library" << fileName;
if (ret) {
//when loading a library we add a reference to it so that the QLibraryPrivate won't get deleted
//this allows to unload the library at a later time
@@ -643,13 +685,33 @@ const char* qt_try_versioninfo(void *pfn, bool *exceptionThrown)
}
#endif
-#ifdef Q_CC_BOR
-typedef const char * __stdcall (*QtPluginQueryVerificationDataFunction)();
-#else
typedef const char * (*QtPluginQueryVerificationDataFunction)();
+
+bool qt_get_verificationdata(QtPluginQueryVerificationDataFunction pfn, QLibraryPrivate *priv, bool *exceptionThrown)
+{
+ *exceptionThrown = false;
+ const char *szData = 0;
+ if (!pfn)
+ return false;
+#ifdef QT_USE_MS_STD_EXCEPTION
+ szData = qt_try_versioninfo((void *)pfn, exceptionThrown);
+ if (*exceptionThrown)
+ return false;
+#else
+ szData = pfn();
#endif
+ uint qt_version;
+ bool debug;
+ if (qt_parse_pattern(szData, &qt_version, &debug)) {
+ priv->metaData.insert(QLatin1String("version"), (int)qt_version);
+ priv->metaData.insert(QLatin1String("debug"), debug);
+ priv->compatPlugin = true;
+ return true;
+ }
+ return false;
+}
-bool qt_get_verificationdata(QtPluginQueryVerificationDataFunction pfn, uint *qt_version, bool *debug, bool *exceptionThrown)
+bool qt_get_metadata(QtPluginQueryVerificationDataFunction pfn, QLibraryPrivate *priv, bool *exceptionThrown)
{
*exceptionThrown = false;
const char *szData = 0;
@@ -662,9 +724,17 @@ bool qt_get_verificationdata(QtPluginQueryVerificationDataFunction pfn, uint *qt
#else
szData = pfn();
#endif
- return qt_parse_pattern(szData, qt_version, debug);
+ if (!szData)
+ return false;
+ QJsonDocument doc = QLibraryPrivate::fromRawMetaData(szData);
+ if (doc.isNull())
+ return false;
+ priv->metaData = doc.object();
+ priv->compatPlugin = false;
+ return true;
}
+
bool QLibraryPrivate::isPlugin()
{
errorString.clear();
@@ -672,7 +742,6 @@ bool QLibraryPrivate::isPlugin()
return pluginState == IsAPlugin;
#ifndef QT_NO_PLUGIN_CHECK
- bool debug = !QLIBRARY_AS_DEBUG;
bool success = false;
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
@@ -692,7 +761,7 @@ bool QLibraryPrivate::isPlugin()
#if defined(Q_OS_UNIX) && !defined(Q_OS_MAC)
if (!pHnd) {
// use unix shortcut to avoid loading the library
- success = qt_unix_query(fileName, &qt_version, &debug, this);
+ success = qt_unix_query(fileName, this);
} else
#endif
{
@@ -713,24 +782,50 @@ bool QLibraryPrivate::isPlugin()
temporary_load = load_sys();
#endif
}
+ QtPluginQueryVerificationDataFunction getMetaData = NULL;
+
+ bool exceptionThrown = false;
+ bool ret = false;
#ifdef Q_OS_WIN
- QtPluginQueryVerificationDataFunction qtPluginQueryVerificationDataFunction = hTempModule ? (QtPluginQueryVerificationDataFunction)
+ if (hTempModule) {
+ getMetaData = (QtPluginQueryVerificationDataFunction)
#ifdef Q_OS_WINCE
- ::GetProcAddress(hTempModule, L"qt_plugin_query_verification_data")
+ ::GetProcAddress(hTempModule, L"qt_plugin_query_metadata")
#else
- ::GetProcAddress(hTempModule, "qt_plugin_query_verification_data")
+ ::GetProcAddress(hTempModule, "qt_plugin_query_metadata")
+#endif
+ ;
+ } else
#endif
- : (QtPluginQueryVerificationDataFunction) resolve("qt_plugin_query_verification_data");
+ {
+ getMetaData = (QtPluginQueryVerificationDataFunction) resolve("qt_plugin_query_metadata");
+ }
+
+ if (getMetaData) {
+ ret = qt_get_metadata(getMetaData, this, &exceptionThrown);
+ } else {
+ // try the old plugin style
+ QtPluginQueryVerificationDataFunction qtPluginQueryVerificationDataFunction = NULL;
+#ifdef Q_OS_WIN
+ if (hTempModule) {
+ qtPluginQueryVerificationDataFunction = (QtPluginQueryVerificationDataFunction)
+#ifdef Q_OS_WINCE
+ ::GetProcAddress(hTempModule, L"qt_plugin_query_verification_data")
#else
- QtPluginQueryVerificationDataFunction qtPluginQueryVerificationDataFunction = NULL;
- qtPluginQueryVerificationDataFunction = (QtPluginQueryVerificationDataFunction) resolve("qt_plugin_query_verification_data");
+ ::GetProcAddress(hTempModule, "qt_plugin_query_verification_data")
#endif
- bool exceptionThrown = false;
- bool ret = qt_get_verificationdata(qtPluginQueryVerificationDataFunction,
- &qt_version, &debug, &exceptionThrown);
+ ;
+ } else
+#endif
+ {
+ qtPluginQueryVerificationDataFunction = (QtPluginQueryVerificationDataFunction) resolve("qt_plugin_query_verification_data");
+ }
+
+ ret = qt_get_verificationdata(qtPluginQueryVerificationDataFunction, this, &exceptionThrown);
+ }
+
if (!exceptionThrown) {
if (!ret) {
- qt_version = 0;
if (temporary_load)
unload_sys();
} else {
@@ -772,6 +867,8 @@ bool QLibraryPrivate::isPlugin()
pluginState = IsNotAPlugin; // be pessimistic
+ uint qt_version = (uint)metaData.value(QLatin1String("version")).toDouble();
+ bool debug = metaData.value(QLatin1String("debug")).toBool();
if ((qt_version & 0x00ff00) > (QT_VERSION & 0x00ff00) || (qt_version & 0xff0000) != (QT_VERSION & 0xff0000)) {
if (qt_debug_component()) {
qWarning("In %s:\n"
diff --git a/src/corelib/plugin/qlibrary_p.h b/src/corelib/plugin/qlibrary_p.h
index a5f366070c..604a5fbdde 100644
--- a/src/corelib/plugin/qlibrary_p.h
+++ b/src/corelib/plugin/qlibrary_p.h
@@ -58,6 +58,9 @@
#include "QtCore/qstringlist.h"
#include "QtCore/qplugin.h"
#include "QtCore/qsharedpointer.h"
+#include "QtCore/qjsonobject.h"
+#include "QtCore/qjsondocument.h"
+#include "QtCore/qendian.h"
#ifdef Q_OS_WIN
# include "QtCore/qt_windows.h"
#endif
@@ -90,16 +93,26 @@ public:
static QLibraryPrivate *findOrCreate(const QString &fileName, const QString &version = QString());
+ static QVector<QStaticPlugin> staticPlugins();
+
+
QWeakPointer<QObject> inst;
QtPluginInstanceFunction instance;
- uint qt_version;
- QString lastModified;
+ QJsonObject metaData;
+ bool compatPlugin;
QString errorString;
QLibrary::LoadHints loadHints;
bool isPlugin();
+ static inline QJsonDocument fromRawMetaData(const char *raw) {
+ raw += strlen("QTMETADATA ");
+ // the size of the embedded JSON object can be found 8 bytes into the data (see qjson_p.h),
+ // but doesn't include the size of the header (8 bytes)
+ QByteArray json(raw, qFromLittleEndian<uint>(*(uint *)(raw + 8)) + 8);
+ return QJsonDocument::fromBinaryData(json);
+ }
private:
explicit QLibraryPrivate(const QString &canonicalFileName, const QString &version);
@@ -112,7 +125,7 @@ private:
QAtomicInt libraryRefCount;
QAtomicInt libraryUnloadCount;
- enum {IsAPlugin, IsNotAPlugin, MightBeAPlugin } pluginState;
+ enum { IsAPlugin, IsNotAPlugin, MightBeAPlugin } pluginState;
friend class QLibraryPrivateHasFriends;
};
diff --git a/src/corelib/plugin/qplugin.h b/src/corelib/plugin/qplugin.h
index 6cc0ceb5c2..90ab8369c9 100644
--- a/src/corelib/plugin/qplugin.h
+++ b/src/corelib/plugin/qplugin.h
@@ -59,15 +59,33 @@ QT_BEGIN_NAMESPACE
#endif
typedef QObject *(*QtPluginInstanceFunction)();
+typedef const char *(*QtPluginMetaDataFunction)();
+
+struct QStaticPlugin
+{
+ QtPluginInstanceFunction instance;
+ QtPluginMetaDataFunction metaData;
+};
+
+void Q_CORE_EXPORT qRegisterStaticPluginFunction(QStaticPlugin staticPlugin);
+
+#if defined (Q_OF_ELF) && defined (Q_CC_GNU)
+# define QT_PLUGIN_VERIFICATION_SECTION \
+ __attribute__ ((section (".qtplugin"))) __attribute__((used))
+# define QT_PLUGIN_METADATA_SECTION \
+ __attribute__ ((section (".qtmetadata"))) __attribute__((used))
+#else
+# define QT_PLUGIN_VERIFICATION_SECTION
+# define QT_PLUGIN_METADATA_SECTION
+#endif
-void Q_CORE_EXPORT qRegisterStaticPluginInstanceFunction(QtPluginInstanceFunction function);
#define Q_IMPORT_PLUGIN(PLUGIN) \
- extern QT_PREPEND_NAMESPACE(QObject) *qt_plugin_instance_##PLUGIN(); \
+ extern const QT_PREPEND_NAMESPACE(QStaticPlugin) qt_static_plugin_##PLUGIN(); \
class Static##PLUGIN##PluginInstance{ \
public: \
Static##PLUGIN##PluginInstance() { \
- qRegisterStaticPluginInstanceFunction(qt_plugin_instance_##PLUGIN); \
+ qRegisterStaticPluginFunction(qt_static_plugin_##PLUGIN()); \
} \
}; \
static Static##PLUGIN##PluginInstance static##PLUGIN##Instance;
@@ -80,21 +98,43 @@ void Q_CORE_EXPORT qRegisterStaticPluginInstanceFunction(QtPluginInstanceFunctio
return _instance; \
}
-# define Q_EXPORT_PLUGIN(PLUGIN) \
- Q_EXPORT_PLUGIN2(PLUGIN, PLUGIN)
+#if defined(QT_STATICPLUGIN)
-# define Q_EXPORT_STATIC_PLUGIN(PLUGIN) \
- Q_EXPORT_STATIC_PLUGIN2(PLUGIN, PLUGIN)
+# define QT_MOC_EXPORT_PLUGIN(PLUGINCLASS) \
+ static QT_PREPEND_NAMESPACE(QObject) *qt_plugin_instance() \
+ Q_PLUGIN_INSTANCE(PLUGINCLASS) \
+ static const char *qt_plugin_query_metadata() { return (const char *)qt_pluginMetaData; } \
+ const QT_PREPEND_NAMESPACE(QStaticPlugin) qt_static_plugin_##PLUGINCLASS() { \
+ QT_PREPEND_NAMESPACE(QStaticPlugin) plugin = { qt_plugin_instance, qt_plugin_query_metadata }; \
+ return plugin; \
+ }
-#if defined(QT_STATICPLUGIN)
+#else
-# define Q_EXPORT_PLUGIN2(PLUGIN, PLUGINCLASS) \
- QT_PREPEND_NAMESPACE(QObject) \
- *qt_plugin_instance_##PLUGIN() \
+# define QT_MOC_EXPORT_PLUGIN(PLUGINCLASS) \
+ Q_EXTERN_C Q_DECL_EXPORT \
+ const char *qt_plugin_query_metadata() \
+ { return (const char *)qt_pluginMetaData; } \
+ Q_EXTERN_C Q_DECL_EXPORT QT_PREPEND_NAMESPACE(QObject) *qt_plugin_instance() \
Q_PLUGIN_INSTANCE(PLUGINCLASS)
-# define Q_EXPORT_STATIC_PLUGIN2(PLUGIN, PLUGINCLASS) \
- Q_EXPORT_PLUGIN2(PLUGIN, PLUGINCLASS)
+#endif
+
+
+#define Q_EXPORT_PLUGIN(PLUGIN) \
+ Q_EXPORT_PLUGIN2(PLUGIN, PLUGIN)
+#define Q_EXPORT_STATIC_PLUGIN(PLUGIN)
+#define Q_EXPORT_STATIC_PLUGIN2(PLUGIN, PLUGINCLASS)
+
+#if defined(QT_STATICPLUGIN)
+
+# define Q_EXPORT_PLUGIN2(PLUGIN, PLUGINCLASS) \
+ static QT_PREPEND_NAMESPACE(QObject) *qt_plugin_instance() \
+ Q_PLUGIN_INSTANCE(PLUGINCLASS) \
+ const QT_PREPEND_NAMESPACE(QStaticPlugin) qt_static_plugin_##PLUGIN() { \
+ QT_PREPEND_NAMESPACE(QStaticPlugin) plugin = { qt_plugin_instance, 0 }; \
+ return plugin; \
+ }
#else
// NOTE: if you change pattern, you MUST change the pattern in
@@ -118,25 +158,13 @@ void Q_CORE_EXPORT qRegisterStaticPluginInstanceFunction(QtPluginInstanceFunctio
"version="QT_VERSION_STR"\n" \
"debug="QPLUGIN_DEBUG_STR;
-# if defined (Q_OF_ELF) && defined (Q_CC_GNU)
-# define Q_PLUGIN_VERIFICATION_SECTION \
- __attribute__ ((section (".qtplugin"))) __attribute__((used))
-# else
-# define Q_PLUGIN_VERIFICATION_SECTION
-# endif
-
-# if defined (Q_OS_WIN32) && defined(Q_CC_BOR)
-# define Q_STANDARD_CALL __stdcall
-# else
-# define Q_STANDARD_CALL
-# endif
# define Q_EXPORT_PLUGIN2(PLUGIN, PLUGINCLASS) \
- Q_PLUGIN_VERIFICATION_SECTION Q_PLUGIN_VERIFICATION_DATA \
+ QT_PLUGIN_VERIFICATION_SECTION Q_PLUGIN_VERIFICATION_DATA \
Q_EXTERN_C Q_DECL_EXPORT \
- const char * Q_STANDARD_CALL qt_plugin_query_verification_data() \
+ const char * qt_plugin_query_verification_data() \
{ return qt_plugin_verification_data; } \
- Q_EXTERN_C Q_DECL_EXPORT QT_PREPEND_NAMESPACE(QObject) * Q_STANDARD_CALL qt_plugin_instance() \
+ Q_EXTERN_C Q_DECL_EXPORT QT_PREPEND_NAMESPACE(QObject) * qt_plugin_instance() \
Q_PLUGIN_INSTANCE(PLUGINCLASS)
# define Q_EXPORT_STATIC_PLUGIN2(PLUGIN, PLUGINCLASS)
diff --git a/src/corelib/plugin/qpluginloader.cpp b/src/corelib/plugin/qpluginloader.cpp
index 4ef04f2ea3..d652b251f1 100644
--- a/src/corelib/plugin/qpluginloader.cpp
+++ b/src/corelib/plugin/qpluginloader.cpp
@@ -291,8 +291,8 @@ QString QPluginLoader::errorString() const
return (!d || d->errorString.isEmpty()) ? tr("Unknown error") : d->errorString;
}
-typedef QList<QtPluginInstanceFunction> StaticInstanceFunctionList;
-Q_GLOBAL_STATIC(StaticInstanceFunctionList, staticInstanceFunctionList)
+typedef QVector<QStaticPlugin> StaticPluginList;
+Q_GLOBAL_STATIC(StaticPluginList, staticPluginList)
/*! \since 4.4
@@ -329,13 +329,13 @@ QLibrary::LoadHints QPluginLoader::loadHints() const
/*!
\relates QPluginLoader
- \since 4.4
+ \since 5.0
- Registers the given \a function with the plugin loader.
+ Registers the given \a plugin with the plugin loader.
*/
-void Q_CORE_EXPORT qRegisterStaticPluginInstanceFunction(QtPluginInstanceFunction function)
+void Q_CORE_EXPORT qRegisterStaticPluginFunction(QStaticPlugin plugin)
{
- staticInstanceFunctionList()->append(function);
+ staticPluginList()->append(plugin);
}
/*!
@@ -345,14 +345,23 @@ void Q_CORE_EXPORT qRegisterStaticPluginInstanceFunction(QtPluginInstanceFunctio
QObjectList QPluginLoader::staticInstances()
{
QObjectList instances;
- StaticInstanceFunctionList *functions = staticInstanceFunctionList();
- if (functions) {
- for (int i = 0; i < functions->count(); ++i)
- instances.append((*functions)[i]());
+ const StaticPluginList *plugins = staticPluginList();
+ if (plugins) {
+ for (int i = 0; i < plugins->size(); ++i)
+ instances += plugins->at(i).instance();
}
return instances;
}
+
+QVector<QStaticPlugin> QLibraryPrivate::staticPlugins()
+{
+ StaticPluginList *plugins = staticPluginList();
+ if (plugins)
+ return *plugins;
+ return QVector<QStaticPlugin>();
+}
+
QT_END_NAMESPACE
#endif // QT_NO_LIBRARY
diff --git a/src/corelib/plugin/qpluginloader.h b/src/corelib/plugin/qpluginloader.h
index ee62986f21..6ca1892fa9 100644
--- a/src/corelib/plugin/qpluginloader.h
+++ b/src/corelib/plugin/qpluginloader.h
@@ -55,7 +55,6 @@ QT_BEGIN_HEADER
QT_BEGIN_NAMESPACE
-
class QLibraryPrivate;
class Q_CORE_EXPORT QPluginLoader : public QObject
@@ -70,7 +69,7 @@ public:
QObject *instance();
- static QObjectList staticInstances();
+ QT_DEPRECATED static QObjectList staticInstances();
bool load();
bool unload();