aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/buildgraph/buildproject.cpp
blob: 3111494463886b96fd102fa65c0742fb494a30dc (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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Build Suite.
**
** 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 Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/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 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "buildproject.h"

#include "artifact.h"
#include "buildgraph.h"
#include "buildproduct.h"
#include "cycledetector.h"
#include "rulesapplicator.h"
#include "rulesevaluationcontext.h"
#include "transformer.h"
#include <language/language.h>
#include <language/loader.h>
#include <language/scriptengine.h>
#include <logging/logger.h>
#include <logging/translator.h>
#include <tools/error.h>
#include <tools/persistence.h>

namespace qbs {
namespace Internal {

BuildProject::BuildProject() : m_evalContext(0), m_dirty(false)
{
}

BuildProject::~BuildProject()
{
    delete m_evalContext;
    qDeleteAll(m_dependencyArtifacts);
}

void BuildProject::store() const
{
    if (!dirty()) {
        qbsDebug() << "[BG] build graph is unchanged in project " << resolvedProject()->id() << ".";
        return;
    }
    const QString fileName = buildGraphFilePath();
    qbsDebug() << "[BG] storing: " << fileName;
    PersistentPool pool;
    PersistentPool::HeadData headData;
    headData.projectConfig = resolvedProject()->buildConfiguration();
    pool.setHeadData(headData);
    pool.setupWriteStream(fileName);
    store(pool);
    m_dirty = false;
}

QString BuildProject::deriveBuildGraphFilePath(const QString &buildDir, const QString &projectId)
{
    return buildDir + QLatin1Char('/') + projectId + QLatin1String(".bg");
}

QString BuildProject::buildGraphFilePath() const
{
    return deriveBuildGraphFilePath(resolvedProject()->buildDirectory, resolvedProject()->id());
}

void BuildProject::setEvaluationContext(RulesEvaluationContext *evalContext)
{
    delete m_evalContext;
    m_evalContext = evalContext;
}

ResolvedProjectPtr BuildProject::resolvedProject() const
{
    return m_resolvedProject;
}

QSet<BuildProductPtr> BuildProject::buildProducts() const
{
    return m_buildProducts;
}

bool BuildProject::dirty() const
{
    return m_dirty;
}

void BuildProject::insertIntoArtifactLookupTable(Artifact *artifact)
{
    QList<Artifact *> &lst = m_artifactLookupTable[artifact->fileName()][artifact->dirPath()];
    if (!lst.contains(artifact))
        lst.append(artifact);
}

void BuildProject::removeFromArtifactLookupTable(Artifact *artifact)
{
    m_artifactLookupTable[artifact->fileName()][artifact->dirPath()].removeOne(artifact);
}

QList<Artifact *> BuildProject::lookupArtifacts(const QString &filePath) const
{
    QString dirPath, fileName;
    FileInfo::splitIntoDirectoryAndFileName(filePath, &dirPath, &fileName);
    return lookupArtifacts(dirPath, fileName);
}

QList<Artifact *> BuildProject::lookupArtifacts(const QString &dirPath, const QString &fileName) const
{
    QList<Artifact *> result = m_artifactLookupTable.value(fileName).value(dirPath);
    return result;
}

void BuildProject::insertFileDependency(Artifact *artifact)
{
    Q_ASSERT(artifact->artifactType == Artifact::FileDependency);
    m_dependencyArtifacts += artifact;
    insertIntoArtifactLookupTable(artifact);
}

/**
 * Copies dependencies between artifacts from the other project to this project.
 */
void BuildProject::rescueDependencies(const BuildProjectPtr &other)
{
    QHash<QString, BuildProductPtr> otherProductsByName;
    foreach (const BuildProductPtr &product, other->m_buildProducts)
        otherProductsByName.insert(product->rProduct->name, product);

    foreach (const BuildProductPtr &product, m_buildProducts) {
        BuildProductPtr otherProduct = otherProductsByName.value(product->rProduct->name);
        if (!otherProduct)
            continue;

        if (qbsLogLevel(LoggerTrace))
            qbsTrace("[BG] rescue dependencies of product '%s'", qPrintable(product->rProduct->name));

        foreach (Artifact *artifact, product->artifacts) {
            if (qbsLogLevel(LoggerTrace))
                qbsTrace("[BG]    artifact '%s'", qPrintable(artifact->fileName()));

            Artifact *otherArtifact = otherProduct->lookupArtifact(artifact->dirPath(), artifact->fileName());
            if (!otherArtifact || !otherArtifact->transformer)
                continue;

            foreach (Artifact *otherChild, otherArtifact->children) {
                // skip transform edges
                if (otherArtifact->transformer->inputs.contains(otherChild))
                    continue;

                QList<Artifact *> children = lookupArtifacts(otherChild->dirPath(), otherChild->fileName());
                foreach (Artifact *child, children) {
                    if (!artifact->children.contains(child))
                        BuildGraph::safeConnect(artifact, child);
                }
            }
        }
    }
}

void BuildProject::markDirty()
{
    m_dirty = true;
}

void BuildProject::addBuildProduct(const BuildProductPtr &product)
{
    m_buildProducts.insert(product);
}

void BuildProject::setResolvedProject(const ResolvedProjectPtr &resolvedProject)
{
    m_resolvedProject = resolvedProject;
}

void BuildProject::removeArtifact(Artifact *artifact)
{
    if (qbsLogLevel(LoggerTrace))
        qbsTrace() << "[BG] remove artifact " << BuildGraph::fileName(artifact);

    BuildGraph::removeGeneratedArtifactFromDisk(artifact);
    artifact->product->artifacts.remove(artifact);
    removeFromArtifactLookupTable(artifact);
    artifact->product->targetArtifacts.remove(artifact);
    foreach (Artifact *parent, artifact->parents) {
        parent->children.remove(artifact);
        if (parent->transformer) {
            parent->transformer->inputs.remove(artifact);
            m_artifactsThatMustGetNewTransformers += parent;
        }
    }
    foreach (Artifact *child, artifact->children)
        child->parents.remove(artifact);
    artifact->children.clear();
    artifact->parents.clear();
    markDirty();
}

void BuildProject::updateNodesThatMustGetNewTransformer()
{
    RulesEvaluationContext::Scope s(evaluationContext());
    foreach (Artifact *artifact, m_artifactsThatMustGetNewTransformers)
        updateNodeThatMustGetNewTransformer(artifact);
    m_artifactsThatMustGetNewTransformers.clear();
}

void BuildProject::updateNodeThatMustGetNewTransformer(Artifact *artifact)
{
    Q_ASSERT(artifact->transformer);

    if (qbsLogLevel(LoggerDebug))
        qbsDebug() << "[BG] updating transformer for " << BuildGraph::fileName(artifact);

    BuildGraph::removeGeneratedArtifactFromDisk(artifact);

    const RuleConstPtr rule = artifact->transformer->rule;
    markDirty();
    artifact->transformer = TransformerPtr();

    ArtifactsPerFileTagMap artifactsPerFileTag;
    foreach (Artifact *input, artifact->children) {
        foreach (const QString &fileTag, input->fileTags)
            artifactsPerFileTag[fileTag] += input;
    }
    RulesApplicator rulesApplier(artifact->product, artifactsPerFileTag);
    rulesApplier.applyRule(rule);
}

void BuildProject::load(PersistentPool &pool)
{
    TimedActivityLogger logger(QLatin1String("Loading project from disk."));
    m_resolvedProject = pool.idLoadS<ResolvedProject>();
    foreach (const ResolvedProductPtr &product, m_resolvedProject->products)
        product->project = m_resolvedProject;

    int count;
    pool.stream() >> count;
    for (; --count >= 0;) {
        BuildProductPtr product = pool.idLoadS<BuildProduct>();
        foreach (Artifact *artifact, product->artifacts) {
            artifact->project = this;
            insertIntoArtifactLookupTable(artifact);
        }
        addBuildProduct(product);
    }

    pool.stream() >> count;
    m_dependencyArtifacts.clear();
    m_dependencyArtifacts.reserve(count);
    for (; --count >= 0;) {
        Artifact *artifact = pool.idLoad<Artifact>();
        artifact->project = this;
        insertFileDependency(artifact);
    }
}

void BuildProject::store(PersistentPool &pool) const
{
    pool.store(m_resolvedProject);
    pool.storeContainer(m_buildProducts);
    pool.storeContainer(m_dependencyArtifacts);
}


BuildProjectPtr BuildProjectResolver::resolveProject(const ResolvedProjectPtr &resolvedProject,
                                                     RulesEvaluationContext *evalContext)
{
    m_productCache.clear();
    m_project = BuildProjectPtr(new BuildProject);
    m_project->setEvaluationContext(evalContext);
    m_project->setResolvedProject(resolvedProject);
    evalContext->initializeObserver(Tr::tr("Resolving project"), resolvedProject->products.count());
    foreach (ResolvedProductPtr rProduct, resolvedProject->products) {
        resolveProduct(rProduct);
        evalContext->incrementProgressValue();
    }
    CycleDetector().visitProject(m_project);
    return m_project;
}

BuildProductPtr BuildProjectResolver::resolveProduct(const ResolvedProductPtr &rProduct)
{
    BuildProductPtr product = m_productCache.value(rProduct);
    if (product)
        return product;

    evalContext()->checkForCancelation();

    product = BuildProduct::create();
    m_productCache.insert(rProduct, product);
    product->project = m_project;
    product->rProduct = rProduct;
    ArtifactsPerFileTagMap artifactsPerFileTag;

    foreach (ResolvedProductPtr dependency, rProduct->dependencies) {
        if (dependency == rProduct)
            throw Error(Tr::tr("circular using"));
        BuildProductPtr referencedProduct = resolveProduct(dependency);
        product->dependencies.append(referencedProduct);
    }

    //add qbsFile artifact
    Artifact *qbsFileArtifact = product->lookupArtifact(rProduct->location.fileName);
    if (!qbsFileArtifact) {
        qbsFileArtifact = new Artifact(m_project.data());
        qbsFileArtifact->artifactType = Artifact::SourceFile;
        qbsFileArtifact->setFilePath(rProduct->location.fileName);
        qbsFileArtifact->properties = rProduct->properties;
        product->insertArtifact(qbsFileArtifact);
    }
    qbsFileArtifact->fileTags.insert("qbs");
    artifactsPerFileTag["qbs"].insert(qbsFileArtifact);

    // read sources
    foreach (const SourceArtifactConstPtr &sourceArtifact, rProduct->allFiles()) {
        QString filePath = sourceArtifact->absoluteFilePath;
        if (product->lookupArtifact(filePath))
            continue; // ignore duplicate artifacts

        Artifact *artifact = product->createArtifact(sourceArtifact);
        foreach (const QString &fileTag, artifact->fileTags)
            artifactsPerFileTag[fileTag].insert(artifact);
    }

    // read manually added transformers
    foreach (const ResolvedTransformer::Ptr rtrafo, rProduct->transformers) {
        ArtifactList inputArtifacts;
        foreach (const QString &inputFileName, rtrafo->inputs) {
            Artifact *artifact = product->lookupArtifact(inputFileName);
            if (!artifact)
                throw Error(QString("Can't find artifact '%0' in the list of source files.").arg(inputFileName));
            inputArtifacts += artifact;
        }
        TransformerPtr transformer = Transformer::create();
        transformer->inputs = inputArtifacts;
        const RulePtr rule = Rule::create();
        rule->inputs = rtrafo->inputs;
        rule->jsImports = rtrafo->jsImports;
        ResolvedModulePtr module = ResolvedModule::create();
        module->name = rtrafo->module->name;
        rule->module = module;
        rule->script = rtrafo->transform;
        foreach (const SourceArtifactConstPtr &sourceArtifact, rtrafo->outputs) {
            Artifact *outputArtifact = product->createArtifact(sourceArtifact);
            outputArtifact->artifactType = Artifact::Generated;
            outputArtifact->transformer = transformer;
            transformer->outputs += outputArtifact;
            product->targetArtifacts += outputArtifact;
            foreach (Artifact *inputArtifact, inputArtifacts)
                BuildGraph::safeConnect(outputArtifact, inputArtifact);
            foreach (const QString &fileTag, outputArtifact->fileTags)
                artifactsPerFileTag[fileTag].insert(outputArtifact);

            RuleArtifactPtr ruleArtifact = RuleArtifact::create();
            ruleArtifact->fileName = outputArtifact->filePath();
            ruleArtifact->fileTags = outputArtifact->fileTags.toList();
            rule->artifacts += ruleArtifact;
        }
        transformer->rule = rule;

        RulesEvaluationContext::Scope s(evalContext());
        BuildGraph::setupScriptEngineForProduct(engine(), rProduct, transformer->rule, scope());
        transformer->setupInputs(engine(), scope());
        transformer->setupOutputs(engine(), scope());
        transformer->createCommands(rtrafo->transform, engine());
        if (transformer->commands.isEmpty())
            throw Error(QString("There's a transformer without commands."), rtrafo->transform->location);
    }

    RulesApplicator(product.data(), artifactsPerFileTag).applyAllRules();

    QSet<Artifact *> productArtifactCandidates;
    for (int i = 0; i < product->rProduct->fileTags.count(); ++i)
        foreach (Artifact *artifact, artifactsPerFileTag.value(product->rProduct->fileTags.at(i)))
            if (artifact->artifactType == Artifact::Generated)
                productArtifactCandidates += artifact;

    if (productArtifactCandidates.isEmpty()) {
        QString msg = QLatin1String("No artifacts generated for product '%1'.");
        throw Error(msg.arg(product->rProduct->name));
    }

    product->targetArtifacts += productArtifactCandidates;
    m_project->addBuildProduct(product);
    return product;
}

RulesEvaluationContext *BuildProjectResolver::evalContext() const
{
    return m_project->evaluationContext();
}

ScriptEngine *BuildProjectResolver::engine() const
{
    return evalContext()->engine();
}

QScriptValue BuildProjectResolver::scope() const
{
    return evalContext()->scope();
}


static bool isConfigCompatible(const QVariantMap &userCfg, const QVariantMap &projectCfg)
{
    QVariantMap::const_iterator it = userCfg.begin();
    for (; it != userCfg.end(); ++it) {
        if (it.value().type() == QVariant::Map) {
            if (!isConfigCompatible(it.value().toMap(), projectCfg.value(it.key()).toMap()))
                return false;
        } else {
            QVariant value = projectCfg.value(it.key());
            if (!value.isNull() && value != it.value()) {
                return false;
            }
        }
    }
    return true;
}

BuildProjectLoader::LoadResult BuildProjectLoader::load(const QString &projectFilePath,
        RulesEvaluationContext *evalContext, const QString &buildRoot, const QVariantMap &cfg,
        const QStringList &loaderSearchPaths)
{
    m_result = LoadResult();
    m_evalContext = evalContext;

    const QString projectId = ResolvedProject::deriveId(cfg);
    const QString buildDir = ResolvedProject::deriveBuildDirectory(buildRoot, projectId);
    const QString buildGraphFilePath = BuildProject::deriveBuildGraphFilePath(buildDir, projectId);

    PersistentPool pool;
    qbsDebug() << "[BG] trying to load: " << buildGraphFilePath;
    if (!pool.load(buildGraphFilePath))
        return m_result;
    if (!isConfigCompatible(cfg, pool.headData().projectConfig)) {
        qbsDebug() << "[BG] Cannot use stored build graph: Incompatible project configuration.";
        return m_result;
    }

    const BuildProjectPtr project = BuildProjectPtr(new BuildProject);
    project->setEvaluationContext(evalContext);
    TimedActivityLogger loadLogger(QLatin1String("Loading build graph"), QLatin1String("[BG] "));
    project->load(pool);
    foreach (const BuildProductPtr &bp, project->buildProducts())
        bp->project = project;
    project->resolvedProject()->location = CodeLocation(projectFilePath, 1, 1);
    project->resolvedProject()->setBuildConfiguration(pool.headData().projectConfig);
    project->resolvedProject()->buildDirectory = buildDir;
    m_result.loadedProject = project;
    loadLogger.finishActivity();

    const FileInfo bgfi(buildGraphFilePath);
    const bool projectFileChanged = bgfi.lastModified() < FileInfo(projectFilePath).lastModified();

    bool referencedProductRemoved = false;
    QList<BuildProductPtr> changedProducts;
    foreach (BuildProductPtr product, project->buildProducts()) {
        const ResolvedProductConstPtr &resolvedProduct = product->rProduct;
        const FileInfo pfi(resolvedProduct->location.fileName);
        if (!pfi.exists()) {
            referencedProductRemoved = true;
        } else if (bgfi.lastModified() < pfi.lastModified()) {
            changedProducts += product;
        } else {
            foreach (const ResolvedGroup::Ptr &group, resolvedProduct->groups) {
                if (!group->wildcards)
                    continue;
                const QSet<QString> files
                        = group->wildcards->expandPatterns(resolvedProduct->sourceDirectory);
                QSet<QString> wcFiles;
                foreach (const SourceArtifactConstPtr &sourceArtifact, group->wildcards->files)
                    wcFiles += sourceArtifact->absoluteFilePath;
                if (files == wcFiles)
                    continue;
                changedProducts += product;
                break;
            }
        }
    }

    if (projectFileChanged || referencedProductRemoved || !changedProducts.isEmpty()) {
        Loader ldr(evalContext->engine());
        ldr.setSearchPaths(loaderSearchPaths);
        const ResolvedProjectPtr changedProject
                = ldr.loadProject(project->resolvedProject()->location.fileName, buildRoot, cfg);
        m_result.changedResolvedProject = changedProject;

        QMap<QString, ResolvedProductPtr> changedProductsMap;
        foreach (BuildProductPtr product, changedProducts) {
            if (changedProductsMap.isEmpty())
                foreach (ResolvedProductPtr cp, changedProject->products)
                    changedProductsMap.insert(cp->name, cp);
            ResolvedProductPtr changedProduct = changedProductsMap.value(product->rProduct->name);
            if (!changedProduct)
                continue;
            onProductChanged(product, changedProduct);
            if (m_result.discardLoadedProject)
                return m_result;
        }

        QSet<QString> oldProductNames, newProductNames;
        foreach (const BuildProductPtr &product, project->buildProducts())
            oldProductNames += product->rProduct->name;
        foreach (const ResolvedProductConstPtr &product, changedProject->products)
            newProductNames += product->name;
        QSet<QString> addedProductNames = newProductNames - oldProductNames;
        if (!addedProductNames.isEmpty()) {
            m_result.discardLoadedProject = true;
            return m_result;
        }
        QSet<QString> removedProductsNames = oldProductNames - newProductNames;
        if (!removedProductsNames.isEmpty()) {
            foreach (const BuildProductPtr &product, project->buildProducts()) {
                if (removedProductsNames.contains(product->rProduct->name))
                    onProductRemoved(product);
            }
        }

        CycleDetector().visitProject(project);
    }

    return m_result;
}

void BuildProjectLoader::onProductRemoved(const BuildProductPtr &product)
{
    qbsDebug() << "[BG] product '" << product->rProduct->name << "' removed.";

    product->project->markDirty();
    product->project->m_buildProducts.remove(product);

    // delete all removed artifacts physically from the disk
    foreach (Artifact *artifact, product->artifacts) {
        artifact->disconnectAll();
        BuildGraph::removeGeneratedArtifactFromDisk(artifact);
    }
}

void BuildProjectLoader::onProductChanged(const BuildProductPtr &product,
                                          const ResolvedProductPtr &changedProduct)
{
    qbsDebug() << "[BG] product '" << product->rProduct->name << "' changed.";

    ArtifactsPerFileTagMap artifactsPerFileTag;
    QSet<SourceArtifactConstPtr> addedSourceArtifacts;
    QList<Artifact *> addedArtifacts, artifactsToRemove;
    QHash<QString, SourceArtifactConstPtr> oldArtifacts, newArtifacts;

    const QList<SourceArtifactPtr> oldProductAllFiles = product->rProduct->allFiles();
    foreach (const SourceArtifactConstPtr &a, oldProductAllFiles)
        oldArtifacts.insert(a->absoluteFilePath, a);
    foreach (const SourceArtifactPtr &a, changedProduct->allFiles()) {
        newArtifacts.insert(a->absoluteFilePath, a);
        if (!oldArtifacts.contains(a->absoluteFilePath)) {
            // artifact added
            qbsDebug() << "[BG] artifact '" << a->absoluteFilePath << "' added to product " << product->rProduct->name;
            addedArtifacts += product->createArtifact(a);
            addedSourceArtifacts += a;
        }
    }

    foreach (const SourceArtifactPtr &a, oldProductAllFiles) {
        const SourceArtifactConstPtr changedArtifact = newArtifacts.value(a->absoluteFilePath);
        if (!changedArtifact) {
            // artifact removed
            qbsDebug() << "[BG] artifact '" << a->absoluteFilePath << "' removed from product " << product->rProduct->name;
            Artifact *artifact = product->lookupArtifact(a->absoluteFilePath);
            Q_ASSERT(artifact);
            removeArtifactAndExclusiveDependents(artifact, &artifactsToRemove);
            continue;
        }
        if (!addedSourceArtifacts.contains(changedArtifact)
            && changedArtifact->properties->value() != a->properties->value())
        {
            qbsInfo("Some properties changed. Regenerating build graph.");
            qbsDebug("Artifact with changed properties: %s", qPrintable(changedArtifact->absoluteFilePath));
            m_result.discardLoadedProject = true;
            return;
        }
        if (changedArtifact->fileTags != a->fileTags) {
            // artifact's filetags have changed
            qbsDebug() << "[BG] filetags have changed for artifact '" << a->absoluteFilePath
                       << "' from " << a->fileTags << " to " << changedArtifact->fileTags;
            Artifact *artifact = product->lookupArtifact(a->absoluteFilePath);
            Q_ASSERT(artifact);

            // handle added filetags
            foreach (const QString &addedFileTag, changedArtifact->fileTags - a->fileTags)
                artifactsPerFileTag[addedFileTag] += artifact;

            // handle removed filetags
            foreach (const QString &removedFileTag, a->fileTags - changedArtifact->fileTags) {
                artifact->fileTags -= removedFileTag;
                foreach (Artifact *parent, artifact->parents) {
                    if (parent->transformer && parent->transformer->rule->inputs.contains(removedFileTag)) {
                        // this parent has been created because of the removed filetag
                        removeArtifactAndExclusiveDependents(parent, &artifactsToRemove);
                    }
                }
            }
        }
    }

    // Discard groups of the old product. Use the groups of the new one.
    product->rProduct->groups = changedProduct->groups;
    product->rProduct->properties = changedProduct->properties;

    // apply rules for new artifacts
    foreach (Artifact *artifact, addedArtifacts)
        foreach (const QString &ft, artifact->fileTags)
            artifactsPerFileTag[ft] += artifact;
    RulesApplicator(product.data(), artifactsPerFileTag).applyAllRules();

    // parents of removed artifacts must update their transformers
    foreach (Artifact *removedArtifact, artifactsToRemove)
        foreach (Artifact *parent, removedArtifact->parents)
            product->project->addToArtifactsThatMustGetNewTransformers(parent);
    product->project->updateNodesThatMustGetNewTransformer();

    // delete all removed artifacts physically from the disk
    foreach (Artifact *artifact, artifactsToRemove) {
        BuildGraph::removeGeneratedArtifactFromDisk(artifact);
        delete artifact;
    }
}

/**
  * Removes the artifact and all the artifacts that depend exclusively on it.
  * Example: if you remove a cpp artifact then the obj artifact is removed but
  * not the resulting application (if there's more then one cpp artifact).
  */
void BuildProjectLoader::removeArtifactAndExclusiveDependents(Artifact *artifact,
                                                              QList<Artifact*> *removedArtifacts)
{
    if (removedArtifacts)
        removedArtifacts->append(artifact);
    foreach (Artifact *parent, artifact->parents) {
        bool removeParent = false;
        BuildGraph::disconnect(parent, artifact);
        if (parent->children.isEmpty()) {
            removeParent = true;
        } else if (parent->transformer) {
            artifact->project->addToArtifactsThatMustGetNewTransformers(parent);
            parent->transformer->inputs.remove(artifact);
            removeParent = parent->transformer->inputs.isEmpty();
        }
        if (removeParent)
            removeArtifactAndExclusiveDependents(parent, removedArtifacts);
    }
    artifact->project->removeArtifact(artifact);
}

} // namespace Internal
} // namespace qbs