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
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmQtAutoMocUic.h"
#include <algorithm>
#include <array>
#include <list>
#include <set>
#include <sstream>
#include <utility>
#include "cm_memory.hxx"
#include "cmAlgorithms.h"
#include "cmCryptoHash.h"
#include "cmGeneratedFileStream.h"
#include "cmMakefile.h"
#include "cmQtAutoGen.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmake.h"
#include "cmsys/FStream.hxx"
#if defined(__APPLE__)
# include <unistd.h>
#endif
static constexpr std::size_t MocUnderscoreLength = 4; // Length of "moc_"
static constexpr std::size_t UiUnderscoreLength = 3; // Length of "ui_"
cmQtAutoMocUic::IncludeKeyT::IncludeKeyT(std::string const& key,
std::size_t basePrefixLength)
: Key(key)
, Dir(SubDirPrefix(key))
, Base(cmSystemTools::GetFilenameWithoutLastExtension(key))
{
if (basePrefixLength != 0) {
Base = Base.substr(basePrefixLength);
}
}
void cmQtAutoMocUic::ParseCacheT::FileT::Clear()
{
Moc.Macro.clear();
Moc.Include.Underscore.clear();
Moc.Include.Dot.clear();
Moc.Depends.clear();
Uic.Include.clear();
Uic.Depends.clear();
}
cmQtAutoMocUic::ParseCacheT::FileHandleT cmQtAutoMocUic::ParseCacheT::Get(
std::string const& fileName) const
{
auto it = Map_.find(fileName);
if (it != Map_.end()) {
return it->second;
}
return FileHandleT();
}
cmQtAutoMocUic::ParseCacheT::GetOrInsertT
cmQtAutoMocUic::ParseCacheT::GetOrInsert(std::string const& fileName)
{
// Find existing entry
{
auto it = Map_.find(fileName);
if (it != Map_.end()) {
return GetOrInsertT{ it->second, false };
}
}
// Insert new entry
return GetOrInsertT{
Map_.emplace(fileName, std::make_shared<FileT>()).first->second, true
};
}
cmQtAutoMocUic::ParseCacheT::ParseCacheT() = default;
cmQtAutoMocUic::ParseCacheT::~ParseCacheT() = default;
void cmQtAutoMocUic::ParseCacheT::Clear()
{
Map_.clear();
}
bool cmQtAutoMocUic::ParseCacheT::ReadFromFile(std::string const& fileName)
{
cmsys::ifstream fin(fileName.c_str());
if (!fin) {
return false;
}
FileHandleT fileHandle;
std::string line;
while (std::getline(fin, line)) {
// Check if this an empty or a comment line
if (line.empty() || line.front() == '#') {
continue;
}
// Drop carriage return character at the end
if (line.back() == '\r') {
line.pop_back();
if (line.empty()) {
continue;
}
}
// Check if this a file name line
if (line.front() != ' ') {
fileHandle = GetOrInsert(line).first;
continue;
}
// Bad line or bad file handle
if (!fileHandle || (line.size() < 6)) {
continue;
}
constexpr std::size_t offset = 5;
if (cmHasLiteralPrefix(line, " mmc:")) {
fileHandle->Moc.Macro = line.substr(offset);
continue;
}
if (cmHasLiteralPrefix(line, " miu:")) {
fileHandle->Moc.Include.Underscore.emplace_back(line.substr(offset),
MocUnderscoreLength);
continue;
}
if (cmHasLiteralPrefix(line, " mid:")) {
fileHandle->Moc.Include.Dot.emplace_back(line.substr(offset), 0);
continue;
}
if (cmHasLiteralPrefix(line, " mdp:")) {
fileHandle->Moc.Depends.emplace_back(line.substr(offset));
continue;
}
if (cmHasLiteralPrefix(line, " uic:")) {
fileHandle->Uic.Include.emplace_back(line.substr(offset),
UiUnderscoreLength);
continue;
}
if (cmHasLiteralPrefix(line, " udp:")) {
fileHandle->Uic.Depends.emplace_back(line.substr(offset));
continue;
}
}
return true;
}
bool cmQtAutoMocUic::ParseCacheT::WriteToFile(std::string const& fileName)
{
cmGeneratedFileStream ofs(fileName);
if (!ofs) {
return false;
}
ofs << "# Generated by CMake. Changes will be overwritten." << std::endl;
for (auto const& pair : Map_) {
ofs << pair.first << std::endl;
FileT const& file = *pair.second;
if (!file.Moc.Macro.empty()) {
ofs << " mmc:" << file.Moc.Macro << std::endl;
}
for (IncludeKeyT const& item : file.Moc.Include.Underscore) {
ofs << " miu:" << item.Key << std::endl;
}
for (IncludeKeyT const& item : file.Moc.Include.Dot) {
ofs << " mid:" << item.Key << std::endl;
}
for (std::string const& item : file.Moc.Depends) {
ofs << " mdp:" << item << std::endl;
}
for (IncludeKeyT const& item : file.Uic.Include) {
ofs << " uic:" << item.Key << std::endl;
}
for (std::string const& item : file.Uic.Depends) {
ofs << " udp:" << item << std::endl;
}
}
return ofs.Close();
}
cmQtAutoMocUic::BaseSettingsT::BaseSettingsT() = default;
cmQtAutoMocUic::BaseSettingsT::~BaseSettingsT() = default;
cmQtAutoMocUic::MocSettingsT::MocSettingsT()
{
RegExpInclude.compile(
"(^|\n)[ \t]*#[ \t]*include[ \t]+"
"[\"<](([^ \">]+/)?moc_[^ \">/]+\\.cpp|[^ \">]+\\.moc)[\">]");
}
cmQtAutoMocUic::MocSettingsT::~MocSettingsT() = default;
bool cmQtAutoMocUic::MocSettingsT::skipped(std::string const& fileName) const
{
return (!Enabled || (SkipList.find(fileName) != SkipList.end()));
}
std::string cmQtAutoMocUic::MocSettingsT::MacrosString() const
{
std::string res;
const auto itB = MacroFilters.cbegin();
const auto itE = MacroFilters.cend();
const auto itL = itE - 1;
auto itC = itB;
for (; itC != itE; ++itC) {
// Separator
if (itC != itB) {
if (itC != itL) {
res += ", ";
} else {
res += " or ";
}
}
// Key
res += itC->Key;
}
return res;
}
cmQtAutoMocUic::UicSettingsT::UicSettingsT()
{
RegExpInclude.compile("(^|\n)[ \t]*#[ \t]*include[ \t]+"
"[\"<](([^ \">]+/)?ui_[^ \">/]+\\.h)[\">]");
}
cmQtAutoMocUic::UicSettingsT::~UicSettingsT() = default;
bool cmQtAutoMocUic::UicSettingsT::skipped(std::string const& fileName) const
{
return (!Enabled || (SkipList.find(fileName) != SkipList.end()));
}
void cmQtAutoMocUic::JobT::LogError(GenT genType,
std::string const& message) const
{
Gen()->AbortError();
Gen()->Log().Error(genType, message);
}
void cmQtAutoMocUic::JobT::LogFileError(GenT genType,
std::string const& filename,
std::string const& message) const
{
Gen()->AbortError();
Gen()->Log().ErrorFile(genType, filename, message);
}
void cmQtAutoMocUic::JobT::LogCommandError(
GenT genType, std::string const& message,
std::vector<std::string> const& command, std::string const& output) const
{
Gen()->AbortError();
Gen()->Log().ErrorCommand(genType, message, command, output);
}
bool cmQtAutoMocUic::JobT::RunProcess(GenT genType,
cmWorkerPool::ProcessResultT& result,
std::vector<std::string> const& command,
std::string* infoMessage)
{
// Log command
if (Log().Verbose()) {
std::string msg;
if ((infoMessage != nullptr) && !infoMessage->empty()) {
msg = *infoMessage;
if (msg.back() != '\n') {
msg += '\n';
}
}
msg += QuotedCommand(command);
msg += '\n';
Log().Info(genType, msg);
}
return cmWorkerPool::JobT::RunProcess(result, command,
BaseConst().AutogenBuildDir);
}
void cmQtAutoMocUic::JobMocPredefsT::Process()
{
// (Re)generate moc_predefs.h on demand
std::unique_ptr<std::string> reason;
if (Log().Verbose()) {
reason = cm::make_unique<std::string>();
}
if (!Update(reason.get())) {
return;
}
std::string const& predefsFileRel = MocConst().PredefsFileRel;
std::string const& predefsFileAbs = MocConst().PredefsFileAbs;
{
cmWorkerPool::ProcessResultT result;
{
// Compose command
std::vector<std::string> cmd = MocConst().PredefsCmd;
// Add includes
cmAppend(cmd, MocConst().Includes);
// Add definitions
for (std::string const& def : MocConst().Definitions) {
cmd.emplace_back("-D" + def);
}
// Execute command
if (!RunProcess(GenT::MOC, result, cmd, reason.get())) {
std::string msg =
cmStrCat("The content generation command for ",
Quoted(predefsFileRel), " failed.\n", result.ErrorMessage);
LogCommandError(GenT::MOC, msg, cmd, result.StdOut);
return;
}
}
// (Re)write predefs file only on demand
if (cmQtAutoGenerator::FileDiffers(predefsFileAbs, result.StdOut)) {
if (!cmQtAutoGenerator::FileWrite(predefsFileAbs, result.StdOut)) {
std::string msg =
cmStrCat("Writing ", Quoted(predefsFileRel), " failed.");
LogFileError(GenT::MOC, predefsFileAbs, msg);
return;
}
} else {
// Touch to update the time stamp
if (Log().Verbose()) {
Log().Info(GenT::MOC, "Touching " + Quoted(predefsFileRel));
}
if (!cmSystemTools::Touch(predefsFileAbs, false)) {
std::string msg =
cmStrCat("Touching ", Quoted(predefsFileAbs), " failed.");
LogFileError(GenT::MOC, predefsFileAbs, msg);
return;
}
}
}
// Read file time afterwards
if (!MocEval().PredefsTime.Load(predefsFileAbs)) {
LogFileError(GenT::MOC, predefsFileAbs, "File time reading failed.");
return;
}
}
bool cmQtAutoMocUic::JobMocPredefsT::Update(std::string* reason) const
{
// Test if the file exists
if (!MocEval().PredefsTime.Load(MocConst().PredefsFileAbs)) {
if (reason != nullptr) {
*reason = "Generating ";
*reason += Quoted(MocConst().PredefsFileRel);
*reason += ", because it doesn't exist.";
}
return true;
}
// Test if the settings changed
if (MocConst().SettingsChanged) {
if (reason != nullptr) {
*reason = "Generating ";
*reason += Quoted(MocConst().PredefsFileRel);
*reason += ", because the moc settings changed.";
}
return true;
}
// Test if the executable is newer
{
std::string const& exec = MocConst().PredefsCmd.at(0);
cmFileTime execTime;
if (execTime.Load(exec)) {
if (MocEval().PredefsTime.Older(execTime)) {
if (reason != nullptr) {
*reason = "Generating ";
*reason += Quoted(MocConst().PredefsFileRel);
*reason += " because it is older than ";
*reason += Quoted(exec);
*reason += ".";
}
return true;
}
}
}
return false;
}
bool cmQtAutoMocUic::JobParseT::ReadFile()
{
// Clear old parse information
FileHandle->ParseData->Clear();
std::string const& fileName = FileHandle->FileName;
// Write info
if (Log().Verbose()) {
Log().Info(GenT::GEN, "Parsing " + Quoted(fileName));
}
// Read file content
{
std::string error;
if (!cmQtAutoGenerator::FileRead(Content, fileName, &error)) {
LogFileError(GenT::GEN, fileName, "Could not read the file: " + error);
return false;
}
}
// Warn if empty
if (Content.empty()) {
Log().WarningFile(GenT::GEN, fileName, "The file is empty.");
return false;
}
return true;
}
void cmQtAutoMocUic::JobParseT::CreateKeys(std::vector<IncludeKeyT>& container,
std::set<std::string> const& source,
std::size_t basePrefixLength)
{
if (source.empty()) {
return;
}
container.reserve(source.size());
for (std::string const& src : source) {
container.emplace_back(src, basePrefixLength);
}
}
void cmQtAutoMocUic::JobParseT::MocMacro()
{
for (KeyExpT const& filter : MocConst().MacroFilters) {
// Run a simple find string check
if (Content.find(filter.Key) == std::string::npos) {
continue;
}
// Run the expensive regular expression check loop
cmsys::RegularExpressionMatch match;
if (filter.Exp.find(Content.c_str(), match)) {
// Keep detected macro name
FileHandle->ParseData->Moc.Macro = filter.Key;
return;
}
}
}
void cmQtAutoMocUic::JobParseT::MocDependecies()
{
if (MocConst().DependFilters.empty()) {
return;
}
// Find dependency strings
std::set<std::string> parseDepends;
for (KeyExpT const& filter : MocConst().DependFilters) {
// Run a simple find string check
if (Content.find(filter.Key) == std::string::npos) {
continue;
}
// Run the expensive regular expression check loop
const char* contentChars = Content.c_str();
cmsys::RegularExpressionMatch match;
while (filter.Exp.find(contentChars, match)) {
{
std::string dep = match.match(1);
if (!dep.empty()) {
parseDepends.emplace(std::move(dep));
}
}
contentChars += match.end();
}
}
// Store dependency strings
{
auto& Depends = FileHandle->ParseData->Moc.Depends;
Depends.reserve(parseDepends.size());
for (std::string const& item : parseDepends) {
Depends.emplace_back(item);
// Replace end of line characters in filenames
std::string& path = Depends.back();
std::replace(path.begin(), path.end(), '\n', ' ');
std::replace(path.begin(), path.end(), '\r', ' ');
}
}
}
void cmQtAutoMocUic::JobParseT::MocIncludes()
{
if (Content.find("moc") == std::string::npos) {
return;
}
std::set<std::string> underscore;
std::set<std::string> dot;
{
const char* contentChars = Content.c_str();
cmsys::RegularExpression const& regExp = MocConst().RegExpInclude;
cmsys::RegularExpressionMatch match;
while (regExp.find(contentChars, match)) {
std::string incString = match.match(2);
std::string const incBase =
cmSystemTools::GetFilenameWithoutLastExtension(incString);
if (cmHasLiteralPrefix(incBase, "moc_")) {
// moc_<BASE>.cpp
// Remove the moc_ part from the base name
underscore.emplace(std::move(incString));
} else {
// <BASE>.moc
dot.emplace(std::move(incString));
}
// Forward content pointer
contentChars += match.end();
}
}
auto& Include = FileHandle->ParseData->Moc.Include;
CreateKeys(Include.Underscore, underscore, MocUnderscoreLength);
CreateKeys(Include.Dot, dot, 0);
}
void cmQtAutoMocUic::JobParseT::UicIncludes()
{
if (Content.find("ui_") == std::string::npos) {
return;
}
std::set<std::string> includes;
{
const char* contentChars = Content.c_str();
cmsys::RegularExpression const& regExp = UicConst().RegExpInclude;
cmsys::RegularExpressionMatch match;
while (regExp.find(contentChars, match)) {
includes.emplace(match.match(2));
// Forward content pointer
contentChars += match.end();
}
}
CreateKeys(FileHandle->ParseData->Uic.Include, includes, UiUnderscoreLength);
}
void cmQtAutoMocUic::JobParseHeaderT::Process()
{
if (!ReadFile()) {
return;
}
// Moc parsing
if (FileHandle->Moc) {
MocMacro();
MocDependecies();
}
// Uic parsing
if (FileHandle->Uic) {
UicIncludes();
}
}
void cmQtAutoMocUic::JobParseSourceT::Process()
{
if (!ReadFile()) {
return;
}
// Moc parsing
if (FileHandle->Moc) {
MocMacro();
MocDependecies();
MocIncludes();
}
// Uic parsing
if (FileHandle->Uic) {
UicIncludes();
}
}
void cmQtAutoMocUic::JobEvaluateT::Process()
{
// Evaluate for moc
if (MocConst().Enabled) {
// Evaluate headers
for (auto const& pair : BaseEval().Headers) {
if (!MocEvalHeader(pair.second)) {
return;
}
}
// Evaluate sources
for (auto const& pair : BaseEval().Sources) {
if (!MocEvalSource(pair.second)) {
return;
}
}
}
// Evaluate for uic
if (UicConst().Enabled) {
if (!UicEval(BaseEval().Headers) || !UicEval(BaseEval().Sources)) {
return;
}
}
// Add discovered header parse jobs
Gen()->CreateParseJobs<JobParseHeaderT>(MocEval().HeadersDiscovered);
// Add generate job after
Gen()->WorkerPool().EmplaceJob<JobGenerateT>();
}
bool cmQtAutoMocUic::JobEvaluateT::MocEvalHeader(SourceFileHandleT source)
{
SourceFileT const& sourceFile = *source;
auto const& parseData = sourceFile.ParseData->Moc;
if (!source->Moc) {
return true;
}
if (!parseData.Macro.empty()) {
// Create a new mapping
MappingHandleT handle = std::make_shared<MappingT>();
handle->SourceFile = std::move(source);
// Absolute build path
if (BaseConst().MultiConfig) {
handle->OutputFile = Gen()->AbsoluteIncludePath(sourceFile.BuildPath);
} else {
handle->OutputFile = Gen()->AbsoluteBuildPath(sourceFile.BuildPath);
}
// Register mapping in headers map
MocRegisterMapping(handle, true);
}
return true;
}
bool cmQtAutoMocUic::JobEvaluateT::MocEvalSource(
SourceFileHandleT const& source)
{
SourceFileT const& sourceFile = *source;
auto const& parseData = sourceFile.ParseData->Moc;
if (!sourceFile.Moc ||
(parseData.Macro.empty() && parseData.Include.Underscore.empty() &&
parseData.Include.Dot.empty())) {
return true;
}
std::string const sourceDir = SubDirPrefix(sourceFile.FileName);
std::string const sourceBase =
cmSystemTools::GetFilenameWithoutLastExtension(sourceFile.FileName);
// For relaxed mode check if the own "moc_" or ".moc" file is included
bool const relaxedMode = MocConst().RelaxedMode;
bool sourceIncludesMocUnderscore = false;
bool sourceIncludesDotMoc = false;
// Check if the sources own "moc_" or ".moc" file is included
if (relaxedMode) {
for (IncludeKeyT const& incKey : parseData.Include.Underscore) {
if (incKey.Base == sourceBase) {
sourceIncludesMocUnderscore = true;
break;
}
}
}
for (IncludeKeyT const& incKey : parseData.Include.Dot) {
if (incKey.Base == sourceBase) {
sourceIncludesDotMoc = true;
break;
}
}
// Check if this source needs to be moc processed but doesn't.
if (!sourceIncludesDotMoc && !parseData.Macro.empty() &&
!(relaxedMode && sourceIncludesMocUnderscore)) {
{
std::string emsg =
cmStrCat("The file contains a ", Quoted(parseData.Macro),
" macro, but does not include ", Quoted(sourceBase + ".moc"),
"!\nConsider to\n - add #include \"", sourceBase,
".moc\"\n - enable SKIP_AUTOMOC for this file");
LogFileError(GenT::MOC, sourceFile.FileName, emsg);
}
return false;
}
// Evaluate "moc_" includes
for (IncludeKeyT const& incKey : parseData.Include.Underscore) {
std::string const headerBase = incKey.Dir + incKey.Base;
SourceFileHandleT header = MocFindIncludedHeader(sourceDir, headerBase);
if (!header) {
{
std::string msg = "The file includes the moc file ";
msg += Quoted(incKey.Key);
msg += ",\nbut the header could not be found "
"in the following locations\n";
msg += MocMessageTestHeaders(headerBase);
LogFileError(GenT::MOC, sourceFile.FileName, msg);
}
return false;
}
// The include might be handled differently in relaxed mode
if (relaxedMode && !sourceIncludesDotMoc && !parseData.Macro.empty() &&
(incKey.Base == sourceBase)) {
// The <BASE>.cpp file includes a Qt macro but does not include the
// <BASE>.moc file. In this case, the moc_<BASE>.cpp should probably
// be generated from <BASE>.cpp instead of <BASE>.h, because otherwise
// it won't build. But warn, since this is not how it is supposed to be
// used. This is for KDE4 compatibility.
{
// Issue a warning
std::string msg = cmStrCat(
"The file contains a ", Quoted(parseData.Macro),
" macro, but does not include ", Quoted(sourceBase + ".moc"),
".\nInstead it includes ", Quoted(incKey.Key),
".\nRunning moc on the source\n ", Quoted(sourceFile.FileName),
"!\nBetter include ", Quoted(sourceBase + ".moc"),
" for compatibility with regular mode.\n",
"This is a CMAKE_AUTOMOC_RELAXED_MODE warning.\n");
Log().WarningFile(GenT::MOC, sourceFile.FileName, msg);
}
// Create mapping
if (!MocRegisterIncluded(incKey.Key, source, source, false)) {
return false;
}
continue;
}
// Check if header is skipped
if (MocConst().skipped(header->FileName)) {
continue;
}
// Create mapping
if (!MocRegisterIncluded(incKey.Key, source, std::move(header), true)) {
return false;
}
}
// Evaluate ".moc" includes
if (relaxedMode) {
// Relaxed mode
for (IncludeKeyT const& incKey : parseData.Include.Dot) {
// Check if this is the sources own .moc file
bool const ownMoc = (incKey.Base == sourceBase);
if (ownMoc && !parseData.Macro.empty()) {
// Create mapping for the regular use case
if (!MocRegisterIncluded(incKey.Key, source, source, false)) {
return false;
}
continue;
}
// Try to find a header instead but issue a warning.
// This is for KDE4 compatibility.
std::string const headerBase = incKey.Dir + incKey.Base;
SourceFileHandleT header = MocFindIncludedHeader(sourceDir, headerBase);
if (!header) {
std::string msg = "The file includes the moc file ";
msg += Quoted(incKey.Key);
msg += ",\nwhich seems to be the moc file from a different source "
"file.\nCMAKE_AUTOMOC_RELAXED_MODE: Also a matching header"
"could not be found in the following locations\n";
msg += MocMessageTestHeaders(headerBase);
LogFileError(GenT::MOC, sourceFile.FileName, msg);
return false;
}
// Check if header is skipped
if (MocConst().skipped(header->FileName)) {
continue;
}
// Issue a warning
if (ownMoc && parseData.Macro.empty()) {
std::string msg = cmStrCat(
"The file includes the moc file ", Quoted(incKey.Key),
", but does not contain a\n", MocConst().MacrosString(),
" macro.\nRunning moc on the header\n ", Quoted(header->FileName),
"!\nBetter include ", Quoted("moc_" + incKey.Base + ".cpp"),
" for a compatibility with regular mode.\n",
"This is a CMAKE_AUTOMOC_RELAXED_MODE warning.\n");
Log().WarningFile(GenT::MOC, sourceFile.FileName, msg);
} else {
std::string msg = cmStrCat(
"The file includes the moc file ", Quoted(incKey.Key),
" instead of ", Quoted("moc_" + incKey.Base + ".cpp"),
".\nRunning moc on the header\n ", Quoted(header->FileName),
"!\nBetter include ", Quoted("moc_" + incKey.Base + ".cpp"),
" for compatibility with regular mode.\n",
"This is a CMAKE_AUTOMOC_RELAXED_MODE warning.\n");
Log().WarningFile(GenT::MOC, sourceFile.FileName, msg);
}
// Create mapping
if (!MocRegisterIncluded(incKey.Key, source, std::move(header), true)) {
return false;
}
}
} else {
// Strict mode
for (IncludeKeyT const& incKey : parseData.Include.Dot) {
// Check if this is the sources own .moc file
bool const ownMoc = (incKey.Base == sourceBase);
if (!ownMoc) {
// Don't allow <BASE>.moc include other than own in regular mode
std::string msg = "The file includes the moc file ";
msg += Quoted(incKey.Key);
msg += ",\nwhich seems to be the moc file from a different "
"source file.\nThis is not supported. Include ";
msg += Quoted(sourceBase + ".moc");
msg += " to run moc on this source file.";
LogFileError(GenT::MOC, sourceFile.FileName, msg);
return false;
}
// Accept but issue a warning if moc isn't required
if (parseData.Macro.empty()) {
std::string msg = cmStrCat(
"The file includes the moc file ", Quoted(incKey.Key),
", but does not contain a ", MocConst().MacrosString(), " macro.");
Log().WarningFile(GenT::MOC, sourceFile.FileName, msg);
}
// Create mapping
if (!MocRegisterIncluded(incKey.Key, source, source, false)) {
return false;
}
}
}
return true;
}
cmQtAutoMocUic::SourceFileHandleT
cmQtAutoMocUic::JobEvaluateT::MocFindIncludedHeader(
std::string const& includerDir, std::string const& includeBase) const
{
// Search in vicinity of the source
{
SourceFileHandleT res = MocFindHeader(includerDir + includeBase);
if (res) {
return res;
}
}
// Search in include directories
for (std::string const& path : MocConst().IncludePaths) {
std::string testPath = cmStrCat(path, '/', includeBase);
SourceFileHandleT res = MocFindHeader(testPath);
if (res) {
return res;
}
}
// Return without success
return SourceFileHandleT();
}
cmQtAutoMocUic::SourceFileHandleT cmQtAutoMocUic::JobEvaluateT::MocFindHeader(
std::string const& basePath) const
{
std::string testPath;
testPath.reserve(basePath.size() + 8);
for (std::string const& ext : BaseConst().HeaderExtensions) {
testPath.clear();
testPath += basePath;
testPath += '.';
testPath += ext;
cmFileTime fileTime;
if (fileTime.Load(testPath)) {
// Compute real path of the file
testPath = cmSystemTools::GetRealPath(testPath);
// Return a known file if it exists already
{
auto it = BaseEval().Headers.find(testPath);
if (it != BaseEval().Headers.end()) {
return it->second;
}
}
// Created and return discovered file entry
SourceFileHandleT& res = MocEval().HeadersDiscovered[testPath];
if (!res) {
res = std::make_shared<SourceFileT>(testPath);
res->FileTime = fileTime;
res->Moc = true;
}
return res;
}
}
// Return without success
return SourceFileHandleT();
}
std::string cmQtAutoMocUic::JobEvaluateT::MocMessageTestHeaders(
std::string const& fileBase) const
{
std::ostringstream res;
{
std::string exts =
cmStrCat(".{", cmJoin(BaseConst().HeaderExtensions, ","),
'}'); // Compose result string
res << " " << fileBase << exts << '\n';
for (std::string const& path : MocConst().IncludePaths) {
res << " " << path << '/' << fileBase << exts << '\n';
}
}
return res.str();
}
bool cmQtAutoMocUic::JobEvaluateT::MocRegisterIncluded(
std::string const& includeString, SourceFileHandleT includerFileHandle,
SourceFileHandleT sourceFileHandle, bool sourceIsHeader) const
{
// Check if this file is already included
MappingHandleT& handle = MocEval().Includes[includeString];
if (handle) {
// Check if the output file would be generated from different source files
if (handle->SourceFile != sourceFileHandle) {
std::string msg = cmStrCat("The source files\n ",
Quoted(includerFileHandle->FileName), '\n');
for (auto const& item : handle->IncluderFiles) {
msg += " ";
msg += Quoted(item->FileName);
msg += '\n';
}
msg += "contain the same include string ";
msg += Quoted(includeString);
msg += ", but\nthe moc file would be generated from different "
"source files\n ";
msg += Quoted(sourceFileHandle->FileName);
msg += " and\n ";
msg += Quoted(handle->SourceFile->FileName);
msg += ".\nConsider to\n"
" - not include the \"moc_<NAME>.cpp\" file\n"
" - add a directory prefix to a \"<NAME>.moc\" include "
"(e.g \"sub/<NAME>.moc\")\n"
" - rename the source file(s)\n";
LogError(GenT::MOC, msg);
return false;
}
// The same mapping already exists. Just add to the includers list.
handle->IncluderFiles.emplace_back(std::move(includerFileHandle));
return true;
}
// Create a new mapping
handle = std::make_shared<MappingT>();
handle->IncludeString = includeString;
handle->IncluderFiles.emplace_back(std::move(includerFileHandle));
handle->SourceFile = std::move(sourceFileHandle);
handle->OutputFile += Gen()->AbsoluteIncludePath(includeString);
// Register mapping in sources/headers map
MocRegisterMapping(handle, sourceIsHeader);
return true;
}
void cmQtAutoMocUic::JobEvaluateT::MocRegisterMapping(
MappingHandleT mappingHandle, bool sourceIsHeader) const
{
auto& regMap =
sourceIsHeader ? MocEval().HeaderMappings : MocEval().SourceMappings;
// Check if source file already gets mapped
auto& regHandle = regMap[mappingHandle->SourceFile->FileName];
if (!regHandle) {
// Yet unknown mapping
regHandle = std::move(mappingHandle);
} else {
// Mappings with include string override those without
if (!mappingHandle->IncludeString.empty()) {
regHandle = std::move(mappingHandle);
}
}
}
bool cmQtAutoMocUic::JobEvaluateT::UicEval(SourceFileMapT const& fileMap)
{
for (auto const& pair : fileMap) {
if (!UicEvalFile(pair.second)) {
return false;
}
}
return true;
}
bool cmQtAutoMocUic::JobEvaluateT::UicEvalFile(
SourceFileHandleT sourceFileHandle)
{
SourceFileT const& sourceFile = *sourceFileHandle;
auto const& Include = sourceFile.ParseData->Uic.Include;
if (!sourceFile.Uic || Include.empty()) {
return true;
}
std::string const sourceDir = SubDirPrefix(sourceFile.FileName);
for (IncludeKeyT const& incKey : Include) {
// Find .ui file name
SourceFileHandleT uiFileHandle =
UicFindIncludedUi(sourceFile.FileName, sourceDir, incKey);
if (!uiFileHandle || UicConst().skipped(uiFileHandle->FileName)) {
continue;
}
// Register mapping
if (!UicRegisterMapping(incKey.Key, std::move(uiFileHandle),
std::move(sourceFileHandle))) {
return false;
}
}
return true;
}
bool cmQtAutoMocUic::JobEvaluateT::UicRegisterMapping(
std::string const& includeString, SourceFileHandleT uiFileHandle,
SourceFileHandleT includerFileHandle)
{
auto& Includes = Gen()->UicEval().Includes;
auto it = Includes.find(includeString);
if (it != Includes.end()) {
MappingHandleT const& handle = it->second;
if (handle->SourceFile != uiFileHandle) {
// The output file already gets generated - from a different .ui file!
std::string msg = cmStrCat("The source files\n ",
Quoted(includerFileHandle->FileName), '\n');
for (auto const& item : handle->IncluderFiles) {
msg += " ";
msg += Quoted(item->FileName);
msg += '\n';
}
msg += "contain the same include string ";
msg += Quoted(includeString);
msg += ", but\nthe uic file would be generated from different "
"user interface files\n ";
msg += Quoted(uiFileHandle->FileName);
msg += " and\n ";
msg += Quoted(handle->SourceFile->FileName);
msg += ".\nConsider to\n"
" - add a directory prefix to a \"ui_<NAME>.h\" include "
"(e.g \"sub/ui_<NAME>.h\")\n"
" - rename the <NAME>.ui file(s) and adjust the \"ui_<NAME>.h\" "
"include(s)\n";
LogError(GenT::UIC, msg);
return false;
}
// Add includer file to existing mapping
handle->IncluderFiles.emplace_back(std::move(includerFileHandle));
} else {
// New mapping handle
MappingHandleT handle = std::make_shared<MappingT>();
handle->IncludeString = includeString;
handle->IncluderFiles.emplace_back(std::move(includerFileHandle));
handle->SourceFile = std::move(uiFileHandle);
handle->OutputFile += Gen()->AbsoluteIncludePath(includeString);
// Register mapping
Includes.emplace(includeString, std::move(handle));
}
return true;
}
cmQtAutoMocUic::SourceFileHandleT
cmQtAutoMocUic::JobEvaluateT::UicFindIncludedUi(
std::string const& sourceFile, std::string const& sourceDir,
IncludeKeyT const& incKey) const
{
std::string searchFileName = cmStrCat(incKey.Base, ".ui");
// Collect search paths list
std::vector<std::string> testFiles;
{
auto& searchPaths = UicConst().SearchPaths;
testFiles.reserve((searchPaths.size() + 1) * 2);
// Vicinity of the source
testFiles.emplace_back(sourceDir + searchFileName);
if (!incKey.Dir.empty()) {
testFiles.emplace_back(cmStrCat(sourceDir, incKey.Dir, searchFileName));
}
// AUTOUIC search paths
if (!searchPaths.empty()) {
for (std::string const& sPath : searchPaths) {
testFiles.emplace_back(cmStrCat(sPath, '/', searchFileName));
}
if (!incKey.Dir.empty()) {
for (std::string const& sPath : searchPaths) {
testFiles.emplace_back(
cmStrCat(sPath, '/', incKey.Dir, searchFileName));
}
}
}
}
// Search for the .ui file!
for (std::string const& testFile : testFiles) {
cmFileTime fileTime;
if (fileTime.Load(testFile)) {
// .ui file found in files system!
std::string realPath = cmSystemTools::GetRealPath(testFile);
// Get or create .ui file handle
SourceFileHandleT& handle = Gen()->UicEval().UiFiles[realPath];
if (!handle) {
// The file wasn't registered, yet
handle = std::make_shared<SourceFileT>(realPath);
handle->FileTime = fileTime;
}
return handle;
}
}
// Log error
{
std::string msg =
cmStrCat("The file includes the uic file ", Quoted(incKey.Key),
",\nbut the user interface file ", Quoted(searchFileName),
"\ncould not be found in the following locations\n");
for (std::string const& testFile : testFiles) {
msg += " ";
msg += Quoted(testFile);
msg += '\n';
}
LogFileError(GenT::UIC, sourceFile, msg);
}
return SourceFileHandleT();
}
void cmQtAutoMocUic::JobGenerateT::Process()
{
// Add moc compile jobs
if (MocConst().Enabled) {
for (auto const& pair : MocEval().HeaderMappings) {
// Register if this mapping is a candidate for mocs_compilation.cpp
bool const compFile = pair.second->IncludeString.empty();
if (compFile) {
MocEval().CompFiles.emplace_back(pair.second->SourceFile->BuildPath);
}
if (!MocGenerate(pair.second, compFile)) {
return;
}
}
for (auto const& pair : MocEval().SourceMappings) {
if (!MocGenerate(pair.second, false)) {
return;
}
}
// Add mocs compilations job on demand
Gen()->WorkerPool().EmplaceJob<JobMocsCompilationT>();
}
// Add uic compile jobs
if (UicConst().Enabled) {
for (auto const& pair : Gen()->UicEval().Includes) {
if (!UicGenerate(pair.second)) {
return;
}
}
}
// Add finish job
Gen()->WorkerPool().EmplaceJob<JobFinishT>();
}
bool cmQtAutoMocUic::JobGenerateT::MocGenerate(MappingHandleT const& mapping,
bool compFile) const
{
std::unique_ptr<std::string> reason;
if (Log().Verbose()) {
reason = cm::make_unique<std::string>();
}
if (MocUpdate(*mapping, reason.get())) {
// Create the parent directory
if (!MakeParentDirectory(mapping->OutputFile)) {
LogFileError(GenT::MOC, mapping->OutputFile,
"Could not create parent directory.");
return false;
}
// Add moc job
Gen()->WorkerPool().EmplaceJob<JobMocT>(mapping, std::move(reason));
// Check if a moc job for a mocs_compilation.cpp entry was generated
if (compFile) {
MocEval().CompUpdated = true;
}
}
return true;
}
bool cmQtAutoMocUic::JobGenerateT::MocUpdate(MappingT const& mapping,
std::string* reason) const
{
std::string const& sourceFile = mapping.SourceFile->FileName;
std::string const& outputFile = mapping.OutputFile;
// Test if the output file exists
cmFileTime outputFileTime;
if (!outputFileTime.Load(outputFile)) {
if (reason != nullptr) {
*reason = "Generating ";
*reason += Quoted(outputFile);
*reason += ", because it doesn't exist, from ";
*reason += Quoted(sourceFile);
}
return true;
}
// Test if any setting changed
if (MocConst().SettingsChanged) {
if (reason != nullptr) {
*reason = "Generating ";
*reason += Quoted(outputFile);
*reason += ", because the uic settings changed, from ";
*reason += Quoted(sourceFile);
}
return true;
}
// Test if the source file is newer
if (outputFileTime.Older(mapping.SourceFile->FileTime)) {
if (reason != nullptr) {
*reason = "Generating ";
*reason += Quoted(outputFile);
*reason += ", because it's older than its source file, from ";
*reason += Quoted(sourceFile);
}
return true;
}
// Test if the moc_predefs file is newer
if (!MocConst().PredefsFileAbs.empty()) {
if (outputFileTime.Older(MocEval().PredefsTime)) {
if (reason != nullptr) {
*reason = "Generating ";
*reason += Quoted(outputFile);
*reason += ", because it's older than ";
*reason += Quoted(MocConst().PredefsFileAbs);
*reason += ", from ";
*reason += Quoted(sourceFile);
}
return true;
}
}
// Test if the moc executable is newer
if (outputFileTime.Older(MocConst().ExecutableTime)) {
if (reason != nullptr) {
*reason = "Generating ";
*reason += Quoted(outputFile);
*reason += ", because it's older than the moc executable, from ";
*reason += Quoted(sourceFile);
}
return true;
}
// Test if a dependency file is newer
{
// Check dependency timestamps
std::string const sourceDir = SubDirPrefix(sourceFile);
for (std::string const& dep : mapping.SourceFile->ParseData->Moc.Depends) {
// Find dependency file
auto const depMatch = MocFindDependency(sourceDir, dep);
if (depMatch.first.empty()) {
Log().WarningFile(GenT::MOC, sourceFile,
"Could not find dependency file " + Quoted(dep));
continue;
}
// Test if dependency file is older
if (outputFileTime.Older(depMatch.second)) {
if (reason != nullptr) {
*reason = "Generating ";
*reason += Quoted(outputFile);
*reason += ", because it's older than its dependency file ";
*reason += Quoted(depMatch.first);
*reason += ", from ";
*reason += Quoted(sourceFile);
}
return true;
}
}
}
return false;
}
std::pair<std::string, cmFileTime>
cmQtAutoMocUic::JobGenerateT::MocFindDependency(
std::string const& sourceDir, std::string const& includeString) const
{
typedef std::pair<std::string, cmFileTime> ResPair;
// Search in vicinity of the source
{
ResPair res{ sourceDir + includeString, {} };
if (res.second.Load(res.first)) {
return res;
}
}
// Search in include directories
for (std::string const& includePath : MocConst().IncludePaths) {
ResPair res{ includePath, {} };
res.first += '/';
res.first += includeString;
if (res.second.Load(res.first)) {
return res;
}
}
// Return empty
return ResPair();
}
bool cmQtAutoMocUic::JobGenerateT::UicGenerate(
MappingHandleT const& mapping) const
{
std::unique_ptr<std::string> reason;
if (Log().Verbose()) {
reason = cm::make_unique<std::string>();
}
if (UicUpdate(*mapping, reason.get())) {
// Create the parent directory
if (!MakeParentDirectory(mapping->OutputFile)) {
LogFileError(GenT::UIC, mapping->OutputFile,
"Could not create parent directory.");
return false;
}
// Add uic job
Gen()->WorkerPool().EmplaceJob<JobUicT>(mapping, std::move(reason));
}
return true;
}
bool cmQtAutoMocUic::JobGenerateT::UicUpdate(MappingT const& mapping,
std::string* reason) const
{
std::string const& sourceFile = mapping.SourceFile->FileName;
std::string const& outputFile = mapping.OutputFile;
// Test if the build file exists
cmFileTime outputFileTime;
if (!outputFileTime.Load(outputFile)) {
if (reason != nullptr) {
*reason = "Generating ";
*reason += Quoted(outputFile);
*reason += ", because it doesn't exist, from ";
*reason += Quoted(sourceFile);
}
return true;
}
// Test if the uic settings changed
if (UicConst().SettingsChanged) {
if (reason != nullptr) {
*reason = "Generating ";
*reason += Quoted(outputFile);
*reason += ", because the uic settings changed, from ";
*reason += Quoted(sourceFile);
}
return true;
}
// Test if the source file is newer
if (outputFileTime.Older(mapping.SourceFile->FileTime)) {
if (reason != nullptr) {
*reason = "Generating ";
*reason += Quoted(outputFile);
*reason += " because it's older than the source file ";
*reason += Quoted(sourceFile);
}
return true;
}
// Test if the uic executable is newer
if (outputFileTime.Older(UicConst().ExecutableTime)) {
if (reason != nullptr) {
*reason = "Generating ";
*reason += Quoted(outputFile);
*reason += ", because it's older than the uic executable, from ";
*reason += Quoted(sourceFile);
}
return true;
}
return false;
}
void cmQtAutoMocUic::JobMocT::Process()
{
std::string const& sourceFile = Mapping->SourceFile->FileName;
std::string const& outputFile = Mapping->OutputFile;
// Compose moc command
std::vector<std::string> cmd;
cmd.push_back(MocConst().Executable);
// Add options
cmAppend(cmd, MocConst().AllOptions);
// Add predefs include
if (!MocConst().PredefsFileAbs.empty()) {
cmd.emplace_back("--include");
cmd.push_back(MocConst().PredefsFileAbs);
}
cmd.emplace_back("-o");
cmd.push_back(outputFile);
cmd.push_back(sourceFile);
// Execute moc command
cmWorkerPool::ProcessResultT result;
if (RunProcess(GenT::MOC, result, cmd, Reason.get())) {
// Moc command success. Print moc output.
if (!result.StdOut.empty()) {
Log().Info(GenT::MOC, result.StdOut);
}
} else {
// Moc command failed
std::string msg =
cmStrCat("The moc process failed to compile\n ", Quoted(sourceFile),
"\ninto\n ", Quoted(outputFile));
if (Mapping->IncluderFiles.empty()) {
msg += ".\n";
} else {
msg += "\nincluded by\n";
for (auto const& item : Mapping->IncluderFiles) {
msg += " ";
msg += Quoted(item->FileName);
msg += '\n';
}
}
msg += result.ErrorMessage;
LogCommandError(GenT::MOC, msg, cmd, result.StdOut);
}
}
void cmQtAutoMocUic::JobUicT::Process()
{
std::string const& sourceFile = Mapping->SourceFile->FileName;
std::string const& outputFile = Mapping->OutputFile;
// Compose uic command
std::vector<std::string> cmd;
cmd.push_back(UicConst().Executable);
{
std::vector<std::string> allOpts = UicConst().TargetOptions;
auto optionIt = UicConst().Options.find(sourceFile);
if (optionIt != UicConst().Options.end()) {
UicMergeOptions(allOpts, optionIt->second,
(BaseConst().QtVersionMajor == 5));
}
cmAppend(cmd, allOpts);
}
cmd.emplace_back("-o");
cmd.emplace_back(outputFile);
cmd.emplace_back(sourceFile);
cmWorkerPool::ProcessResultT result;
if (RunProcess(GenT::UIC, result, cmd, Reason.get())) {
// Uic command success
// Print uic output
if (!result.StdOut.empty()) {
Log().Info(GenT::UIC, result.StdOut);
}
} else {
// Uic command failed
std::string msg =
cmStrCat("The uic process failed to compile\n ", Quoted(sourceFile),
"\ninto\n ", Quoted(outputFile), "\nincluded by\n");
for (auto const& item : Mapping->IncluderFiles) {
msg += " ";
msg += Quoted(item->FileName);
msg += '\n';
}
msg += result.ErrorMessage;
LogCommandError(GenT::UIC, msg, cmd, result.StdOut);
}
}
void cmQtAutoMocUic::JobMocsCompilationT::Process()
{
// Compose mocs compilation file content
std::string content =
"// This file is autogenerated. Changes will be overwritten.\n";
if (MocEval().CompFiles.empty()) {
// Placeholder content
content += "// No files found that require moc or the moc files are "
"included\n";
content += "enum some_compilers { need_more_than_nothing };\n";
} else {
// Valid content
char const clampB = BaseConst().MultiConfig ? '<' : '"';
char const clampE = BaseConst().MultiConfig ? '>' : '"';
for (std::string const& mocfile : MocEval().CompFiles) {
content += "#include ";
content += clampB;
content += mocfile;
content += clampE;
content += '\n';
}
}
std::string const& compAbs = MocConst().CompFileAbs;
if (cmQtAutoGenerator::FileDiffers(compAbs, content)) {
// Actually write mocs compilation file
if (Log().Verbose()) {
Log().Info(GenT::MOC, "Generating MOC compilation " + compAbs);
}
if (!FileWrite(compAbs, content)) {
LogFileError(GenT::MOC, compAbs,
"mocs compilation file writing failed.");
}
} else if (MocEval().CompUpdated) {
// Only touch mocs compilation file
if (Log().Verbose()) {
Log().Info(GenT::MOC, "Touching mocs compilation " + compAbs);
}
if (!cmSystemTools::Touch(compAbs, false)) {
LogFileError(GenT::MOC, compAbs,
"mocs compilation file touching failed.");
}
}
}
void cmQtAutoMocUic::JobFinishT::Process()
{
Gen()->AbortSuccess();
}
cmQtAutoMocUic::cmQtAutoMocUic() = default;
cmQtAutoMocUic::~cmQtAutoMocUic() = default;
bool cmQtAutoMocUic::Init(cmMakefile* makefile)
{
// Utility lambdas
auto InfoGet = [makefile](const char* key) {
return makefile->GetSafeDefinition(key);
};
auto InfoGetBool = [makefile](const char* key) {
return makefile->IsOn(key);
};
auto InfoGetList = [makefile](const char* key) -> std::vector<std::string> {
std::vector<std::string> list;
cmExpandList(makefile->GetSafeDefinition(key), list);
return list;
};
auto InfoGetLists =
[makefile](const char* key) -> std::vector<std::vector<std::string>> {
std::vector<std::vector<std::string>> lists;
{
std::string const value = makefile->GetSafeDefinition(key);
std::string::size_type pos = 0;
while (pos < value.size()) {
std::string::size_type next = value.find(ListSep, pos);
std::string::size_type length =
(next != std::string::npos) ? next - pos : value.size() - pos;
// Remove enclosing braces
if (length >= 2) {
std::string::const_iterator itBeg = value.begin() + (pos + 1);
std::string::const_iterator itEnd = itBeg + (length - 2);
lists.emplace_back(cmExpandedList(std::string(itBeg, itEnd)));
}
pos += length;
pos += ListSep.size();
}
}
return lists;
};
auto InfoGetConfig = [makefile, this](const char* key) -> std::string {
const char* valueConf = nullptr;
{
std::string keyConf = cmStrCat(key, '_', InfoConfig());
valueConf = makefile->GetDefinition(keyConf);
}
if (valueConf == nullptr) {
return makefile->GetSafeDefinition(key);
}
return std::string(valueConf);
};
auto InfoGetConfigList =
[&InfoGetConfig](const char* key) -> std::vector<std::string> {
std::vector<std::string> list;
cmExpandList(InfoGetConfig(key), list);
return list;
};
auto LogInfoError = [this](std::string const& msg) -> bool {
std::ostringstream err;
err << "In " << Quoted(this->InfoFile()) << ":\n" << msg;
this->Log().Error(GenT::GEN, err.str());
return false;
};
auto MatchSizes = [&LogInfoError](const char* keyA, const char* keyB,
std::size_t sizeA,
std::size_t sizeB) -> bool {
if (sizeA == sizeB) {
return true;
}
std::ostringstream err;
err << "Lists sizes mismatch " << keyA << '(' << sizeA << ") " << keyB
<< '(' << sizeB << ')';
return LogInfoError(err.str());
};
// -- Read info file
if (!makefile->ReadListFile(InfoFile())) {
return LogInfoError("File processing failed");
}
// -- Meta
Logger_.RaiseVerbosity(InfoGet("AM_VERBOSITY"));
BaseConst_.MultiConfig = InfoGetBool("AM_MULTI_CONFIG");
{
unsigned long num = 1;
if (cmStrToULong(InfoGet("AM_PARALLEL"), &num)) {
num = std::max<unsigned long>(num, 1);
num = std::min<unsigned long>(num, ParallelMax);
}
WorkerPool_.SetThreadCount(static_cast<unsigned int>(num));
}
BaseConst_.HeaderExtensions =
makefile->GetCMakeInstance()->GetHeaderExtensions();
// - Files and directories
BaseConst_.IncludeProjectDirsBefore =
InfoGetBool("AM_CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE");
BaseConst_.ProjectSourceDir = InfoGet("AM_CMAKE_SOURCE_DIR");
BaseConst_.ProjectBinaryDir = InfoGet("AM_CMAKE_BINARY_DIR");
BaseConst_.CurrentSourceDir = InfoGet("AM_CMAKE_CURRENT_SOURCE_DIR");
BaseConst_.CurrentBinaryDir = InfoGet("AM_CMAKE_CURRENT_BINARY_DIR");
BaseConst_.AutogenBuildDir = InfoGet("AM_BUILD_DIR");
if (BaseConst_.AutogenBuildDir.empty()) {
return LogInfoError("Autogen build directory missing.");
}
BaseConst_.AutogenIncludeDir = InfoGetConfig("AM_INCLUDE_DIR");
if (BaseConst_.AutogenIncludeDir.empty()) {
return LogInfoError("Autogen include directory missing.");
}
BaseConst_.CMakeExecutable = InfoGetConfig("AM_CMAKE_EXECUTABLE");
if (BaseConst_.CMakeExecutable.empty()) {
return LogInfoError("CMake executable file name missing.");
}
if (!BaseConst_.CMakeExecutableTime.Load(BaseConst_.CMakeExecutable)) {
std::string error =
cmStrCat("The CMake executable ", Quoted(BaseConst_.CMakeExecutable),
" does not exist.");
return LogInfoError(error);
}
BaseConst_.ParseCacheFile = InfoGetConfig("AM_PARSE_CACHE_FILE");
if (BaseConst_.ParseCacheFile.empty()) {
return LogInfoError("Parse cache file name missing.");
}
// - Settings file
SettingsFile_ = InfoGetConfig("AM_SETTINGS_FILE");
if (SettingsFile_.empty()) {
return LogInfoError("Settings file name missing.");
}
// - Qt environment
{
unsigned long qtv = BaseConst_.QtVersionMajor;
if (cmStrToULong(InfoGet("AM_QT_VERSION_MAJOR"), &qtv)) {
BaseConst_.QtVersionMajor = static_cast<unsigned int>(qtv);
}
}
// - Moc
MocConst_.Executable = InfoGet("AM_QT_MOC_EXECUTABLE");
if (!MocConst().Executable.empty()) {
MocConst_.Enabled = true;
// Load the executable file time
if (!MocConst_.ExecutableTime.Load(MocConst_.Executable)) {
std::string error =
cmStrCat("The moc executable ", Quoted(MocConst_.Executable),
" does not exist.");
return LogInfoError(error);
}
for (std::string& sfl : InfoGetList("AM_MOC_SKIP")) {
MocConst_.SkipList.insert(std::move(sfl));
}
MocConst_.Definitions = InfoGetConfigList("AM_MOC_DEFINITIONS");
MocConst_.IncludePaths = InfoGetConfigList("AM_MOC_INCLUDES");
MocConst_.Options = InfoGetList("AM_MOC_OPTIONS");
MocConst_.RelaxedMode = InfoGetBool("AM_MOC_RELAXED_MODE");
for (std::string const& item : InfoGetList("AM_MOC_MACRO_NAMES")) {
MocConst_.MacroFilters.emplace_back(
item, ("[\n][ \t]*{?[ \t]*" + item).append("[^a-zA-Z0-9_]"));
}
{
auto addFilter = [this, &LogInfoError](std::string const& key,
std::string const& exp) -> bool {
auto filterErr = [&LogInfoError, &key, &exp](const char* err) -> bool {
std::ostringstream ferr;
ferr << "AUTOMOC_DEPEND_FILTERS: " << err << '\n';
ferr << " Key: " << Quoted(key) << '\n';
ferr << " Exp: " << Quoted(exp) << '\n';
return LogInfoError(ferr.str());
};
if (key.empty()) {
return filterErr("Key is empty");
}
if (exp.empty()) {
return filterErr("Regular expression is empty");
}
this->MocConst_.DependFilters.emplace_back(key, exp);
if (!this->MocConst_.DependFilters.back().Exp.is_valid()) {
return filterErr("Regular expression compiling failed");
}
return true;
};
// Insert default filter for Q_PLUGIN_METADATA
if (BaseConst().QtVersionMajor != 4) {
if (!addFilter("Q_PLUGIN_METADATA",
"[\n][ \t]*Q_PLUGIN_METADATA[ \t]*\\("
"[^\\)]*FILE[ \t]*\"([^\"]+)\"")) {
return false;
}
}
// Insert user defined dependency filters
std::vector<std::string> flts = InfoGetList("AM_MOC_DEPEND_FILTERS");
if ((flts.size() % 2) != 0) {
return LogInfoError(
"AUTOMOC_DEPEND_FILTERS list size is not a multiple of 2");
}
for (auto itC = flts.begin(), itE = flts.end(); itC != itE; itC += 2) {
if (!addFilter(*itC, *(itC + 1))) {
return false;
}
}
}
MocConst_.PredefsCmd = InfoGetList("AM_MOC_PREDEFS_CMD");
}
// - Uic
UicConst_.Executable = InfoGet("AM_QT_UIC_EXECUTABLE");
if (!UicConst().Executable.empty()) {
UicConst_.Enabled = true;
// Load the executable file time
if (!UicConst_.ExecutableTime.Load(UicConst_.Executable)) {
std::string error =
cmStrCat("The uic executable ", Quoted(UicConst_.Executable),
" does not exist.");
return LogInfoError(error);
}
for (std::string& sfl : InfoGetList("AM_UIC_SKIP")) {
UicConst_.SkipList.insert(std::move(sfl));
}
UicConst_.SearchPaths = InfoGetList("AM_UIC_SEARCH_PATHS");
UicConst_.TargetOptions = InfoGetConfigList("AM_UIC_TARGET_OPTIONS");
{
const char* keyFiles = "AM_UIC_OPTIONS_FILES";
const char* keyOpts = "AM_UIC_OPTIONS_OPTIONS";
auto sources = InfoGetList(keyFiles);
auto options = InfoGetLists(keyOpts);
if (!MatchSizes(keyFiles, keyOpts, sources.size(), options.size())) {
return false;
}
auto fitEnd = sources.cend();
auto fit = sources.begin();
auto oit = options.begin();
while (fit != fitEnd) {
UicConst_.Options[*fit] = std::move(*oit);
++fit;
++oit;
}
}
}
// - Headers and sources
{
auto makeSource =
[&LogInfoError](std::string const& fileName,
std::string const& fileFlags) -> SourceFileHandleT {
if (fileFlags.size() != 2) {
LogInfoError("Invalid file flags string size");
return SourceFileHandleT();
}
cmFileTime fileTime;
if (!fileTime.Load(fileName)) {
LogInfoError("The source file " + cmQtAutoGen::Quoted(fileName) +
" does not exist.");
return SourceFileHandleT();
}
SourceFileHandleT sfh = std::make_shared<SourceFileT>(fileName);
sfh->FileTime = fileTime;
sfh->Moc = (fileFlags[0] == 'M');
sfh->Uic = (fileFlags[1] == 'U');
return sfh;
};
// Headers
{
// Get file lists
const char *keyFiles = "AM_HEADERS", *keyFlags = "AM_HEADERS_FLAGS";
std::vector<std::string> files = InfoGetList(keyFiles);
std::vector<std::string> flags = InfoGetList(keyFlags);
std::vector<std::string> builds;
if (!MatchSizes(keyFiles, keyFlags, files.size(), flags.size())) {
return false;
}
if (MocConst().Enabled) {
const char* keyPaths = "AM_HEADERS_BUILD_PATHS";
builds = InfoGetList(keyPaths);
if (!MatchSizes(keyFiles, keyPaths, files.size(), builds.size())) {
return false;
}
}
// Process file lists
for (std::size_t ii = 0; ii != files.size(); ++ii) {
std::string& fileName(files[ii]);
SourceFileHandleT sfh = makeSource(fileName, flags[ii]);
if (!sfh) {
return false;
}
if (MocConst().Enabled) {
sfh->BuildPath = std::move(builds[ii]);
if (sfh->BuildPath.empty()) {
Log().ErrorFile(GenT::GEN, this->InfoFile(),
"Header file build path is empty");
return false;
}
}
BaseEval().Headers.emplace(std::move(fileName), std::move(sfh));
}
}
// Sources
{
const char *keyFiles = "AM_SOURCES", *keyFlags = "AM_SOURCES_FLAGS";
std::vector<std::string> files = InfoGetList(keyFiles);
std::vector<std::string> flags = InfoGetList(keyFlags);
if (!MatchSizes(keyFiles, keyFlags, files.size(), flags.size())) {
return false;
}
// Process file lists
for (std::size_t ii = 0; ii != files.size(); ++ii) {
std::string& fileName(files[ii]);
SourceFileHandleT sfh = makeSource(fileName, flags[ii]);
if (!sfh) {
return false;
}
BaseEval().Sources.emplace(std::move(fileName), std::move(sfh));
}
}
}
// Init derived information
// ------------------------
// Moc variables
if (MocConst().Enabled) {
// Mocs compilation file
MocConst_.CompFileAbs = AbsoluteBuildPath("mocs_compilation.cpp");
// Moc predefs file
if (!MocConst_.PredefsCmd.empty()) {
MocConst_.PredefsFileRel = "moc_predefs";
if (BaseConst_.MultiConfig) {
MocConst_.PredefsFileRel += '_';
MocConst_.PredefsFileRel += InfoConfig();
}
MocConst_.PredefsFileRel += ".h";
MocConst_.PredefsFileAbs = AbsoluteBuildPath(MocConst().PredefsFileRel);
}
// Sort include directories on demand
if (BaseConst().IncludeProjectDirsBefore) {
// Move strings to temporary list
std::list<std::string> includes(MocConst().IncludePaths.begin(),
MocConst().IncludePaths.end());
MocConst_.IncludePaths.clear();
MocConst_.IncludePaths.reserve(includes.size());
// Append project directories only
{
std::array<std::string const*, 2> const movePaths = {
{ &BaseConst().ProjectBinaryDir, &BaseConst().ProjectSourceDir }
};
for (std::string const* ppath : movePaths) {
std::list<std::string>::iterator it = includes.begin();
while (it != includes.end()) {
std::string const& path = *it;
if (cmHasPrefix(path, *ppath)) {
MocConst_.IncludePaths.push_back(path);
it = includes.erase(it);
} else {
++it;
}
}
}
}
// Append remaining directories
MocConst_.IncludePaths.insert(MocConst_.IncludePaths.end(),
includes.begin(), includes.end());
}
// Compose moc includes list
{
std::set<std::string> frameworkPaths;
for (std::string const& path : MocConst().IncludePaths) {
MocConst_.Includes.push_back("-I" + path);
// Extract framework path
if (cmHasLiteralSuffix(path, ".framework/Headers")) {
// Go up twice to get to the framework root
std::vector<std::string> pathComponents;
cmSystemTools::SplitPath(path, pathComponents);
frameworkPaths.emplace(cmSystemTools::JoinPath(
pathComponents.begin(), pathComponents.end() - 2));
}
}
// Append framework includes
for (std::string const& path : frameworkPaths) {
MocConst_.Includes.emplace_back("-F");
MocConst_.Includes.push_back(path);
}
}
// Setup single list with all options
{
// Add includes
MocConst_.AllOptions.insert(MocConst_.AllOptions.end(),
MocConst().Includes.begin(),
MocConst().Includes.end());
// Add definitions
for (std::string const& def : MocConst().Definitions) {
MocConst_.AllOptions.push_back("-D" + def);
}
// Add options
MocConst_.AllOptions.insert(MocConst_.AllOptions.end(),
MocConst().Options.begin(),
MocConst().Options.end());
}
}
return true;
}
template <class JOBTYPE>
void cmQtAutoMocUic::CreateParseJobs(SourceFileMapT const& sourceMap)
{
cmFileTime const parseCacheTime = BaseEval().ParseCacheTime;
ParseCacheT& parseCache = BaseEval().ParseCache;
for (auto& src : sourceMap) {
// Get or create the file parse data reference
ParseCacheT::GetOrInsertT cacheEntry = parseCache.GetOrInsert(src.first);
src.second->ParseData = std::move(cacheEntry.first);
// Create a parse job if the cache file was missing or is older
if (cacheEntry.second || src.second->FileTime.Newer(parseCacheTime)) {
BaseEval().ParseCacheChanged = true;
WorkerPool().EmplaceJob<JOBTYPE>(src.second);
}
}
}
void cmQtAutoMocUic::InitJobs()
{
// Add moc_predefs.h job
if (MocConst().Enabled && !MocConst().PredefsCmd.empty()) {
WorkerPool().EmplaceJob<JobMocPredefsT>();
}
// Add header parse jobs
CreateParseJobs<JobParseHeaderT>(BaseEval().Headers);
// Add source parse jobs
CreateParseJobs<JobParseSourceT>(BaseEval().Sources);
// Add evaluate job
WorkerPool().EmplaceJob<JobEvaluateT>();
}
bool cmQtAutoMocUic::Process()
{
SettingsFileRead();
ParseCacheRead();
if (!CreateDirectories()) {
return false;
}
InitJobs();
if (!WorkerPool_.Process(this)) {
return false;
}
if (JobError_) {
return false;
}
if (!ParseCacheWrite()) {
return false;
}
if (!SettingsFileWrite()) {
return false;
}
return true;
}
void cmQtAutoMocUic::SettingsFileRead()
{
// Compose current settings strings
{
cmCryptoHash cryptoHash(cmCryptoHash::AlgoSHA256);
std::string const sep(";");
auto cha = [&cryptoHash, &sep](std::string const& value) {
cryptoHash.Append(value);
cryptoHash.Append(sep);
};
if (MocConst_.Enabled) {
cryptoHash.Initialize();
cha(MocConst().Executable);
for (auto const& value : MocConst().AllOptions) {
cha(value);
}
cha(BaseConst().IncludeProjectDirsBefore ? "TRUE" : "FALSE");
for (auto const& value : MocConst().PredefsCmd) {
cha(value);
}
for (auto const& filter : MocConst().DependFilters) {
cha(filter.Key);
}
for (auto const& filter : MocConst().MacroFilters) {
cha(filter.Key);
}
SettingsStringMoc_ = cryptoHash.FinalizeHex();
}
if (UicConst().Enabled) {
cryptoHash.Initialize();
cha(UicConst().Executable);
for (auto const& value : UicConst().TargetOptions) {
cha(value);
}
for (const auto& item : UicConst().Options) {
cha(item.first);
for (auto const& svalue : item.second) {
cha(svalue);
}
}
SettingsStringUic_ = cryptoHash.FinalizeHex();
}
}
// Read old settings and compare
{
std::string content;
if (cmQtAutoGenerator::FileRead(content, SettingsFile_)) {
if (MocConst().Enabled) {
if (SettingsStringMoc_ != SettingsFind(content, "moc")) {
MocConst_.SettingsChanged = true;
}
}
if (UicConst().Enabled) {
if (SettingsStringUic_ != SettingsFind(content, "uic")) {
UicConst_.SettingsChanged = true;
}
}
// In case any setting changed remove the old settings file.
// This triggers a full rebuild on the next run if the current
// build is aborted before writing the current settings in the end.
if (MocConst().SettingsChanged || UicConst().SettingsChanged) {
cmSystemTools::RemoveFile(SettingsFile_);
}
} else {
// Settings file read failed
if (MocConst().Enabled) {
MocConst_.SettingsChanged = true;
}
if (UicConst().Enabled) {
UicConst_.SettingsChanged = true;
}
}
}
}
bool cmQtAutoMocUic::SettingsFileWrite()
{
// Only write if any setting changed
if (MocConst().SettingsChanged || UicConst().SettingsChanged) {
if (Log().Verbose()) {
Log().Info(GenT::GEN, "Writing settings file " + Quoted(SettingsFile_));
}
// Compose settings file content
std::string content;
{
auto SettingAppend = [&content](const char* key,
std::string const& value) {
if (!value.empty()) {
content += key;
content += ':';
content += value;
content += '\n';
}
};
SettingAppend("moc", SettingsStringMoc_);
SettingAppend("uic", SettingsStringUic_);
}
// Write settings file
std::string error;
if (!cmQtAutoGenerator::FileWrite(SettingsFile_, content, &error)) {
Log().ErrorFile(GenT::GEN, SettingsFile_,
"Settings file writing failed. " + error);
// Remove old settings file to trigger a full rebuild on the next run
cmSystemTools::RemoveFile(SettingsFile_);
return false;
}
}
return true;
}
void cmQtAutoMocUic::ParseCacheRead()
{
const char* reason = nullptr;
// Don't read the cache if it is invalid
if (!BaseEval().ParseCacheTime.Load(BaseConst().ParseCacheFile)) {
reason = "Refreshing parse cache because it doesn't exist.";
} else if (MocConst().SettingsChanged || UicConst().SettingsChanged) {
reason = "Refreshing parse cache because the settings changed.";
} else if (BaseEval().ParseCacheTime.Older(
BaseConst().CMakeExecutableTime)) {
reason =
"Refreshing parse cache because it is older than the CMake executable.";
}
if (reason != nullptr) {
// Don't read but refresh the complete parse cache
if (Log().Verbose()) {
Log().Info(GenT::GEN, reason);
}
BaseEval().ParseCacheChanged = true;
} else {
// Read parse cache
BaseEval().ParseCache.ReadFromFile(BaseConst().ParseCacheFile);
}
}
bool cmQtAutoMocUic::ParseCacheWrite()
{
if (BaseEval().ParseCacheChanged) {
if (Log().Verbose()) {
Log().Info(GenT::GEN,
"Writing parse cache file " +
Quoted(BaseConst().ParseCacheFile));
}
if (!BaseEval().ParseCache.WriteToFile(BaseConst().ParseCacheFile)) {
Log().ErrorFile(GenT::GEN, BaseConst().ParseCacheFile,
"Parse cache file writing failed.");
return false;
}
}
return true;
}
bool cmQtAutoMocUic::CreateDirectories()
{
// Create AUTOGEN include directory
if (!cmSystemTools::MakeDirectory(BaseConst().AutogenIncludeDir)) {
Log().ErrorFile(GenT::GEN, BaseConst().AutogenIncludeDir,
"Could not create directory.");
return false;
}
return true;
}
void cmQtAutoMocUic::Abort(bool error)
{
if (error) {
JobError_.store(true);
}
WorkerPool_.Abort();
}
std::string cmQtAutoMocUic::AbsoluteBuildPath(
std::string const& relativePath) const
{
std::string res(BaseConst().AutogenBuildDir);
res += '/';
res += relativePath;
return res;
}
std::string cmQtAutoMocUic::AbsoluteIncludePath(
std::string const& relativePath) const
{
std::string res(BaseConst().AutogenIncludeDir);
res += '/';
res += relativePath;
return res;
}
|