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
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
|
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "project.h"
#include "buildconfiguration.h"
#include "buildinfo.h"
#include "buildmanager.h"
#include "buildsystem.h"
#include "deployconfiguration.h"
#include "devicesupport/devicekitaspects.h"
#include "devicesupport/idevice.h"
#include "editorconfiguration.h"
#include "environmentaspect.h"
#include "kit.h"
#include "kitmanager.h"
#include "msvctoolchain.h"
#include "projectexplorer.h"
#include "projectexplorerconstants.h"
#include "projectexplorersettings.h"
#include "projectexplorertr.h"
#include "projectmanager.h"
#include "projectnodes.h"
#include "projecttree.h"
#include "runconfiguration.h"
#include "runconfigurationaspects.h"
#include "target.h"
#include "taskhub.h"
#include "toolchainkitaspect.h"
#include "toolchainmanager.h"
#include "userfileaccessor.h"
#include <coreplugin/documentmanager.h>
#include <coreplugin/editormanager/documentmodel.h>
#include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
#include <coreplugin/idocument.h>
#include <texteditor/codestyleeditor.h>
#include <utils/algorithm.h>
#include <utils/environment.h>
#include <utils/fileutils.h>
#include <utils/macroexpander.h>
#include <utils/mimeconstants.h>
#include <utils/mimeutils.h>
#include <utils/pointeralgorithm.h>
#include <utils/qtcassert.h>
#include <utils/stringutils.h>
#include <QFileDialog>
#include <QHash>
#include <queue>
#ifdef WITH_TESTS
#include <coreplugin/editormanager/editormanager.h>
#include <utils/temporarydirectory.h>
#include <QEventLoop>
#include <QSignalSpy>
#include <QTest>
#include <QTimer>
#endif
using namespace Utils;
using namespace Core;
namespace PE = ProjectExplorer;
namespace ProjectExplorer {
/*!
\class ProjectExplorer::Project
\brief The Project class implements a project node in the project explorer.
*/
/*!
\fn void ProjectExplorer::Project::environmentChanged()
A convenience signal emitted if activeBuildConfiguration emits
environmentChanged or if the active build configuration changes
(including due to the active target changing).
*/
/*!
\fn void ProjectExplorer::Project::buildConfigurationEnabledChanged()
A convenience signal emitted if activeBuildConfiguration emits
isEnabledChanged() or if the active build configuration changes
(including due to the active target changing).
*/
const char ACTIVE_TARGET_KEY[] = "ProjectExplorer.Project.ActiveTarget";
const char TARGET_KEY_PREFIX[] = "ProjectExplorer.Project.Target.";
const char TARGET_COUNT_KEY[] = "ProjectExplorer.Project.TargetCount";
const char EDITOR_SETTINGS_KEY[] = "ProjectExplorer.Project.EditorSettings";
const char PLUGIN_SETTINGS_KEY[] = "ProjectExplorer.Project.PluginSettings";
const char PROJECT_ENV_KEY[] = "ProjectExplorer.Project.Environment";
static bool isListedFileNode(const Node *node)
{
return node->asContainerNode() || node->listInProject();
}
static bool nodeLessThan(const Node *n1, const Node *n2)
{
return n1->filePath() < n2->filePath();
}
const Project::NodeMatcher Project::AllFiles = [](const Node *node) {
return isListedFileNode(node);
};
const Project::NodeMatcher Project::SourceFiles = [](const Node *node) {
return isListedFileNode(node) && !node->isGenerated();
};
const Project::NodeMatcher Project::GeneratedFiles = [](const Node *node) {
return isListedFileNode(node) && node->isGenerated();
};
const Project::NodeMatcher Project::HiddenRccFolders = [](const Node *node) {
return node->isFolderNodeType() && node->filePath().fileName() == ".rcc";
};
// --------------------------------------------------------------------
// ProjectDocument:
// --------------------------------------------------------------------
class ProjectDocument : public IDocument
{
public:
ProjectDocument(const QString &mimeType, const FilePath &fileName, Project *project)
: m_project(project)
{
QTC_CHECK(project);
setFilePath(fileName);
setMimeType(mimeType);
}
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const final
{
Q_UNUSED(state)
Q_UNUSED(type)
return BehaviorSilent;
}
Result<> reload(ReloadFlag flag, ChangeType type) final
{
Q_UNUSED(flag)
Q_UNUSED(type)
emit m_project->projectFileIsDirty(filePath());
return ResultOk;
}
private:
Project *m_project;
};
// -------------------------------------------------------------------------
// Project
// -------------------------------------------------------------------------
class ProjectPrivate
{
public:
ProjectPrivate(Project *q) : q(q) {}
~ProjectPrivate();
Project * const q;
Id m_id;
bool m_needsInitialExpansion = false;
bool m_canBuildProducts = false;
bool m_hasMakeInstallEquivalent = false;
bool m_supportsBuilding = true;
bool m_isEditModePreferred = true;
bool m_shuttingDown = false;
std::function<BuildSystem *(BuildConfiguration *)> m_buildSystemCreator;
std::unique_ptr<IDocument> m_document;
std::vector<std::unique_ptr<IDocument>> m_extraProjectDocuments;
std::unique_ptr<ProjectNode> m_rootProjectNode;
std::unique_ptr<ContainerNode> m_containerNode;
std::vector<std::unique_ptr<Target>> m_targets;
Target *m_activeTarget = nullptr;
EditorConfiguration m_editorConfiguration;
Context m_projectLanguages;
Store m_pluginSettings;
std::unique_ptr<Internal::UserFileAccessor> m_accessor;
QHash<Id, QPair<QString, std::function<void()>>> m_generators;
std::function<Tasks(const Kit *)> m_issuesGenerator;
Tasks m_tasks;
PerProjectProjectExplorerSettings m_projectExplorerSettings{q};
QString m_buildSystemName;
QString m_displayName;
MacroExpander m_macroExpander;
FilePath m_rootProjectDirectory;
mutable QList<const Node *> m_sortedNodeList;
Store m_extraData;
QList<Store> m_vanishedTargets;
};
ProjectPrivate::~ProjectPrivate()
{
for (const Task &t : std::as_const(m_tasks))
TaskHub::removeTask(t);
// Make sure our root node is null when deleting the actual node
std::unique_ptr<ProjectNode> oldNode = std::move(m_rootProjectNode);
}
Project::Project(const QString &mimeType, const FilePath &fileName)
: d(new ProjectPrivate(this))
{
d->m_document = std::make_unique<ProjectDocument>(mimeType, fileName, this);
DocumentManager::addDocument(d->m_document.get());
d->m_macroExpander.setDisplayName(::PE::Tr::tr("Project"));
d->m_macroExpander.registerVariable("Project:Name", ::PE::Tr::tr("Project Name"), [this] {
return displayName();
});
// Only set up containernode after d is set so that it will find the project directory!
d->m_containerNode = std::make_unique<ContainerNode>(this);
KitManager *km = KitManager::instance();
connect(km, &KitManager::kitUpdated, this, &Project::handleKitUpdated);
connect(km, &KitManager::kitRemoved, this, &Project::handleKitRemoval);
ProjectExplorerSettings::registerCallback(
this, &ProjectExplorerSettings::syncRunConfigurations, [this] {
syncRunConfigurations(false);
});
}
Project::~Project()
{
delete d;
}
void Project::handleKitUpdated(Kit *k)
{
for (const std::unique_ptr<Target> &target : d->m_targets) {
if (k == target->kit()) {
target->handleKitUpdated();
break;
}
}
}
void Project::handleKitRemoval(Kit *k)
{
for (const std::unique_ptr<Target> &target : d->m_targets) {
if (k == target->kit()) {
d->m_vanishedTargets.append(target->toMap());
removeTarget(target.get());
emit vanishedTargetsChanged();
break;
}
}
}
void Project::syncRunConfigurations(bool force)
{
const Key key = "RcSync";
const QVariant projectSyncSetting = namedSettings(key);
const SyncRunConfigs prevSyncSetting = projectSyncSetting.isValid()
? static_cast<SyncRunConfigs>(projectSyncSetting.toInt()) : SyncRunConfigs::Off;
const SyncRunConfigs curSyncSetting
= ProjectExplorerSettings::get(this).syncRunConfigurations.value();
setNamedSettings(key, static_cast<int>(curSyncSetting));
// Invariant: If the setting has not changed, everything is as it should be.
if (curSyncSetting == prevSyncSetting && !force)
return;
// Nothing to do here: Everything stays as it is for now and will slowly
// go out of sync over time.
if (curSyncSetting == SyncRunConfigs::Off)
return;
if (curSyncSetting == SyncRunConfigs::SameKit) {
// Nothing to do here: Everything stays as it is for now and will slowly
// go out of sync over time.
if (prevSyncSetting == SyncRunConfigs::All && !force)
return;
}
// Collect all run configurations.
QList<RunConfiguration *> allRcs;
for (Target * const t : targets()) {
for (BuildConfiguration * const bc : t->buildConfigurations())
allRcs << bc->runConfigurations();
}
while (!allRcs.isEmpty()) {
RunConfiguration * const rc = allRcs.takeFirst();
const QList<BuildConfiguration *> syncableBcs = rc->syncableBuildConfigurations();
// At the end of this loop, all build configurations in syncableBcs should
// have a run configuration with the same uniqueId() as rc, unless the
// source and target build configurations are incompatible with regards
// to the run configuration, for instance because the target factory
// cannot create that type of run configuration.
for (BuildConfiguration * const bc : std::as_const(syncableBcs)) {
// First we check whether this build configuration already has a counterpart
// to our run configuration. In that case, we don't need to make a copy.
bool needsCloning = true;
for (RunConfiguration * const otherRc : bc->runConfigurations()) {
// The run configurations do not refer to the same application.
if (otherRc->buildKey() != rc->buildKey())
continue;
// These run configs were already linked together in the past, in which
// case we keep this relationship alive, or they were both auto-created
// and are still in pristine state, which means they are identical.
// This also means that the other run configuration does not have to be
// handled on its own anymore.
if (otherRc->uniqueId() == rc->uniqueId()
&& (rc->uniqueId() != rc->buildKey()
|| (!rc->isCustomized() && !otherRc->isCustomized()))) {
const bool removed = allRcs.removeOne(otherRc);
QTC_CHECK(removed);
if (rc->isCustomized() || otherRc->isCustomized()) {
otherRc->cloneFromOther(rc);
otherRc->setDisplayName(rc->displayName());
}
needsCloning = false;
break;
}
// These run configs are equal, so we assume they should get linked.
if (rc->equals(otherRc)) {
otherRc->setUniqueId(rc->uniqueId());
const bool removed = allRcs.removeOne(otherRc);
QTC_CHECK(removed);
needsCloning = false;
break;
}
}
if (needsCloning) {
if (const auto clone = rc->clone(bc))
bc->addRunConfiguration(clone, NameHandling::Keep);
}
}
}
}
PerProjectProjectExplorerSettings &Project::projectExplorerSettings() const
{
return d->m_projectExplorerSettings;
}
QString Project::buildSystemName() const
{
return d->m_buildSystemName;
}
QString Project::displayName() const
{
return d->m_displayName;
}
Id Project::type() const
{
QTC_CHECK(d->m_id.isValid());
return d->m_id;
}
void Project::markAsShuttingDown()
{
d->m_shuttingDown = true;
}
bool Project::isShuttingDown() const
{
return d->m_shuttingDown;
}
QString Project::mimeType() const
{
return d->m_document->mimeType();
}
bool Project::canBuildProducts() const
{
return d->m_canBuildProducts;
}
BuildSystem *Project::createBuildSystem(BuildConfiguration *bc) const
{
QTC_ASSERT(d->m_buildSystemCreator, return nullptr);
return d->m_buildSystemCreator(bc);
}
FilePath Project::projectFilePath() const
{
QTC_ASSERT(d->m_document, return {});
return d->m_document->filePath();
}
void Project::addTarget(std::unique_ptr<Target> &&t)
{
auto pointer = t.get();
QTC_ASSERT(t && !contains(d->m_targets, pointer), return);
QTC_ASSERT(!target(t->kit()), return);
Q_ASSERT(t->project() == this);
// add it
d->m_targets.emplace_back(std::move(t));
emit addedTarget(pointer);
// check activeTarget:
if (!activeTarget())
setActiveTarget(pointer, SetActive::Cascade);
}
Target *Project::addTargetForDefaultKit()
{
return addTargetForKit(KitManager::defaultKit());
}
Target *Project::addTargetForKit(Kit *kit)
{
if (!kit || target(kit))
return nullptr;
auto t = Target::create(this, kit);
Target *pointer = t.get();
t->updateDefaultBuildConfigurations();
QTC_ASSERT(!t->buildConfigurations().isEmpty(), return nullptr);
addTarget(std::move(t));
return pointer;
}
void Project::removeTarget(Target *target)
{
QTC_ASSERT(target, return);
QTC_ASSERT(contains(d->m_targets, target), return);
if (BuildManager::isBuilding(target))
return;
target->markAsShuttingDown();
for (BuildConfiguration * const bc : target->buildConfigurations())
emit ProjectManager::instance()->aboutToRemoveBuildConfiguration(bc);
emit aboutToRemoveTarget(target);
auto keep = take(d->m_targets, target);
if (target == d->m_activeTarget) {
Target *newActiveTarget = (d->m_targets.size() == 0 ? nullptr : d->m_targets.at(0).get());
setActiveTarget(newActiveTarget, SetActive::Cascade);
}
emit removedTarget(target);
}
const QList<Target *> Project::targets() const
{
return toRawPointer<QList>(d->m_targets);
}
Target *Project::activeTarget() const
{
return d->m_activeTarget;
}
void Project::setActiveTargetHelper(Target *target)
{
if (d->m_activeTarget == target)
return;
// Allow to set nullptr just before the last target is removed or when no target exists.
if ((!target && d->m_targets.size() == 0) ||
(target && contains(d->m_targets, target))) {
d->m_activeTarget = target;
emit activeTargetChanged(d->m_activeTarget);
emit activeBuildConfigurationChanged(target ? target->activeBuildConfiguration() : nullptr);
if (this == ProjectManager::startupProject()) {
emit ProjectManager::instance()->activeBuildConfigurationChanged(
activeBuildConfiguration());
}
if (this == ProjectTree::currentProject()) {
emit ProjectManager::instance()->currentBuildConfigurationChanged(
activeBuildConfiguration());
}
ProjectExplorerPlugin::updateActions();
}
}
bool Project::needsInitialExpansion() const
{
return d->m_needsInitialExpansion;
}
void Project::setNeedsInitialExpansion(bool needsExpansion)
{
d->m_needsInitialExpansion = needsExpansion;
}
void Project::setExtraProjectFiles(const QSet<FilePath> &projectDocumentPaths,
const DocGenerator &docGenerator,
const DocUpdater &docUpdater)
{
QSet<FilePath> uniqueNewFiles = projectDocumentPaths;
uniqueNewFiles.remove(projectFilePath()); // Make sure to never add the main project file!
QSet<FilePath> existingWatches = transform<QSet>(d->m_extraProjectDocuments,
&IDocument::filePath);
const QSet<FilePath> toAdd = uniqueNewFiles - existingWatches;
const QSet<FilePath> toRemove = existingWatches - uniqueNewFiles;
Utils::erase(d->m_extraProjectDocuments, [&toRemove](const std::unique_ptr<IDocument> &d) {
return toRemove.contains(d->filePath());
});
if (docUpdater) {
for (const auto &doc : std::as_const(d->m_extraProjectDocuments))
docUpdater(doc.get());
}
QList<IDocument *> toRegister;
for (const FilePath &p : toAdd) {
if (docGenerator) {
std::unique_ptr<IDocument> doc = docGenerator(p);
QTC_ASSERT(doc, continue);
d->m_extraProjectDocuments.push_back(std::move(doc));
} else {
auto document = std::make_unique<ProjectDocument>(d->m_document->mimeType(), p, this);
toRegister.append(document.get());
d->m_extraProjectDocuments.emplace_back(std::move(document));
}
}
DocumentManager::addDocuments(toRegister);
}
void Project::updateExtraProjectFiles(const QSet<FilePath> &projectDocumentPaths,
const DocUpdater &docUpdater)
{
for (const FilePath &fp : projectDocumentPaths) {
for (const auto &doc : d->m_extraProjectDocuments) {
if (doc->filePath() == fp) {
docUpdater(doc.get());
break;
}
}
}
}
void Project::updateExtraProjectFiles(const DocUpdater &docUpdater)
{
for (const auto &doc : std::as_const(d->m_extraProjectDocuments))
docUpdater(doc.get());
}
Target *Project::target(Id id) const
{
return findOrDefault(d->m_targets, equal(&Target::id, id));
}
Target *Project::target(Kit *k) const
{
return findOrDefault(d->m_targets, equal(&Target::kit, k));
}
void Project::setActiveTarget(Target *target, SetActive cascade)
{
if (isShuttingDown())
return;
setActiveTargetHelper(target);
if (!target) // never cascade setting no target
return;
if (cascade != SetActive::Cascade || !ProjectManager::isProjectConfigurationCascading())
return;
Utils::Id kitId = target->kit()->id();
for (Project *otherProject : ProjectManager::projects()) {
if (otherProject == this)
continue;
if (Target *otherTarget = Utils::findOrDefault(otherProject->targets(),
[kitId](Target *t) { return t->kit()->id() == kitId; }))
otherProject->setActiveTargetHelper(otherTarget);
}
}
void Project::setActiveBuildConfiguration(BuildConfiguration *bc, SetActive cascade)
{
QTC_ASSERT(bc->project() == this, return);
if (bc != bc->target()->activeBuildConfiguration())
bc->target()->setActiveBuildConfiguration(bc, cascade);
if (bc->target() != activeTarget())
setActiveTarget(bc->target(), cascade);
}
Kit *Project::activeKit() const
{
return activeTarget() ? activeTarget()->kit() : nullptr;
}
RunConfiguration *Project::activeRunConfiguration() const
{
return activeTarget() ? activeTarget()->activeRunConfiguration() : nullptr;
}
BuildConfiguration *Project::activeBuildConfiguration() const
{
return activeTarget() ? activeTarget()->activeBuildConfiguration() : nullptr;
}
DeployConfiguration *Project::activeDeployConfiguration() const
{
return activeTarget() ? activeTarget()->activeDeployConfiguration() : nullptr;
}
BuildSystem *Project::activeBuildSystem() const
{
return activeTarget() ? activeTarget()->buildSystem() : nullptr;
}
bool Project::isParsing() const
{
if (BuildSystem * const bs = activeBuildSystem())
return bs->isParsing() || bs->isWaitingForParse();
return false;
}
QList<BuildConfiguration *> Project::allBuildConfigurations() const
{
QList<BuildConfiguration *> buildConfigs;
for (Target * const t : targets())
buildConfigs << t->buildConfigurations();
return buildConfigs;
}
void Project::setIssuesGenerator(const std::function<Tasks(const Kit *)> &generator)
{
d->m_issuesGenerator = generator;
}
QList<Store> Project::vanishedTargets() const
{
return d->m_vanishedTargets;
}
void Project::removeVanishedTarget(int index)
{
QTC_ASSERT(index >= 0 && index < d->m_vanishedTargets.size(), return);
d->m_vanishedTargets.removeAt(index);
emit vanishedTargetsChanged();
}
void Project::removeVanishedTarget(const Utils::Store &store)
{
if (d->m_vanishedTargets.removeAll(store))
emit vanishedTargetsChanged();
}
void Project::removeAllVanishedTargets()
{
d->m_vanishedTargets.clear();
emit vanishedTargetsChanged();
}
Target *Project::createKitAndTargetFromStore(const Utils::Store &store)
{
Id deviceTypeId = Id::fromSetting(store.value(Target::deviceTypeKey()));
if (!deviceTypeId.isValid())
deviceTypeId = Constants::DESKTOP_DEVICE_TYPE;
const QString formerKitName = store.value(Target::displayNameKey()).toString();
Kit *k = KitManager::registerKit(
[deviceTypeId, &formerKitName](Kit *kit) {
const QString kitName = makeUniquelyNumbered(formerKitName,
transform(KitManager::kits(),
&Kit::unexpandedDisplayName));
kit->setUnexpandedDisplayName(kitName);
RunDeviceTypeKitAspect::setDeviceTypeId(kit, deviceTypeId);
kit->setup();
});
QTC_ASSERT(k, return nullptr);
auto t = Target::create(this, k);
if (!t->fromMap(store))
return nullptr;
if (t->buildConfigurations().isEmpty())
return nullptr;
auto pointer = t.get();
addTarget(std::move(t));
return pointer;
}
Tasks Project::projectIssues(const Kit *k) const
{
if (!k->isValid())
return {createTask(Task::TaskType::Error, ::PE::Tr::tr("Kit is not valid."))};
if (const Task t = checkBuildDevice(k, projectFilePath()); !t.isNull())
return {t};
if (d->m_issuesGenerator)
return d->m_issuesGenerator(k);
return {};
}
Task Project::checkBuildDevice(const Kit *k, const Utils::FilePath &projectFile)
{
IDeviceConstPtr buildDevice = BuildDeviceKitAspect::device(k);
if (!buildDevice)
return createTask(Task::TaskType::Error, ::PE::Tr::tr("Kit has no build device."));
const Utils::Result<> supportsBuilding = buildDevice->supportsBuildingProject(
projectFile.parentDir());
if (!supportsBuilding) {
return createTask(
Task::TaskType::Error,
::PE::Tr::tr("Build device \"%2\" cannot handle project file \"%1\".")
.arg(projectFile.toUserOutput())
.arg(buildDevice->displayName())
+ QString(" ") + supportsBuilding.error());
}
return {};
}
void Project::addTask(const Task &task)
{
d->m_tasks << task;
TaskHub::addTask(task);
}
bool Project::copySteps(Target *sourceTarget, Kit *targetKit)
{
QTC_ASSERT(targetKit, return false);
bool fatalError = false;
QStringList buildconfigurationError;
QStringList deployconfigurationError;
QStringList runconfigurationError;
std::unique_ptr<Target> tempTarget;
Target *newTarget = target(targetKit->id());
if (!newTarget) {
tempTarget = Target::create(this, targetKit);
newTarget = tempTarget.get();
}
for (BuildConfiguration *sourceBc : sourceTarget->buildConfigurations()) {
BuildConfiguration *newBc = sourceBc->clone(newTarget);
if (!newBc) {
buildconfigurationError << sourceBc->displayName();
continue;
}
newBc->setDisplayName(sourceBc->displayName());
newBc->setBuildDirectory(
BuildConfiguration::rawBuildDirectoryFromTemplate(targetKit, projectFilePath()));
newTarget->addBuildConfiguration(newBc);
if (sourceTarget->activeBuildConfiguration() == sourceBc)
newTarget->setActiveBuildConfiguration(newBc, SetActive::NoCascade);
}
if (!newTarget->activeBuildConfiguration()) {
QList<BuildConfiguration *> bcs = newTarget->buildConfigurations();
if (!bcs.isEmpty())
newTarget->setActiveBuildConfiguration(bcs.first(), SetActive::NoCascade);
}
if (buildconfigurationError.count() == sourceTarget->buildConfigurations().count())
fatalError = true;
if (fatalError) {
// That could be a more granular error message
QMessageBox::critical(ICore::dialogParent(),
::PE::Tr::tr("Incompatible Kit"),
::PE::Tr::tr("Kit %1 is incompatible with kit %2.")
.arg(sourceTarget->kit()->displayName())
.arg(targetKit->displayName()));
} else if (!buildconfigurationError.isEmpty()
|| !deployconfigurationError.isEmpty()
|| ! runconfigurationError.isEmpty()) {
QString error;
if (!buildconfigurationError.isEmpty())
error += ::PE::Tr::tr("Build configurations:") + QLatin1Char('\n')
+ buildconfigurationError.join(QLatin1Char('\n'));
if (!deployconfigurationError.isEmpty()) {
if (!error.isEmpty())
error.append(QLatin1Char('\n'));
error += ::PE::Tr::tr("Deploy configurations:") + QLatin1Char('\n')
+ deployconfigurationError.join(QLatin1Char('\n'));
}
if (!runconfigurationError.isEmpty()) {
if (!error.isEmpty())
error.append(QLatin1Char('\n'));
error += ::PE::Tr::tr("Run configurations:") + QLatin1Char('\n')
+ runconfigurationError.join(QLatin1Char('\n'));
}
QMessageBox msgBox(ICore::dialogParent());
msgBox.setIcon(QMessageBox::Warning);
msgBox.setWindowTitle(::PE::Tr::tr("Partially Incompatible Kit"));
msgBox.setText(::PE::Tr::tr("Some configurations could not be copied."));
msgBox.setDetailedText(error);
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
fatalError = msgBox.exec() != QDialog::Accepted;
}
if (fatalError)
return false;
addTarget(std::move(tempTarget));
return true;
}
bool Project::copySteps(const Utils::Store &store, Kit *targetKit)
{
Target *t = target(targetKit->id());
if (!t) {
auto t = Target::create(this, targetKit);
if (!t->fromMap(store))
return false;
if (t->buildConfigurations().isEmpty())
return false;
addTarget(std::move(t));
return true;
}
return t->addConfigurationsFromMap(store, /*setActiveConfigurations=*/false);
}
void Project::setDisplayName(const QString &name)
{
if (name == d->m_displayName)
return;
d->m_displayName = name;
emit displayNameChanged();
}
void Project::setType(Id id)
{
QTC_ASSERT(!d->m_id.isValid(), return); // Id may not change ever!
d->m_id = id;
}
void Project::setRootProjectNode(std::unique_ptr<ProjectNode> &&root)
{
QTC_ASSERT(d->m_rootProjectNode.get() != root.get() || !root, return);
if (root && root->isEmpty()) {
// Something went wrong with parsing: At least the project file needs to be
// shown so that the user can fix the breakage.
// Do not leak root and use default project tree in this case.
root.reset();
}
if (root) {
ProjectTree::applyTreeManager(root.get(), ProjectTree::AsyncPhase);
ProjectTree::applyTreeManager(root.get(), ProjectTree::FinalPhase);
root->setParentFolderNode(d->m_containerNode.get());
}
std::unique_ptr<ProjectNode> oldNode = std::move(d->m_rootProjectNode);
d->m_rootProjectNode = std::move(root);
if (oldNode || d->m_rootProjectNode)
handleSubTreeChanged(d->m_containerNode.get());
}
void Project::handleSubTreeChanged(FolderNode *node)
{
QList<const Node *> nodeList;
if (d->m_rootProjectNode) {
d->m_rootProjectNode->forEachGenericNode([&nodeList](const Node *n) {
nodeList.append(n);
});
sort(nodeList, &nodeLessThan);
}
d->m_sortedNodeList = nodeList;
ProjectTree::emitSubtreeChanged(node);
emit fileListChanged();
}
void Project::saveSettings()
{
emit aboutToSaveSettings();
if (!d->m_accessor)
d->m_accessor = std::make_unique<Internal::UserFileAccessor>(this);
if (!targets().isEmpty()) {
Store map;
toMap(map);
d->m_accessor->saveSettings(map);
}
}
Project::RestoreResult Project::restoreSettings(QString *errorMessage)
{
if (!KitManager::waitForLoaded()) {
if (errorMessage)
*errorMessage = Tr::tr("Could not load kits in a reasonable amount of time.");
return RestoreResult::Error;
}
if (!d->m_accessor)
d->m_accessor = std::make_unique<Internal::UserFileAccessor>(this);
Store map = d->m_accessor->restoreSettings();
RestoreResult result = fromMap(map, errorMessage);
if (result == RestoreResult::Ok)
emit settingsLoaded();
syncRunConfigurations(false);
return result;
}
/*!
* Returns a sorted list of all files matching the predicate \a filter.
*/
FilePaths Project::files(const NodeMatcher &filter) const
{
QTC_ASSERT(filter, return {});
FilePaths result;
if (d->m_sortedNodeList.empty() && filter(containerNode()))
result.append(projectFilePath());
FilePath lastAdded;
for (const Node *n : std::as_const(d->m_sortedNodeList)) {
if (!filter(n))
continue;
// Remove duplicates:
const FilePath path = n->filePath();
if (path == lastAdded)
continue; // skip duplicates
lastAdded = path;
result.append(path);
}
return result;
}
/*!
Serializes all data into a Store.
This map is then saved in the .user file of the project.
Just put all your data into the map.
\note Do not forget to call your base class' toMap function.
\note Do not forget to call setActiveBuildConfiguration when
creating new build configurations.
*/
void Project::toMap(Store &map) const
{
const QList<Target *> ts = targets();
const QList<Store> vts = vanishedTargets();
map.insert(ACTIVE_TARGET_KEY, ts.indexOf(d->m_activeTarget));
map.insert(TARGET_COUNT_KEY, ts.size() + vts.size());
int index = 0;
for (Target *t : ts) {
map.insert(numberedKey(TARGET_KEY_PREFIX, index), variantFromStore(t->toMap()));
++index;
}
for (const Store &store : vts) {
map.insert(numberedKey(TARGET_KEY_PREFIX, index), variantFromStore(store));
++index;
}
map.insert(EDITOR_SETTINGS_KEY, variantFromStore(d->m_editorConfiguration.toMap()));
if (!d->m_pluginSettings.isEmpty())
map.insert(PLUGIN_SETTINGS_KEY, variantFromStore(d->m_pluginSettings));
}
/*!
Returns the directory that contains the project.
This includes the absolute path.
*/
FilePath Project::projectDirectory() const
{
return projectFilePath().absolutePath();
}
void Project::changeRootProjectDirectory()
{
FilePath rootPath = FileUtils::getExistingDirectory(::PE::Tr::tr("Select the Root Directory"),
rootProjectDirectory(),
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
if (rootPath != d->m_rootProjectDirectory) {
d->m_rootProjectDirectory = rootPath;
setNamedSettings(Constants::PROJECT_ROOT_PATH_KEY, d->m_rootProjectDirectory.toUrlishString());
emit rootProjectDirectoryChanged();
}
}
/*!
Returns the common root directory that contains all files which belong to a project.
*/
FilePath Project::rootProjectDirectory() const
{
if (!d->m_rootProjectDirectory.isEmpty())
return d->m_rootProjectDirectory;
return projectDirectory();
}
ProjectNode *Project::rootProjectNode() const
{
return d->m_rootProjectNode.get();
}
ContainerNode *Project::containerNode() const
{
return d->m_containerNode.get();
}
Project::RestoreResult Project::fromMap(const Store &map, QString *errorMessage)
{
Q_UNUSED(errorMessage)
if (map.contains(EDITOR_SETTINGS_KEY)) {
Store values = storeFromVariant(map.value(EDITOR_SETTINGS_KEY));
d->m_editorConfiguration.fromMap(values);
}
if (map.contains(PLUGIN_SETTINGS_KEY))
d->m_pluginSettings = storeFromVariant(map.value(PLUGIN_SETTINGS_KEY));
bool ok;
int maxI(map.value(TARGET_COUNT_KEY, 0).toInt(&ok));
if (!ok || maxI < 0)
maxI = 0;
int active = map.value(ACTIVE_TARGET_KEY, 0).toInt(&ok);
if (!ok || active < 0 || active >= maxI)
active = 0;
if (active >= 0 && active < maxI)
createTargetFromMap(map, active); // sets activeTarget since it is the first target created!
for (int i = 0; i < maxI; ++i) {
if (i == active) // already covered!
continue;
createTargetFromMap(map, i);
}
d->m_rootProjectDirectory = FilePath::fromString(
namedSettings(Constants::PROJECT_ROOT_PATH_KEY).toString());
d->m_projectExplorerSettings.restore();
return RestoreResult::Ok;
}
void Project::createTargetFromMap(const Store &map, int index)
{
const Key key = numberedKey(TARGET_KEY_PREFIX, index);
if (!map.contains(key))
return;
const Store targetMap = storeFromVariant(map.value(key));
Id id = idFromMap(targetMap);
if (target(id)) {
qWarning("Warning: Duplicated target id found, not restoring second target with id '%s'. Continuing.",
qPrintable(id.toString()));
return;
}
Kit *k = KitManager::kit(id);
if (!k) {
d->m_vanishedTargets.append(targetMap);
const QString formerKitName = targetMap.value(Target::displayNameKey()).toString();
TaskHub::addTask<BuildSystemTask>(
Task::Warning,
::PE::Tr::tr(
"Project \"%1\" was configured for "
"kit \"%2\" with id %3, which does not exist anymore. You can create a new kit "
"or copy the steps of the vanished kit to another kit in %4 mode.")
.arg(displayName(), formerKitName, id.toString(), Tr::tr("Projects")));
return;
}
auto t = Target::create(this, k);
if (!t->fromMap(targetMap))
return;
if (t->buildConfigurations().isEmpty())
return;
addTarget(std::move(t));
}
EditorConfiguration *Project::editorConfiguration() const
{
return &d->m_editorConfiguration;
}
bool Project::isKnownFile(const FilePath &filename) const
{
if (d->m_sortedNodeList.empty())
return filename == projectFilePath();
const FileNode element(filename, FileType::Unknown);
return std::binary_search(std::begin(d->m_sortedNodeList), std::end(d->m_sortedNodeList),
&element, nodeLessThan);
}
const Node *Project::nodeForFilePath(const FilePath &filePath,
const NodeMatcher &extraMatcher) const
{
const QList<const Node *> nodes = nodesForFilePath(filePath, extraMatcher);
return nodes.isEmpty() ? nullptr : nodes.first();
}
QList<const Node *> Project::nodesForFilePath(const Utils::FilePath &filePath,
const NodeMatcher &extraMatcher) const
{
const FileNode dummy(filePath, FileType::Unknown);
const auto range = std::equal_range(d->m_sortedNodeList.cbegin(), d->m_sortedNodeList.cend(),
&dummy, &nodeLessThan);
QList<const Node *> nodes;
for (auto it = range.first; it != range.second; ++it) {
if ((*it)->filePath() == filePath && (!extraMatcher || extraMatcher(*it)))
nodes << *it;
}
return nodes;
}
ProjectNode *Project::productNodeForFilePath(
const Utils::FilePath &filePath, const NodeMatcher &extraMatcher) const
{
const QList<const Node *> fileNodes = nodesForFilePath(filePath, extraMatcher);
QList<ProjectNode *> candidates;
for (const Node * const fileNode : fileNodes) {
for (ProjectNode *projectNode = fileNode->parentProjectNode(); projectNode;
projectNode = projectNode->parentProjectNode()) {
if (projectNode->isProduct()) {
// If there are several candidates, we prefer real products to pseudo-products.
// See QTCREATORBUG-33224. For now, we assume this is what all callers want.
// If the need arises, we can make this configurable.
if (projectNode->productType() == ProductType::App
|| projectNode->productType() == ProductType::Lib) {
return projectNode;
}
candidates << projectNode;
}
}
}
return candidates.isEmpty() ? nullptr : candidates.first();
}
FilePaths Project::binariesForSourceFile(const FilePath &sourceFile) const
{
if (!rootProjectNode())
return {};
const QList<Node *> fileNodes = rootProjectNode()->findNodes([&sourceFile](Node *n) {
return n->filePath() == sourceFile;
});
FilePaths binaries;
for (const Node * const fileNode : fileNodes) {
for (ProjectNode *projectNode = fileNode->parentProjectNode(); projectNode;
projectNode = projectNode->parentProjectNode()) {
if (!projectNode->isProduct())
continue;
if (projectNode->productType() == ProductType::App
|| projectNode->productType() == ProductType::Lib) {
const QList<Node *> binaryNodes = projectNode->findNodes([](Node *n) {
return n->asFileNode() && (n->asFileNode()->fileType() == FileType::App
|| n->asFileNode()->fileType() == FileType::Lib);
});
binaries << Utils::transform(binaryNodes, &Node::filePath);
}
break;
}
}
return binaries;
}
void Project::setProjectLanguages(Context language)
{
if (d->m_projectLanguages == language)
return;
d->m_projectLanguages = language;
emit projectLanguagesUpdated();
}
void Project::addProjectLanguage(Id id)
{
Context lang = projectLanguages();
int pos = lang.indexOf(id);
if (pos < 0)
lang.add(id);
setProjectLanguages(lang);
}
void Project::removeProjectLanguage(Id id)
{
Context lang = projectLanguages();
int pos = lang.indexOf(id);
if (pos >= 0)
lang.removeAt(pos);
setProjectLanguages(lang);
}
void Project::setProjectLanguage(Id id, bool enabled)
{
if (enabled)
addProjectLanguage(id);
else
removeProjectLanguage(id);
}
void Project::setHasMakeInstallEquivalent(bool enabled)
{
d->m_hasMakeInstallEquivalent = enabled;
}
void Project::setSupportsBuilding(bool value)
{
d->m_supportsBuilding = value;
}
void Project::setBuildSystemName(const QString &name)
{
d->m_buildSystemName = name;
}
Task Project::createTask(Task::TaskType type, const QString &description)
{
return Task(type, description, FilePath(), -1, Id());
}
void Project::setBuildSystemCreator(const std::function<BuildSystem *(BuildConfiguration *)> &creator)
{
d->m_buildSystemCreator = creator;
}
Context Project::projectContext() const
{
return Context(d->m_id);
}
Context Project::projectLanguages() const
{
return d->m_projectLanguages;
}
QVariant Project::namedSettings(const Key &name) const
{
return d->m_pluginSettings.value(name);
}
void Project::setNamedSettings(const Key &name, const QVariant &value)
{
if (value.isNull())
d->m_pluginSettings.remove(name);
else
d->m_pluginSettings.insert(name, value);
}
void Project::setAdditionalEnvironment(const EnvironmentItems &envItems)
{
setNamedSettings(PROJECT_ENV_KEY, EnvironmentItem::toStringList(envItems));
emit environmentChanged();
}
EnvironmentItems Project::additionalEnvironment() const
{
return EnvironmentItem::fromStringList(namedSettings(PROJECT_ENV_KEY).toStringList());
}
bool Project::needsConfiguration() const
{
return d->m_targets.size() == 0;
}
bool Project::supportsBuilding() const
{
return d->m_supportsBuilding;
}
void Project::configureAsExampleProject(Kit * /*kit*/)
{
}
bool Project::hasMakeInstallEquivalent() const
{
return d->m_hasMakeInstallEquivalent;
}
void Project::setup(const QList<BuildInfo> &infoList)
{
for (const BuildInfo &info : infoList)
setup(info);
}
BuildConfiguration *Project::setup(const BuildInfo &info)
{
Kit *k = KitManager::kit(info.kitId);
if (!k)
return nullptr;
Target *t = target(k);
std::unique_ptr<Target> newTarget;
if (!t) {
newTarget = Target::create(this, k);
t = newTarget.get();
}
QTC_ASSERT(t, return nullptr);
BuildConfiguration *bc = nullptr;
if (info.factory) {
bc = info.factory->create(t, info);
if (bc)
t->addBuildConfiguration(bc);
}
if (newTarget)
addTarget(std::move(newTarget));
return bc;
}
MacroExpander *Project::macroExpander() const
{
return &d->m_macroExpander;
}
ProjectNode *Project::findNodeForBuildKey(const QString &buildKey) const
{
if (!d->m_rootProjectNode)
return nullptr;
return d->m_rootProjectNode->findProjectNode([buildKey](const ProjectNode *node) {
return node->buildKey() == buildKey;
});
}
ProjectImporter *Project::projectImporter() const
{
return nullptr;
}
void Project::setCanBuildProducts()
{
d->m_canBuildProducts = true;
}
void Project::setIsEditModePreferred(bool preferEditMode)
{
d->m_isEditModePreferred = preferEditMode;
}
void Project::setExtraData(const Key &key, const QVariant &data)
{
d->m_extraData.insert(key, data);
}
QVariant Project::extraData(const Key &key) const
{
return d->m_extraData.value(key);
}
QStringList Project::availableQmlPreviewTranslations(QString *errorMessage)
{
const auto projectDirectory = rootProjectDirectory().toFileInfo().absoluteFilePath();
const QDir languageDirectory(projectDirectory + "/i18n");
const auto qmFiles = languageDirectory.entryList({"qml_*.qm"});
if (qmFiles.isEmpty() && errorMessage)
errorMessage->append(::PE::Tr::tr("Could not find any qml_*.qm file at \"%1\"")
.arg(languageDirectory.absolutePath()));
return transform(qmFiles, [](const QString &qmFile) {
const int localeStartPosition = qmFile.lastIndexOf("_") + 1;
const int localeEndPosition = qmFile.size() - QString(".qm").size();
const QString locale = qmFile.left(localeEndPosition).mid(localeStartPosition);
return locale;
});
}
QList<IDocument *> Project::modifiedDocuments() const
{
QList<IDocument *> modifiedProjectDocuments;
for (IDocument *doc : DocumentModel::openedDocuments()) {
if (doc->isModified() && isKnownFile(doc->filePath()))
modifiedProjectDocuments.append(doc);
}
return modifiedProjectDocuments;
}
bool Project::isModified() const
{
return !modifiedDocuments().isEmpty();
}
bool Project::isEditModePreferred() const
{
return d->m_isEditModePreferred;
}
void Project::registerGenerator(Utils::Id id, const QString &displayName,
const std::function<void ()> &runner)
{
d->m_generators.insert(id, qMakePair(displayName, runner));
}
const QList<QPair<Id, QString>> Project::allGenerators() const
{
QList<QPair<Id, QString>> generators;
for (auto it = d->m_generators.cbegin(); it != d->m_generators.cend(); ++it)
generators << qMakePair(it.key(), it.value().first);
if (const BuildSystem * const bs = activeBuildSystem())
generators += bs->generators();
return generators;
}
void Project::runGenerator(Utils::Id id)
{
const auto it = d->m_generators.constFind(id);
if (it != d->m_generators.constEnd()) {
it.value().second();
return;
}
if (BuildSystem * const bs = activeBuildSystem())
bs->runGenerator(id);
}
void Project::addVariablesToMacroExpander(const QByteArray &prefix,
const QString &descriptor,
MacroExpander *expander,
const std::function<Project *()> &projectGetter)
{
const auto kitGetter = [projectGetter] { return ProjectExplorer::activeKit(projectGetter()); };
const auto bcGetter = [projectGetter] { return activeBuildConfig(projectGetter()); };
const auto rcGetter = [projectGetter] { return activeRunConfig(projectGetter()); };
const QByteArray fullPrefix = (prefix.endsWith(':') ? prefix : prefix + ':');
const QByteArray prefixWithoutColon = fullPrefix.chopped(1);
expander->registerVariable(fullPrefix + "Name",
//: %1 is something like "Active project"
::PE::Tr::tr("%1: Name.").arg(descriptor),
[projectGetter]() -> QString {
if (const Project *const project = projectGetter())
return project->displayName();
return {};
});
expander->registerFileVariables(prefixWithoutColon,
//: %1 is something like "Active project"
::PE::Tr::tr("%1: Full path to main file.").arg(descriptor),
[projectGetter]() -> FilePath {
if (const Project *const project = projectGetter())
return project->projectFilePath();
return {};
});
expander->registerVariable(fullPrefix + "ProjectDirectory",
//: %1 is something like "Active project"
::PE::Tr::tr("%1: Full path to Project Directory.").arg(descriptor),
[projectGetter]() -> QString {
if (const Project *const project = projectGetter())
return project->projectDirectory().toUserOutput();
return {};
});
expander->registerVariable(fullPrefix + "Kit:Name",
//: %1 is something like "Active project"
::PE::Tr::tr("%1: The name of the active kit.").arg(descriptor),
[kitGetter]() -> QString {
if (const Kit *const kit = kitGetter())
return kit->displayName();
return {};
});
expander->registerVariable(fullPrefix + "BuildConfig:Name",
//: %1 is something like "Active project"
::PE::Tr::tr("%1: Name of the active build configuration.")
.arg(descriptor),
[bcGetter]() -> QString {
if (const BuildConfiguration *const bc = bcGetter())
return bc->displayName();
return {};
});
expander->registerVariable(fullPrefix + "BuildConfig:Type",
//: %1 is something like "Active project"
::PE::Tr::tr("%1: Type of the active build configuration.")
.arg(descriptor),
[bcGetter]() -> QString {
const BuildConfiguration *const bc = bcGetter();
const BuildConfiguration::BuildType type
= bc ? bc->buildType() : BuildConfiguration::Unknown;
return BuildConfiguration::buildTypeName(type);
});
expander->registerVariable(fullPrefix + "BuildConfig:Path",
//: %1 is something like "Active project"
::PE::Tr::tr("%1: Full build path of active build configuration.")
.arg(descriptor),
[bcGetter]() -> QString {
if (const BuildConfiguration *const bc = bcGetter())
return bc->buildDirectory().toUserOutput();
return {};
});
expander->registerPrefix(
fullPrefix + "BuildConfig:Env",
"USER",
//: %1 is something like "Active project"
::PE::Tr::tr("%1: Variables in the active build environment.").arg(descriptor),
[bcGetter](const QString &var) -> QString {
if (BuildConfiguration *const bc = bcGetter())
return bc->environment().expandedValueForKey(var);
return {};
});
expander->registerVariable(fullPrefix + "RunConfig:Name",
//: %1 is something like "Active project"
::PE::Tr::tr("%1: Name of the active run configuration.")
.arg(descriptor),
[rcGetter]() -> QString {
if (const RunConfiguration *const rc = rcGetter())
return rc->displayName();
return {};
});
expander->registerFileVariables(fullPrefix + "RunConfig:Executable",
//: %1 is something like "Active project"
::PE::Tr::tr("%1: Executable of the active run configuration.")
.arg(descriptor),
[rcGetter]() -> FilePath {
if (const RunConfiguration *const rc = rcGetter())
return rc->commandLine().executable();
return {};
});
expander->registerPrefix(
fullPrefix + "RunConfig:Env",
"USER",
//: %1 is something like "Active project"
::PE::Tr::tr("%1: Variables in the environment of the active run configuration.")
.arg(descriptor),
[rcGetter](const QString &var) -> QString {
if (const RunConfiguration *const rc = rcGetter()) {
if (const auto envAspect = rc->aspect<EnvironmentAspect>())
return envAspect->environment().expandedValueForKey(var);
}
return {};
});
expander->registerVariable(fullPrefix + "RunConfig:WorkingDir",
//: %1 is something like "Active project"
::PE::Tr::tr(
"%1: Working directory of the active run configuration.")
.arg(descriptor),
[rcGetter]() -> QString {
if (const RunConfiguration *const rc = rcGetter()) {
if (const auto wdAspect
= rc->aspect<WorkingDirectoryAspect>())
return wdAspect->workingDirectory().toUrlishString();
}
return {};
});
}
TextEditor::ProjectWrapper wrapProject(Project *p)
{
return TextEditor::ProjectWrapper(p, [](const void *p) {
return reinterpret_cast<const Project *>(p)->projectFilePath();
});
}
Project *unwrapProject(const TextEditor::ProjectWrapper &w)
{
return reinterpret_cast<Project *>(w.project());
}
static Project::QmlCodeModelInfoFromQtVersionHook s_qtversionExtraProjectInfoHook;
void Project::setQmlCodeModelInfoFromQtVersionHook(QmlCodeModelInfoFromQtVersionHook hook)
{
s_qtversionExtraProjectInfoHook = hook;
}
static void findAllQrcFiles(const FilePath &filePath, FilePaths &out)
{
filePath.iterateDirectory(
[&out](const FilePath &path) {
out.append(path.canonicalPath());
return IterationPolicy::Continue;
},
{{"*.qrc"}, QDir::Files});
}
static bool s_qmlCodeModelIsUsed = false;
void Project::setQmlCodeModelIsUsed()
{
s_qmlCodeModelIsUsed = true;
}
void Project::updateQmlCodeModel(Kit *kit, BuildConfiguration *bc)
{
if (!s_qmlCodeModelIsUsed)
return;
QmlCodeModelInfo projectInfo = gatherQmlCodeModelInfo(kit, bc);
emit ProjectManager::instance()->extraProjectInfoChanged(bc, projectInfo);
}
void Project::resetQmlCodeModel()
{
if (!s_qmlCodeModelIsUsed)
return;
emit ProjectManager::instance()->requestCodeModelReset();
}
static QStringList getQmlExtensions()
{
using namespace Utils::Constants;
static const QSet<QString> qmlTypeNames
= {QML_MIMETYPE, QBS_MIMETYPE, QMLPROJECT_MIMETYPE, QMLTYPES_MIMETYPE, QMLUI_MIMETYPE};
QStringList extensions;
for (const QString &mtn : qmlTypeNames) {
const MimeType mt = mimeTypeForName(mtn);
for (const QString &pattern : mt.globPatterns()) {
if (pattern.size() > 2 && pattern.at(0) == '*' && pattern.at(1) == '.')
extensions.append(pattern.mid(1));
}
}
return extensions;
}
QmlCodeModelInfo Project::gatherQmlCodeModelInfo(Kit *kit, BuildConfiguration *bc)
{
QmlCodeModelInfo projectInfo;
projectInfo.sourceFiles = files([extensions = getQmlExtensions()](const Node *n) {
if (!Project::SourceFiles(n))
return false;
const FileNode *fn = n->asFileNode();
return fn && fn->fileType() == FileType::QML
&& Utils::anyOf(extensions, [fp = fn->filePath()](const QString &ext) {
return fp.endsWith(ext);
});
});
if (projectInfo.sourceFiles.isEmpty())
return projectInfo;
projectInfo.qmlDumpEnvironment = Environment::systemEnvironment();
projectInfo.tryQmlDump = false;
FilePath baseDir;
auto addAppDir = [&baseDir, &projectInfo](const FilePath &mdir) {
const FilePath dir = mdir.cleanPath();
if (!baseDir.path().isEmpty()) {
const QString rDir = dir.relativePathFromDir(baseDir);
// do not add directories outside the build directory
// this might happen for example when we think an executable path belongs to
// a bundle, and we need to remove extra directories, but that was not the case
if (rDir.split(u'/').contains(QStringLiteral(u"..")))
return;
}
if (!projectInfo.applicationDirectories.contains(dir))
projectInfo.applicationDirectories.append(dir);
};
if (bc && bc->buildSystem()) {
// Append QML2_IMPORT_PATH if it is defined in build configuration.
// It enables qmlplugindump to correctly dump custom plugins or other dependent
// plugins that are not installed in default Qt qml installation directory.
projectInfo.qmlDumpEnvironment.appendOrSet("QML2_IMPORT_PATH",
bc->environment().expandedValueForKey(
"QML2_IMPORT_PATH"));
// Treat every target (library or application) in the build directory
FilePath dir = bc->buildDirectory();
baseDir = dir.absoluteFilePath();
addAppDir(dir);
// Qml loads modules from the following sources
// 1. The build directory of the executable
// 2. Any QML_IMPORT_PATH (environment variable) or IMPORT_PATH (parameter to qt_add_qml_module)
// 3. The Qt import path
// For an IDE things are a bit more complicated because source files might be edited,
// and the directory of the executable might be outdated.
// Here we try to get the directory of the executable, adding all targets
for (const BuildTargetInfo &target : bc->buildSystem()->applicationTargets()) {
if (target.targetFilePath.isEmpty())
continue;
FilePath dir = target.targetFilePath.parentDir();
projectInfo.applicationDirectories.append(dir);
// unfortunately the build directory of the executable where cmake puts the qml
// might be different than the directory of the executable:
if (HostOsInfo::isWindowsHost()) {
// On Windows systems QML type information is located one directory higher as we build
// in dedicated "debug" and "release" directories
addAppDir(dir.parentDir());
} else if (HostOsInfo::isMacHost()) {
// On macOS and iOS when building a bundle this is not the case and
// we have to go up up to three additional directories
// (BundleName.app/Contents/MacOS or BundleName.app/Contents for iOS)
if (dir.fileName() == u"MacOS")
dir = dir.parentDir();
if (dir.fileName() == u"Contents")
dir = dir.parentDir().parentDir();
addAppDir(dir);
}
}
// Let leaves do corrections.
bc->buildSystem()->updateQmlCodeModelInfo(projectInfo);
}
QSet<FilePath> rccDirs;
if (rootProjectNode()) {
rootProjectNode()->forEachNode(
{},
[&rccDirs](FolderNode *n) {
static const QString rcc = "rcc";
static const QString dotRcc = ".rcc";
static const QString dotQt = ".qt";
const FilePath &filePath = n->filePath();
const QStringView fileName = filePath.fileNameView();
if (fileName == dotRcc
|| (fileName == rcc && filePath.parentDir().fileNameView() == dotQt)) {
rccDirs.insert(n->filePath());
}
},
{});
}
for (const FilePath &fp : std::as_const(rccDirs))
findAllQrcFiles(fp, projectInfo.generatedQrcFiles);
if (s_qtversionExtraProjectInfoHook && kit)
s_qtversionExtraProjectInfoHook(kit, projectInfo);
return projectInfo;
}
#ifdef WITH_TESTS
namespace Internal {
static FilePath constructTestPath(const QString &basePath)
{
FilePath drive;
if (HostOsInfo::isWindowsHost())
drive = "C:";
return drive.stringAppended(basePath);
}
const FilePath TEST_PROJECT_PATH = constructTestPath("/tmp/foobar/baz.project");
const FilePath TEST_PROJECT_NONEXISTING_FILE = constructTestPath("/tmp/foobar/nothing.cpp");
const FilePath TEST_PROJECT_CPP_FILE = constructTestPath("/tmp/foobar/main.cpp");
const FilePath TEST_PROJECT_GENERATED_FILE = constructTestPath("/tmp/foobar/generated.foo");
const QString TEST_PROJECT_MIMETYPE = "application/vnd.test.qmakeprofile";
const QString TEST_PROJECT_DISPLAYNAME = "testProjectFoo";
const char TEST_PROJECT_ID[] = "Test.Project.Id";
class TestBuildSystem : public BuildSystem
{
public:
using BuildSystem::BuildSystem;
static QString name() { return "test"; }
void triggerParsing() final {}
bool canRenameFile(Node *, const FilePath &, const FilePath &) override
{
++canRenameFileCount;
return true;
}
bool renameFiles(Node *, const FilePairs &, FilePaths *) override
{
++renameFilesCount;
return true;
}
int canRenameFileCount = 0;
int renameFilesCount = 0;
};
class RejectingAllRenameBuildSystem : public TestBuildSystem
{
public:
using TestBuildSystem::TestBuildSystem;
static QString name() { return "RejectingAll"; }
bool renameFiles(Node *, const FilePairs &, FilePaths *) override
{
++renameFilesCount;
return false;
}
};
class PartiallyRejectingRenameBuildSystem : public TestBuildSystem
{
public:
using TestBuildSystem::TestBuildSystem;
static QString name() { return "RejectingSome"; }
static inline FilePaths pathsToReject;
bool renameFiles(Node *, const FilePairs &filePairs, FilePaths *notRenamed) override
{
++renameFilesCount;
for (auto &[o, _] : filePairs) {
if (pathsToReject.contains(o) && notRenamed)
notRenamed->append(o);
}
return true;
}
};
class ReparsingBuildSystem : public TestBuildSystem
{
public:
using TestBuildSystem::TestBuildSystem;
static QString name() { return "Reparsing"; }
static inline QPointer<Project> projectToReparse;
bool renameFiles(Node *originNode, const FilePairs &filePairs, FilePaths *notRenamed) override
{
const bool ok = TestBuildSystem::renameFiles(originNode, filePairs, notRenamed);
if (notRenamed) {
for (const auto &pair : filePairs)
notRenamed->append(pair.first);
}
if (projectToReparse) {
auto newRoot = std::make_unique<ProjectNode>(projectToReparse->projectFilePath());
projectToReparse->setRootProjectNode(std::move(newRoot));
if (ProjectTree::instance())
emit ProjectTree::instance()->subtreeChanged(nullptr);
}
return ok;
}
};
class TestBuildConfigurationFactory : public BuildConfigurationFactory
{
public:
TestBuildConfigurationFactory()
{
setSupportedProjectType(TEST_PROJECT_ID);
setBuildGenerator([](const Kit *, const FilePath &projectFilePath, bool) {
BuildInfo bi;
bi.buildSystemName = TestBuildSystem::name();
bi.buildDirectory = projectFilePath.parentDir();
bi.displayName = "default";
return QList<BuildInfo>{bi};
});
registerBuildConfiguration<BuildConfiguration>("TestProject.BuildConfiguration");
}
};
class TestProject : public Project
{
public:
TestProject() : Project(TEST_PROJECT_MIMETYPE, TEST_PROJECT_PATH)
{
setType(TEST_PROJECT_ID);
setDisplayName(TEST_PROJECT_DISPLAYNAME);
setBuildSystemCreator<TestBuildSystem>();
target = addTargetForKit(&testKit);
}
bool needsConfiguration() const final { return false; }
Kit testKit;
Target *target = nullptr;
};
class RenameTestProject : public Project
{
public:
explicit RenameTestProject(const TemporaryDirectory &td)
: Project(TEST_PROJECT_MIMETYPE, td.path())
{
setType(TEST_PROJECT_ID);
setDisplayName(TEST_PROJECT_DISPLAYNAME);
}
bool needsConfiguration() const final { return false; }
template<typename BS>
void initialize()
{
setBuildSystemCreator<BS>();
target = addTargetForKit(&kit);
createNodes();
Q_ASSERT(dynamic_cast<BS *>(target->buildSystem()));
}
TestBuildSystem *testBuildSystem()
{
return dynamic_cast<TestBuildSystem *>(target->buildSystem());
}
Kit kit;
Target *target = nullptr;
FilePath sourceFile, secondSourceFile;
private:
void createNodes()
{
sourceFile = projectFilePath().pathAppended("test.cpp");
secondSourceFile = projectFilePath().pathAppended("test2.cpp");
QVERIFY(sourceFile.writeFileContents("content1"));
QVERIFY(secondSourceFile.writeFileContents("content2"));
auto root = std::make_unique<ProjectNode>(projectFilePath());
std::vector<std::unique_ptr<FileNode>> vec;
vec.emplace_back(std::make_unique<FileNode>(projectFilePath(), FileType::Project));
vec.emplace_back(std::make_unique<FileNode>(sourceFile, FileType::Source));
vec.emplace_back(std::make_unique<FileNode>(secondSourceFile, FileType::Source));
root->addNestedNodes(std::move(vec));
setRootProjectNode(std::move(root));
}
};
static FilePath makeRenamedFilePath(const FilePath &original)
{
return original.chopped(4).stringAppended("_renamed.cpp");
}
class ProjectTest : public QObject
{
Q_OBJECT
private slots:
void testSetup()
{
TestProject project;
QCOMPARE(project.displayName(), TEST_PROJECT_DISPLAYNAME);
QVERIFY(!project.rootProjectNode());
QVERIFY(project.containerNode());
QVERIFY(project.macroExpander());
QCOMPARE(project.mimeType(), TEST_PROJECT_MIMETYPE);
QCOMPARE(project.projectFilePath(), TEST_PROJECT_PATH);
QCOMPARE(project.projectDirectory(), TEST_PROJECT_PATH.parentDir());
QCOMPARE(project.isKnownFile(TEST_PROJECT_PATH), true);
QCOMPARE(project.isKnownFile(TEST_PROJECT_NONEXISTING_FILE), false);
QCOMPARE(project.isKnownFile(TEST_PROJECT_CPP_FILE), false);
QCOMPARE(project.files(Project::AllFiles), FilePaths{TEST_PROJECT_PATH});
QCOMPARE(project.files(Project::GeneratedFiles), {});
QCOMPARE(project.type(), Id(TEST_PROJECT_ID));
QVERIFY(!project.activeBuildSystem()->isParsing());
QVERIFY(!project.activeBuildSystem()->hasParsingData());
}
void testChangeDisplayName()
{
TestProject project;
QSignalSpy spy(&project, &Project::displayNameChanged);
const QString newName = "other name";
project.setDisplayName(newName);
QCOMPARE(spy.count(), 1);
QVariantList args = spy.takeFirst();
QCOMPARE(args, {});
project.setDisplayName(newName);
QCOMPARE(spy.count(), 0);
}
void testParsingSuccess()
{
TestProject project;
QSignalSpy startSpy(project.activeBuildSystem(), &BuildSystem::parsingStarted);
QSignalSpy stopSpy(project.activeBuildSystem(), &BuildSystem::parsingFinished);
{
BuildSystem::ParseGuard guard = project.activeBuildSystem()->guardParsingRun();
QCOMPARE(startSpy.count(), 1);
QCOMPARE(stopSpy.count(), 0);
QVERIFY(project.activeBuildSystem()->isParsing());
QVERIFY(!project.activeBuildSystem()->hasParsingData());
guard.markAsSuccess();
}
QCOMPARE(startSpy.count(), 1);
QCOMPARE(stopSpy.count(), 1);
QCOMPARE(stopSpy.at(0), {QVariant(true)});
QVERIFY(!project.activeBuildSystem()->isParsing());
QVERIFY(project.activeBuildSystem()->hasParsingData());
}
void testParsingFail()
{
TestProject project;
QSignalSpy startSpy(project.activeBuildSystem(), &BuildSystem::parsingStarted);
QSignalSpy stopSpy(project.activeBuildSystem(), &BuildSystem::parsingFinished);
{
BuildSystem::ParseGuard guard = project.activeBuildSystem()->guardParsingRun();
QCOMPARE(startSpy.count(), 1);
QCOMPARE(stopSpy.count(), 0);
QVERIFY(project.activeBuildSystem()->isParsing());
QVERIFY(!project.activeBuildSystem()->hasParsingData());
}
QCOMPARE(startSpy.count(), 1);
QCOMPARE(stopSpy.count(), 1);
QCOMPARE(stopSpy.at(0), {QVariant(false)});
QVERIFY(!project.activeBuildSystem()->isParsing());
QVERIFY(!project.activeBuildSystem()->hasParsingData());
}
static std::unique_ptr<ProjectNode> createFileTree(Project *project)
{
std::unique_ptr<ProjectNode> root = std::make_unique<ProjectNode>(project->projectDirectory());
std::vector<std::unique_ptr<FileNode>> nodes;
nodes.emplace_back(std::make_unique<FileNode>(TEST_PROJECT_PATH, FileType::Project));
nodes.emplace_back(std::make_unique<FileNode>(TEST_PROJECT_CPP_FILE, FileType::Source));
nodes.emplace_back(std::make_unique<FileNode>(TEST_PROJECT_GENERATED_FILE, FileType::Source));
nodes.back()->setIsGenerated(true);
root->addNestedNodes(std::move(nodes));
return root;
}
void testProjectTree()
{
TestProject project;
QSignalSpy fileSpy(&project, &Project::fileListChanged);
project.setRootProjectNode(nullptr);
QCOMPARE(fileSpy.count(), 0);
QVERIFY(!project.rootProjectNode());
project.setRootProjectNode(std::make_unique<ProjectNode>(project.projectDirectory()));
QCOMPARE(fileSpy.count(), 0);
QVERIFY(!project.rootProjectNode());
std::unique_ptr<ProjectNode> root = createFileTree(&project);
ProjectNode *rootNode = root.get();
project.setRootProjectNode(std::move(root));
QCOMPARE(fileSpy.count(), 1);
QCOMPARE(project.rootProjectNode(), rootNode);
// Test known files:
QCOMPARE(project.isKnownFile(TEST_PROJECT_PATH), true);
QCOMPARE(project.isKnownFile(TEST_PROJECT_NONEXISTING_FILE), false);
QCOMPARE(project.isKnownFile(TEST_PROJECT_CPP_FILE), true);
QCOMPARE(project.isKnownFile(TEST_PROJECT_GENERATED_FILE), true);
FilePaths allFiles = project.files(Project::AllFiles);
QCOMPARE(allFiles.count(), 3);
QVERIFY(allFiles.contains(TEST_PROJECT_PATH));
QVERIFY(allFiles.contains(TEST_PROJECT_CPP_FILE));
QVERIFY(allFiles.contains(TEST_PROJECT_GENERATED_FILE));
QCOMPARE(project.files(Project::GeneratedFiles), FilePaths{TEST_PROJECT_GENERATED_FILE});
FilePaths sourceFiles = project.files(Project::SourceFiles);
QCOMPARE(sourceFiles.count(), 2);
QVERIFY(sourceFiles.contains(TEST_PROJECT_PATH));
QVERIFY(sourceFiles.contains(TEST_PROJECT_CPP_FILE));
project.setRootProjectNode(nullptr);
QCOMPARE(fileSpy.count(), 2);
QVERIFY(!project.rootProjectNode());
}
void testMultipleBuildConfigs()
{
// Find suitable kit.
Kit * const kit = findOr(KitManager::kits(), nullptr, [](const Kit *k) {
return k->isValid();
});
if (!kit)
QSKIP("The test requires at least one valid kit.");
// Copy project from qrc file and set it up.
QTemporaryDir * const tempDir = TemporaryDirectory::masterTemporaryDirectory();
QVERIFY(tempDir->isValid());
const FilePath projectDir = FilePath::fromString(tempDir->path() + "/generic-project");
const auto copyResult = FilePath(":/projectexplorer/testdata/generic-project").copyRecursively(projectDir);
if (!copyResult)
qDebug() << copyResult.error();
QVERIFY(copyResult);
const QFileInfoList files = QDir(projectDir.toUrlishString()).entryInfoList(QDir::Files | QDir::Dirs);
for (const QFileInfo &f : files)
QFile(f.absoluteFilePath()).setPermissions(f.permissions() | QFile::WriteUser);
const auto theProject = ProjectExplorerPlugin::openProject(projectDir.pathAppended("generic-project.creator"));
QVERIFY2(theProject, qPrintable(theProject.errorMessage()));
theProject.project()->configureAsExampleProject(kit);
QCOMPARE(theProject.project()->targets().size(), 1);
Target * const target = theProject.project()->activeTarget();
QVERIFY(target);
QCOMPARE(target->buildConfigurations().size(), 6);
target->setActiveBuildConfiguration(target->buildConfigurations().at(1), SetActive::Cascade);
BuildSystem * const bs = theProject.project()->activeBuildSystem();
QVERIFY(bs);
QCOMPARE(bs, target->activeBuildConfiguration()->buildSystem());
if (bs->isWaitingForParse() || bs->isParsing()) {
QEventLoop loop;
QTimer t;
t.setSingleShot(true);
connect(&t, &QTimer::timeout, &loop, &QEventLoop::quit);
connect(bs, &BuildSystem::parsingFinished, &loop, &QEventLoop::quit);
t.start(10000);
QVERIFY(loop.exec());
QVERIFY(t.isActive());
}
QVERIFY(!bs->isWaitingForParse() && !bs->isParsing());
QCOMPARE(ProjectManager::startupProject(), theProject.project());
QCOMPARE(ProjectTree::currentProject(), theProject.project());
QVERIFY(EditorManager::openEditor(projectDir.pathAppended("main.cpp")));
QVERIFY(ProjectTree::currentNode());
ProjectTree::instance()->expandAll();
ProjectManager::closeAllProjects(); // QTCREATORBUG-25655
}
void testSourceToBinaryMapping()
{
// Find suitable kit.
Kit * const kit = findOr(KitManager::kits(), nullptr, [](const Kit *k) {
return k->isValid() && ToolchainKitAspect::cxxToolchain(k);
});
if (!kit)
QSKIP("The test requires at least one kit with a toolchain.");
const auto toolchain = ToolchainKitAspect::cxxToolchain(kit);
QVERIFY(toolchain);
if (const auto msvcToolchain = dynamic_cast<Internal::MsvcToolchain *>(toolchain)) {
while (!msvcToolchain->environmentInitialized()) {
QSignalSpy parsingFinishedSpy(ToolchainManager::instance(),
&ToolchainManager::toolchainUpdated);
QVERIFY(parsingFinishedSpy.wait(10000));
}
}
// Copy project from qrc.
QTemporaryDir * const tempDir = TemporaryDirectory::masterTemporaryDirectory();
QVERIFY(tempDir->isValid());
const FilePath projectDir = FilePath::fromString(tempDir->path() + "/multi-target-project");
if (!projectDir.exists()) {
const auto result = FilePath(":/projectexplorer/testdata/multi-target-project")
.copyRecursively(projectDir);
if (!result)
qDebug() << result.error();
QVERIFY(result);
const QFileInfoList files = QDir(projectDir.toUrlishString()).entryInfoList(QDir::Files);
for (const QFileInfo &f : files)
QFile(f.absoluteFilePath()).setPermissions(f.permissions() | QFile::WriteUser);
}
// Load Project.
QFETCH(QString, projectFileName);
const auto theProject = ProjectExplorerPlugin::openProject(projectDir.pathAppended(projectFileName));
if (!theProject
&& !ProjectManager::canOpenProjectForMimeType(Utils::mimeTypeForFile(projectFileName))) {
QSKIP("This test requires the presence of the qmake/cmake/qbs project managers "
"to be fully functional");
}
QVERIFY2(theProject, qPrintable(theProject.errorMessage()));
theProject.project()->configureAsExampleProject(kit);
QCOMPARE(theProject.project()->targets().size(), 1);
BuildSystem * const bs = theProject.project()->activeBuildSystem();
QVERIFY(bs);
if (bs->isWaitingForParse() || bs->isParsing()) {
QSignalSpy parsingFinishedSpy(bs, &BuildSystem::parsingFinished);
QVERIFY(parsingFinishedSpy.wait(10000));
}
QVERIFY(!bs->isWaitingForParse() && !bs->isParsing());
if (QLatin1String(QTest::currentDataTag()) == QLatin1String("qbs")) {
BuildManager::buildProjectWithoutDependencies(theProject.project());
if (BuildManager::isBuilding()) {
QSignalSpy buildingFinishedSpy(BuildManager::instance(), &BuildManager::buildQueueFinished);
QVERIFY(buildingFinishedSpy.wait(10000));
}
QVERIFY(!BuildManager::isBuilding());
QSignalSpy projectUpdateSpy(theProject.project(), &Project::fileListChanged);
QVERIFY(projectUpdateSpy.wait(5000));
}
// Check mapping
const auto binariesForSource = [&](const QString &fileName) {
return theProject.project()->binariesForSourceFile(projectDir.pathAppended(fileName));
};
QCOMPARE(binariesForSource("multi-target-project-main.cpp").size(), 1);
QCOMPARE(binariesForSource("multi-target-project-lib.cpp").size(), 1);
QCOMPARE(binariesForSource("multi-target-project-shared.h").size(), 2);
}
void testSourceToBinaryMapping_data()
{
QTest::addColumn<QString>("projectFileName");
QTest::addRow("cmake") << "CMakeLists.txt";
QTest::addRow("qbs") << "multi-target-project.qbs";
QTest::addRow("qmake") << "multi-target-project.pro";
}
void testRenameFile()
{
TemporaryDirectory tempDir("testProject_renameFile");
RenameTestProject testProject(tempDir);
testProject.initialize<TestBuildSystem>();
auto sourceFileNode = const_cast<Node *>(testProject.nodeForFilePath(testProject.sourceFile));
QVERIFY(sourceFileNode);
const FilePath testRenamed = makeRenamedFilePath(testProject.sourceFile);
QList<std::pair<Node *, FilePath>> nodesToRename{{sourceFileNode, testRenamed}};
const FilePairs result = ProjectExplorerPlugin::renameFiles(nodesToRename);
QCOMPARE(result.size(), 1);
QCOMPARE(testProject.testBuildSystem()->canRenameFileCount, 1);
QCOMPARE(testProject.testBuildSystem()->renameFilesCount, 1);
QCOMPARE(testProject.sourceFile.exists(), false);
QCOMPARE(testRenamed.exists(), true);
}
void testRenameFile_NullNode()
{
TemporaryDirectory tempDir("testProject_renameFile_NullNode");
RenameTestProject testProject(tempDir);
testProject.initialize<TestBuildSystem>();
const FilePath testRenamed = makeRenamedFilePath(testProject.sourceFile);
QList<std::pair<Node *, FilePath>> nodesToRename{{nullptr, testRenamed}};
const FilePairs result = ProjectExplorerPlugin::renameFiles(nodesToRename);
QVERIFY(result.isEmpty());
}
void testRenameMultipleFiles()
{
TemporaryDirectory tempDir("testProject_renameMultipleFiles");
RenameTestProject testProject(tempDir);
testProject.initialize<TestBuildSystem>();
const FilePath renamedFilePath1 = makeRenamedFilePath(testProject.sourceFile);
const FilePath renamedFilePath2 = makeRenamedFilePath(testProject.secondSourceFile);
auto testNode1 = const_cast<Node *>(testProject.nodeForFilePath(testProject.sourceFile));
auto testNode2 = const_cast<Node *>(testProject.nodeForFilePath(testProject.secondSourceFile));
QList<std::pair<Node *, FilePath>>
nodesToRename{{testNode1, renamedFilePath1}, {testNode2, renamedFilePath2}};
const FilePairs result = ProjectExplorerPlugin::renameFiles(nodesToRename);
QCOMPARE(result.size(), 2);
QCOMPARE(testProject.testBuildSystem()->canRenameFileCount, 2);
QCOMPARE(testProject.testBuildSystem()->renameFilesCount, 1);
QCOMPARE(testProject.sourceFile.exists(), false);
QCOMPARE(testProject.secondSourceFile.exists(), false);
QCOMPARE(renamedFilePath1.exists(), true);
QCOMPARE(renamedFilePath2.exists(), true);
}
void testRenameFile_BuildSystemRejectsAll()
{
TemporaryDirectory tempDir("testProject_renameFile_BuildSystemRejectsAll");
RenameTestProject testProject(tempDir);
testProject.initialize<RejectingAllRenameBuildSystem>();
Node *sourcNode = const_cast<Node *>(testProject.nodeForFilePath(testProject.sourceFile));
const FilePath renamedPath = makeRenamedFilePath(testProject.sourceFile);
QList<std::pair<Node *, FilePath>> toRename{{sourcNode, renamedPath}};
const FilePairs result = ProjectExplorerPlugin::renameFiles(toRename);
QVERIFY(result.isEmpty());
QCOMPARE(testProject.testBuildSystem()->canRenameFileCount, 1);
QCOMPARE(testProject.testBuildSystem()->renameFilesCount, 1);
QCOMPARE(testProject.sourceFile.exists(), false);
QCOMPARE(renamedPath.exists(), true);
}
void testRenameFile_BuildSystemRejectsPartial()
{
TemporaryDirectory tempDir("testProject_renameFile_BuildSystemRejectsPartial");
RenameTestProject testProject(tempDir);
testProject.initialize<PartiallyRejectingRenameBuildSystem>();
PartiallyRejectingRenameBuildSystem::pathsToReject = {testProject.sourceFile};
const FilePath renamed1 = makeRenamedFilePath(testProject.sourceFile);
const FilePath renamed2 = makeRenamedFilePath(testProject.secondSourceFile);
Node *node1 = const_cast<Node *>(testProject.nodeForFilePath(testProject.sourceFile));
Node *node2 = const_cast<Node *>(testProject.nodeForFilePath(testProject.secondSourceFile));
QList<std::pair<Node *, FilePath>> toRename{{node1, renamed1}, {node2, renamed2}};
const FilePairs result = ProjectExplorerPlugin::renameFiles(toRename);
QCOMPARE(result.size(), 1);
QVERIFY(result.contains({testProject.secondSourceFile, renamed2}));
QCOMPARE(testProject.testBuildSystem()->canRenameFileCount, 2);
QCOMPARE(testProject.testBuildSystem()->renameFilesCount, 1);
QCOMPARE(testProject.sourceFile.exists(), false);
QCOMPARE(testProject.secondSourceFile.exists(), false);
QCOMPARE(renamed1.exists(), true);
QCOMPARE(renamed2.exists(), true);
}
void testRenameFile_QmlCrashSimulation()
{
TemporaryDirectory tempDir("testProject_renameFile_QmlCrashSimulation");
RenameTestProject testProject(tempDir);
testProject.initialize<ReparsingBuildSystem>();
ReparsingBuildSystem::projectToReparse = &testProject;
Node *sourceNode = const_cast<Node *>(testProject.nodeForFilePath(testProject.sourceFile));
const FilePath renamedPath = makeRenamedFilePath(testProject.sourceFile);
QList<std::pair<Node *, FilePath>> toRename{{sourceNode, renamedPath}};
const FilePairs result = ProjectExplorerPlugin::renameFiles(toRename);
QVERIFY(renamedPath.exists());
QCOMPARE(testProject.sourceFile.exists(), false);
QVERIFY(result.isEmpty());
}
};
static TestBuildConfigurationFactory testBuildConfigFactory;
QObject *createProjectTest()
{
return new ProjectTest;
}
} // namespace Internal
#endif // WITH_TESTS
} // namepace ProjectExplorer
#ifdef WITH_TESTS
#include <project.moc>
#endif
|