aboutsummaryrefslogtreecommitdiffstats
path: root/share/qbs/modules/Exporter/qbs/qbsexporter.js
blob: 145474a4b013bd38789b9a945a14fe8de443f942 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qbs.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

var FileInfo = require("qbs.FileInfo");
var ModUtils = require("qbs.ModUtils");
var ExporterHelpers = require("../exporter.js");

function tagListToString(tagList)
{
    return JSON.stringify(tagList);
}

function stringToTagList(tagListString)
{
    return JSON.parse(tagListString);
}

function writeTargetArtifactGroup(output, tagList, artifactList, moduleInstallDir, moduleFile)
{
    // Do not add our qbs module file itself.
    if (tagListToString(tagList) === tagListToString(output.fileTags))
        return;

    moduleFile.writeLine("    Group {");
    moduleFile.writeLine("        filesAreTargets: true");
    var filteredTagList = tagList.filter(function(t) { return t !== "installable"; });
    moduleFile.writeLine("        fileTags: " + JSON.stringify(filteredTagList));
    moduleFile.writeLine("        files: [");
    for (i = 0; i < artifactList.length; ++i) {
        var artifact = artifactList[i];
        var installedArtifactFilePath = ModUtils.artifactInstalledFilePath(artifact);

        // Use relative file paths for relocatability.
        var relativeInstalledArtifactFilePath = FileInfo.relativePath(moduleInstallDir,
                                                                      installedArtifactFilePath);
        moduleFile.writeLine("            " + JSON.stringify(relativeInstalledArtifactFilePath)
                             + ",");
    }
    moduleFile.writeLine("        ]");
    moduleFile.writeLine("    }");

}

function writeTargetArtifactGroups(product, output, moduleFile)
{
    var relevantArtifacts = [];
    for (var i = 0; i < (product.Exporter.qbs._artifactTypes || []).length; ++i) {
        var tag = product.Exporter.qbs._artifactTypes[i];
        var artifactsForTag = product.artifacts[tag] || [];
        for (var j = 0; j < artifactsForTag.length; ++j) {
            if (!relevantArtifacts.includes(artifactsForTag[j]))
                relevantArtifacts.push(artifactsForTag[j]);
        }
    }
    var artifactsByTags = {};
    var artifactCount = relevantArtifacts ? relevantArtifacts.length : 0;
    for (i = 0; i < artifactCount; ++i) {
        var artifact = relevantArtifacts[i];
        if (!artifact.fileTags.includes("installable"))
            continue;

        // Put all artifacts with the same set of file tags into the same group, so we don't
        // create more groups than necessary.
        var key = tagListToString(artifact.fileTags);
        var currentList = artifactsByTags[key];
        if (currentList)
            currentList.push(artifact);
        else
            currentList = [artifact];
        artifactsByTags[key] = currentList;
    }
    var moduleInstallDir = FileInfo.path(ModUtils.artifactInstalledFilePath(output));
    for (var tagListKey in artifactsByTags) {
        writeTargetArtifactGroup(output, stringToTagList(tagListKey), artifactsByTags[tagListKey],
                   moduleInstallDir, moduleFile);
    }
}

function stringifyValue(project, product, moduleInstallDir, prop, value)
{
    if (value instanceof Array) {
        var repr = "[";
        for (var i = 0; i < value.length; ++i) {
            repr += stringifyValue(project, product, moduleInstallDir, prop, value[i]) + ", ";
        }
        repr += "]";
        return repr;
    }
    if (typeof(value) !== "string") {
        var value = JSON.stringify(value);
        if (prop.type === "variant")
            value = '(' + value + ')';
        return value;
    }

    var valuePrefixToStrip = ExporterHelpers.getPrefixToStrip(project, product, prop.name, value);
    if (valuePrefixToStrip) {
        var fullInstallPrefix = FileInfo.joinPaths(product.qbs.installRoot, product.qbs.installPrefix);
        var deployedModuleInstallDir = moduleInstallDir.slice(fullInstallPrefix.length);
        return "FileInfo.cleanPath(FileInfo.joinPaths(path, FileInfo.relativePath("
                + JSON.stringify(deployedModuleInstallDir) + ", "
                + JSON.stringify(value.slice(valuePrefixToStrip.length) || "/") + ")))";
    }

    return JSON.stringify(value);
}

function writeProperty(project, product, moduleInstallDir, prop, indentation, considerValue,
                       moduleFile)
{
    var line = indentation;
    var separatorIndex = prop.name.lastIndexOf(".");
    var isModuleProperty = separatorIndex !== -1;
    var needsDeclaration = !prop.isBuiltin && !isModuleProperty;
    if (needsDeclaration)
        line += "property " + prop.type +  " ";
    var moduleName;
    if (isModuleProperty) {
        moduleName = prop.name.slice(0, separatorIndex);
        if ((product.Exporter.qbs.excludedDependencies || []).includes(moduleName))
            return;
    }
    line += prop.name + ": ";

    // We emit the literal value, unless the source code clearly refers to values from inside the
    // original project, in which case the evaluated value is used.
    if (considerValue && /(project|product|exportingProduct)\./.test(prop.sourceCode)) {
        var value;
        if (isModuleProperty) {
            var propertyName = prop.name.slice(separatorIndex + 1);
            value = product.exports[moduleName][propertyName];
        } else {
            value = product.exports[prop.name];
        }
        line += stringifyValue(project, product, moduleInstallDir, prop, value);
    } else {
        line += prop.sourceCode.replace(/importingProduct\./g, "product.");
    }
    moduleFile.writeLine(line);
}

function writeProperties(project, product, moduleInstallDir, list, indentation, considerValue,
                         moduleFile)
{
    for (var i = 0; i < list.length; ++i) {
        writeProperty(project, product, moduleInstallDir, list[i], indentation, considerValue,
                      moduleFile);
    }
}

// This writes properties set on other modules in the Export item, i.e. property assignments
// like "cpp.includePaths: '...'".
function writeModuleProperties(project, product, output, moduleFile)
{
    var moduleInstallDir = FileInfo.path(ModUtils.artifactInstalledFilePath(output));
    var filteredProps = product.exports.properties.filter(function(p) {
        return p.name !== "name";
    });

    // The right-hand side can refer to values from the exporting product, in which case
    // the evaluated value, rather than the source code, needs to go into the module file.
    var considerValues = true;
    writeProperties(project, product, moduleInstallDir, filteredProps, "    ", considerValues,
                    moduleFile);
}

function writeItem(product, item, indentation, moduleFile)
{
    moduleFile.writeLine(indentation + item.name + " {");
    var newIndentation = indentation + "    ";

    // These are sub-items of the Export item, whose properties entirely live in the context
    // of the importing product. Therefore, they must never use pre-evaluated values.
    var considerValues = false;
    writeProperties(undefined, product, undefined, item.properties, newIndentation, considerValues,
                    moduleFile)

    for (var i = 0; i < item.childItems.length; ++i)
        writeItem(product, item.childItems[i], newIndentation, moduleFile);
    moduleFile.writeLine(indentation + "}");
}

function isExcludedDependency(product, childItem)
{
    if ((product.Exporter.qbs.excludedDependencies || []).length === 0)
        return false;
    if (childItem.name !== "Depends")
        return false;
    for (var i = 0; i < childItem.properties.length; ++i) {
        var prop = childItem.properties[i];
        var unquotedRhs = prop.sourceCode.slice(1, -1);
        if (prop.name === "name" && product.Exporter.qbs.excludedDependencies.includes(unquotedRhs))
            return true;
    }
    return false;
}

function writeChildItems(product, moduleFile)
{
    for (var i = 0; i < product.exports.childItems.length; ++i) {
        var item = product.exports.childItems[i];
        if (!isExcludedDependency(product, item))
            writeItem(product, item, "    ", moduleFile);
    }
}

function writeImportStatements(product, moduleFile)
{
    var imports = product.exports.imports;

    // We potentially use FileInfo ourselves when transforming paths in stringifyValue().
    if (!imports.includes("import qbs.FileInfo"))
        imports.push("import qbs.FileInfo");

    for (var i = 0; i < imports.length; ++i)
        moduleFile.writeLine(imports[i]);
}