summaryrefslogtreecommitdiff
path: root/src/lib/corelib/loader/modulepropertymerger.cpp
blob: 3bec5adc378f5194fb27f0094ca49a6a77f7ae6b (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
/****************************************************************************
**
** Copyright (C) 2023 The Qt Company Ltd.
** 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 "modulepropertymerger.h"

#include <language/evaluator.h>
#include <language/item.h>
#include <language/value.h>
#include <logging/translator.h>
#include <tools/profiling.h>
#include <tools/set.h>
#include <tools/setupprojectparameters.h>

namespace qbs::Internal {
class ModulePropertyMerger::Private
{
public:
    Private(const SetupProjectParameters &parameters, Evaluator &evaluator, Logger &logger)
        : parameters(parameters), evaluator(evaluator), logger(logger) {}

    int compareValuePriorities(const Item *productItem, const ValueConstPtr &v1,
                               const ValueConstPtr &v2);
    ValuePtr mergeListValues(const Item *productItem, const ValuePtr &currentHead, const ValuePtr &newElem);
    void mergePropertyFromLocalInstance(const Item *productItem, Item *loadingItem,
                                const QString &loadingName, Item *globalInstance,
                                const QString &name, const ValuePtr &value);
    bool doFinalMerge(const Item *productItem, Item *moduleItem);
    bool doFinalMerge(const Item *productItem, const PropertyDeclaration &propertyDecl,
                      ValuePtr &propertyValue);

    const SetupProjectParameters &parameters;
    Evaluator &evaluator;
    Logger &logger;
    qint64 elapsedTime = 0;
};

void ModulePropertyMerger::mergeFromLocalInstance(
    const Item *productItem, Item *loadingItem, const QString &loadingName,
    const Item *localInstance, Item *globalInstance)
{
    AccumulatingTimer t(d->parameters.logElapsedTime() ? &d->elapsedTime : nullptr);

    for (auto it = localInstance->properties().constBegin();
         it != localInstance->properties().constEnd(); ++it) {
        d->mergePropertyFromLocalInstance(productItem, loadingItem, loadingName,
                                  globalInstance, it.key(), it.value());
    }
}

void ModulePropertyMerger::doFinalMerge(const Item *productItem)
{
    AccumulatingTimer t(d->parameters.logElapsedTime() ? &d->elapsedTime : nullptr);

    Set<const Item *> itemsToInvalidate;
    for (const Item::Module &module : productItem->modules()) {
        if (d->doFinalMerge(productItem, module.item))
            itemsToInvalidate << module.item;
    }
    const auto collectDependentItems = [&itemsToInvalidate](const Item *item,
                                                            const auto &collect) -> bool {
        const bool alreadyInSet = itemsToInvalidate.contains(item);
        bool addItem = false;
        for (const Item::Module &m : item->modules()) {
            if (collect(m.item, collect))
                addItem = true;
        }
        if (addItem && !alreadyInSet)
            itemsToInvalidate << item;
        return addItem || alreadyInSet;
    };
    collectDependentItems(productItem, collectDependentItems);
    for (const Item * const item : itemsToInvalidate)
        d->evaluator.clearCache(item);
}

void ModulePropertyMerger::printProfilingInfo(int indent)
{
    if (!d->parameters.logElapsedTime())
        return;
    d->logger.qbsLog(LoggerInfo, true) << QByteArray(indent, ' ')
                                       << Tr::tr("Merging module property values took %1.")
                                              .arg(elapsedTimeString(d->elapsedTime));
}

ModulePropertyMerger::ModulePropertyMerger(
    const SetupProjectParameters &parameters, Evaluator &evaluator, Logger &logger)
    : d(makePimpl<Private>(parameters, evaluator, logger)) { }
ModulePropertyMerger::~ModulePropertyMerger() = default;

int ModulePropertyMerger::Private::compareValuePriorities(
    const Item *productItem, const ValueConstPtr &v1, const ValueConstPtr &v2)
{
    QBS_CHECK(v1);
    QBS_CHECK(v2);
    QBS_CHECK(v1->scope() != v2->scope());
    QBS_CHECK(v1->type() == Value::JSSourceValueType || v2->type() == Value::JSSourceValueType);

    const int prio1 = v1->priority(productItem);
    const int prio2 = v2->priority(productItem);
    if (prio1 != prio2)
        return prio1 - prio2;
    const int prioDiff = v1->scopeName().compare(v2->scopeName()); // Sic! See 8ff1dd0044
    QBS_CHECK(prioDiff != 0);
    return prioDiff;
}

ValuePtr ModulePropertyMerger::Private::mergeListValues(
    const Item *productItem, const ValuePtr &currentHead, const ValuePtr &newElem)
{
    QBS_CHECK(newElem);
    QBS_CHECK(!newElem->next());

    if (!currentHead)
        return !newElem->expired(productItem) ? newElem : newElem->next();

    QBS_CHECK(!currentHead->expired(productItem));

    if (newElem->expired(productItem))
        return currentHead;

    if (compareValuePriorities(productItem, currentHead, newElem) < 0) {
        newElem->setNext(currentHead);
        return newElem;
    }
    currentHead->setNext(mergeListValues(productItem, currentHead->next(), newElem));
    return currentHead;
}

void ModulePropertyMerger::Private::mergePropertyFromLocalInstance(
    const Item *productItem, Item *loadingItem, const QString &loadingName, Item *globalInstance,
    const QString &name, const ValuePtr &value)
{
    const PropertyDeclaration decl = globalInstance->propertyDeclaration(name);
    if (!decl.isValid()) {
        if (value->type() == Value::ItemValueType || value->createdByPropertiesBlock())
            return;
        throw ErrorInfo(Tr::tr("Property '%1' is not declared.")
                            .arg(name), value->location());
    }
    if (const ErrorInfo error = decl.checkForDeprecation(
            parameters.deprecationWarningMode(), value->location(), logger);
        error.hasError()) {
        handlePropertyError(error, parameters, logger);
        return;
    }
    if (value->setInternally()) { // E.g. qbs.architecture after multiplexing.
        globalInstance->setProperty(decl.name(), value);
        return;
    }
    QBS_CHECK(value->type() != Value::ItemValueType);
    const ValuePtr globalVal = globalInstance->ownProperty(decl.name());
    value->setScope(loadingItem, loadingName);
    QBS_CHECK(globalVal);

    // Values set internally cannot be overridden by JS values.
    // The same goes for values set on the command line.
    // Note that in both cases, there is no merging for list properties: The override is absolute.
    if (globalVal->setInternally() || globalVal->setByCommandLine())
        return;

    QBS_CHECK(value->type() == Value::JSSourceValueType);

    if (decl.isScalar()) {
        QBS_CHECK(!globalVal->expired(productItem));
        QBS_CHECK(!value->expired(productItem));
        if (compareValuePriorities(productItem, globalVal, value) < 0) {
            value->setCandidates(globalVal->candidates());
            globalVal->setCandidates({});
            value->addCandidate(globalVal);
            globalInstance->setProperty(decl.name(), value);
        } else {
            globalVal->addCandidate(value);
        }
    } else {
        if (const ValuePtr &newChainStart = mergeListValues(productItem, globalVal, value);
            newChainStart != globalVal) {
            globalInstance->setProperty(decl.name(), newChainStart);
        }
    }
}

bool ModulePropertyMerger::Private::doFinalMerge(const Item *productItem, Item *moduleItem)
{
    if (!moduleItem->isPresentModule())
        return false;
    bool mustInvalidateCache = false;
    for (auto it = moduleItem->properties().begin(); it != moduleItem->properties().end(); ++it) {
        if (doFinalMerge(productItem, moduleItem->propertyDeclaration(it.key()), it.value()))
            mustInvalidateCache = true;
    }
    return mustInvalidateCache;
}

bool ModulePropertyMerger::Private::doFinalMerge(
    const Item *productItem, const PropertyDeclaration &propertyDecl, ValuePtr &propertyValue)
{
    if (propertyValue->type() == Value::VariantValueType) {
        QBS_CHECK(!propertyValue->next());
        return false;
    }
    if (!propertyDecl.isValid())
        return false; // Caught later by dedicated checker.
    propertyValue->resetPriority();
    if (propertyDecl.isScalar()) {
        if (propertyValue->candidates().empty())
            return false;
        std::pair<int, std::vector<ValuePtr>> candidatesWithHighestPrio;
        candidatesWithHighestPrio.first = propertyValue->priority(productItem);
        candidatesWithHighestPrio.second.push_back(propertyValue);
        for (const ValuePtr &v : propertyValue->candidates()) {
            const int prio = v->priority(productItem);
            if (prio < candidatesWithHighestPrio.first)
                continue;
            if (prio > candidatesWithHighestPrio.first) {
                candidatesWithHighestPrio.first = prio;
                candidatesWithHighestPrio.second = {v};
                continue;
            }
            candidatesWithHighestPrio.second.push_back(v);
        }
        ValuePtr chosenValue = candidatesWithHighestPrio.second.front();
        if (int(candidatesWithHighestPrio.second.size()) > 1) {
            ErrorInfo error(Tr::tr("Conflicting scalar values for property '%1'.")
                                .arg(propertyDecl.name()));
            error.append({}, chosenValue->location());
            QBS_CHECK(chosenValue->type() == Value::JSSourceValueType);
            QStringView sourcCode = static_cast<JSSourceValue *>(
                                        chosenValue.get())->sourceCode();
            for (int i = 1; i < int(candidatesWithHighestPrio.second.size()); ++i) {
                const ValuePtr &v = candidatesWithHighestPrio.second.at(i);
                QBS_CHECK(v->type() == Value::JSSourceValueType);

                // Note that this is a bit silly: The source code could still evaluate to
                // different values in the end.
                if (static_cast<JSSourceValue *>(v.get())->sourceCode() != sourcCode)
                    error.append({}, v->location());
            }
            if (error.items().size() > 2)
                logger.printWarning(error);
        }

        if (propertyValue == chosenValue)
            return false;
        propertyValue = chosenValue;
        return true;
    }
    if (!propertyValue->next())
        return false;
    std::vector<ValuePtr> singleValuesBefore;
    for (ValuePtr current = propertyValue; current;) {
        singleValuesBefore.push_back(current);
        const ValuePtr next = current->next();
        if (next)
            current->setNext({});
        current = next;
    }
    ValuePtr newValue;
    for (const ValuePtr &v : singleValuesBefore)
        newValue = mergeListValues(productItem, newValue, v);
    std::vector<ValuePtr> singleValuesAfter;
    for (ValuePtr current = propertyValue; current; current = current->next())
        singleValuesAfter.push_back(current);
    propertyValue = newValue;
    return singleValuesBefore != singleValuesAfter;
}

} // namespace qbs::Internal