aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/corelib/buildgraph/transformerchangetracking.cpp
blob: 07300b25ba8bd478e34f5279495a5b7054ee4494 (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
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qbs.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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 General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** 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-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "transformerchangetracking.h"

#include "requesteddependencies.h"
#include "rulecommands.h"
#include "transformer.h"
#include <language/language.h>
#include <language/property.h>
#include <language/propertymapinternal.h>
#include <language/qualifiedid.h>
#include <logging/categories.h>
#include <tools/fileinfo.h>
#include <tools/qbsassert.h>
#include <tools/qttools.h>

#include <QtCore/qvariant.h>

namespace qbs {
namespace Internal {

class TrafoChangeTracker
{
public:
    TrafoChangeTracker(const TransformerPtr &transformer,
                       const ResolvedProductPtr &product,
                       const QMap<QString, ResolvedProductPtr> &productsByName,
                       const QMap<QString, const ResolvedProject *> projectsByName)
        : m_transformer(transformer),
          m_product(product),
          m_productsByName(productsByName),
          m_projectsByName(projectsByName)
    {
    }

    bool propertyChangeFound();
    bool environmentChangeFound();

private:
    SourceArtifactConstPtr findSourceArtifact(const QString &artifactFilePath,
                                              QMap<QString, SourceArtifactConstPtr> &artifactMap);
    QVariantMap propertyMapByKind(const Property &property) const;
    void invalidateTransformer();
    bool checkForPropertyChange(const Property &restoredProperty,
                                const QVariantMap &newProperties) const;
    bool checkForImportFileChange(const std::vector<QString> &importedFiles,
                                  const FileTime &referenceTime,
                                  const char *context) const;

    const TransformerPtr &m_transformer;
    const ResolvedProductPtr &m_product;
    const QMap<QString, ResolvedProductPtr> &m_productsByName;
    const QMap<QString, const ResolvedProject *> m_projectsByName;
};

bool checkForPropertyChanges(const TransformerPtr &transformer,
                             const ResolvedProductPtr &product,
                             const QMap<QString, ResolvedProductPtr> &productsByName,
                             const QMap<QString, const ResolvedProject *> projectsByName)
{
    return TrafoChangeTracker(transformer, product, productsByName, projectsByName)
            .propertyChangeFound();
}

bool checkForEnvironmentChanges(const TransformerPtr &transformer,
                                const ResolvedProductPtr &product,
                                const QMap<QString, ResolvedProductPtr> &productsByName,
                                const QMap<QString, const ResolvedProject *> projectsByName)
{
    return TrafoChangeTracker(transformer, product, productsByName, projectsByName)
            .environmentChangeFound();
}

SourceArtifactConstPtr TrafoChangeTracker::findSourceArtifact(const QString &artifactFilePath,
        QMap<QString, SourceArtifactConstPtr> &artifactMap)
{
    SourceArtifactConstPtr &artifact = artifactMap[artifactFilePath];
    if (!artifact) {
        for (const SourceArtifactConstPtr &a : m_product->allFiles()) {
            if (a->absoluteFilePath == artifactFilePath) {
                artifact = a;
                break;
            }
        }
    }
    return artifact;
}

template<typename T> static QVariantMap getParameterValue(
        const QHash<T, QVariantMap> &parameters,
        const QString &depName)
{
    for (auto it = parameters.cbegin(); it != parameters.cend(); ++it) {
        if (it.key()->name == depName)
            return it.value();
    }
    return QVariantMap();
}

QVariantMap TrafoChangeTracker::propertyMapByKind(const Property &property) const
{
    const auto getProduct = [&property, this]() {
        return property.productName == m_product->uniqueName()
                ? m_product : m_productsByName.value(property.productName);
    };
    switch (property.kind) {
    case Property::PropertyInModule: {
        const ResolvedProductConstPtr &p = getProduct();
        return p ? p->moduleProperties->value() : QVariantMap();
    }
    case Property::PropertyInProduct: {
        const ResolvedProductConstPtr &p = getProduct();
        return p ? p->productProperties : QVariantMap();
    }
    case Property::PropertyInProject: {
        const ResolvedProject *project = property.productName == m_product->project->name
                ? m_product->project.get() : m_projectsByName.value(property.productName);
        return project ? project->projectProperties() : QVariantMap();
    }
    case Property::PropertyInParameters: {
        const int sepIndex = property.moduleName.indexOf(QLatin1Char(':'));
        const QString depName = property.moduleName.left(sepIndex);
        const ResolvedProductConstPtr &p = getProduct();
        if (!p)
            return QVariantMap();
        QVariantMap v = getParameterValue(p->dependencyParameters, depName);
        if (!v.empty())
            return v;
        return getParameterValue(p->moduleParameters, depName);
    }
    default:
        QBS_CHECK(false);
    }
    return QVariantMap();
}

void TrafoChangeTracker::invalidateTransformer()
{
    const JavaScriptCommandPtr &pseudoCommand = JavaScriptCommand::create();
    pseudoCommand->setSourceCode(QStringLiteral("random stuff that will cause "
                                                "commandsEqual() to fail"));
    m_transformer->commands.addCommand(pseudoCommand);
}

bool TrafoChangeTracker::propertyChangeFound()
{
    // This check must come first, as it can prevent build data rescuing.
    for (const Property &property : qAsConst(m_transformer->propertiesRequestedInCommands)) {
        if (checkForPropertyChange(property, propertyMapByKind(property))) {
            invalidateTransformer();
            return true;
        }
    }

    QMap<QString, SourceArtifactConstPtr> artifactMap;
    for (auto it = m_transformer->propertiesRequestedFromArtifactInCommands.cbegin();
         it != m_transformer->propertiesRequestedFromArtifactInCommands.cend(); ++it) {
        const SourceArtifactConstPtr artifact
                = findSourceArtifact(it.key(), artifactMap);
        if (!artifact)
            continue;
        for (const Property &property : qAsConst(it.value())) {
            if (checkForPropertyChange(property, artifact->properties->value())) {
                invalidateTransformer();
                return true;
            }
        }
    }

    const FileTime referenceTime = m_transformer->product()->topLevelProject()->lastResolveTime; // TODO: This will need to be a transformer-specific timestamp
    if (checkForImportFileChange(m_transformer->importedFilesUsedInCommands, referenceTime,
                                 "command")) {
        invalidateTransformer();
        return true;
    }

    if (!m_transformer->depsRequestedInCommands.isUpToDate(m_product->topLevelProject())) {
        invalidateTransformer();
        return true;
    }

    for (const Property &property : qAsConst(m_transformer->propertiesRequestedInPrepareScript)) {
        if (checkForPropertyChange(property, propertyMapByKind(property)))
            return true;
    }

    if (checkForImportFileChange(m_transformer->importedFilesUsedInPrepareScript, referenceTime,
                                 "prepare script")) {
        return true;
    }

    for (auto it = m_transformer->propertiesRequestedFromArtifactInPrepareScript.constBegin();
         it != m_transformer->propertiesRequestedFromArtifactInPrepareScript.constEnd(); ++it) {
        const SourceArtifactConstPtr &artifact = findSourceArtifact(it.key(), artifactMap); // TODO: This can use the actual artifact data later
        if (!artifact)
            continue;
        for (const Property &property : qAsConst(it.value())) {
            if (checkForPropertyChange(property, artifact->properties->value()))
                return true;
        }
    }

    if (!m_transformer->depsRequestedInPrepareScript.isUpToDate(m_product->topLevelProject()))
        return true;

    return false;
}

bool TrafoChangeTracker::checkForPropertyChange(const Property &restoredProperty,
                                                const QVariantMap &newProperties) const
{
    QVariant v;
    switch (restoredProperty.kind) {
    case Property::PropertyInProduct:
    case Property::PropertyInProject:
        v = newProperties.value(restoredProperty.propertyName);
        break;
    case Property::PropertyInModule:
        v = moduleProperty(newProperties, restoredProperty.moduleName,
                           restoredProperty.propertyName);
        break;
    case Property::PropertyInParameters: {
        const int sepIndex = restoredProperty.moduleName.indexOf(QLatin1Char(':'));
        QualifiedId moduleName
                = QualifiedId::fromString(restoredProperty.moduleName.mid(sepIndex + 1));
        QVariantMap map = newProperties;
        while (!moduleName.empty())
            map = map.value(moduleName.takeFirst()).toMap();
        v = map.value(restoredProperty.propertyName);
    }
    }
    if (restoredProperty.value != v) {
        qCDebug(lcBuildGraph).noquote().nospace()
                << "Value for property '" << restoredProperty.moduleName << "."
                << restoredProperty.propertyName << "' has changed.\n"
                << "Old value was '" << restoredProperty.value << "'.\n"
                << "New value is '" << v << "'.";
        return true;
    }
    return false;
}

bool TrafoChangeTracker::checkForImportFileChange(const std::vector<QString> &importedFiles,
                                                  const FileTime &referenceTime,
                                                  const char *context) const
{
    for (const QString &importedFile : importedFiles) {
        const FileInfo fi(importedFile);
        if (!fi.exists()) {
            qCDebug(lcBuildGraph) << context << "imported file" << importedFile
                                  << "is gone, need to re-run";
            return true;
        }
        if (fi.lastModified() > referenceTime) {
            qCDebug(lcBuildGraph) << context << "imported file" << importedFile
                                  << "has been updated, need to re-run";
            return true;
        }
    }
    return false;
}

bool TrafoChangeTracker::environmentChangeFound()
{
    // TODO: Also check results of getEnv() from commands here; we currently do not track them
    for (const AbstractCommandPtr &c : qAsConst(m_transformer->commands.commands())) {
        if (c->type() != AbstractCommand::ProcessCommandType)
            continue;
        for (const QString &var : std::static_pointer_cast<ProcessCommand>(c)->relevantEnvVars()) {
            if (m_transformer->product()->topLevelProject()->environment.value(var) // TODO: These will need to get stored per transformer
                    != m_product->topLevelProject()->environment.value(var)) {
                invalidateTransformer();
                return true;
            }
        }
    }
    return false;
}

} // namespace Internal
} // namespace qbs