aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/corelib/language/moduleproviderloader.cpp
blob: 3e62d9ed9f66b6a0911692387f2ec4d0c0b77c72 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Copyright (C) 2021 Ivan Komissarov (abbapoh@gmail.com)
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qbs.
**
** $QT_BEGIN_LICENSE:LGPL$
** 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 https://www.qt.io/terms-conditions. For further
** information use the contact form at https://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 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "moduleproviderloader.h"

#include "builtindeclarations.h"
#include "evaluator.h"
#include "itemreader.h"
#include "moduleloader.h"
#include "probesresolver.h"

#include <language/scriptengine.h>
#include <language/value.h>

#include <logging/categories.h>
#include <logging/translator.h>

#include <tools/fileinfo.h>
#include <tools/jsliterals.h>
#include <tools/stlutils.h>
#include <tools/stringconstants.h>

#include <QtCore/qtemporaryfile.h>

namespace qbs {
namespace Internal {

ModuleProviderLoader::ModuleProviderLoader(ItemReader *reader, Evaluator *evaluator,
                                           ProbesResolver *probesResolver, Logger &logger)
    : m_reader(reader)
    , m_evaluator(evaluator)
    , m_probesResolver(probesResolver)
    , m_logger(logger)
{
}

ModuleProviderLoader::ModuleProviderResult ModuleProviderLoader::executeModuleProviders(
        ProductContext &productContext,
        const CodeLocation &dependsItemLocation,
        const QualifiedId &moduleName,
        FallbackMode fallbackMode)
{
    ModuleProviderLoader::ModuleProviderResult result;
    std::vector<Provider> providersToRun;
    qCDebug(lcModuleLoader) << "Module" << moduleName.toString()
                            << "not found, checking for module providers";
    const auto providerNames = getModuleProviders(productContext.item);
    if (providerNames) {
        providersToRun = transformed<std::vector<Provider>>(*providerNames, [](const auto &name) {
            return Provider{name, ModuleProviderLookup::Named}; });
    } else {
        for (QualifiedId providerName = moduleName; !providerName.empty();
            providerName.pop_back()) {
                providersToRun.push_back({providerName, ModuleProviderLookup::Scoped});
        }
    }
    result = executeModuleProvidersHelper(productContext, dependsItemLocation, providersToRun);

    if (fallbackMode == FallbackMode::Enabled
            && !result.providerFound
            && !providerNames) {
            qCDebug(lcModuleLoader) << "Specific module provider not found for"
                                << moduleName.toString()  << ", setting up fallback.";
        result = executeModuleProvidersHelper(
                productContext,
                dependsItemLocation,
                {{moduleName, ModuleProviderLookup::Fallback}});
    }

    return result;
}

ModuleProviderLoader::ModuleProviderResult ModuleProviderLoader::executeModuleProvidersHelper(
        ProductContext &product,
        const CodeLocation &dependsItemLocation,
        const std::vector<Provider> &providers)
{
    if (providers.empty())
        return {};
    QStringList allSearchPaths;
    ModuleProviderResult result;
    const auto qbsModule = evaluateQbsModule(product);
    for (const auto &[name, lookupType] : providers) {
        const QVariantMap config = getModuleProviderConfig(product).value(name.toString()).toMap();
        ModuleProviderInfo &info = m_storedModuleProviderInfo.providers[
            {name.toString(), config, qbsModule, int(lookupType)}];
        const bool fromCache = !info.name.isEmpty();
        if (!fromCache) {
            info.name = name;
            info.config = config;
            info.providerFile = findModuleProviderFile(name, lookupType);
            if (!info.providerFile.isEmpty()) {
                qCDebug(lcModuleLoader) << "Running provider" << name << "at" << info.providerFile;
                info.searchPaths = evaluateModuleProvider(
                        product, dependsItemLocation, name, info.providerFile, config, qbsModule);
                info.transientOutput = m_parameters.dryRun();
            }
        }
        if (info.providerFile.isEmpty()) {
            if (lookupType == ModuleProviderLookup::Named)
                throw ErrorInfo(Tr::tr("Unknown provider '%1'").arg(name.toString()));
            continue;
        }
        if (fromCache)
            qCDebug(lcModuleLoader) << "Re-using provider" << name << "from cache";

        result.providerFound = true;
        if (info.searchPaths.empty()) {
            qCDebug(lcModuleLoader)
                    << "Module provider did run, but did not set up any modules.";
            continue;
        }
        qCDebug(lcModuleLoader) << "Module provider added" << info.searchPaths.size()
                                << "new search path(s)";

        allSearchPaths << info.searchPaths;
    }
    if (allSearchPaths.isEmpty())
        return result;

    m_reader->pushExtraSearchPaths(allSearchPaths);
    result.providerAddedSearchPaths = true;

    return result;
}

QVariantMap ModuleProviderLoader::getModuleProviderConfig(
        ProductContext &product)
{
    if (product.theModuleProviderConfig)
        return *product.theModuleProviderConfig;
    QVariantMap providerConfig;
    const ItemValueConstPtr configItemValue =
            product.item->itemProperty(StringConstants::moduleProviders());
    if (configItemValue) {
        const std::function<void(const Item *, QualifiedId)> collectMap
                = [this, &providerConfig, &collectMap](const Item *item, const QualifiedId &name) {
            const Item::PropertyMap &props = item->properties();
            for (auto it = props.begin(); it != props.end(); ++it) {
                QVariant value;
                switch (it.value()->type()) {
                case Value::ItemValueType: {
                    const auto childItem = static_cast<ItemValue *>(it.value().get())->item();
                    childItem->setScope(item->scope());
                    collectMap(childItem, QualifiedId(name) << it.key());
                    continue;
                }
                case Value::JSSourceValueType:
                    value = m_evaluator->value(item, it.key()).toVariant();
                    break;
                case Value::VariantValueType:
                    value = static_cast<VariantValue *>(it.value().get())->value();
                    break;
                }
                QVariantMap m = providerConfig.value(name.toString()).toMap();
                m.insert(it.key(), value);
                providerConfig.insert(name.toString(), m);
            }
        };
        configItemValue->item()->setScope(product.item);
        collectMap(configItemValue->item(), QualifiedId());
    }
    for (auto it = product.moduleProperties.begin(); it != product.moduleProperties.end(); ++it) {
        if (!it.key().startsWith(QStringLiteral("moduleProviders.")))
            continue;
        const QString provider = it.key().mid(QStringLiteral("moduleProviders.").size());
        const QVariantMap providerConfigFromBuildConfig = it.value().toMap();
        if (providerConfigFromBuildConfig.empty())
            continue;
        QVariantMap currentMapForProvider = providerConfig.value(provider).toMap();
        for (auto propIt = providerConfigFromBuildConfig.begin();
             propIt != providerConfigFromBuildConfig.end(); ++propIt) {
            currentMapForProvider.insert(propIt.key(), propIt.value());
        }
        providerConfig.insert(provider, currentMapForProvider);
    }
    return *(product.theModuleProviderConfig = std::move(providerConfig));
}

std::optional<std::vector<QualifiedId>> ModuleProviderLoader::getModuleProviders(Item *item)
{
    while (item) {
        const auto providers =
                m_evaluator->optionalStringListValue(item, StringConstants::qbsModuleProviders());
        if (providers) {
            return transformed<std::vector<QualifiedId>>(*providers, [](const auto &provider) {
                return QualifiedId::fromString(provider); });
        }
        item = item->parent();
    }
    return std::nullopt;
}

QString ModuleProviderLoader::findModuleProviderFile(
        const QualifiedId &name, ModuleProviderLookup lookupType)
{
    for (const QString &path : m_reader->allSearchPaths()) {
        QString fullPath = FileInfo::resolvePath(path, QStringLiteral("module-providers"));
        switch (lookupType) {
        case ModuleProviderLookup::Named: {
            const auto result =
                    FileInfo::resolvePath(fullPath, name.toString() + QStringLiteral(".qbs"));
            if (FileInfo::exists(result)) {
                fullPath = result;
                break;
            }
            [[fallthrough]];
        }
        case ModuleProviderLookup::Scoped:
            for (const QString &component : name)
                fullPath = FileInfo::resolvePath(fullPath, component);
            fullPath = FileInfo::resolvePath(fullPath, QStringLiteral("provider.qbs"));
            break;
        case ModuleProviderLookup::Fallback:
            fullPath = FileInfo::resolvePath(fullPath, QStringLiteral("__fallback/provider.qbs"));
            break;
        }
        if (!FileInfo::exists(fullPath)) {
            qCDebug(lcModuleLoader) << "No module provider found at" << fullPath;
            continue;
        }
        return fullPath;
    }
    return {};
}

QVariantMap ModuleProviderLoader::evaluateQbsModule(ProductContext &product) const
{
    const QString properties[] = {
        QStringLiteral("sysroot"),
    };
    const auto qbsItemValue = std::static_pointer_cast<ItemValue>(
        product.item->property(StringConstants::qbsModule()));
    QVariantMap result;
    for (const auto &property : properties) {
        auto value = m_evaluator->value(qbsItemValue->item(), property).toVariant();
        if (value.isValid())
            result[property] = std::move(value);
    }
    return result;
}

Item *ModuleProviderLoader::createProviderScope(
    ProductContext &product, const QVariantMap &qbsModule)
{
    const auto qbsItemValue = std::static_pointer_cast<ItemValue>(
        product.item->property(StringConstants::qbsModule()));

    Item *fakeQbsModule = Item::create(product.item->pool(), ItemType::Scope);

    for (auto it = qbsModule.begin(), end = qbsModule.end(); it != end; ++it) {
        fakeQbsModule->setProperty(it.key(), VariantValue::create(it.value()));
    }

    Item *scope = Item::create(product.item->pool(), ItemType::Scope);
    scope->setFile(qbsItemValue->item()->file());
    scope->setProperty(StringConstants::qbsModule(), ItemValue::create(fakeQbsModule));
    return scope;
}

QStringList ModuleProviderLoader::evaluateModuleProvider(
        ProductContext &product,
        const CodeLocation &dependsItemLocation,
        const QualifiedId &name,
        const QString &providerFile,
        const QVariantMap &moduleConfig,
        const QVariantMap &qbsModule)
{
    QTemporaryFile dummyItemFile;
    if (!dummyItemFile.open()) {
        throw ErrorInfo(Tr::tr("Failed to create temporary file for running module provider "
                               "for dependency '%1': %2").arg(name.toString(),
                                                              dummyItemFile.errorString()));
    }
    m_tempQbsFiles << dummyItemFile.fileName();
    qCDebug(lcModuleLoader) << "Instantiating module provider at" << providerFile;
    const QString projectBuildDir = product.project->item->variantProperty(
                StringConstants::buildDirectoryProperty())->value().toString();
    const QString searchPathBaseDir = ModuleProviderInfo::outputDirPath(projectBuildDir, name);

    // include qbs module into hash
    auto jsConfig = moduleConfig;
    jsConfig[StringConstants::qbsModule()] = qbsModule;

    QTextStream stream(&dummyItemFile);
    using Qt::endl;
    setupDefaultCodec(stream);
    stream << "import qbs.FileInfo" << endl;
    stream << "import qbs.Utilities" << endl;
    stream << "import '" << providerFile << "' as Provider" << endl;
    stream << "Provider {" << endl;
    stream << "    name: " << toJSLiteral(name.toString()) << endl;
    stream << "    property var config: (" << toJSLiteral(jsConfig) << ')' << endl;
    stream << "    outputBaseDir: FileInfo.joinPaths(baseDirPrefix, "
              "        Utilities.getHash(JSON.stringify(config)))" << endl;
    stream << "    property string baseDirPrefix: " << toJSLiteral(searchPathBaseDir) << endl;
    stream << "    property stringList searchPaths: (relativeSearchPaths || [])"
              "        .map(function(p) { return FileInfo.joinPaths(outputBaseDir, p); })"
           << endl;
    stream << "}" << endl;
    stream.flush();
    Item * const providerItem =
            m_reader->readFile(dummyItemFile.fileName(), dependsItemLocation);
    if (providerItem->type() != ItemType::ModuleProvider) {
        throw ErrorInfo(Tr::tr("File '%1' declares an item of type '%2', "
                               "but '%3' was expected.")
            .arg(providerFile, providerItem->typeName(),
                 BuiltinDeclarations::instance().nameForType(ItemType::ModuleProvider)));
    }

    providerItem->setScope(createProviderScope(product, qbsModule));

    providerItem->overrideProperties(moduleConfig, name.toString(), m_parameters, m_logger);

    m_probesResolver->resolveProbes(&product, providerItem);

    EvalContextSwitcher contextSwitcher(m_evaluator->engine(), EvalContext::ModuleProvider);
    return m_evaluator->stringListValue(providerItem, QStringLiteral("searchPaths"));
}

} // namespace Internal
} // namespace qbs