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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
|
/****************************************************************************
**
** Copyright (C) 2014 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://www.qt.io/licensing. For further information
** use the contact form at http://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 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** 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 "language.h"
#include "artifactproperties.h"
#include "resolvedfilecontext.h"
#include "scriptengine.h"
#include <buildgraph/artifact.h>
#include <buildgraph/productbuilddata.h>
#include <buildgraph/projectbuilddata.h>
#include <buildgraph/rulegraph.h> // TODO: Move to language?
#include <buildgraph/transformer.h>
#include <jsextensions/jsextensions.h>
#include <logging/translator.h>
#include <tools/buildgraphlocker.h>
#include <tools/hostosinfo.h>
#include <tools/error.h>
#include <tools/propertyfinder.h>
#include <tools/persistence.h>
#include <tools/qbsassert.h>
#include <QDir>
#include <QDirIterator>
#include <QMap>
#include <QMutexLocker>
#include <QScriptValue>
namespace qbs {
namespace Internal {
template<typename T> bool equals(const T *v1, const T *v2)
{
if (v1 == v2)
return true;
if (!v1 != !v2)
return false;
return *v1 == *v2;
}
FileTagger::FileTagger(const QStringList &patterns, const FileTags &fileTags)
: m_fileTags(fileTags)
{
setPatterns(patterns);
}
void FileTagger::setPatterns(const QStringList &patterns)
{
m_patterns.clear();
foreach (const QString &pattern, patterns) {
QBS_CHECK(!pattern.isEmpty());
m_patterns << QRegExp(pattern, Qt::CaseSensitive, QRegExp::Wildcard);
}
}
/*!
* \class FileTagger
* \brief The \c FileTagger class maps 1:1 to the respective item in a qbs source file.
*/
void FileTagger::load(PersistentPool &pool)
{
setPatterns(pool.idLoadStringList());
pool.stream() >> m_fileTags;
}
void FileTagger::store(PersistentPool &pool) const
{
QStringList patterns;
foreach (const QRegExp ®Exp, m_patterns)
patterns << regExp.pattern();
pool.storeStringList(patterns);
pool.stream() << m_fileTags;
}
/*!
* \class SourceArtifact
* \brief The \c SourceArtifact class represents a source file.
* Everything except the file path is inherited from the surrounding \c ResolvedGroup.
* (TODO: Not quite true. Artifacts in transformers will be generated by the transformer, but are
* still represented as source artifacts. We may or may not want to change this; if we do,
* SourceArtifact could simply have a back pointer to the group in addition to the file path.)
* \sa ResolvedGroup
*/
void SourceArtifact::load(PersistentPool &pool)
{
pool.stream() >> absoluteFilePath;
pool.stream() >> fileTags;
pool.stream() >> overrideFileTags;
properties = pool.idLoadS<PropertyMapInternal>();
}
void SourceArtifact::store(PersistentPool &pool) const
{
pool.stream() << absoluteFilePath;
pool.stream() << fileTags;
pool.stream() << overrideFileTags;
pool.store(properties);
}
void SourceWildCards::load(PersistentPool &pool)
{
prefix = pool.idLoadString();
patterns = pool.idLoadStringList();
excludePatterns = pool.idLoadStringList();
pool.loadContainerS(files);
}
void SourceWildCards::store(PersistentPool &pool) const
{
pool.storeString(prefix);
pool.storeStringList(patterns);
pool.storeStringList(excludePatterns);
pool.storeContainer(files);
}
/*!
* \class ResolvedGroup
* \brief The \c ResolvedGroup class corresponds to the Group item in a qbs source file.
*/
/*!
* \variable ResolvedGroup::files
* \brief The files listed in the group item's "files" binding.
* Note that these do not include expanded wildcards.
*/
/*!
* \variable ResolvedGroup::wildcards
* \brief Represents the wildcard elements in this group's "files" binding.
* If no wildcards are specified there, this variable is null.
* \sa SourceWildCards
*/
/*!
* \brief Returns all files specified in the group item as source artifacts.
* This includes the expanded list of wildcards.
*/
QList<SourceArtifactPtr> ResolvedGroup::allFiles() const
{
QList<SourceArtifactPtr> lst = files;
if (wildcards)
lst.append(wildcards->files);
return lst;
}
void ResolvedGroup::load(PersistentPool &pool)
{
name = pool.idLoadString();
pool.stream()
>> enabled
>> location;
prefix = pool.idLoadString();
pool.loadContainerS(files);
wildcards = pool.idLoadS<SourceWildCards>();
properties = pool.idLoadS<PropertyMapInternal>();
pool.stream()
>> fileTags
>> overrideTags;
}
void ResolvedGroup::store(PersistentPool &pool) const
{
pool.storeString(name);
pool.stream()
<< enabled
<< location;
pool.storeString(prefix);
pool.storeContainer(files);
pool.store(wildcards);
pool.store(properties);
pool.stream()
<< fileTags
<< overrideTags;
}
/*!
* \class RuleArtifact
* \brief The \c RuleArtifact class represents an Artifact item encountered in the context
* of a Rule item.
* When applying the rule, one \c Artifact object will be constructed from each \c RuleArtifact
* object. During that process, the \c RuleArtifact's bindings are evaluated and the results
* are inserted into the corresponding \c Artifact's properties.
* \sa Rule
*/
void RuleArtifact::load(PersistentPool &pool)
{
pool.stream()
>> filePath
>> fileTags
>> alwaysUpdated
>> location;
int i;
pool.stream() >> i;
bindings.clear();
bindings.reserve(i);
Binding binding;
for (; --i >= 0;) {
pool.stream() >> binding.name >> binding.code >> binding.location;
bindings += binding;
}
}
void RuleArtifact::store(PersistentPool &pool) const
{
pool.stream()
<< filePath
<< fileTags
<< alwaysUpdated
<< location;
pool.stream() << bindings.count();
for (int i = bindings.count(); --i >= 0;) {
const Binding &binding = bindings.at(i);
pool.stream() << binding.name << binding.code << binding.location;
}
}
/*!
* \class ScriptFunction
* \brief The \c ScriptFunction class represents the JavaScript code found in the "prepare" binding
* of a \c Rule or \c Transformer item in a qbs file.
* \sa Rule
* \sa ResolvedTransformer
*/
ScriptFunction::ScriptFunction()
{
}
ScriptFunction::~ScriptFunction()
{
}
/*!
* \variable ScriptFunction::script
* \brief The actual Javascript code, taken verbatim from the qbs source file.
*/
/*!
* \variable ScriptFunction::location
* \brief The exact location of the script in the qbs source file.
* This is mostly needed for diagnostics.
*/
bool ScriptFunction::isValid() const
{
return location.line() != -1;
}
void ScriptFunction::load(PersistentPool &pool)
{
pool.stream()
>> sourceCode
>> argumentNames
>> location;
fileContext = pool.idLoadS<ResolvedFileContext>();
}
void ScriptFunction::store(PersistentPool &pool) const
{
pool.stream()
<< sourceCode
<< argumentNames
<< location;
pool.store(fileContext);
}
bool operator==(const ScriptFunction &a, const ScriptFunction &b)
{
return a.sourceCode == b.sourceCode
&& a.location == b.location
&& a.argumentNames == b.argumentNames
&& equals(a.fileContext.data(), b.fileContext.data());
}
void ResolvedModule::load(PersistentPool &pool)
{
name = pool.idLoadString();
moduleDependencies = pool.idLoadStringList();
setupBuildEnvironmentScript = pool.idLoadS<ScriptFunction>();
setupRunEnvironmentScript = pool.idLoadS<ScriptFunction>();
}
void ResolvedModule::store(PersistentPool &pool) const
{
pool.storeString(name);
pool.storeStringList(moduleDependencies);
pool.store(setupBuildEnvironmentScript);
pool.store(setupRunEnvironmentScript);
}
bool operator==(const ResolvedModule &m1, const ResolvedModule &m2)
{
return m1.name == m2.name
&& m1.moduleDependencies.toSet() == m2.moduleDependencies.toSet()
&& equals(m1.setupBuildEnvironmentScript.data(), m2.setupBuildEnvironmentScript.data())
&& equals(m1.setupRunEnvironmentScript.data(), m2.setupRunEnvironmentScript.data());
}
QString Rule::toString() const
{
QStringList outputTagsSorted = collectedOutputFileTags().toStringList();
outputTagsSorted.sort();
QStringList inputTagsSorted = inputs.toStringList();
inputTagsSorted.sort();
return QLatin1Char('[') + outputTagsSorted.join(QLatin1String(","))
+ QLatin1String("][")
+ inputTagsSorted.join(QLatin1String(",")) + QLatin1Char(']');
}
bool Rule::acceptsAsInput(Artifact *artifact) const
{
return artifact->fileTags().matches(inputs);
}
FileTags Rule::staticOutputFileTags() const
{
FileTags result;
foreach (const RuleArtifactConstPtr &artifact, artifacts)
result.unite(artifact->fileTags);
return result;
}
FileTags Rule::collectedOutputFileTags() const
{
return outputFileTags.isEmpty() ? staticOutputFileTags() : outputFileTags;
}
bool Rule::isDynamic() const
{
return outputArtifactsScript->isValid();
}
void Rule::load(PersistentPool &pool)
{
name = pool.idLoadString();
prepareScript = pool.idLoadS<ScriptFunction>();
outputArtifactsScript = pool.idLoadS<ScriptFunction>();
module = pool.idLoadS<ResolvedModule>();
pool.stream()
>> inputs
>> outputFileTags
>> auxiliaryInputs
>> excludedAuxiliaryInputs
>> usings
>> explicitlyDependsOn
>> multiplex;
pool.loadContainerS(artifacts);
}
void Rule::store(PersistentPool &pool) const
{
pool.storeString(name);
pool.store(prepareScript);
pool.store(outputArtifactsScript);
pool.store(module);
pool.stream()
<< inputs
<< outputFileTags
<< auxiliaryInputs
<< excludedAuxiliaryInputs
<< usings
<< explicitlyDependsOn
<< multiplex;
pool.storeContainer(artifacts);
}
ResolvedProduct::ResolvedProduct()
: enabled(true)
{
}
ResolvedProduct::~ResolvedProduct()
{
}
void ResolvedProduct::accept(BuildGraphVisitor *visitor) const
{
if (!buildData)
return;
foreach (BuildGraphNode * const node, buildData->roots)
node->accept(visitor);
}
/*!
* \brief Returns all files of all groups as source artifacts.
* This includes the expanded list of wildcards.
*/
QList<SourceArtifactPtr> ResolvedProduct::allFiles() const
{
QList<SourceArtifactPtr> lst;
foreach (const GroupConstPtr &group, groups)
lst += group->allFiles();
return lst;
}
/*!
* \brief Returns all files of all enabled groups as source artifacts.
* \sa ResolvedProduct::allFiles()
*/
QList<SourceArtifactPtr> ResolvedProduct::allEnabledFiles() const
{
QList<SourceArtifactPtr> lst;
foreach (const GroupConstPtr &group, groups) {
if (group->enabled)
lst += group->allFiles();
}
return lst;
}
FileTags ResolvedProduct::fileTagsForFileName(const QString &fileName) const
{
FileTags result;
foreach (FileTaggerConstPtr tagger, fileTaggers) {
foreach (const QRegExp &pattern, tagger->patterns()) {
if (FileInfo::globMatches(pattern, fileName)) {
result.unite(tagger->fileTags());
break;
}
}
}
return result;
}
void ResolvedProduct::load(PersistentPool &pool)
{
pool.stream()
>> enabled
>> fileTags
>> name
>> profile
>> targetName
>> sourceDirectory
>> destinationDirectory
>> location;
productProperties = pool.loadVariantMap();
moduleProperties = pool.idLoadS<PropertyMapInternal>();
pool.loadContainerS(rules);
pool.loadContainerS(dependencies);
pool.loadContainerS(fileTaggers);
pool.loadContainerS(modules);
pool.loadContainerS(transformers);
pool.loadContainerS(scanners);
pool.loadContainerS(groups);
pool.loadContainerS(artifactProperties);
buildData.reset(pool.idLoad<ProductBuildData>());
}
void ResolvedProduct::store(PersistentPool &pool) const
{
pool.stream()
<< enabled
<< fileTags
<< name
<< profile
<< targetName
<< sourceDirectory
<< destinationDirectory
<< location;
pool.store(productProperties);
pool.store(moduleProperties);
pool.storeContainer(rules);
pool.storeContainer(dependencies);
pool.storeContainer(fileTaggers);
pool.storeContainer(modules);
pool.storeContainer(transformers);
pool.storeContainer(scanners);
pool.storeContainer(groups);
pool.storeContainer(artifactProperties);
pool.store(buildData.data());
}
QList<const ResolvedModule*> topSortModules(const QHash<const ResolvedModule*, QList<const ResolvedModule*> > &moduleChildren,
const QList<const ResolvedModule*> &modules,
QSet<QString> &seenModuleNames)
{
QList<const ResolvedModule*> result;
foreach (const ResolvedModule *m, modules) {
if (m->name.isNull())
continue;
result.append(topSortModules(moduleChildren, moduleChildren.value(m), seenModuleNames));
if (!seenModuleNames.contains(m->name)) {
seenModuleNames.insert(m->name);
result.append(m);
}
}
return result;
}
static QScriptValue js_getEnv(QScriptContext *context, QScriptEngine *engine)
{
if (Q_UNLIKELY(context->argumentCount() < 1))
return context->throwError(QScriptContext::SyntaxError,
QLatin1String("getEnv expects 1 argument"));
QVariant v = engine->property("_qbs_procenv");
QProcessEnvironment *procenv = reinterpret_cast<QProcessEnvironment*>(v.value<void*>());
return engine->toScriptValue(procenv->value(context->argument(0).toString()));
}
static QScriptValue js_putEnv(QScriptContext *context, QScriptEngine *engine)
{
if (Q_UNLIKELY(context->argumentCount() < 2))
return context->throwError(QScriptContext::SyntaxError,
QLatin1String("putEnv expects 2 arguments"));
QVariant v = engine->property("_qbs_procenv");
QProcessEnvironment *procenv = reinterpret_cast<QProcessEnvironment*>(v.value<void*>());
procenv->insert(context->argument(0).toString(), context->argument(1).toString());
return engine->undefinedValue();
}
enum EnvType
{
BuildEnv, RunEnv
};
static QProcessEnvironment getProcessEnvironment(ScriptEngine *engine, EnvType envType,
const QList<ResolvedModuleConstPtr> &modules,
const PropertyMapConstPtr &productConfiguration,
const QProcessEnvironment &env)
{
QMap<QString, const ResolvedModule *> moduleMap;
foreach (const ResolvedModuleConstPtr &module, modules)
moduleMap.insert(module->name, module.data());
QHash<const ResolvedModule*, QList<const ResolvedModule*> > moduleParents;
QHash<const ResolvedModule*, QList<const ResolvedModule*> > moduleChildren;
foreach (ResolvedModuleConstPtr module, modules) {
foreach (const QString &moduleName, module->moduleDependencies) {
const ResolvedModule * const depmod = moduleMap.value(moduleName);
QBS_ASSERT(depmod, return env);
moduleParents[depmod].append(module.data());
moduleChildren[module.data()].append(depmod);
}
}
QList<const ResolvedModule *> rootModules;
foreach (ResolvedModuleConstPtr module, modules) {
if (moduleParents.value(module.data()).isEmpty()) {
QBS_ASSERT(module, return env);
rootModules.append(module.data());
}
}
QProcessEnvironment procenv = env;
{
QVariant v;
v.setValue<void*>(&procenv);
engine->setProperty("_qbs_procenv", v);
}
engine->clearImportsCache();
QScriptValue scope = engine->newObject();
const QScriptValue getEnvValue = engine->newFunction(js_getEnv, 1);
const QScriptValue putEnvValue = engine->newFunction(js_putEnv, 1);
scope.setProperty(QLatin1String("getEnv"), getEnvValue);
scope.setProperty(QLatin1String("putEnv"), putEnvValue);
QSet<QString> seenModuleNames;
QList<const ResolvedModule *> topSortedModules = topSortModules(moduleChildren, rootModules, seenModuleNames);
foreach (const ResolvedModule *module, topSortedModules) {
if ((envType == BuildEnv && module->setupBuildEnvironmentScript->sourceCode.isEmpty()) ||
(envType == RunEnv && module->setupBuildEnvironmentScript->sourceCode.isEmpty()
&& module->setupRunEnvironmentScript->sourceCode.isEmpty()))
continue;
ScriptFunctionConstPtr setupScript;
if (envType == BuildEnv) {
setupScript = module->setupBuildEnvironmentScript;
} else {
if (!module->setupRunEnvironmentScript)
setupScript = module->setupBuildEnvironmentScript;
else
setupScript = module->setupRunEnvironmentScript;
}
// handle imports
engine->import(setupScript->fileContext, scope, scope);
JsExtensions::setupExtensions(setupScript->fileContext->jsExtensions(), scope);
// expose properties of direct module dependencies
QScriptValue scriptValue;
QVariantMap productModules = productConfiguration->value()
.value(QLatin1String("modules")).toMap();
foreach (const ResolvedModule * const depmod, moduleChildren.value(module)) {
scriptValue = engine->newObject();
QVariantMap moduleCfg = productModules.value(depmod->name).toMap();
for (QVariantMap::const_iterator it = moduleCfg.constBegin(); it != moduleCfg.constEnd(); ++it)
scriptValue.setProperty(it.key(), engine->toScriptValue(it.value()));
scope.setProperty(depmod->name, scriptValue);
}
// expose the module's properties
QVariantMap moduleCfg = productModules.value(module->name).toMap();
for (QVariantMap::const_iterator it = moduleCfg.constBegin(); it != moduleCfg.constEnd(); ++it)
scope.setProperty(it.key(), engine->toScriptValue(it.value()));
QScriptContext *ctx = engine->currentContext();
ctx->pushScope(scope);
scriptValue = engine->evaluate(setupScript->sourceCode + QLatin1String("()"));
ctx->popScope();
if (Q_UNLIKELY(engine->hasErrorOrException(scriptValue))) {
QString envTypeStr = (envType == BuildEnv
? QLatin1String("build") : QLatin1String("run"));
throw ErrorInfo(Tr::tr("Error while setting up %1 environment: %2")
.arg(envTypeStr, scriptValue.toString()));
}
}
engine->setProperty("_qbs_procenv", QVariant());
return procenv;
}
void ResolvedProduct::setupBuildEnvironment(ScriptEngine *engine, const QProcessEnvironment &env) const
{
if (!buildEnvironment.isEmpty())
return;
buildEnvironment = getProcessEnvironment(engine, BuildEnv, modules, moduleProperties, env);
}
void ResolvedProduct::setupRunEnvironment(ScriptEngine *engine, const QProcessEnvironment &env) const
{
if (!runEnvironment.isEmpty())
return;
runEnvironment = getProcessEnvironment(engine, RunEnv, modules, moduleProperties, env);
}
void ResolvedProduct::registerArtifactWithChangedInputs(Artifact *artifact)
{
QBS_CHECK(buildData);
QBS_CHECK(artifact->product == this);
QBS_CHECK(artifact->transformer);
if (artifact->transformer->rule->multiplex) {
// Reapplication of rules only makes sense for multiplex rules (e.g. linker).
buildData->artifactsWithChangedInputsPerRule[artifact->transformer->rule] += artifact;
}
}
void ResolvedProduct::unregisterArtifactWithChangedInputs(Artifact *artifact)
{
QBS_CHECK(buildData);
QBS_CHECK(artifact->product == this);
QBS_CHECK(artifact->transformer);
buildData->artifactsWithChangedInputsPerRule[artifact->transformer->rule] -= artifact;
}
void ResolvedProduct::unmarkForReapplication(const RuleConstPtr &rule)
{
QBS_CHECK(buildData);
buildData->artifactsWithChangedInputsPerRule.remove(rule);
}
bool ResolvedProduct::isMarkedForReapplication(const RuleConstPtr &rule) const
{
return !buildData->artifactsWithChangedInputsPerRule.value(rule).isEmpty();
}
ArtifactSet ResolvedProduct::lookupArtifactsByFileTag(const FileTag &tag) const
{
QBS_CHECK(buildData);
return buildData->artifactsByFileTag.value(tag);
}
ArtifactSet ResolvedProduct::targetArtifacts() const
{
QBS_CHECK(buildData);
ArtifactSet taSet;
foreach (Artifact * const a, buildData->rootArtifacts()) {
if (a->fileTags().matches(fileTags))
taSet << a;
}
return taSet;
}
TopLevelProject *ResolvedProduct::topLevelProject() const
{
return project->topLevelProject();
}
QString ResolvedProduct::uniqueName(const QString &name, const QString &profile)
{
QBS_CHECK(!profile.isEmpty());
return name + QLatin1Char('.') + profile;
}
QString ResolvedProduct::uniqueName() const
{
return uniqueName(name, profile);
}
static QStringList findGeneratedFiles(const Artifact *base, const FileTags &tags)
{
QStringList result;
foreach (const Artifact *parent, base->parentArtifacts()) {
if (tags.isEmpty() || parent->fileTags().matches(tags))
result << parent->filePath();
}
if (result.isEmpty() || tags.isEmpty())
foreach (const Artifact *parent, base->parentArtifacts())
result << findGeneratedFiles(parent, tags);
return result;
}
QStringList ResolvedProduct::generatedFiles(const QString &baseFile, const FileTags &tags) const
{
ProductBuildData *data = buildData.data();
if (!data)
return QStringList();
foreach (const Artifact *art, ArtifactSet::fromNodeSet(data->nodes)) {
if (art->filePath() == baseFile)
return findGeneratedFiles(art, tags);
}
return QStringList();
}
QString ResolvedProduct::buildDirectory() const
{
const QString result = productProperties.value(QLatin1String("buildDirectory")).toString();
QBS_CHECK(!result.isEmpty());
return result;
}
void ResolvedProduct::cacheExecutablePath(const QString &origFilePath, const QString &fullFilePath)
{
QMutexLocker locker(&m_executablePathCacheLock);
m_executablePathCache.insert(origFilePath, fullFilePath);
}
QString ResolvedProduct::cachedExecutablePath(const QString &origFilePath) const
{
QMutexLocker locker(&m_executablePathCacheLock);
return m_executablePathCache.value(origFilePath);
}
ResolvedProject::ResolvedProject() : enabled(true), m_topLevelProject(0)
{
}
void ResolvedProject::accept(BuildGraphVisitor *visitor) const
{
foreach (const ResolvedProductPtr &product, products)
product->accept(visitor);
foreach (const ResolvedProjectPtr &subProject, subProjects)
subProject->accept(visitor);
}
TopLevelProject *ResolvedProject::topLevelProject()
{
if (m_topLevelProject)
return m_topLevelProject;
TopLevelProject *tlp = dynamic_cast<TopLevelProject *>(this);
if (tlp) {
m_topLevelProject = tlp;
return m_topLevelProject;
}
QBS_CHECK(!parentProject.isNull());
m_topLevelProject = parentProject->topLevelProject();
return m_topLevelProject;
}
QList<ResolvedProjectPtr> ResolvedProject::allSubProjects() const
{
QList<ResolvedProjectPtr> projectList = subProjects;
foreach (const ResolvedProjectConstPtr &subProject, subProjects)
projectList << subProject->allSubProjects();
return projectList;
}
QList<ResolvedProductPtr> ResolvedProject::allProducts() const
{
QList<ResolvedProductPtr> productList = products;
foreach (const ResolvedProjectConstPtr &subProject, subProjects)
productList << subProject->allProducts();
return productList;
}
void ResolvedProject::load(PersistentPool &pool)
{
name = pool.idLoadString();
int count;
pool.stream()
>> location
>> enabled
>> count;
products.clear();
products.reserve(count);
for (; --count >= 0;) {
ResolvedProductPtr rProduct = pool.idLoadS<ResolvedProduct>();
if (rProduct->buildData) {
foreach (BuildGraphNode * const node, rProduct->buildData->nodes) {
node->product = rProduct;
// restore parent links
foreach (BuildGraphNode *child, node->children)
child->parents.insert(node);
}
}
products.append(rProduct);
}
pool.stream() >> count;
subProjects.clear();
subProjects.reserve(count);
for (; --count >= 0;) {
ResolvedProjectPtr p = pool.idLoadS<ResolvedProject>();
subProjects.append(p);
}
m_projectProperties = pool.loadVariantMap();
}
void ResolvedProject::store(PersistentPool &pool) const
{
pool.storeString(name);
pool.stream()
<< location
<< enabled
<< products.count();
foreach (const ResolvedProductConstPtr &product, products)
pool.store(product);
pool.stream() << subProjects.count();
foreach (const ResolvedProjectConstPtr &project, subProjects)
pool.store(project);
pool.store(m_projectProperties);
}
TopLevelProject::TopLevelProject()
: bgLocker(0), locked(false), lastResolveTime(FileTime::oldestTime())
{
}
TopLevelProject::~TopLevelProject()
{
delete bgLocker;
}
QString TopLevelProject::deriveId(const QString &profile, const QVariantMap &config)
{
const QVariantMap qbsProperties = config.value(QLatin1String("qbs")).toMap();
const QString buildVariant = qbsProperties.value(QLatin1String("buildVariant")).toString();
QString prefix = profile;
if (prefix.isEmpty())
prefix = QLatin1String("no-profile");
return prefix + QLatin1Char('-') + buildVariant;
}
QString TopLevelProject::deriveBuildDirectory(const QString &buildRoot, const QString &id)
{
return buildRoot + QLatin1Char('/') + id;
}
void TopLevelProject::setBuildConfiguration(const QVariantMap &config)
{
m_buildConfiguration = config;
m_id = deriveId(profile(), config);
}
QString TopLevelProject::profile() const
{
return projectProperties().value(QLatin1String("profile")).toString();
}
QString TopLevelProject::buildGraphFilePath() const
{
return ProjectBuildData::deriveBuildGraphFilePath(buildDirectory, id());
}
void TopLevelProject::store(const Logger &logger) const
{
// TODO: Use progress observer here.
if (!buildData)
return;
if (!buildData->isDirty) {
logger.qbsDebug() << "[BG] build graph is unchanged in project " << id() << ".";
return;
}
const QString fileName = buildGraphFilePath();
logger.qbsDebug() << "[BG] storing: " << fileName;
PersistentPool pool(logger);
PersistentPool::HeadData headData;
headData.projectConfig = buildConfiguration();
pool.setHeadData(headData);
pool.setupWriteStream(fileName);
store(pool);
pool.finalizeWriteStream();
buildData->isDirty = false;
}
void TopLevelProject::load(PersistentPool &pool)
{
ResolvedProject::load(pool);
pool.stream() >> m_id;
pool.stream() >> usedEnvironment;
pool.stream() >> fileExistsResults;
pool.stream() >> fileLastModifiedResults;
QHash<QString, QString> envHash;
pool.stream() >> envHash;
for (QHash<QString, QString>::const_iterator i = envHash.begin(); i != envHash.end(); ++i)
environment.insert(i.key(), i.value());
pool.stream() >> buildSystemFiles;
pool.stream() >> lastResolveTime;
buildData.reset(pool.idLoad<ProjectBuildData>());
QBS_CHECK(buildData);
buildData->isDirty = false;
}
void TopLevelProject::store(PersistentPool &pool) const
{
ResolvedProject::store(pool);
pool.stream() << m_id;
pool.stream() << usedEnvironment << fileExistsResults
<< fileLastModifiedResults;
QHash<QString, QString> envHash;
foreach (const QString &key, environment.keys())
envHash.insert(key, environment.value(key));
pool.stream() << envHash;
pool.stream() << buildSystemFiles;
pool.stream() << lastResolveTime;
pool.store(buildData.data());
}
/*!
* \class SourceWildCards
* \brief Objects of the \c SourceWildCards class result from giving wildcards in a
* \c ResolvedGroup's "files" binding.
* \sa ResolvedGroup
*/
/*!
* \variable SourceWildCards::prefix
* \brief Inherited from the \c ResolvedGroup
* \sa ResolvedGroup
*/
/*!
* \variable SourceWildCards::patterns
* \brief All elements of the \c ResolvedGroup's "files" binding that contain wildcards.
* \sa ResolvedGroup
*/
/*!
* \variable SourceWildCards::excludePatterns
* \brief Corresponds to the \c ResolvedGroup's "excludeFiles" binding.
* \sa ResolvedGroup
*/
/*!
* \variable SourceWildCards::files
* \brief The \c SourceArtifacts resulting from the expanded list of matching files.
*/
QSet<QString> SourceWildCards::expandPatterns(const GroupConstPtr &group,
const QString &baseDir) const
{
QSet<QString> files = expandPatterns(group, patterns, baseDir);
files -= expandPatterns(group, excludePatterns, baseDir);
return files;
}
QSet<QString> SourceWildCards::expandPatterns(const GroupConstPtr &group,
const QStringList &patterns, const QString &baseDir) const
{
QSet<QString> files;
QString expandedPrefix = prefix;
if (expandedPrefix.startsWith(QLatin1String("~/")))
expandedPrefix.replace(0, 1, QDir::homePath());
foreach (QString pattern, patterns) {
pattern.prepend(expandedPrefix);
pattern.replace(QLatin1Char('\\'), QLatin1Char('/'));
QStringList parts = pattern.split(QLatin1Char('/'), QString::SkipEmptyParts);
if (FileInfo::isAbsolute(pattern)) {
QString rootDir;
if (HostOsInfo::isWindowsHost()) {
rootDir = parts.takeFirst();
if (!rootDir.endsWith(QLatin1Char('/')))
rootDir.append(QLatin1Char('/'));
} else {
rootDir = QLatin1Char('/');
}
expandPatterns(files, group, parts, rootDir);
} else {
expandPatterns(files, group, parts, baseDir);
}
}
return files;
}
static bool isQbsBuildDir(const QDir &dir)
{
return dir.exists(dir.dirName() + QLatin1String(".bg"));
}
void SourceWildCards::expandPatterns(QSet<QString> &result, const GroupConstPtr &group,
const QStringList &parts,
const QString &baseDir) const
{
// People might build directly in the project source directory. This is okay, since
// we keep the build data in a "container" directory. However, we must make sure we don't
// match any generated files therein as source files.
if (isQbsBuildDir(baseDir))
return;
QStringList changed_parts = parts;
bool recursive = false;
QString part = changed_parts.takeFirst();
while (part == QLatin1String("**")) {
recursive = true;
if (changed_parts.isEmpty()) {
part = QLatin1String("*");
break;
}
part = changed_parts.takeFirst();
}
const bool isDir = !changed_parts.isEmpty();
const QString &filePattern = part;
const QDirIterator::IteratorFlags itFlags = recursive
? QDirIterator::Subdirectories
: QDirIterator::NoIteratorFlags;
QDir::Filters itFilters = isDir
? QDir::Dirs
: QDir::Files;
if (isDir && !FileInfo::isPattern(filePattern))
itFilters |= QDir::Hidden;
if (filePattern != QLatin1String("..") && filePattern != QLatin1String("."))
itFilters |= QDir::NoDotAndDotDot;
QDirIterator it(baseDir, QStringList(filePattern), itFilters, itFlags);
while (it.hasNext()) {
const QString filePath = it.next();
if (isQbsBuildDir(it.fileInfo().dir()))
continue; // See above.
QBS_ASSERT(FileInfo(filePath).isDir() == isDir, break);
if (isDir)
expandPatterns(result, group, changed_parts, filePath);
else
result += QDir::cleanPath(filePath);
}
}
void ResolvedTransformer::load(PersistentPool &pool)
{
module = pool.idLoadS<ResolvedModule>();
pool.stream() >> inputs;
pool.loadContainerS(outputs);
transform = pool.idLoadS<ScriptFunction>();
pool.stream() >> explicitlyDependsOn;
}
void ResolvedTransformer::store(PersistentPool &pool) const
{
pool.store(module);
pool.stream() << inputs;
pool.storeContainer(outputs);
pool.store(transform);
pool.stream() << explicitlyDependsOn;
}
template<typename T> QMap<QString, T> listToMap(const QList<T> &list)
{
QMap<QString, T> map;
foreach (const T &elem, list)
map.insert(keyFromElem(elem), elem);
return map;
}
template<typename T> bool listsAreEqual(const QList<T> &l1, const QList<T> &l2)
{
if (l1.count() != l2.count())
return false;
const QMap<QString, T> map1 = listToMap(l1);
const QMap<QString, T> map2 = listToMap(l2);
foreach (const QString &key, map1.keys()) {
const T value2 = map2.value(key);
if (!value2)
return false;
if (!equals(map1.value(key).data(), value2.data()))
return false;
}
return true;
}
QString keyFromElem(const SourceArtifactPtr &sa) { return sa->absoluteFilePath; }
QString keyFromElem(const ResolvedTransformerPtr &t) { return t->transform->sourceCode; }
QString keyFromElem(const RulePtr &r) { return r->toString(); }
QString keyFromElem(const ArtifactPropertiesPtr &ap)
{
QStringList lst = ap->fileTagsFilter().toStringList();
lst.sort();
return lst.join(QLatin1String(","));
}
bool operator==(const SourceArtifact &sa1, const SourceArtifact &sa2)
{
return sa1.absoluteFilePath == sa2.absoluteFilePath
&& sa1.fileTags == sa2.fileTags
&& sa1.overrideFileTags == sa2.overrideFileTags
&& sa1.properties->value() == sa2.properties->value();
}
bool sourceArtifactSetsAreEqual(const QList<SourceArtifactPtr> &l1,
const QList<SourceArtifactPtr> &l2)
{
return listsAreEqual(l1, l2);
}
bool operator==(const ResolvedTransformer &t1, const ResolvedTransformer &t2)
{
return equals(t1.module.data(), t2.module.data())
&& t1.inputs.toSet() == t2.inputs.toSet()
&& sourceArtifactSetsAreEqual(t1.outputs, t2.outputs)
&& equals(t1.transform.data(), t2.transform.data())
&& t1.explicitlyDependsOn == t2.explicitlyDependsOn;
}
bool transformerListsAreEqual(const QList<ResolvedTransformerPtr> &l1,
const QList<ResolvedTransformerPtr> &l2)
{
return listsAreEqual(l1, l2);
}
bool operator==(const Rule &r1, const Rule &r2)
{
if (r1.artifacts.count() != r2.artifacts.count())
return false;
for (int i = 0; i < r1.artifacts.count(); ++i) {
if (!equals(r1.artifacts.at(i).data(), r2.artifacts.at(i).data()))
return false;
}
return r1.module->name == r2.module->name
&& equals(r1.prepareScript.data(), r2.prepareScript.data())
&& equals(r1.outputArtifactsScript.data(), r2.outputArtifactsScript.data())
&& r1.inputs == r2.inputs
&& r1.outputFileTags == r2.outputFileTags
&& r1.auxiliaryInputs == r2.auxiliaryInputs
&& r1.excludedAuxiliaryInputs == r2.excludedAuxiliaryInputs
&& r1.usings == r2.usings
&& r1.explicitlyDependsOn == r2.explicitlyDependsOn
&& r1.multiplex == r2.multiplex;
}
bool ruleListsAreEqual(const QList<RulePtr> &l1, const QList<RulePtr> &l2)
{
return listsAreEqual(l1, l2);
}
bool operator==(const RuleArtifact &a1, const RuleArtifact &a2)
{
return a1.filePath == a2.filePath
&& a1.fileTags == a2.fileTags
&& a1.alwaysUpdated == a2.alwaysUpdated
&& a1.bindings.toList().toSet() == a2.bindings.toList().toSet();
}
bool operator==(const RuleArtifact::Binding &b1, const RuleArtifact::Binding &b2)
{
return b1.code == b2.code && b1.name == b2.name;
}
uint qHash(const RuleArtifact::Binding &b)
{
return qHash(qMakePair(b.code, b.name.join(QLatin1String(","))));
}
bool artifactPropertyListsAreEqual(const QList<ArtifactPropertiesPtr> &l1,
const QList<ArtifactPropertiesPtr> &l2)
{
return listsAreEqual(l1, l2);
}
void ResolvedScanner::load(PersistentPool &pool)
{
module = pool.idLoadS<ResolvedModule>();
pool.stream() >> inputs >> recursive;
searchPathsScript = pool.idLoadS<ScriptFunction>();
scanScript = pool.idLoadS<ScriptFunction>();
}
void ResolvedScanner::store(PersistentPool &pool) const
{
pool.store(module);
pool.stream() << inputs << recursive;
pool.store(searchPathsScript);
pool.store(scanScript);
}
} // namespace Internal
} // namespace qbs
|