aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/corelib/language/modulemerger.cpp
blob: 5a5bd3ac026f8283307ccc1c0aa667f43c8bd205 (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
/****************************************************************************
**
** Copyright (C) 2016 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 "modulemerger.h"

#include "value.h"

#include <logging/translator.h>
#include <tools/qbsassert.h>
#include <tools/qttools.h>
#include <tools/stlutils.h>
#include <tools/stringconstants.h>

namespace qbs {
namespace Internal {

ModuleMerger::ModuleMerger(Logger &logger, Item *productItem, const QString &productName,
                           const Item::Modules::iterator &modulesBegin,
                           const Item::Modules::iterator &modulesEnd)
    : m_logger(logger)
    , m_productItem(productItem)
    , m_mergedModule(*modulesBegin)
    , m_isBaseModule(m_mergedModule.name.first() == StringConstants::qbsModule())
    , m_isShadowProduct(productName.startsWith(StringConstants::shadowProductPrefix()))
    , m_modulesBegin(std::next(modulesBegin))
    , m_modulesEnd(modulesEnd)
{
    QBS_CHECK(modulesBegin->item->type() == ItemType::ModuleInstance);
}

void ModuleMerger::replaceItemInValues(QualifiedId moduleName, Item *containerItem, Item *toReplace)
{
    QBS_CHECK(!moduleName.empty());
    QBS_CHECK(containerItem != m_mergedModule.item);
    const QString moduleNamePrefix = moduleName.takeFirst();
    const Item::PropertyMap &properties = containerItem->properties();
    for (auto it = properties.begin(); it != properties.end(); ++it) {
        if (it.key() != moduleNamePrefix)
            continue;
        Value * const val = it.value().get();
        QBS_CHECK(val);
        QBS_CHECK(val->type() == Value::ItemValueType);
        const auto itemVal = static_cast<ItemValue *>(val);
        if (moduleName.empty()) {
            QBS_CHECK(itemVal->item() == toReplace);
            itemVal->setItem(m_mergedModule.item);
        } else {
            replaceItemInValues(moduleName, itemVal->item(), toReplace);
        }
    }
}

void ModuleMerger::start()
{
    // Iterate over any module that our product depends on. These modules
    // may depend on m_mergedModule and contribute property assignments.
    Item::PropertyMap props;
    for (auto module = m_modulesBegin; module != m_modulesEnd; module++)
        mergeModule(&props, *module);

    // Module property assignments in the product have the highest priority
    // and are thus prepended.
    Item::Module m;
    m.item = m_productItem;
    mergeModule(&props, m);

    // The module's prototype is the essential unmodified module as loaded
    // from the cache.
    Item *moduleProto = m_mergedModule.item->prototype();
    while (moduleProto->prototype())
        moduleProto = moduleProto->prototype();

    // The prototype item might contain default values which get appended in
    // case of list properties. Scalar properties will only be set if not
    // already specified above.
    Item::PropertyMap mergedProps = m_mergedModule.item->properties();
    for (auto it = props.constBegin(); it != props.constEnd(); ++it) {
        appendPrototypeValueToNextChain(moduleProto, it.key(), it.value());
        mergedProps[it.key()] = it.value();
    }

    m_mergedModule.item->setProperties(mergedProps);

    // Update all sibling instances of the to-be-merged module to behave identical
    // to the merged module.
    for (Item *moduleInstanceContainer : qAsConst(m_moduleInstanceContainers)) {
        Item::Modules modules;
        for (const Item::Module &dep : moduleInstanceContainer->modules()) {
            const bool isTheModule = dep.name == m_mergedModule.name;
            Item::Module m = dep;
            if (isTheModule && m.item != m_mergedModule.item) {
                QBS_CHECK(m.item->type() == ItemType::ModuleInstance);
                replaceItemInValues(m.name, moduleInstanceContainer, m.item);
                m.item = m_mergedModule.item;
                m.required = m_mergedModule.required;
                m.versionRange = m_mergedModule.versionRange;
            }
            modules << m;
        }
        moduleInstanceContainer->setModules(modules);
    }
}

void ModuleMerger::mergeModule(Item::PropertyMap *dstProps, const Item::Module &module)
{
    const Item::Module *dep = findModule(module.item, m_mergedModule.name);
    if (!dep)
        return;

    const bool mergingProductItem = (module.item == m_productItem);
    Item *srcItem = dep->item;
    Item *origSrcItem = srcItem;
    do {
        if (m_seenInstances.insert(srcItem).second) {
            for (auto it = srcItem->properties().constBegin();
                    it != srcItem->properties().constEnd(); ++it) {
                const ValuePtr &srcVal = it.value();
                if (srcVal->type() == Value::ItemValueType)
                    continue;
                if (it.key() == StringConstants::qbsSourceDirPropertyInternal())
                    continue;
                const PropertyDeclaration srcDecl = srcItem->propertyDeclaration(it.key());
                if (!srcDecl.isValid())
                    continue;

                // Scalar variant values could stem from product multiplexing, in which case
                // the merged qbs module instance needs to get that value.
                if (srcVal->type() == Value::VariantValueType
                        && (!srcDecl.isScalar() || !m_isBaseModule)) {
                    continue;
                }

                ValuePtr clonedSrcVal = srcVal->clone();
                clonedSrcVal->setDefiningItem(origSrcItem);

                ValuePtr &dstVal = (*dstProps)[it.key()];
                if (dstVal) {
                    if (srcDecl.isScalar()) {
                        // Scalar properties get replaced.
                        if (dstVal->type() == Value::JSSourceValueType) {
                            const JSSourceValuePtr dstJsVal =
                                    std::static_pointer_cast<JSSourceValue>(dstVal);
                            const JSSourceValuePtr srcJsVal =
                                    std::static_pointer_cast<JSSourceValue>(srcVal);
                            const bool overriddenInProduct =
                                    m_mergedModule.item->properties().contains(it.key());

                            if (dstJsVal->sourceCode() != srcJsVal->sourceCode()
                                    && !mergingProductItem && !overriddenInProduct
                                    && !m_isShadowProduct) {
                                m_logger.qbsWarning()
                                        << Tr::tr("Conflicting scalar values at %1 and %2.").arg(
                                           dstJsVal->location().toString(),
                                           srcJsVal->location().toString());
                            }
                        }
                    } else {
                        // List properties get prepended
                        QBS_CHECK(!clonedSrcVal->next());
                        clonedSrcVal->setNext(dstVal);
                    }
                }
                dstVal = clonedSrcVal;
            }
        }
        srcItem = srcItem->prototype();
    } while (srcItem && srcItem->type() == ItemType::ModuleInstance);

    // Update dependency constraints
    if (dep->required)
        m_mergedModule.required = true;
    m_mergedModule.versionRange.narrowDown(dep->versionRange);

    // We need to touch the unmerged module instances later once more
    m_moduleInstanceContainers << module.item;
}

void ModuleMerger::appendPrototypeValueToNextChain(Item *moduleProto, const QString &propertyName,
        const ValuePtr &sv)
{
    const PropertyDeclaration pd = m_mergedModule.item->propertyDeclaration(propertyName);
    if (pd.isScalar())
        return;
    if (!m_clonedModulePrototype) {
        m_clonedModulePrototype = Item::create(moduleProto->pool(), ItemType::Module);
        m_clonedModulePrototype->setScope(m_mergedModule.item);
        m_clonedModulePrototype->setLocation(moduleProto->location());
        moduleProto->copyProperty(StringConstants::nameProperty(), m_clonedModulePrototype);
    }
    const ValuePtr &protoValue = moduleProto->property(propertyName);
    QBS_CHECK(protoValue);
    const ValuePtr clonedValue = protoValue->clone();
    lastInNextChain(sv)->setNext(clonedValue);
    clonedValue->setDefiningItem(m_clonedModulePrototype);
    m_clonedModulePrototype->setPropertyDeclaration(propertyName, pd);
    m_clonedModulePrototype->setProperty(propertyName, clonedValue);
}

ValuePtr ModuleMerger::lastInNextChain(const ValuePtr &v)
{
    ValuePtr n = v;
    while (n->next())
        n = n->next();
    return n;
}

const Item::Module *ModuleMerger::findModule(const Item *item, const QualifiedId &name)
{
    for (const auto &module : item->modules()) {
        if (module.name == name)
            return &module;
    }
    return nullptr;
}

void ModuleMerger::merge(Logger &logger, Item *product, const QString &productName,
                         Item::Modules *topSortedModules)
{
    for (auto it = topSortedModules->begin(); it != topSortedModules->end(); ++it)
        ModuleMerger(logger, product, productName, it, topSortedModules->end()).start();
}



} // namespace Internal
} // namespace qbs