aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/corelib/buildgraph/buildgraph.cpp
blob: d641627e7302428e6f25c681654fc2bc656d8e88 (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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
/****************************************************************************
**
** 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 "buildgraph.h"

#include "artifact.h"
#include "artifactsscriptvalue.h"
#include "cycledetector.h"
#include "dependencyparametersscriptvalue.h"
#include "projectbuilddata.h"
#include "productbuilddata.h"
#include "rulenode.h"
#include "transformer.h"

#include <jsextensions/jsextensions.h>
#include <jsextensions/moduleproperties.h>
#include <language/artifactproperties.h>
#include <language/language.h>
#include <language/preparescriptobserver.h>
#include <language/propertymapinternal.h>
#include <language/resolvedfilecontext.h>
#include <language/scriptengine.h>
#include <logging/categories.h>
#include <logging/logger.h>
#include <language/property.h>
#include <logging/translator.h>
#include <tools/error.h>
#include <tools/fileinfo.h>
#include <tools/qbsassert.h>
#include <tools/qttools.h>
#include <tools/scripttools.h>
#include <tools/stlutils.h>
#include <tools/stringconstants.h>

#include <QtCore/qdir.h>
#include <QtCore/qfile.h>

#include <algorithm>
#include <iterator>
#include <vector>

namespace qbs {
namespace Internal {

static QString childItemsProperty() { return QStringLiteral("childItems"); }
static QString exportsProperty() { return QStringLiteral("exports"); }

// TODO: Introduce productscriptvalue.{h,cpp}.

static JSValue getDataObject(JSContext *ctx, JSValue obj)
{
    return getJsProperty(ctx, obj, StringConstants::dataPropertyInternal());
}

static JSValue getValueFromData(JSContext *ctx, JSValue data,
                                ModulePropertiesScriptValueCommonPropertyKeys key)
{
    return JS_GetPropertyUint32(ctx, data, key);
}

static JSValue getValueFromObject(JSContext *ctx, JSValue obj,
                                ModulePropertiesScriptValueCommonPropertyKeys key)
{
    const ScopedJsValue data(ctx, getDataObject(ctx, obj));
    return getValueFromData(ctx, data, key);
}

void getPropertyNames(JSContext *ctx, JSPropertyEnum **ptab, uint32_t *plen,
                      const QVariantMap &properties, const QStringList &extraPropertyNames,
                      JSValue extraObject)
{
    JSPropertyEnum *basePTab = nullptr;
    uint32_t basePlen = 0;
    JS_GetOwnPropertyNames(ctx, &basePTab, &basePlen, extraObject,
                           JS_GPN_STRING_MASK | JS_GPN_ENUM_ONLY);
    *plen = uint32_t(extraPropertyNames.size() + properties.size() + basePlen);
    *ptab = reinterpret_cast<JSPropertyEnum *>(js_malloc(ctx, *plen * sizeof **ptab));
    JSPropertyEnum *entry = *ptab;
    for (auto it = properties.begin(); it != properties.end(); ++it, ++entry) {
        entry->atom = JS_NewAtom(ctx, it.key().toUtf8().constData());
        entry->is_enumerable = 1;
    }
    for (const QString &prop : extraPropertyNames) {
        entry->atom = JS_NewAtom(ctx, prop.toUtf8().constData());
        entry->is_enumerable = 1;
        ++entry;
    }
    for (uint32_t i = 0; i < basePlen; ++i, ++entry) {
        entry->atom = basePTab[i].atom;
        entry->is_enumerable = 1;
    }
    js_free(ctx, basePTab);
}

static int getProductPropertyNames(JSContext *ctx, JSPropertyEnum **ptab, uint32_t *plen,
                                   JSValueConst obj)
{
    ScriptEngine * const engine = ScriptEngine::engineForContext(ctx);
    const ScopedJsValue data(ctx, getDataObject(engine->context(), obj));
    const auto product = attachedPointer<ResolvedProduct>(
                obj, engine->productPropertyScriptClass());
    QBS_ASSERT(product, return -1);

    // The "moduleName" convenience property is only available for the "main product" in a rule,
    // and the "parameters" property exists only for elements of the "dependencies" array for
    // which dependency parameters are present.
    const auto hasModuleName = [&] {
        const ScopedJsValue v(ctx, getValueFromData(ctx, data, ModuleNameKey));
        return JS_IsString(v);
    };
    const auto hasDependencyParams = [&] {
        const ScopedJsValue v(ctx, getValueFromData(ctx, data, DependencyParametersKey));
        return JS_IsObject(v);
    };
    QStringList additionalProperties;
    if (hasModuleName())
        additionalProperties.push_back(StringConstants::moduleNameProperty());
    else if (hasDependencyParams())
        additionalProperties.push_back(StringConstants::parametersProperty());
    getPropertyNames(ctx, ptab, plen, product->productProperties, additionalProperties,
                     engine->baseProductScriptValue(product));
    return 0;
}

static int getProductProperty(JSContext *ctx, JSPropertyDescriptor *desc, JSValueConst obj,
                              JSAtom prop)
{
    if (desc) {
        desc->getter = desc->setter = desc->value = JS_UNDEFINED;
        desc->flags = JS_PROP_ENUMERABLE;
    }
    const QString name = getJsString(ctx, prop);
    if (name == StringConstants::parametersProperty()) {
        if (desc)
            desc->value = getValueFromObject(ctx, obj, DependencyParametersKey);
        return 1;
    }
    if (name == StringConstants::moduleNameProperty()) {
        if (desc)
            desc->value = getValueFromObject(ctx, obj, ModuleNameKey);
        return 1;
    }

    ScriptEngine * const engine = ScriptEngine::engineForContext(ctx);
    const auto product = attachedPointer<ResolvedProduct>(
                obj, engine->productPropertyScriptClass());
    QBS_ASSERT(product, return -1);

    const JSValue baseProductValue = engine->baseProductScriptValue(product);
    if (name == exportsProperty()) {
        if (desc)
            desc->value = getJsProperty(ctx, baseProductValue, name);
        engine->addRequestedExport(product);
        return 1;
    }

    const auto it = product->productProperties.constFind(name);
    if (it == product->productProperties.cend()) {
        ScopedJsValue v(ctx, JS_GetProperty(ctx, baseProductValue, prop));
        const int ret = JS_IsUndefined(v) ? 0 : 1;
        if (desc)
            desc->value = v.release();
        return ret;
    }

    engine->addPropertyRequestedInScript(Property(product->uniqueName(), QString(), name,
                                                  it.value(), Property::PropertyInProduct));
    if (desc)
        desc->value = engine->toScriptValue(it.value());
    return 1;
}

static JSValue setupProjectScriptValue(ScriptEngine *engine, const ResolvedProjectConstPtr &project)
{
    JSValue &obj = engine->projectScriptValue(project.get());
    if (JS_IsObject(obj))
        return JS_DupValue(engine->context(), obj);
    obj = engine->newObject();
    setJsProperty(engine->context(), obj, StringConstants::filePathProperty(),
                  project->location.filePath());
    setJsProperty(engine->context(), obj, StringConstants::pathProperty(),
                  FileInfo::path(project->location.filePath()));
    const QVariantMap &projectProperties = project->projectProperties();
    for (QVariantMap::const_iterator it = projectProperties.begin();
            it != projectProperties.end(); ++it) {
        const ScopedJsValue val(engine->context(), engine->toScriptValue(it.value()));
        engine->setObservedProperty(obj, it.key(), val);
    }
    engine->observer()->addProjectObjectId(jsObjectId(obj), project->name);
    return JS_DupValue(engine->context(), obj);
}

static void setupBaseProductScriptValue(ScriptEngine *engine, const ResolvedProduct *product);

class DependenciesFunction
{
public:
    DependenciesFunction(ScriptEngine *engine) : m_engine(engine) { }

    void init(JSValue &productScriptValue, JSValue exportsScriptValue)
    {
        const QByteArray name = StringConstants::dependenciesProperty().toUtf8();
        const ScopedJsValue depfunc(
                    m_engine->context(),
                    JS_NewCFunction(m_engine->context(), &js_internalProductDependencies,
                                    name.constData(), 0));
        const ScopedJsAtom depAtom(m_engine->context(), name);
        JS_DefineProperty(m_engine->context(), productScriptValue, depAtom, JS_UNDEFINED, depfunc,
                          JS_UNDEFINED, JS_PROP_HAS_GET | JS_PROP_ENUMERABLE);
        const ScopedJsValue exportedDepfunc(
                    m_engine->context(),
                    JS_NewCFunction(m_engine->context(), &js_exportedProductDependencies,
                                    name.constData(), 0));
        JS_DefineProperty(m_engine->context(), exportsScriptValue, depAtom, JS_UNDEFINED,
                          exportedDepfunc, JS_UNDEFINED, JS_PROP_HAS_GET | JS_PROP_ENUMERABLE);
    }

private:
    enum class DependencyType { Internal, Exported };
    static JSValue js_productDependencies(const ResolvedProduct *product,
                                          JSContext *ctx, JSValueConst this_val, int argc,
                                          JSValueConst *argv, DependencyType depType)
    {
        Q_UNUSED(this_val)
        Q_UNUSED(argc)
        Q_UNUSED(argv)

        ScriptEngine * const engine = ScriptEngine::engineForContext(ctx);
        JSValue result = JS_NewArray(ctx);
        quint32 idx = 0;
        const bool exportCase = depType == DependencyType::Exported;
        std::vector<ResolvedProductPtr> productDeps;
        if (exportCase) {
            if (!product->exportedModule.productDependencies.empty()) {
                const auto allProducts = product->topLevelProject()->allProducts();
                const auto getProductForName = [&allProducts](const QString &name) {
                    const auto cmp = [name](const ResolvedProductConstPtr &p) {
                        return p->uniqueName() == name;
                    };
                    const auto it = std::find_if(allProducts.cbegin(), allProducts.cend(), cmp);
                    QBS_ASSERT(it != allProducts.cend(), return ResolvedProductPtr());
                    return *it;
                };
                std::transform(product->exportedModule.productDependencies.cbegin(),
                               product->exportedModule.productDependencies.cend(),
                               std::back_inserter(productDeps), getProductForName);
            }
        } else {
            productDeps = product->dependencies;
        }
        for (const ResolvedProductPtr &dependency : std::as_const(productDeps)) {
            setupBaseProductScriptValue(engine, dependency.get());
            JSValue obj = JS_NewObjectClass(engine->context(),
                                            engine->productPropertyScriptClass());
            attachPointerTo(obj, dependency.get());
            const QVariantMap &params
                    = (exportCase ? product->exportedModule.dependencyParameters.value(dependency)
                                  : product->dependencyParameters.value(dependency));
            JSValue data = JS_NewObjectClass(engine->context(), engine->dataWithPtrClass());
            JS_SetPropertyUint32(ctx, data, DependencyParametersKey, dependencyParametersValue(
                                     product->uniqueName(), dependency->name, params, engine));
            defineJsProperty(ctx, obj, StringConstants::dataPropertyInternal(), data);
            JS_SetPropertyUint32(ctx, result, idx++, obj);
        }
        if (exportCase) {
            for (const ExportedModuleDependency &m : product->exportedModule.moduleDependencies) {
                JSValue obj = engine->newObject();
                setJsProperty(ctx, obj, StringConstants::nameProperty(), m.name);
                JSValue exportsValue = engine->newObject();
                setJsProperty(ctx, obj, exportsProperty(), exportsValue);
                setJsProperty(ctx, exportsValue, StringConstants::dependenciesProperty(),
                              JS_NewArray(ctx));
                for (auto modIt = m.moduleProperties.begin(); modIt != m.moduleProperties.end();
                     ++modIt) {
                    const QVariantMap entries = modIt.value().toMap();
                    if (entries.empty())
                        continue;
                    JSValue moduleObj = engine->newObject();
                    ModuleProperties::setModuleScriptValue(engine, exportsValue, moduleObj,
                                                           modIt.key());
                    for (auto valIt = entries.begin(); valIt != entries.end(); ++valIt) {
                        setJsProperty(ctx, moduleObj, valIt.key(),
                                      engine->toScriptValue(valIt.value()));
                    }
                }
                JS_SetPropertyUint32(ctx, result, idx++, obj);
            }
            return result;
        }
        for (const auto &dependency : product->modules) {
            if (dependency->isProduct)
                continue;
            JSValue obj = JS_NewObjectClass(ctx, engine->modulePropertyScriptClass());
            attachPointerTo(obj, dependency.get());
            const QVariantMap &params = product->moduleParameters.value(dependency);
            JSValue data = createDataForModuleScriptValue(engine, nullptr);
            JS_SetPropertyUint32(ctx, data, DependencyParametersKey, dependencyParametersValue(
                                     product->uniqueName(), dependency->name, params, engine));
            defineJsProperty(ctx, obj, StringConstants::dataPropertyInternal(), data);
            JS_SetPropertyUint32(ctx, result, idx++, obj);
        }
        return result;
    }

    static JSValue js_internalProductDependencies(JSContext *ctx, JSValueConst this_val,
                                                  int argc, JSValueConst *argv)
    {
        ScriptEngine * const engine = ScriptEngine::engineForContext(ctx);
        const auto product = attachedPointer<ResolvedProduct>(this_val, engine->dataWithPtrClass());
        engine->addDependenciesArrayRequested(product);
        return js_productDependencies(product, ctx, this_val, argc, argv,
                                      DependencyType::Internal);
    }

    static JSValue js_exportedProductDependencies(JSContext *ctx, JSValueConst this_val,
                                                  int argc, JSValueConst *argv)
    {
        const auto product = attachedPointer<ResolvedProduct>(
                    this_val, ScriptEngine::engineForContext(ctx)->dataWithPtrClass());
        return js_productDependencies(product, ctx, this_val, argc, argv, DependencyType::Exported);
    }

    ScriptEngine *m_engine;
};

static JSValue setupExportedPropertyScriptValue(const ExportedProperty &property,
                                                     ScriptEngine *engine)
{
    JSValue propertyScriptValue = engine->newObject();
    setJsProperty(engine->context(), propertyScriptValue, StringConstants::nameProperty(),
                  property.fullName);
    setJsProperty(engine->context(), propertyScriptValue, StringConstants::typeProperty(),
                  PropertyDeclaration::typeString(property.type));
    setJsProperty(engine->context(), propertyScriptValue, StringConstants::sourceCodeProperty(),
                  property.sourceCode);
    JS_SetPropertyStr(engine->context(), propertyScriptValue, "isBuiltin",
                      JS_NewBool(engine->context(), property.isBuiltin));
    return propertyScriptValue;
}

static void setupExportedPropertiesScriptValue(JSValue &parentObject,
                                               const std::vector<ExportedProperty> &properties,
                                               ScriptEngine *engine)
{
    JSValue propertiesScriptValue = JS_NewArray(engine->context());
    quint32 arrayIndex = 0;
    for (const ExportedProperty &p : properties) {
        JS_SetPropertyUint32(engine->context(), propertiesScriptValue, arrayIndex++,
                             setupExportedPropertyScriptValue(p, engine));
    }
    JS_SetPropertyStr(engine->context(), parentObject, "properties", propertiesScriptValue);
}

static JSValue setupExportedItemScriptValue(const ExportedItem *item, ScriptEngine *engine)
{
    JSValue itemScriptValue = engine->newObject();
    setJsProperty(engine->context(), itemScriptValue, StringConstants::nameProperty(), item->name);
    setupExportedPropertiesScriptValue(itemScriptValue, item->properties, engine);
    JSValue childrenScriptValue = JS_NewArray(engine->context());
    quint32 arrayIndex = 0;
    for (const auto &childItem : item->children) {
        JS_SetPropertyUint32(engine->context(), childrenScriptValue, arrayIndex++,
                             setupExportedItemScriptValue(childItem.get(), engine));
    }
    setJsProperty(engine->context(), itemScriptValue, childItemsProperty(), childrenScriptValue);
    return itemScriptValue;
}

static JSValue setupExportsScriptValue(const ResolvedProduct *product, ScriptEngine *engine)
{
    const ExportedModule &module = product->exportedModule;
    JSValue exportsScriptValue = JS_NewObjectClass(engine->context(), engine->dataWithPtrClass());
    attachPointerTo(exportsScriptValue, product);
    for (auto it = module.propertyValues.cbegin(); it != module.propertyValues.cend(); ++it) {
        setJsProperty(engine->context(), exportsScriptValue, it.key(),
                      engine->toScriptValue(it.value()));
    }
    setupExportedPropertiesScriptValue(exportsScriptValue, module.m_properties, engine);
    JSValue childrenScriptValue = JS_NewArray(engine->context());
    quint32 arrayIndex = 0;
    for (const auto &exportedItem : module.children) {
        JS_SetPropertyUint32(engine->context(), childrenScriptValue, arrayIndex++,
                             setupExportedItemScriptValue(exportedItem.get(), engine));
    }
    setJsProperty(engine->context(), exportsScriptValue, childItemsProperty(), childrenScriptValue);
    JSValue importsScriptValue = JS_NewArray(engine->context());
    arrayIndex = 0;
    for (const QString &importStatement : module.importStatements) {
        JS_SetPropertyUint32(engine->context(), importsScriptValue, arrayIndex++,
                             makeJsString(engine->context(), importStatement));
    }
    setJsProperty(engine->context(), exportsScriptValue, StringConstants::importsProperty(),
                  importsScriptValue);
    for (auto it = module.modulePropertyValues.cbegin(); it != module.modulePropertyValues.cend();
         ++it) {
        const QVariantMap entries = it.value().toMap();
        if (entries.empty())
            continue;
        JSValue moduleObject = engine->newObject();
        ModuleProperties::setModuleScriptValue(engine, exportsScriptValue, moduleObject, it.key());
        for (auto valIt = entries.begin(); valIt != entries.end(); ++valIt) {
            setJsProperty(engine->context(), moduleObject, valIt.key(),
                          engine->toScriptValue(valIt.value()));
        }
    }
    return exportsScriptValue;
}

static void setupBaseProductScriptValue(ScriptEngine *engine, const ResolvedProduct *product)
{
    JSValue &productScriptValue = engine->baseProductScriptValue(product);
    if (JS_IsObject(productScriptValue))
        return;
    const ScopedJsValue proto(engine->context(), JS_NewObject(engine->context()));
    productScriptValue = JS_NewObjectProtoClass(engine->context(), proto,
                                                engine->dataWithPtrClass());
    attachPointerTo(productScriptValue, product);
    ModuleProperties::init(engine, productScriptValue, product);

    const QByteArray funcName = StringConstants::artifactsProperty().toUtf8();
    const ScopedJsValue artifactsFunc(
                engine->context(),
                JS_NewCFunction(engine->context(), &artifactsScriptValueForProduct,
                                funcName.constData(), 0));
    const ScopedJsAtom artifactsAtom(engine->context(), funcName);
    JS_DefineProperty(engine->context(), productScriptValue, artifactsAtom,
                      JS_UNDEFINED, artifactsFunc, JS_UNDEFINED,
                      JS_PROP_HAS_GET | JS_PROP_ENUMERABLE);

    // FIXME: Proper un-observe rather than ref count decrease here.
    ScopedJsValue exportsScriptValue(engine->context(), setupExportsScriptValue(product, engine));
    DependenciesFunction(engine).init(productScriptValue, exportsScriptValue);

    // TODO: Why are these necessary? We catch accesses to product.exports in getProductProperty().
    // But the exportsQbs() and exportsPkgConfig() tests fail without them.
    engine->setObservedProperty(productScriptValue, exportsProperty(), exportsScriptValue);
    engine->observer()->addExportsObjectId(jsObjectId(exportsScriptValue), product);
}

void setupScriptEngineForFile(ScriptEngine *engine, const FileContextBaseConstPtr &fileContext,
        JSValue targetObject, const ObserveMode &observeMode)
{
    engine->import(fileContext, targetObject, observeMode);
    JsExtensions::setupExtensions(engine, fileContext->jsExtensions(), targetObject);
}

void setupScriptEngineForProduct(ScriptEngine *engine, ResolvedProduct *product,
                                 const ResolvedModule *module, JSValue targetObject,
                                 bool setBuildEnvironment)
{
    JSValue projectScriptValue = setupProjectScriptValue(engine, product->project.lock());
    setJsProperty(engine->context(), targetObject, StringConstants::projectVar(),
                  projectScriptValue);
    if (setBuildEnvironment) {
        QVariant v;
        v.setValue<void*>(&product->buildEnvironment);
        engine->setProperty(StringConstants::qbsProcEnvVarInternal(), v);
    }
    JSClassID scriptClass = engine->productPropertyScriptClass();
    if (scriptClass == 0) {
        engine->setProductPropertyScriptClass(engine->registerClass("ProductProperties", nullptr,
                nullptr, JS_UNDEFINED, &getProductPropertyNames, &getProductProperty));
        scriptClass = engine->productPropertyScriptClass();
    }
    setupBaseProductScriptValue(engine, product);
    JSValue productScriptValue = JS_NewObjectClass(engine->context(), scriptClass);
    attachPointerTo(productScriptValue, product);
    setJsProperty(engine->context(), targetObject, StringConstants::productVar(),
                  productScriptValue);

    JSValue data = JS_NewObjectClass(engine->context(), engine->dataWithPtrClass());
    // If the Rule is in a Module, set up the 'moduleName' property
    if (!module->name.isEmpty()) {
        JS_SetPropertyUint32(engine->context(), data, ModuleNameKey,
                             makeJsString(engine->context(), module->name));
    }
    defineJsProperty(engine->context(), productScriptValue,
                     StringConstants::dataPropertyInternal(), data);
}

bool findPath(BuildGraphNode *u, BuildGraphNode *v, QList<BuildGraphNode *> &path)
{
    if (u == v) {
        path.push_back(v);
        return true;
    }

    for (BuildGraphNode * const childNode : std::as_const(u->children)) {
        if (findPath(childNode, v, path)) {
            path.prepend(u);
            return true;
        }
    }

    return false;
}

/*
 * Creates the build graph edge p -> c, which represents the dependency "c must be built before p".
 */
void connect(BuildGraphNode *p, BuildGraphNode *c)
{
    QBS_CHECK(p != c);
    qCDebug(lcBuildGraph).noquote() << "connect" << p->toString() << "->" << c->toString();
    if (c->type() == BuildGraphNode::ArtifactNodeType) {
        auto const ac = static_cast<Artifact *>(c);
        for (const Artifact *child : filterByType<Artifact>(p->children)) {
            if (child == ac)
                return;
            const bool filePathsMustBeDifferent = child->artifactType == Artifact::Generated
                    || child->product == ac->product || child->artifactType != ac->artifactType;
            if (filePathsMustBeDifferent && child->filePath() == ac->filePath()) {
                throw ErrorInfo(QStringLiteral("%1 already has a child artifact %2 as "
                                                    "different object.").arg(p->toString(),
                                                                             ac->filePath()),
                                CodeLocation(), true);
            }
        }
    }
    p->children.insert(c);
    c->parents.insert(p);
    p->product->topLevelProject()->buildData->setDirty();
}

static bool existsPath_impl(BuildGraphNode *u, BuildGraphNode *v, NodeSet *seen)
{
    if (u == v)
        return true;

    if (!seen->insert(u).second)
        return false;

    return Internal::any_of(u->children, [v, seen](const auto &child) {
        return existsPath_impl(child, v, seen);
    });
}

static bool existsPath(BuildGraphNode *u, BuildGraphNode *v)
{
    NodeSet seen;
    return existsPath_impl(u, v, &seen);
}

static QStringList toStringList(const QList<BuildGraphNode *> &path)
{
    QStringList lst;
    for (BuildGraphNode *node : path)
        lst << node->toString();
    return lst;
}

bool safeConnect(Artifact *u, Artifact *v)
{
    QBS_CHECK(u != v);
    qCDebug(lcBuildGraph) << "safeConnect:" << relativeArtifactFileName(u)
                          << "->" << relativeArtifactFileName(v);

    if (existsPath(v, u)) {
        QList<BuildGraphNode *> circle;
        findPath(v, u, circle);
        qCDebug(lcBuildGraph) << "safeConnect: circle detected " << toStringList(circle);
        return false;
    }

    connect(u, v);
    return true;
}

void disconnect(BuildGraphNode *u, BuildGraphNode *v)
{
    qCDebug(lcBuildGraph).noquote() << "disconnect:" << u->toString() << v->toString();
    u->children.remove(v);
    v->parents.remove(u);
    u->onChildDisconnected(v);
}

void removeGeneratedArtifactFromDisk(Artifact *artifact, const Logger &logger)
{
    if (artifact->artifactType != Artifact::Generated)
        return;
    removeGeneratedArtifactFromDisk(artifact->filePath(), logger);
}

void removeGeneratedArtifactFromDisk(const QString &filePath, const Logger &logger)
{
    QFile file(filePath);
    if (!file.exists())
        return;
    logger.qbsDebug() << "removing " << filePath;
    if (!file.remove())
        logger.qbsWarning() << QStringLiteral("Cannot remove '%1'.").arg(filePath);
}

QString relativeArtifactFileName(const Artifact *artifact)
{
    const QString &buildDir = artifact->product->topLevelProject()->buildDirectory;
    QString str = artifact->filePath();
    if (str.startsWith(buildDir))
        str.remove(0, buildDir.size());
    if (str.startsWith(QLatin1Char('/')))
        str.remove(0, 1);
    return str;
}

Artifact *lookupArtifact(const ResolvedProductConstPtr &product,
        const ProjectBuildData *projectBuildData, const QString &dirPath, const QString &fileName,
        bool compareByName)
{
    for (const auto &fileResource : projectBuildData->lookupFiles(dirPath, fileName)) {
        if (fileResource->fileType() != FileResourceBase::FileTypeArtifact)
            continue;
        const auto artifact = static_cast<Artifact *>(fileResource);
        if (compareByName
                ? artifact->product->uniqueName() == product->uniqueName()
                : artifact->product == product) {
            return artifact;
        }
    }
    return nullptr;
}

Artifact *lookupArtifact(const ResolvedProductConstPtr &product, const QString &dirPath,
                         const QString &fileName, bool compareByName)
{
    return lookupArtifact(product, product->topLevelProject()->buildData.get(), dirPath, fileName,
                          compareByName);
}

Artifact *lookupArtifact(const ResolvedProductConstPtr &product, const QString &filePath,
                         bool compareByName)
{
    QString dirPath, fileName;
    FileInfo::splitIntoDirectoryAndFileName(filePath, &dirPath, &fileName);
    return lookupArtifact(product, dirPath, fileName, compareByName);
}

Artifact *lookupArtifact(const ResolvedProductConstPtr &product, const ProjectBuildData *buildData,
                         const QString &filePath, bool compareByName)
{
    QString dirPath, fileName;
    FileInfo::splitIntoDirectoryAndFileName(filePath, &dirPath, &fileName);
    return lookupArtifact(product, buildData, dirPath, fileName, compareByName);
}

Artifact *lookupArtifact(const ResolvedProductConstPtr &product, const Artifact *artifact,
                         bool compareByName)
{
    return lookupArtifact(product, artifact->dirPath(), artifact->fileName(), compareByName);
}

Artifact *createArtifact(const ResolvedProductPtr &product,
                         const SourceArtifactConstPtr &sourceArtifact)
{
    const auto artifact = new Artifact;
    artifact->artifactType = Artifact::SourceFile;
    setArtifactData(artifact, sourceArtifact);
    insertArtifact(product, artifact);
    return artifact;
}

void setArtifactData(Artifact *artifact, const SourceArtifactConstPtr &sourceArtifact)
{
    artifact->targetOfModule = sourceArtifact->targetOfModule;
    artifact->setFilePath(sourceArtifact->absoluteFilePath);
    artifact->setFileTags(sourceArtifact->fileTags);
    artifact->properties = sourceArtifact->properties;
}

void updateArtifactFromSourceArtifact(const ResolvedProductPtr &product,
                                      const SourceArtifactConstPtr &sourceArtifact)
{
    Artifact * const artifact = lookupArtifact(product, sourceArtifact->absoluteFilePath, false);
    QBS_CHECK(artifact);
    const FileTags oldFileTags = artifact->fileTags();
    const QVariantMap oldModuleProperties = artifact->properties->value();
    setArtifactData(artifact, sourceArtifact);
    if (oldFileTags != artifact->fileTags()
            || oldModuleProperties != artifact->properties->value()) {
        invalidateArtifactAsRuleInputIfNecessary(artifact);
    }
}

void insertArtifact(const ResolvedProductPtr &product, Artifact *artifact)
{
    qCDebug(lcBuildGraph) << "insert artifact" << artifact->filePath();
    QBS_CHECK(!artifact->product);
    QBS_CHECK(!artifact->filePath().isEmpty());
    artifact->product = product;
    product->topLevelProject()->buildData->insertIntoLookupTable(artifact);
    product->buildData->addArtifact(artifact);
}

void provideFullFileTagsAndProperties(Artifact *artifact)
{
    artifact->properties = artifact->product->moduleProperties;
    FileTags allTags = artifact->pureFileTags.empty()
            ? artifact->product->fileTagsForFileName(artifact->fileName()) : artifact->pureFileTags;
    for (const auto &props : artifact->product->artifactProperties) {
        if (allTags.intersects(props->fileTagsFilter())) {
            artifact->properties = props->propertyMap();
            allTags += props->extraFileTags();
            break;
        }
    }
    artifact->setFileTags(allTags);

    // Let a positive value of qbs.install imply the file tag "installable".
    if (artifact->properties->qbsPropertyValue(StringConstants::installProperty()).toBool())
        artifact->addFileTag("installable");
}

void applyPerArtifactProperties(Artifact *artifact)
{
    if (artifact->pureProperties.empty())
        return;
    QVariantMap props = artifact->properties->value();
    for (const auto &property : artifact->pureProperties)
        setConfigProperty(props, property.first, property.second);
    artifact->properties = artifact->properties->clone();
    artifact->properties->setValue(props);
}

void updateGeneratedArtifacts(ResolvedProduct *product)
{
    if (!product->buildData)
        return;
    for (Artifact * const artifact : filterByType<Artifact>(product->buildData->allNodes())) {
        if (artifact->artifactType == Artifact::Generated) {
            const FileTags oldFileTags = artifact->fileTags();
            const QVariantMap oldModuleProperties = artifact->properties->value();
            provideFullFileTagsAndProperties(artifact);
            applyPerArtifactProperties(artifact);
            if (oldFileTags != artifact->fileTags()
                    || oldModuleProperties != artifact->properties->value()) {
                invalidateArtifactAsRuleInputIfNecessary(artifact);
            }
        }
    }
}

// This is needed for artifacts which are inputs to rules whose outputArtifacts script
// returned an empty array for this input. Since there is no transformer, our usual change
// tracking procedure will not notice if the artifact's file tags or module properties have
// changed, so we need to force a re-run of the outputArtifacts script.
void invalidateArtifactAsRuleInputIfNecessary(Artifact *artifact)
{
    for (RuleNode * const parentRuleNode : filterByType<RuleNode>(artifact->parents)) {
        if (!parentRuleNode->rule()->isDynamic())
            continue;
        bool artifactNeedsExplicitInvalidation = true;
        for (Artifact * const output : filterByType<Artifact>(parentRuleNode->parents)) {
            if (output->children.contains(artifact)
                    && !output->childrenAddedByScanner.contains(artifact)) {
                artifactNeedsExplicitInvalidation = false;
                break;
            }
        }
        if (artifactNeedsExplicitInvalidation)
            parentRuleNode->removeOldInputArtifact(artifact);
    }
}

static void doSanityChecksForProduct(const ResolvedProductConstPtr &product,
        const Set<ResolvedProductPtr> &allProducts, const Logger &logger)
{
    qCDebug(lcBuildGraph) << "Sanity checking product" << product->uniqueName();
    CycleDetector cycleDetector(logger);
    cycleDetector.visitProduct(product);
    const ProductBuildData * const buildData = product->buildData.get();
    for (const auto &m : product->modules)
        QBS_CHECK(m->product == product.get());
    qCDebug(lcBuildGraph) << "enabled:" << product->enabled << "build data:" << buildData;
    if (product->enabled)
        QBS_CHECK(buildData);
    if (!product->buildData)
        return;
    for (BuildGraphNode * const node : std::as_const(buildData->rootNodes())) {
        qCDebug(lcBuildGraph).noquote() << "Checking root node" << node->toString();
        QBS_CHECK(buildData->allNodes().contains(node));
    }
    Set<QString> filePaths;
    for (BuildGraphNode * const node : std::as_const(buildData->allNodes())) {
        qCDebug(lcBuildGraph).noquote() << "Sanity checking node" << node->toString();
        QBS_CHECK(node->product == product);
        for (const BuildGraphNode * const parent : std::as_const(node->parents))
            QBS_CHECK(parent->children.contains(node));
        for (BuildGraphNode * const child : std::as_const(node->children)) {
            QBS_CHECK(child->parents.contains(node));
            QBS_CHECK(!child->product.expired());
            QBS_CHECK(child->product->buildData);
            QBS_CHECK(child->product->buildData->allNodes().contains(child));
            QBS_CHECK(allProducts.contains(child->product.lock()));
        }

        Artifact * const artifact = node->type() == BuildGraphNode::ArtifactNodeType
                ? static_cast<Artifact *>(node) : nullptr;
        if (!artifact) {
            QBS_CHECK(node->type() == BuildGraphNode::RuleNodeType);
            auto const ruleNode = static_cast<RuleNode *>(node);
            QBS_CHECK(ruleNode->rule());
            QBS_CHECK(ruleNode->rule()->product);
            QBS_CHECK(ruleNode->rule()->product == ruleNode->product.get());
            QBS_CHECK(ruleNode->rule()->product == product.get());
            QBS_CHECK(contains(product->rules, std::const_pointer_cast<Rule>(ruleNode->rule())));
            continue;
        }

        QBS_CHECK(product->topLevelProject()->buildData->fileDependencies.contains(
                      artifact->fileDependencies));
        QBS_CHECK(artifact->artifactType == Artifact::SourceFile ||
                  !filePaths.contains(artifact->filePath()));
        filePaths << artifact->filePath();

        for (Artifact * const child : std::as_const(artifact->childrenAddedByScanner))
            QBS_CHECK(artifact->children.contains(child));
        const TransformerConstPtr transformer = artifact->transformer;
        if (artifact->artifactType == Artifact::SourceFile)
            continue;

        const auto parentRuleNodes = filterByType<RuleNode>(artifact->children);
        QBS_CHECK(std::distance(parentRuleNodes.begin(), parentRuleNodes.end()) == 1);

        QBS_CHECK(transformer);
        QBS_CHECK(transformer->rule);
        QBS_CHECK(transformer->rule->product);
        QBS_CHECK(transformer->rule->product == artifact->product.get());
        QBS_CHECK(transformer->rule->product == product.get());
        QBS_CHECK(transformer->outputs.contains(artifact));
        QBS_CHECK(contains(product->rules, std::const_pointer_cast<Rule>(transformer->rule)));
        qCDebug(lcBuildGraph)
                << "The transformer has" << transformer->outputs.size() << "outputs.";
        ArtifactSet transformerOutputChildren;
        for (const Artifact * const output : std::as_const(transformer->outputs)) {
            QBS_CHECK(output->transformer == transformer);
            transformerOutputChildren.unite(ArtifactSet::filtered(output->children));
            for (const Artifact *a : filterByType<Artifact>(output->children)) {
                for (const Artifact *other : filterByType<Artifact>(output->children)) {
                    if (other != a && other->filePath() == a->filePath()
                            && (other->artifactType != Artifact::SourceFile
                                || a->artifactType != Artifact::SourceFile
                                || other->product == a->product)) {
                        throw ErrorInfo(QStringLiteral("There is more than one artifact for "
                                "file '%1' in the child list for output '%2'.")
                                .arg(a->filePath(), output->filePath()), CodeLocation(), true);
                    }
                }
            }
        }
        if (lcBuildGraph().isDebugEnabled()) {
            qCDebug(lcBuildGraph) << "The transformer output children are:";
            for (const Artifact * const a : std::as_const(transformerOutputChildren))
                qCDebug(lcBuildGraph) << "\t" << a->fileName();
            qCDebug(lcBuildGraph) << "The transformer inputs are:";
            for (const Artifact * const a : std::as_const(transformer->inputs))
                qCDebug(lcBuildGraph) << "\t" << a->fileName();
        }
        QBS_CHECK(transformer->inputs.size() <= transformerOutputChildren.size());
        for (Artifact * const transformerInput : std::as_const(transformer->inputs))
            QBS_CHECK(transformerOutputChildren.contains(transformerInput));
        transformer->artifactsMapRequestedInPrepareScript.doSanityChecks();
        transformer->artifactsMapRequestedInCommands.doSanityChecks();
    }
}

static void doSanityChecks(const ResolvedProjectPtr &project,
                           const Set<ResolvedProductPtr> &allProducts, Set<QString> &productNames,
                           const Logger &logger)
{
    logger.qbsDebug() << "Sanity checking project '" << project->name << "'";
    for (const ResolvedProjectPtr &subProject : std::as_const(project->subProjects))
        doSanityChecks(subProject, allProducts, productNames, logger);

    for (const auto &product : project->products) {
        QBS_CHECK(product->project == project);
        QBS_CHECK(product->topLevelProject() == project->topLevelProject());
        doSanityChecksForProduct(product, allProducts, logger);
        QBS_CHECK(!productNames.contains(product->uniqueName()));
        productNames << product->uniqueName();
    }
}

void doSanityChecks(const ResolvedProjectPtr &project, const Logger &logger)
{
    if (qEnvironmentVariableIsEmpty("QBS_SANITY_CHECKS"))
        return;
    Set<QString> productNames;
    const auto allProducts = rangeTo<Set<ResolvedProductPtr>>(project->allProducts());
    doSanityChecks(project, allProducts, productNames, logger);
}

} // namespace Internal
} // namespace qbs