1import qbs.FileInfo
2import qbs.TextFile
3
4QtModule {
5    isPlugin: true
6
7    property string className
8    property stringList extendsModules
9
10    enableLinking: {
11        if (!base)
12            return false;
13        if (!isStaticLibrary)
14            return false;
15        if (!Qt.plugin_support.linkPlugins)
16            return false;
17        if (!(Qt.plugin_support.enabledPlugins || []).contains(qtModuleName))
18            return false;
19        if (!extendsModules || extendsModules.length === 0)
20            return true;
21        for (var i = 0; i < extendsModules.length; ++i) {
22            var moduleName = extendsModules[i];
23            if (product.Qt[moduleName] && product.Qt[moduleName].present)
24                return true;
25        }
26        return false;
27    }
28
29    Rule {
30        condition: enableLinking
31        multiplex: true
32        Artifact {
33            filePath: product.targetName + "_qt_plugin_import_"
34                      + product.moduleProperty(product.moduleName, "qtModuleName") + ".cpp"
35            fileTags: "cpp"
36        }
37
38        prepare: {
39            var cmd = new JavaScriptCommand();
40            var pluginName = product.moduleProperty(product.moduleName, "qtModuleName");
41            cmd.description = "Creating static import for plugin '" + pluginName + "'.";
42            cmd.sourceCode = function() {
43                var f = new TextFile(output.filePath, TextFile.WriteOnly);
44                var className = product.moduleProperty(product.moduleName, "className");
45                f.writeLine("#include <QtPlugin>\n\nQ_IMPORT_PLUGIN(" + className + ")");
46                f.close();
47            };
48            return cmd;
49        }
50    }
51}
52