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
2351
2352
2353
2354
2355
2356
2357
2358
2359
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmMakefileTargetGenerator.h"
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdio>
#include <iterator>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <cm/memory>
#include <cm/string_view>
#include <cmext/algorithm>
#include <cmext/string_view>
#include "cm_codecvt.hxx"
#include "cmComputeLinkInformation.h"
#include "cmCustomCommand.h"
#include "cmCustomCommandGenerator.h"
#include "cmFileSet.h"
#include "cmGeneratedFileStream.h"
#include "cmGeneratorExpression.h"
#include "cmGeneratorTarget.h"
#include "cmGlobalUnixMakefileGenerator3.h"
#include "cmLinkLineComputer.h" // IWYU pragma: keep
#include "cmList.h"
#include "cmLocalCommonGenerator.h"
#include "cmLocalGenerator.h"
#include "cmLocalUnixMakefileGenerator3.h"
#include "cmMakefile.h"
#include "cmMakefileExecutableTargetGenerator.h"
#include "cmMakefileLibraryTargetGenerator.h"
#include "cmMakefileUtilityTargetGenerator.h"
#include "cmMessageType.h"
#include "cmOutputConverter.h"
#include "cmPolicies.h"
#include "cmRange.h"
#include "cmRulePlaceholderExpander.h"
#include "cmSourceFile.h"
#include "cmSourceFileLocationKind.h"
#include "cmState.h"
#include "cmStateDirectory.h"
#include "cmStateSnapshot.h"
#include "cmStateTypes.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmTarget.h"
#include "cmValue.h"
#include "cmake.h"
cmMakefileTargetGenerator::cmMakefileTargetGenerator(cmGeneratorTarget* target)
: cmCommonTargetGenerator(target)
{
this->CustomCommandDriver = OnBuild;
this->LocalGenerator =
static_cast<cmLocalUnixMakefileGenerator3*>(target->GetLocalGenerator());
this->GlobalGenerator = static_cast<cmGlobalUnixMakefileGenerator3*>(
this->LocalGenerator->GetGlobalGenerator());
cmake* cm = this->GlobalGenerator->GetCMakeInstance();
this->NoRuleMessages = false;
if (cmValue ruleStatus =
cm->GetState()->GetGlobalProperty("RULE_MESSAGES")) {
this->NoRuleMessages = cmIsOff(*ruleStatus);
}
switch (this->GeneratorTarget->GetPolicyStatusCMP0113()) {
case cmPolicies::WARN:
CM_FALLTHROUGH;
case cmPolicies::OLD:
this->CMP0113New = false;
break;
case cmPolicies::NEW:
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
this->CMP0113New = true;
break;
}
this->MacOSXContentGenerator =
cm::make_unique<MacOSXContentGeneratorType>(this);
}
cmMakefileTargetGenerator::~cmMakefileTargetGenerator() = default;
std::unique_ptr<cmMakefileTargetGenerator> cmMakefileTargetGenerator::New(
cmGeneratorTarget* tgt)
{
std::unique_ptr<cmMakefileTargetGenerator> result;
switch (tgt->GetType()) {
case cmStateEnums::EXECUTABLE:
result = cm::make_unique<cmMakefileExecutableTargetGenerator>(tgt);
break;
case cmStateEnums::STATIC_LIBRARY:
case cmStateEnums::SHARED_LIBRARY:
case cmStateEnums::MODULE_LIBRARY:
case cmStateEnums::OBJECT_LIBRARY:
result = cm::make_unique<cmMakefileLibraryTargetGenerator>(tgt);
break;
case cmStateEnums::INTERFACE_LIBRARY:
case cmStateEnums::UTILITY:
result = cm::make_unique<cmMakefileUtilityTargetGenerator>(tgt);
break;
default:
return result;
// break; /* unreachable */
}
return result;
}
std::string cmMakefileTargetGenerator::GetConfigName() const
{
auto const& configNames = this->LocalGenerator->GetConfigNames();
assert(configNames.size() == 1);
return configNames.front();
}
void cmMakefileTargetGenerator::GetDeviceLinkFlags(
std::string& linkFlags, const std::string& linkLanguage)
{
cmGeneratorTarget::DeviceLinkSetter setter(*this->GetGeneratorTarget());
std::vector<std::string> linkOpts;
this->GeneratorTarget->GetLinkOptions(linkOpts, this->GetConfigName(),
linkLanguage);
// LINK_OPTIONS are escaped.
this->LocalGenerator->AppendCompileOptions(linkFlags, linkOpts);
}
void cmMakefileTargetGenerator::GetTargetLinkFlags(
std::string& flags, const std::string& linkLanguage)
{
this->LocalGenerator->AppendFlags(
flags, this->GeneratorTarget->GetSafeProperty("LINK_FLAGS"));
std::string linkFlagsConfig =
cmStrCat("LINK_FLAGS_", cmSystemTools::UpperCase(this->GetConfigName()));
this->LocalGenerator->AppendFlags(
flags, this->GeneratorTarget->GetSafeProperty(linkFlagsConfig));
std::vector<std::string> opts;
this->GeneratorTarget->GetLinkOptions(opts, this->GetConfigName(),
linkLanguage);
// LINK_OPTIONS are escaped.
this->LocalGenerator->AppendCompileOptions(flags, opts);
this->LocalGenerator->AppendPositionIndependentLinkerFlags(
flags, this->GeneratorTarget, this->GetConfigName(), linkLanguage);
this->LocalGenerator->AppendDependencyInfoLinkerFlags(
flags, this->GeneratorTarget, this->GetConfigName(), linkLanguage);
}
void cmMakefileTargetGenerator::CreateRuleFile()
{
// Create a directory for this target.
this->TargetBuildDirectory =
this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
this->TargetBuildDirectoryFull =
this->LocalGenerator->ConvertToFullPath(this->TargetBuildDirectory);
cmSystemTools::MakeDirectory(this->TargetBuildDirectoryFull);
// Construct the rule file name.
this->BuildFileName = cmStrCat(this->TargetBuildDirectory, "/build.make");
this->BuildFileNameFull =
cmStrCat(this->TargetBuildDirectoryFull, "/build.make");
// Construct the rule file name.
this->ProgressFileNameFull =
cmStrCat(this->TargetBuildDirectoryFull, "/progress.make");
// reset the progress count
this->NumberOfProgressActions = 0;
// Open the rule file. This should be copy-if-different because the
// rules may depend on this file itself.
this->BuildFileStream = cm::make_unique<cmGeneratedFileStream>(
this->BuildFileNameFull, false,
this->GlobalGenerator->GetMakefileEncoding());
if (!this->BuildFileStream) {
return;
}
this->BuildFileStream->SetCopyIfDifferent(true);
this->LocalGenerator->WriteDisclaimer(*this->BuildFileStream);
if (this->GlobalGenerator->AllowDeleteOnError()) {
std::vector<std::string> no_depends;
std::vector<std::string> no_commands;
this->LocalGenerator->WriteMakeRule(
*this->BuildFileStream, "Delete rule output on recipe failure.",
".DELETE_ON_ERROR", no_depends, no_commands, false);
}
this->LocalGenerator->WriteSpecialTargetsTop(*this->BuildFileStream);
}
void cmMakefileTargetGenerator::WriteTargetBuildRules()
{
this->GeneratorTarget->CheckCxxModuleStatus(this->GetConfigName());
if (this->GeneratorTarget->HaveCxx20ModuleSources()) {
this->Makefile->IssueMessage(
MessageType::FATAL_ERROR,
cmStrCat("The \"", this->GeneratorTarget->GetName(),
"\" target contains C++ module sources which are not supported "
"by the generator"));
}
// -- Write the custom commands for this target
// Evaluates generator expressions and expands prop_value
auto evaluatedFiles = [this](const std::string& prop_value) -> cmList {
cmList files{ cmGeneratorExpression::Evaluate(
prop_value, this->LocalGenerator, this->GetConfigName(),
this->GeneratorTarget) };
return files;
};
// Look for additional files registered for cleaning in this directory.
if (cmValue prop_value =
this->Makefile->GetProperty("ADDITIONAL_MAKE_CLEAN_FILES")) {
auto const files = evaluatedFiles(*prop_value);
this->CleanFiles.insert(files.begin(), files.end());
}
// Look for additional files registered for cleaning in this target.
if (cmValue prop_value =
this->GeneratorTarget->GetProperty("ADDITIONAL_CLEAN_FILES")) {
auto const files = evaluatedFiles(*prop_value);
// For relative path support
std::string const& binaryDir =
this->LocalGenerator->GetCurrentBinaryDirectory();
for (std::string const& cfl : files) {
this->CleanFiles.insert(cmSystemTools::CollapseFullPath(cfl, binaryDir));
}
}
// Look for ISPC extra object files generated by this target
auto ispcAdditionalObjs =
this->GeneratorTarget->GetGeneratedISPCObjects(this->GetConfigName());
for (std::string const& ispcObj : ispcAdditionalObjs) {
this->CleanFiles.insert(
this->LocalGenerator->MaybeRelativeToCurBinDir(ispcObj));
}
// add custom commands to the clean rules?
bool clean = cmIsOff(this->Makefile->GetProperty("CLEAN_NO_CUSTOM"));
// First generate the object rule files. Save a list of all object
// files for this target.
std::vector<cmSourceFile const*> customCommands;
this->GeneratorTarget->GetCustomCommands(customCommands,
this->GetConfigName());
for (cmSourceFile const* sf : customCommands) {
if (this->CMP0113New &&
!this->LocalGenerator->GetCommandsVisited(this->GeneratorTarget)
.insert(sf)
.second) {
continue;
}
cmCustomCommandGenerator ccg(*sf->GetCustomCommand(),
this->GetConfigName(), this->LocalGenerator);
this->GenerateCustomRuleFile(ccg);
if (clean) {
const std::vector<std::string>& outputs = ccg.GetOutputs();
for (std::string const& output : outputs) {
this->CleanFiles.insert(
this->LocalGenerator->MaybeRelativeToCurBinDir(output));
}
const std::vector<std::string>& byproducts = ccg.GetByproducts();
for (std::string const& byproduct : byproducts) {
this->CleanFiles.insert(
this->LocalGenerator->MaybeRelativeToCurBinDir(byproduct));
}
}
}
// Add byproducts from build events to the clean rules
if (clean) {
std::vector<cmCustomCommand> buildEventCommands =
this->GeneratorTarget->GetPreBuildCommands();
cm::append(buildEventCommands,
this->GeneratorTarget->GetPreLinkCommands());
cm::append(buildEventCommands,
this->GeneratorTarget->GetPostBuildCommands());
for (const auto& be : buildEventCommands) {
cmCustomCommandGenerator beg(be, this->GetConfigName(),
this->LocalGenerator);
const std::vector<std::string>& byproducts = beg.GetByproducts();
for (std::string const& byproduct : byproducts) {
this->CleanFiles.insert(
this->LocalGenerator->MaybeRelativeToCurBinDir(byproduct));
}
}
}
std::vector<cmSourceFile const*> headerSources;
this->GeneratorTarget->GetHeaderSources(headerSources,
this->GetConfigName());
this->OSXBundleGenerator->GenerateMacOSXContentStatements(
headerSources, this->MacOSXContentGenerator.get(), this->GetConfigName());
std::vector<cmSourceFile const*> extraSources;
this->GeneratorTarget->GetExtraSources(extraSources, this->GetConfigName());
this->OSXBundleGenerator->GenerateMacOSXContentStatements(
extraSources, this->MacOSXContentGenerator.get(), this->GetConfigName());
cmValue pchExtension = this->Makefile->GetDefinition("CMAKE_PCH_EXTENSION");
std::vector<cmSourceFile const*> externalObjects;
this->GeneratorTarget->GetExternalObjects(externalObjects,
this->GetConfigName());
for (cmSourceFile const* sf : externalObjects) {
auto const& objectFileName = sf->GetFullPath();
if (!cmHasSuffix(objectFileName, pchExtension)) {
this->ExternalObjects.push_back(objectFileName);
}
}
std::map<std::string, std::string> file_set_map;
auto const* tgt = this->GeneratorTarget->Target;
for (auto const& name : tgt->GetAllFileSetNames()) {
auto const* file_set = tgt->GetFileSet(name);
if (!file_set) {
this->Makefile->IssueMessage(
MessageType::INTERNAL_ERROR,
cmStrCat("Target \"", tgt->GetName(),
"\" is tracked to have file set \"", name,
"\", but it was not found."));
continue;
}
auto fileEntries = file_set->CompileFileEntries();
auto directoryEntries = file_set->CompileDirectoryEntries();
auto directories = file_set->EvaluateDirectoryEntries(
directoryEntries, this->LocalGenerator, this->GetConfigName(),
this->GeneratorTarget);
std::map<std::string, std::vector<std::string>> files;
for (auto const& entry : fileEntries) {
file_set->EvaluateFileEntry(directories, files, entry,
this->LocalGenerator, this->GetConfigName(),
this->GeneratorTarget);
}
for (auto const& it : files) {
for (auto const& filename : it.second) {
file_set_map[filename] = file_set->GetType();
}
}
}
std::vector<cmSourceFile const*> objectSources;
this->GeneratorTarget->GetObjectSources(objectSources,
this->GetConfigName());
// validate that all languages requested are enabled.
std::set<std::string> requiredLangs;
if (this->HaveRequiredLanguages(objectSources, requiredLangs)) {
for (cmSourceFile const* sf : objectSources) {
// Generate this object file's rule file.
this->WriteObjectRuleFiles(*sf);
}
}
for (cmSourceFile const* sf : objectSources) {
auto const& path = sf->GetFullPath();
auto const it = file_set_map.find(path);
if (it != file_set_map.end()) {
auto const& file_set_type = it->second;
if (file_set_type == "CXX_MODULES"_s) {
if (sf->GetLanguage() != "CXX"_s) {
this->Makefile->IssueMessage(
MessageType::FATAL_ERROR,
cmStrCat(
"Target \"", tgt->GetName(), "\" contains the source\n ", path,
"\nin a file set of type \"", file_set_type,
R"(" but the source is not classified as a "CXX" source.)"));
}
}
}
}
}
void cmMakefileTargetGenerator::WriteCommonCodeRules()
{
const char* root = (this->Makefile->IsOn("CMAKE_MAKE_INCLUDE_FROM_ROOT")
? "$(CMAKE_BINARY_DIR)/"
: "");
// Include the dependencies for the target.
std::string dependFileNameFull =
cmStrCat(this->TargetBuildDirectoryFull, "/depend.make");
*this->BuildFileStream
<< "# Include any dependencies generated for this target.\n"
<< this->GlobalGenerator->IncludeDirective << " " << root
<< cmSystemTools::ConvertToOutputPath(
this->LocalGenerator->MaybeRelativeToTopBinDir(dependFileNameFull))
<< "\n";
// Scan any custom commands to check if DEPFILE option is specified
bool ccGenerateDeps = false;
std::vector<cmSourceFile const*> customCommands;
this->GeneratorTarget->GetCustomCommands(customCommands,
this->GetConfigName());
for (cmSourceFile const* sf : customCommands) {
if (!sf->GetCustomCommand()->GetDepfile().empty()) {
ccGenerateDeps = true;
break;
}
}
std::string depsUseCompiler = "CMAKE_DEPENDS_USE_COMPILER";
bool compilerGenerateDeps =
this->GlobalGenerator->SupportsCompilerDependencies() &&
(!this->Makefile->IsDefinitionSet(depsUseCompiler) ||
this->Makefile->IsOn(depsUseCompiler));
bool linkerGenerateDeps =
this->GeneratorTarget->HasLinkDependencyFile(this->GetConfigName());
if (compilerGenerateDeps || linkerGenerateDeps || ccGenerateDeps) {
std::string const compilerDependFile =
cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.make");
*this->BuildFileStream << "# Include any dependencies generated by the "
"compiler for this target.\n"
<< this->GlobalGenerator->IncludeDirective << " "
<< root
<< cmSystemTools::ConvertToOutputPath(
this->LocalGenerator->MaybeRelativeToTopBinDir(
compilerDependFile))
<< "\n\n";
// Write an empty dependency file.
cmGeneratedFileStream depFileStream(
compilerDependFile, false, this->GlobalGenerator->GetMakefileEncoding());
depFileStream << "# Empty compiler generated dependencies file for "
<< this->GeneratorTarget->GetName() << ".\n"
<< "# This may be replaced when dependencies are built.\n";
// remove internal dependency file
cmSystemTools::RemoveFile(
cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.internal"));
std::string compilerDependTimestamp =
cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.ts");
if (!cmSystemTools::FileExists(compilerDependTimestamp)) {
// Write a dependency timestamp file.
cmGeneratedFileStream timestampFileStream(
compilerDependTimestamp, false,
this->GlobalGenerator->GetMakefileEncoding());
timestampFileStream
<< "# CMAKE generated file: DO NOT EDIT!\n"
<< "# Timestamp file for compiler generated dependencies "
"management for "
<< this->GeneratorTarget->GetName() << ".\n";
}
}
if (compilerGenerateDeps) {
// deactivate no longer needed legacy dependency files
// Write an empty dependency file.
cmGeneratedFileStream legacyDepFileStream(
dependFileNameFull, false, this->GlobalGenerator->GetMakefileEncoding());
legacyDepFileStream
<< "# Empty dependencies file for " << this->GeneratorTarget->GetName()
<< ".\n"
<< "# This may be replaced when dependencies are built.\n";
// remove internal dependency file
cmSystemTools::RemoveFile(
cmStrCat(this->TargetBuildDirectoryFull, "/depend.internal"));
} else {
// make sure the depend file exists
if (!cmSystemTools::FileExists(dependFileNameFull)) {
// Write an empty dependency file.
cmGeneratedFileStream depFileStream(
dependFileNameFull, false,
this->GlobalGenerator->GetMakefileEncoding());
depFileStream << "# Empty dependencies file for "
<< this->GeneratorTarget->GetName() << ".\n"
<< "# This may be replaced when dependencies are built.\n";
}
}
if (!this->NoRuleMessages) {
// Include the progress variables for the target.
*this->BuildFileStream
<< "# Include the progress variables for this target.\n"
<< this->GlobalGenerator->IncludeDirective << " " << root
<< cmSystemTools::ConvertToOutputPath(
this->LocalGenerator->MaybeRelativeToTopBinDir(
this->ProgressFileNameFull))
<< "\n\n";
}
// Open the flags file. This should be copy-if-different because the
// rules may depend on this file itself.
this->FlagFileNameFull =
cmStrCat(this->TargetBuildDirectoryFull, "/flags.make");
this->FlagFileStream = cm::make_unique<cmGeneratedFileStream>(
this->FlagFileNameFull, false,
this->GlobalGenerator->GetMakefileEncoding());
if (!this->FlagFileStream) {
return;
}
this->FlagFileStream->SetCopyIfDifferent(true);
this->LocalGenerator->WriteDisclaimer(*this->FlagFileStream);
// Include the flags for the target.
*this->BuildFileStream
<< "# Include the compile flags for this target's objects.\n"
<< this->GlobalGenerator->IncludeDirective << " " << root
<< cmSystemTools::ConvertToOutputPath(
this->LocalGenerator->MaybeRelativeToTopBinDir(
this->FlagFileNameFull))
<< "\n\n";
}
void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
{
// write language flags for target
std::set<std::string> languages;
this->GeneratorTarget->GetLanguages(
languages, this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
// put the compiler in the rules.make file so that if it changes
// things rebuild
for (std::string const& language : languages) {
std::string compiler = cmStrCat("CMAKE_", language, "_COMPILER");
*this->FlagFileStream << "# compile " << language << " with "
<< this->Makefile->GetSafeDefinition(compiler)
<< "\n";
}
bool const escapeOctothorpe = this->GlobalGenerator->CanEscapeOctothorpe();
for (std::string const& language : languages) {
std::string defines = this->GetDefines(language, this->GetConfigName());
std::string includes = this->GetIncludes(language, this->GetConfigName());
if (escapeOctothorpe) {
// Escape comment characters so they do not terminate assignment.
cmSystemTools::ReplaceString(defines, "#", "\\#");
cmSystemTools::ReplaceString(includes, "#", "\\#");
}
*this->FlagFileStream << language << "_DEFINES = " << defines << "\n\n";
*this->FlagFileStream << language << "_INCLUDES = " << includes << "\n\n";
std::vector<std::string> architectures =
this->GeneratorTarget->GetAppleArchs(this->GetConfigName(), language);
architectures.emplace_back();
for (const std::string& arch : architectures) {
std::string flags =
this->GetFlags(language, this->GetConfigName(), arch);
if (escapeOctothorpe) {
cmSystemTools::ReplaceString(flags, "#", "\\#");
}
*this->FlagFileStream << language << "_FLAGS" << arch << " = " << flags
<< "\n\n";
}
}
}
void cmMakefileTargetGenerator::MacOSXContentGeneratorType::operator()(
cmSourceFile const& source, const char* pkgloc, const std::string& config)
{
// Skip OS X content when not building a Framework or Bundle.
if (!this->Generator->GetGeneratorTarget()->IsBundleOnApple()) {
return;
}
std::string macdir =
this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc,
config);
// Get the input file location.
std::string const& input = source.GetFullPath();
// Get the output file location.
std::string output =
cmStrCat(macdir, '/', cmSystemTools::GetFilenameName(input));
this->Generator->CleanFiles.insert(
this->Generator->LocalGenerator->MaybeRelativeToCurBinDir(output));
output = this->Generator->LocalGenerator->MaybeRelativeToTopBinDir(output);
// Create a rule to copy the content into the bundle.
std::vector<std::string> depends;
std::vector<std::string> commands;
depends.push_back(input);
std::string copyEcho = cmStrCat("Copying OS X content ", output);
this->Generator->LocalGenerator->AppendEcho(
commands, copyEcho, cmLocalUnixMakefileGenerator3::EchoBuild);
std::string copyCommand =
cmStrCat("$(CMAKE_COMMAND) -E copy ",
this->Generator->LocalGenerator->ConvertToOutputFormat(
input, cmOutputConverter::SHELL),
' ',
this->Generator->LocalGenerator->ConvertToOutputFormat(
output, cmOutputConverter::SHELL));
commands.push_back(std::move(copyCommand));
this->Generator->LocalGenerator->WriteMakeRule(
*this->Generator->BuildFileStream, nullptr, output, depends, commands,
false);
this->Generator->ExtraFiles.insert(output);
}
void cmMakefileTargetGenerator::WriteObjectRuleFiles(
cmSourceFile const& source)
{
// Identify the language of the source file.
const std::string& lang = source.GetLanguage();
if (lang.empty()) {
// don't know anything about this file so skip it
return;
}
// Use compiler to generate dependencies, if supported.
bool const compilerGenerateDeps =
this->GlobalGenerator->SupportsCompilerDependencies() &&
cmIsOn(this->Makefile->GetDefinition(
cmStrCat("CMAKE_", lang, "_DEPENDS_USE_COMPILER")));
auto const scanner = compilerGenerateDeps ? cmDependencyScannerKind::Compiler
: cmDependencyScannerKind::CMake;
// Get the full path name of the object file.
std::string const& objectName =
this->GeneratorTarget->GetObjectName(&source);
std::string const obj =
cmStrCat(this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget),
'/', objectName);
// Avoid generating duplicate rules.
if (this->ObjectFiles.find(obj) == this->ObjectFiles.end()) {
this->ObjectFiles.insert(obj);
} else {
std::ostringstream err;
err << "Warning: Source file \"" << source.GetFullPath()
<< "\" is listed multiple times for target \""
<< this->GeneratorTarget->GetName() << "\".";
cmSystemTools::Message(err.str(), "Warning");
return;
}
// Create the directory containing the object file. This may be a
// subdirectory under the target's directory.
{
std::string const dir = cmSystemTools::GetFilenamePath(obj);
cmSystemTools::MakeDirectory(this->LocalGenerator->ConvertToFullPath(dir));
}
// Save this in the target's list of object files.
this->Objects.push_back(obj);
this->CleanFiles.insert(obj);
std::vector<std::string> depends;
// The object file should be checked for dependency integrity.
std::string objFullPath =
cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(), '/', obj);
objFullPath = cmSystemTools::CollapseFullPath(objFullPath);
std::string const srcFullPath =
cmSystemTools::CollapseFullPath(source.GetFullPath());
this->LocalGenerator->AddImplicitDepends(this->GeneratorTarget, lang,
objFullPath, srcFullPath, scanner);
this->LocalGenerator->AppendRuleDepend(depends,
this->FlagFileNameFull.c_str());
this->LocalGenerator->AppendRuleDepends(depends,
this->FlagFileDepends[lang]);
// generate the depend scanning rule
this->WriteObjectDependRules(source, depends);
std::string const config = this->GetConfigName();
std::string const configUpper = cmSystemTools::UpperCase(config);
// Add precompile headers dependencies
std::vector<std::string> architectures =
this->GeneratorTarget->GetAppleArchs(config, lang);
if (architectures.empty()) {
architectures.emplace_back();
}
std::string filterArch;
std::unordered_map<std::string, std::string> pchSources;
for (const std::string& arch : architectures) {
const std::string pchSource =
this->GeneratorTarget->GetPchSource(config, lang, arch);
if (pchSource == source.GetFullPath()) {
filterArch = arch;
}
if (!pchSource.empty()) {
pchSources.insert(std::make_pair(pchSource, arch));
}
}
if (!pchSources.empty() && !source.GetProperty("SKIP_PRECOMPILE_HEADERS")) {
for (const std::string& arch : architectures) {
std::string const& pchHeader =
this->GeneratorTarget->GetPchHeader(config, lang, arch);
depends.push_back(pchHeader);
if (pchSources.find(source.GetFullPath()) == pchSources.end()) {
depends.push_back(
this->GeneratorTarget->GetPchFile(config, lang, arch));
}
this->LocalGenerator->AddImplicitDepends(
this->GeneratorTarget, lang, objFullPath, pchHeader, scanner);
}
}
if (lang != "ISPC") {
auto const& headers =
this->GeneratorTarget->GetGeneratedISPCHeaders(config);
if (!headers.empty()) {
depends.insert(depends.end(), headers.begin(), headers.end());
}
}
std::string relativeObj =
cmStrCat(this->LocalGenerator->GetHomeRelativeOutputPath(), obj);
// Write the build rule.
// Build the set of compiler flags.
std::string flags;
// Explicitly add the explicit language flag before any other flag
// so user flags can override it.
this->GeneratorTarget->AddExplicitLanguageFlags(flags, source);
// Add language-specific flags.
std::string const langFlags =
cmStrCat("$(", lang, "_FLAGS", filterArch, ")");
this->LocalGenerator->AppendFlags(flags, langFlags);
cmGeneratorExpressionInterpreter genexInterpreter(
this->LocalGenerator, config, this->GeneratorTarget, lang);
// Add Fortran format flags.
if (lang == "Fortran") {
this->AppendFortranFormatFlags(flags, source);
this->AppendFortranPreprocessFlags(flags, source);
}
std::string ispcHeaderRelative;
std::string ispcHeaderForShell;
if (lang == "ISPC") {
std::string ispcSource =
cmSystemTools::GetFilenameWithoutLastExtension(objectName);
ispcSource = cmSystemTools::GetFilenameWithoutLastExtension(ispcSource);
cmValue const ispcSuffixProp =
this->GeneratorTarget->GetProperty("ISPC_HEADER_SUFFIX");
assert(ispcSuffixProp);
std::string directory = this->GeneratorTarget->GetObjectDirectory(config);
if (cmValue prop =
this->GeneratorTarget->GetProperty("ISPC_HEADER_DIRECTORY")) {
directory =
cmStrCat(this->LocalGenerator->GetBinaryDirectory(), '/', *prop);
}
ispcHeaderRelative = cmStrCat(directory, '/', ispcSource, *ispcSuffixProp);
ispcHeaderForShell = this->LocalGenerator->ConvertToOutputFormat(
ispcHeaderRelative, cmOutputConverter::SHELL);
}
// Add flags from source file properties.
const std::string COMPILE_FLAGS("COMPILE_FLAGS");
if (cmValue cflags = source.GetProperty(COMPILE_FLAGS)) {
const std::string& evaluatedFlags =
genexInterpreter.Evaluate(*cflags, COMPILE_FLAGS);
this->LocalGenerator->AppendFlags(flags, evaluatedFlags);
*this->FlagFileStream << "# Custom flags: " << relativeObj
<< "_FLAGS = " << evaluatedFlags << "\n"
<< "\n";
}
const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
if (cmValue coptions = source.GetProperty(COMPILE_OPTIONS)) {
const std::string& evaluatedOptions =
genexInterpreter.Evaluate(*coptions, COMPILE_OPTIONS);
this->LocalGenerator->AppendCompileOptions(flags, evaluatedOptions);
*this->FlagFileStream << "# Custom options: " << relativeObj
<< "_OPTIONS = " << evaluatedOptions << "\n"
<< "\n";
}
// Add precompile headers compile options.
if (!pchSources.empty() && !source.GetProperty("SKIP_PRECOMPILE_HEADERS")) {
std::string pchOptions;
auto const pchIt = pchSources.find(source.GetFullPath());
if (pchIt != pchSources.end()) {
pchOptions = this->GeneratorTarget->GetPchCreateCompileOptions(
config, lang, pchIt->second);
} else {
pchOptions =
this->GeneratorTarget->GetPchUseCompileOptions(config, lang);
}
const std::string& evaluatedFlags =
genexInterpreter.Evaluate(pchOptions, COMPILE_OPTIONS);
this->LocalGenerator->AppendCompileOptions(flags, evaluatedFlags);
*this->FlagFileStream << "# PCH options: " << relativeObj
<< "_OPTIONS = " << evaluatedFlags << "\n"
<< "\n";
}
// Add include directories from source file properties.
std::vector<std::string> includes;
const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
if (cmValue cincludes = source.GetProperty(INCLUDE_DIRECTORIES)) {
const std::string& evaluatedIncludes =
genexInterpreter.Evaluate(*cincludes, INCLUDE_DIRECTORIES);
this->LocalGenerator->AppendIncludeDirectories(includes, evaluatedIncludes,
source);
*this->FlagFileStream << "# Custom include directories: " << relativeObj
<< "_INCLUDE_DIRECTORIES = " << evaluatedIncludes
<< "\n"
<< "\n";
}
// Add language-specific defines.
std::set<std::string> defines;
// Add source-specific preprocessor definitions.
const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
if (cmValue compile_defs = source.GetProperty(COMPILE_DEFINITIONS)) {
const std::string& evaluatedDefs =
genexInterpreter.Evaluate(*compile_defs, COMPILE_DEFINITIONS);
this->LocalGenerator->AppendDefines(defines, evaluatedDefs);
*this->FlagFileStream << "# Custom defines: " << relativeObj
<< "_DEFINES = " << evaluatedDefs << "\n"
<< "\n";
}
std::string const defPropName =
cmStrCat("COMPILE_DEFINITIONS_", configUpper);
if (cmValue config_compile_defs = source.GetProperty(defPropName)) {
const std::string& evaluatedDefs =
genexInterpreter.Evaluate(*config_compile_defs, COMPILE_DEFINITIONS);
this->LocalGenerator->AppendDefines(defines, evaluatedDefs);
*this->FlagFileStream << "# Custom defines: " << relativeObj << "_DEFINES_"
<< configUpper << " = " << evaluatedDefs << "\n"
<< "\n";
}
// Get the output paths for source and object files.
std::string const sourceFile = this->LocalGenerator->ConvertToOutputFormat(
source.GetFullPath(), cmOutputConverter::SHELL);
// Construct the build message.
std::vector<std::string> no_depends;
std::vector<std::string> commands;
// add in a progress call if needed
this->NumberOfProgressActions++;
if (!this->NoRuleMessages) {
cmLocalUnixMakefileGenerator3::EchoProgress progress;
this->MakeEchoProgress(progress);
std::string buildEcho =
cmStrCat("Building ", lang, " object ", relativeObj);
this->LocalGenerator->AppendEcho(commands, buildEcho,
cmLocalUnixMakefileGenerator3::EchoBuild,
&progress);
}
std::string targetOutPathReal;
std::string targetOutPathPDB;
std::string targetOutPathCompilePDB;
{
std::string targetFullPathReal;
std::string targetFullPathPDB;
std::string targetFullPathCompilePDB =
this->ComputeTargetCompilePDB(this->GetConfigName());
if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
targetFullPathReal = this->GeneratorTarget->GetFullPath(
this->GetConfigName(), cmStateEnums::RuntimeBinaryArtifact, true);
targetFullPathPDB = cmStrCat(
this->GeneratorTarget->GetPDBDirectory(this->GetConfigName()), '/',
this->GeneratorTarget->GetPDBName(this->GetConfigName()));
}
targetOutPathReal = this->LocalGenerator->ConvertToOutputFormat(
this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathReal),
cmOutputConverter::SHELL);
targetOutPathPDB = this->LocalGenerator->ConvertToOutputFormat(
targetFullPathPDB, cmOutputConverter::SHELL);
targetOutPathCompilePDB = this->LocalGenerator->ConvertToOutputFormat(
this->LocalGenerator->MaybeRelativeToCurBinDir(targetFullPathCompilePDB),
cmOutputConverter::SHELL);
if (this->LocalGenerator->IsMinGWMake() &&
cmHasLiteralSuffix(targetOutPathCompilePDB, "\\")) {
// mingw32-make incorrectly interprets 'a\ b c' as 'a b' and 'c'
// (but 'a\ b "c"' as 'a\', 'b', and 'c'!). Workaround this by
// avoiding a trailing backslash in the argument.
targetOutPathCompilePDB.back() = '/';
}
std::string const compilePdbOutputPath =
this->GeneratorTarget->GetCompilePDBDirectory(this->GetConfigName());
cmSystemTools::MakeDirectory(compilePdbOutputPath);
}
cmRulePlaceholderExpander::RuleVariables vars;
vars.CMTargetName = this->GeneratorTarget->GetName().c_str();
vars.CMTargetType =
cmState::GetTargetTypeName(this->GeneratorTarget->GetType()).c_str();
vars.Language = lang.c_str();
vars.Target = targetOutPathReal.c_str();
vars.TargetPDB = targetOutPathPDB.c_str();
vars.TargetCompilePDB = targetOutPathCompilePDB.c_str();
vars.Source = sourceFile.c_str();
std::string const shellObj =
this->LocalGenerator->ConvertToOutputFormat(obj, cmOutputConverter::SHELL);
vars.Object = shellObj.c_str();
std::string objectDir = this->GeneratorTarget->GetSupportDirectory();
objectDir = this->LocalGenerator->ConvertToOutputFormat(
this->LocalGenerator->MaybeRelativeToCurBinDir(objectDir),
cmOutputConverter::SHELL);
vars.ObjectDir = objectDir.c_str();
std::string objectFileDir = cmSystemTools::GetFilenamePath(obj);
objectFileDir = this->LocalGenerator->ConvertToOutputFormat(
this->LocalGenerator->MaybeRelativeToCurBinDir(objectFileDir),
cmOutputConverter::SHELL);
vars.ObjectFileDir = objectFileDir.c_str();
vars.Flags = flags.c_str();
vars.ISPCHeader = ispcHeaderForShell.c_str();
std::string definesString = cmStrCat("$(", lang, "_DEFINES)");
this->LocalGenerator->JoinDefines(defines, definesString, lang);
vars.Defines = definesString.c_str();
std::string includesString = this->LocalGenerator->GetIncludeFlags(
includes, this->GeneratorTarget, lang, config);
this->LocalGenerator->AppendFlags(includesString,
"$(" + lang + "_INCLUDES)");
vars.Includes = includesString.c_str();
std::string dependencyTarget;
std::string shellDependencyFile;
std::string dependencyTimestamp;
if (compilerGenerateDeps) {
dependencyTarget = this->LocalGenerator->EscapeForShell(
this->LocalGenerator->ConvertToMakefilePath(
this->LocalGenerator->MaybeRelativeToTopBinDir(relativeObj)));
vars.DependencyTarget = dependencyTarget.c_str();
auto depFile = cmStrCat(obj, ".d");
shellDependencyFile = this->LocalGenerator->ConvertToOutputFormat(
depFile, cmOutputConverter::SHELL);
vars.DependencyFile = shellDependencyFile.c_str();
this->CleanFiles.insert(depFile);
dependencyTimestamp = this->LocalGenerator->MaybeRelativeToTopBinDir(
cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.ts"));
}
// At the moment, it is assumed that C, C++, Fortran, and CUDA have both
// assembly and preprocessor capabilities. The same is true for the
// ability to export compile commands
bool const lang_has_preprocessor =
((lang == "C") || (lang == "CXX") || (lang == "OBJC") ||
(lang == "OBJCXX") || (lang == "Fortran") || (lang == "CUDA") ||
lang == "ISPC" || lang == "HIP" || lang == "ASM");
bool const lang_has_assembly = lang_has_preprocessor;
bool const lang_can_export_cmds = lang_has_preprocessor;
auto rulePlaceholderExpander =
this->LocalGenerator->CreateRulePlaceholderExpander();
// Construct the compile rules.
{
std::string cudaCompileMode;
if (lang == "CUDA") {
if (this->GeneratorTarget->GetPropertyAsBool(
"CUDA_SEPARABLE_COMPILATION")) {
const std::string& rdcFlag =
this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_RDC_FLAG");
cudaCompileMode = cmStrCat(cudaCompileMode, rdcFlag, " ");
}
static std::array<cm::string_view, 4> const compileModes{
{ "PTX"_s, "CUBIN"_s, "FATBIN"_s, "OPTIX"_s }
};
bool useNormalCompileMode = true;
for (cm::string_view mode : compileModes) {
auto propName = cmStrCat("CUDA_", mode, "_COMPILATION");
auto defName = cmStrCat("_CMAKE_CUDA_", mode, "_FLAG");
if (this->GeneratorTarget->GetPropertyAsBool(propName)) {
const std::string& flag =
this->Makefile->GetRequiredDefinition(defName);
cudaCompileMode = cmStrCat(cudaCompileMode, flag);
useNormalCompileMode = false;
break;
}
}
if (useNormalCompileMode) {
const std::string& wholeFlag =
this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_WHOLE_FLAG");
cudaCompileMode = cmStrCat(cudaCompileMode, wholeFlag);
}
vars.CudaCompileMode = cudaCompileMode.c_str();
}
cmList compileCommands;
const std::string& compileRule = this->Makefile->GetRequiredDefinition(
"CMAKE_" + lang + "_COMPILE_OBJECT");
compileCommands.assign(compileRule);
if (this->GeneratorTarget->GetPropertyAsBool("EXPORT_COMPILE_COMMANDS") &&
lang_can_export_cmds && compileCommands.size() == 1) {
std::string compileCommand = compileCommands[0];
// no launcher for CMAKE_EXPORT_COMPILE_COMMANDS
rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
compileCommand, vars);
std::string const workingDirectory =
this->LocalGenerator->GetCurrentBinaryDirectory();
std::string::size_type lfPos = compileCommand.find(langFlags);
if (lfPos != std::string::npos) {
compileCommand.replace(lfPos, langFlags.size(),
this->GetFlags(lang, this->GetConfigName()));
}
std::string const langDefines = std::string("$(") + lang + "_DEFINES)";
std::string::size_type const ldPos = compileCommand.find(langDefines);
if (ldPos != std::string::npos) {
compileCommand.replace(ldPos, langDefines.size(),
this->GetDefines(lang, this->GetConfigName()));
}
std::string const langIncludes = std::string("$(") + lang + "_INCLUDES)";
std::string::size_type const liPos = compileCommand.find(langIncludes);
if (liPos != std::string::npos) {
compileCommand.replace(liPos, langIncludes.size(),
this->GetIncludes(lang, this->GetConfigName()));
}
cmValue const eliminate[] = {
this->Makefile->GetDefinition("CMAKE_START_TEMP_FILE"),
this->Makefile->GetDefinition("CMAKE_END_TEMP_FILE")
};
for (cmValue const& el : eliminate) {
if (el) {
cmSystemTools::ReplaceString(compileCommand, *el, "");
}
}
this->GlobalGenerator->AddCXXCompileCommand(
source.GetFullPath(), workingDirectory, compileCommand, relativeObj);
}
// See if we need to use a compiler launcher like ccache or distcc
std::string compilerLauncher;
if (!compileCommands.empty()) {
compilerLauncher = GetCompilerLauncher(lang, config);
}
cmValue const skipCodeCheck = source.GetProperty("SKIP_LINTING");
if (!skipCodeCheck.IsOn()) {
std::string const codeCheck = this->GenerateCodeCheckRules(
source, compilerLauncher, "$(CMAKE_COMMAND)", config, nullptr);
if (!codeCheck.empty()) {
compileCommands.front().insert(0, codeCheck);
}
}
// If compiler launcher was specified and not consumed above, it
// goes to the beginning of the command line.
if (!compileCommands.empty() && !compilerLauncher.empty()) {
cmList args{ compilerLauncher, cmList::EmptyElements::Yes };
if (!args.empty()) {
args[0] = this->LocalGenerator->ConvertToOutputFormat(
args[0], cmOutputConverter::SHELL);
for (std::string& i : cmMakeRange(args.begin() + 1, args.end())) {
i = this->LocalGenerator->EscapeForShell(i);
}
}
compileCommands.front().insert(0, args.join(" ") + " ");
}
std::string launcher;
{
std::string val = this->LocalGenerator->GetRuleLauncher(
this->GeneratorTarget, "RULE_LAUNCH_COMPILE",
this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
if (cmNonempty(val)) {
launcher = cmStrCat(val, ' ');
}
}
std::string flagsWithDeps(flags);
if (compilerGenerateDeps) {
// Injects dependency computation
auto depFlags = this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_DEPFILE_FLAGS_", lang));
if (!depFlags.empty()) {
// Add dependency flags
rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
depFlags, vars);
flagsWithDeps.append(1, ' ');
flagsWithDeps.append(depFlags);
}
vars.Flags = flagsWithDeps.c_str();
const auto& extraCommands = this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_", lang, "_DEPENDS_EXTRA_COMMANDS"));
if (!extraCommands.empty()) {
compileCommands.append(extraCommands);
}
const auto& depFormat = this->Makefile->GetRequiredDefinition(
cmStrCat("CMAKE_", lang, "_DEPFILE_FORMAT"));
if (depFormat == "msvc"_s) {
// compiler must be launched through a wrapper to pick-up dependencies
std::string depFilter =
"$(CMAKE_COMMAND) -E cmake_cl_compile_depends ";
depFilter += cmStrCat("--dep-file=", shellDependencyFile);
depFilter +=
cmStrCat(" --working-dir=",
this->LocalGenerator->ConvertToOutputFormat(
this->LocalGenerator->GetCurrentBinaryDirectory(),
cmOutputConverter::SHELL));
const auto& prefix = this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_", lang, "_CL_SHOWINCLUDES_PREFIX"));
depFilter += cmStrCat(" --filter-prefix=",
this->LocalGenerator->ConvertToOutputFormat(
prefix, cmOutputConverter::SHELL));
depFilter += " -- ";
compileCommands.front().insert(0, depFilter);
}
}
// Expand placeholders in the commands.
for (std::string& compileCommand : compileCommands) {
compileCommand = cmStrCat(launcher, compileCommand);
rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
compileCommand, vars);
}
// Change the command working directory to the local build tree.
this->LocalGenerator->CreateCDCommand(
compileCommands, this->LocalGenerator->GetCurrentBinaryDirectory(),
this->LocalGenerator->GetBinaryDirectory());
cm::append(commands, compileCommands);
}
// Check for extra outputs created by the compilation.
cmList outputs;
outputs.emplace_back(relativeObj);
if (cmValue extra_outputs_str = source.GetProperty("OBJECT_OUTPUTS")) {
std::string evaluated_outputs = cmGeneratorExpression::Evaluate(
*extra_outputs_str, this->LocalGenerator, config);
if (!evaluated_outputs.empty()) {
// Register these as extra files to clean.
outputs.append(evaluated_outputs);
}
}
if (!ispcHeaderRelative.empty()) {
// can't move ispcHeader as vars is using it
outputs.emplace_back(ispcHeaderRelative);
}
if (outputs.size() > 1) {
this->CleanFiles.insert(outputs.begin() + 1, outputs.end());
}
if (compilerGenerateDeps) {
depends.push_back(dependencyTimestamp);
}
// Write the rule.
this->WriteMakeRule(*this->BuildFileStream, nullptr, outputs, depends,
commands);
if (compilerGenerateDeps) {
// set back flags without dependency generation
vars.Flags = flags.c_str();
}
bool do_preprocess_rules = lang_has_preprocessor &&
this->LocalGenerator->GetCreatePreprocessedSourceRules();
bool do_assembly_rules =
lang_has_assembly && this->LocalGenerator->GetCreateAssemblySourceRules();
if (do_preprocess_rules || do_assembly_rules) {
std::vector<std::string> force_depends;
force_depends.emplace_back("cmake_force");
std::string::size_type dot_pos = relativeObj.rfind('.');
std::string relativeObjBase = relativeObj.substr(0, dot_pos);
dot_pos = obj.rfind('.');
std::string objBase = obj.substr(0, dot_pos);
if (do_preprocess_rules) {
commands.clear();
std::string const relativeObjI = relativeObjBase + ".i";
std::string const objI = objBase + ".i";
std::string preprocessEcho =
cmStrCat("Preprocessing ", lang, " source to ", objI);
this->LocalGenerator->AppendEcho(
commands, preprocessEcho, cmLocalUnixMakefileGenerator3::EchoBuild);
std::string preprocessRuleVar =
cmStrCat("CMAKE_", lang, "_CREATE_PREPROCESSED_SOURCE");
if (cmValue preprocessRule =
this->Makefile->GetDefinition(preprocessRuleVar)) {
cmList preprocessCommands{ *preprocessRule };
std::string shellObjI = this->LocalGenerator->ConvertToOutputFormat(
objI, cmOutputConverter::SHELL);
vars.PreprocessedSource = shellObjI.c_str();
// Expand placeholders in the commands.
for (std::string& preprocessCommand : preprocessCommands) {
// no launcher for preprocessor commands
rulePlaceholderExpander->ExpandRuleVariables(
this->LocalGenerator, preprocessCommand, vars);
}
this->LocalGenerator->CreateCDCommand(
preprocessCommands,
this->LocalGenerator->GetCurrentBinaryDirectory(),
this->LocalGenerator->GetBinaryDirectory());
cm::append(commands, preprocessCommands);
} else {
std::string cmd =
cmStrCat("$(CMAKE_COMMAND) -E cmake_unimplemented_variable ",
preprocessRuleVar);
commands.push_back(std::move(cmd));
}
this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
relativeObjI, force_depends,
commands, false);
}
if (do_assembly_rules) {
commands.clear();
std::string relativeObjS = relativeObjBase + ".s";
std::string objS = objBase + ".s";
std::string assemblyEcho =
cmStrCat("Compiling ", lang, " source to assembly ", objS);
this->LocalGenerator->AppendEcho(
commands, assemblyEcho, cmLocalUnixMakefileGenerator3::EchoBuild);
std::string assemblyRuleVar =
cmStrCat("CMAKE_", lang, "_CREATE_ASSEMBLY_SOURCE");
if (cmValue assemblyRule =
this->Makefile->GetDefinition(assemblyRuleVar)) {
cmList assemblyCommands{ *assemblyRule };
std::string shellObjS = this->LocalGenerator->ConvertToOutputFormat(
objS, cmOutputConverter::SHELL);
vars.AssemblySource = shellObjS.c_str();
// Expand placeholders in the commands.
for (std::string& assemblyCommand : assemblyCommands) {
// no launcher for assembly commands
rulePlaceholderExpander->ExpandRuleVariables(this->LocalGenerator,
assemblyCommand, vars);
}
this->LocalGenerator->CreateCDCommand(
assemblyCommands, this->LocalGenerator->GetCurrentBinaryDirectory(),
this->LocalGenerator->GetBinaryDirectory());
cm::append(commands, assemblyCommands);
} else {
std::string cmd =
cmStrCat("$(CMAKE_COMMAND) -E cmake_unimplemented_variable ",
assemblyRuleVar);
commands.push_back(std::move(cmd));
}
this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
relativeObjS, force_depends,
commands, false);
}
}
}
void cmMakefileTargetGenerator::WriteTargetCleanRules()
{
std::vector<std::string> depends;
std::vector<std::string> commands;
// Construct the clean target name.
std::string const cleanTarget = cmStrCat(
this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget),
"/clean");
// Construct the clean command.
this->LocalGenerator->AppendCleanCommand(commands, this->CleanFiles,
this->GeneratorTarget);
this->LocalGenerator->CreateCDCommand(
commands, this->LocalGenerator->GetCurrentBinaryDirectory(),
this->LocalGenerator->GetBinaryDirectory());
// Write the rule.
this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
cleanTarget, depends, commands, true);
}
bool cmMakefileTargetGenerator::WriteMakeRule(
std::ostream& os, const char* comment,
const std::vector<std::string>& outputs,
const std::vector<std::string>& depends,
const std::vector<std::string>& commands, bool in_help)
{
bool symbolic = false;
if (outputs.empty()) {
return symbolic;
}
// Check whether we need to bother checking for a symbolic output.
bool const need_symbolic = this->GlobalGenerator->GetNeedSymbolicMark();
// Check whether the first output is marked as symbolic.
if (need_symbolic) {
if (cmSourceFile* sf = this->Makefile->GetSource(outputs[0])) {
symbolic = sf->GetPropertyAsBool("SYMBOLIC");
}
}
// We always attach the actual commands to the first output.
this->LocalGenerator->WriteMakeRule(os, comment, outputs[0], depends,
commands, symbolic, in_help);
// For single outputs, we are done.
if (outputs.size() == 1) {
return symbolic;
}
// For multiple outputs, make the extra ones depend on the first one.
std::vector<std::string> const output_depends(1, outputs[0]);
for (std::string const& output : cmMakeRange(outputs).advance(1)) {
// Touch the extra output so "make" knows that it was updated,
// but only if the output was actually created.
std::string const out = this->LocalGenerator->ConvertToOutputFormat(
this->LocalGenerator->MaybeRelativeToTopBinDir(output),
cmOutputConverter::SHELL);
std::vector<std::string> output_commands;
bool o_symbolic = false;
if (need_symbolic) {
if (cmSourceFile const* sf = this->Makefile->GetSource(output)) {
o_symbolic = sf->GetPropertyAsBool("SYMBOLIC");
}
}
symbolic = symbolic && o_symbolic;
if (!o_symbolic) {
output_commands.push_back("@$(CMAKE_COMMAND) -E touch_nocreate " + out);
}
this->LocalGenerator->WriteMakeRule(os, nullptr, output, output_depends,
output_commands, o_symbolic, in_help);
if (!o_symbolic) {
// At build time, remove the first output if this one does not exist
// so that "make" will rerun the real commands that create this one.
MultipleOutputPairsType::value_type p(output, outputs[0]);
this->MultipleOutputPairs.insert(p);
}
}
return symbolic;
}
void cmMakefileTargetGenerator::WriteTargetLinkDependRules()
{
if (!this->GeneratorTarget->HasLinkDependencyFile(this->GetConfigName())) {
return;
}
auto depFile = this->LocalGenerator->GetLinkDependencyFile(
this->GeneratorTarget, this->GetConfigName());
this->CleanFiles.insert(depFile);
this->LocalGenerator->AddImplicitDepends(
this->GeneratorTarget, "LINK",
this->GeneratorTarget->GetFullPath(this->GetConfigName()), depFile,
cmDependencyScannerKind::Compiler);
}
std::string cmMakefileTargetGenerator::GetClangTidyReplacementsFilePath(
std::string const& directory, cmSourceFile const& source,
std::string const& config) const
{
(void)config;
auto const& objectName = this->GeneratorTarget->GetObjectName(&source);
auto fixesFile = cmSystemTools::CollapseFullPath(cmStrCat(
directory, '/',
this->GeneratorTarget->GetLocalGenerator()->MaybeRelativeToTopBinDir(
cmStrCat(this->GeneratorTarget->GetLocalGenerator()
->GetCurrentBinaryDirectory(),
'/',
this->GeneratorTarget->GetLocalGenerator()->GetTargetDirectory(
this->GeneratorTarget),
'/', objectName, ".yaml"))));
return fixesFile;
}
void cmMakefileTargetGenerator::WriteTargetDependRules()
{
// must write the targets depend info file
std::string dir =
this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
this->InfoFileNameFull = cmStrCat(dir, "/DependInfo.cmake");
this->InfoFileNameFull =
this->LocalGenerator->ConvertToFullPath(this->InfoFileNameFull);
this->InfoFileStream =
cm::make_unique<cmGeneratedFileStream>(this->InfoFileNameFull);
if (!this->InfoFileStream) {
return;
}
this->InfoFileStream->SetCopyIfDifferent(true);
this->LocalGenerator->WriteDependLanguageInfo(*this->InfoFileStream,
this->GeneratorTarget);
// Store multiple output pairs in the depend info file.
if (!this->MultipleOutputPairs.empty()) {
/* clang-format off */
*this->InfoFileStream
<< "\n"
<< "# Pairs of files generated by the same build rule.\n"
<< "set(CMAKE_MULTIPLE_OUTPUT_PAIRS\n";
/* clang-format on */
for (auto const& pi : this->MultipleOutputPairs) {
*this->InfoFileStream
<< " " << cmOutputConverter::EscapeForCMake(pi.first) << " "
<< cmOutputConverter::EscapeForCMake(pi.second) << "\n";
}
*this->InfoFileStream << " )\n\n";
}
// Store list of targets linked directly or transitively.
{
/* clang-format off */
*this->InfoFileStream
<< "\n"
"# Targets to which this target links which contain Fortran sources.\n"
"set(CMAKE_Fortran_TARGET_LINKED_INFO_FILES\n";
/* clang-format on */
std::vector<std::string> const dirs =
this->GetLinkedTargetDirectories("Fortran", this->GetConfigName());
for (std::string const& d : dirs) {
*this->InfoFileStream << " \"" << d << "/DependInfo.cmake\"\n";
}
*this->InfoFileStream << " )\n";
}
std::string const& working_dir =
this->LocalGenerator->GetCurrentBinaryDirectory();
/* clang-format off */
*this->InfoFileStream
<< "\n"
<< "# Fortran module output directory.\n"
<< "set(CMAKE_Fortran_TARGET_MODULE_DIR \""
<< this->GeneratorTarget->GetFortranModuleDirectory(working_dir)
<< "\")\n";
if (this->GeneratorTarget->IsFortranBuildingInstrinsicModules()) {
*this->InfoFileStream
<< "\n"
<< "# Fortran compiler is building intrinsic modules.\n"
<< "set(CMAKE_Fortran_TARGET_BUILDING_INSTRINSIC_MODULES ON) \n";
}
/* clang-format on */
// and now write the rule to use it
std::vector<std::string> depends;
std::vector<std::string> commands;
// Construct the name of the dependency generation target.
std::string const depTarget = cmStrCat(
this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget),
"/depend");
// Add a command to call CMake to scan dependencies. CMake will
// touch the corresponding depends file after scanning dependencies.
std::ostringstream depCmd;
// TODO: Account for source file properties and directory-level
// definitions when scanning for dependencies.
#if !defined(_WIN32) || defined(__CYGWIN__)
// This platform supports symlinks, so cmSystemTools will translate
// paths. Make sure PWD is set to the original name of the home
// output directory to help cmSystemTools to create the same
// translation table for the dependency scanning process.
depCmd << "cd "
<< (this->LocalGenerator->ConvertToOutputFormat(
this->LocalGenerator->GetBinaryDirectory(),
cmOutputConverter::SHELL))
<< " && ";
#endif
// Generate a call this signature:
//
// cmake -E cmake_depends <generator>
// <home-src-dir> <start-src-dir>
// <home-out-dir> <start-out-dir>
// <dep-info> --color=$(COLOR)
//
// This gives the dependency scanner enough information to recreate
// the state of our local generator sufficiently for its needs.
depCmd << "$(CMAKE_COMMAND) -E cmake_depends \""
<< this->GlobalGenerator->GetName() << "\" "
<< this->LocalGenerator->ConvertToOutputFormat(
this->LocalGenerator->GetSourceDirectory(),
cmOutputConverter::SHELL)
<< " "
<< this->LocalGenerator->ConvertToOutputFormat(
this->LocalGenerator->GetCurrentSourceDirectory(),
cmOutputConverter::SHELL)
<< " "
<< this->LocalGenerator->ConvertToOutputFormat(
this->LocalGenerator->GetBinaryDirectory(),
cmOutputConverter::SHELL)
<< " "
<< this->LocalGenerator->ConvertToOutputFormat(
this->LocalGenerator->GetCurrentBinaryDirectory(),
cmOutputConverter::SHELL)
<< " "
<< this->LocalGenerator->ConvertToOutputFormat(
cmSystemTools::CollapseFullPath(this->InfoFileNameFull),
cmOutputConverter::SHELL);
if (this->LocalGenerator->GetColorMakefile()) {
depCmd << " \"--color=$(COLOR)\"";
}
commands.push_back(depCmd.str());
// Make sure all custom command outputs in this target are built.
if (this->CustomCommandDriver == OnDepends) {
this->DriveCustomCommands(depends);
}
// Write the rule.
this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
depTarget, depends, commands, true);
}
void cmMakefileTargetGenerator::DriveCustomCommands(
std::vector<std::string>& depends)
{
// Depend on all custom command outputs.
cm::append(depends, this->CustomCommandOutputs);
}
void cmMakefileTargetGenerator::WriteObjectDependRules(
cmSourceFile const& source, std::vector<std::string>& depends)
{
// Create the list of dependencies known at cmake time. These are
// shared between the object file and dependency scanning rule.
depends.push_back(source.GetFullPath());
if (cmValue objectDeps = source.GetProperty("OBJECT_DEPENDS")) {
cmExpandList(*objectDeps, depends);
}
}
void cmMakefileTargetGenerator::WriteDeviceLinkRule(
std::vector<std::string>& commands, const std::string& output)
{
std::string architecturesStr =
this->GeneratorTarget->GetSafeProperty("CUDA_ARCHITECTURES");
if (cmIsOff(architecturesStr)) {
this->Makefile->IssueMessage(MessageType::FATAL_ERROR,
"CUDA_SEPARABLE_COMPILATION on Clang "
"requires CUDA_ARCHITECTURES to be set.");
return;
}
cmLocalUnixMakefileGenerator3* localGen{ this->LocalGenerator };
cmList architectures{ architecturesStr };
std::string const& relPath = localGen->GetHomeRelativeOutputPath();
// Ensure there are no duplicates.
const std::vector<std::string> linkDeps = [&]() -> std::vector<std::string> {
std::vector<std::string> deps;
this->AppendTargetDepends(deps, true);
this->GeneratorTarget->GetLinkDepends(deps, this->GetConfigName(), "CUDA");
for (std::string const& obj : this->Objects) {
deps.emplace_back(cmStrCat(relPath, obj));
}
std::unordered_set<std::string> const depsSet(deps.begin(), deps.end());
deps.clear();
std::copy(depsSet.begin(), depsSet.end(), std::back_inserter(deps));
return deps;
}();
const std::string objectDir = this->GeneratorTarget->ObjectDirectory;
const std::string relObjectDir =
localGen->MaybeRelativeToCurBinDir(objectDir);
// Construct a list of files associated with this executable that
// may need to be cleaned.
std::vector<std::string> cleanFiles;
cleanFiles.push_back(localGen->MaybeRelativeToCurBinDir(output));
std::string profiles;
std::vector<std::string> fatbinaryDepends;
std::string const registerFile =
cmStrCat(objectDir, "cmake_cuda_register.h");
// Link device code for each architecture.
for (const std::string& architectureKind : architectures) {
std::string registerFileCmd;
// The generated register file contains macros that when expanded
// register the device routines. Because the routines are the same for
// all architectures the register file will be the same too. Thus
// generate it only on the first invocation to reduce overhead.
if (fatbinaryDepends.empty()) {
std::string const registerFileRel =
cmStrCat(relPath, relObjectDir, "cmake_cuda_register.h");
registerFileCmd =
cmStrCat(" --register-link-binaries=", registerFileRel);
cleanFiles.push_back(registerFileRel);
}
// Clang always generates real code, so strip the specifier.
const std::string architecture =
architectureKind.substr(0, architectureKind.find('-'));
const std::string cubin =
cmStrCat(objectDir, "sm_", architecture, ".cubin");
profiles += cmStrCat(" -im=profile=sm_", architecture, ",file=", cubin);
fatbinaryDepends.emplace_back(cubin);
std::string command = cmStrCat(
this->Makefile->GetRequiredDefinition("CMAKE_CUDA_DEVICE_LINKER"),
" -arch=sm_", architecture, registerFileCmd, " -o=$@ ",
cmJoin(linkDeps, " "));
localGen->WriteMakeRule(*this->BuildFileStream, nullptr, cubin, linkDeps,
{ command }, false);
}
// Combine all architectures into a single fatbinary.
const std::string fatbinaryCommand =
cmStrCat(this->Makefile->GetRequiredDefinition("CMAKE_CUDA_FATBINARY"),
" -64 -cmdline=--compile-only -compress-all -link "
"--embedded-fatbin=$@",
profiles);
const std::string fatbinaryOutput =
cmStrCat(objectDir, "cmake_cuda_fatbin.h");
const std::string fatbinaryOutputRel =
cmStrCat(relPath, relObjectDir, "cmake_cuda_fatbin.h");
localGen->WriteMakeRule(*this->BuildFileStream, nullptr, fatbinaryOutputRel,
fatbinaryDepends, { fatbinaryCommand }, false);
// Compile the stub that registers the kernels and contains the
// fatbinaries.
cmRulePlaceholderExpander::RuleVariables vars;
vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
vars.CMTargetType =
cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType()).c_str();
vars.Language = "CUDA";
vars.Object = output.c_str();
vars.Fatbinary = fatbinaryOutput.c_str();
vars.RegisterFile = registerFile.c_str();
std::string linkFlags;
this->GetDeviceLinkFlags(linkFlags, "CUDA");
vars.LinkFlags = linkFlags.c_str();
std::string const flags = this->GetFlags("CUDA", this->GetConfigName());
vars.Flags = flags.c_str();
std::string compileCmd = this->GetLinkRule("CMAKE_CUDA_DEVICE_LINK_COMPILE");
auto rulePlaceholderExpander = localGen->CreateRulePlaceholderExpander();
rulePlaceholderExpander->ExpandRuleVariables(localGen, compileCmd, vars);
commands.emplace_back(compileCmd);
localGen->WriteMakeRule(*this->BuildFileStream, nullptr, output,
{ fatbinaryOutputRel }, commands, false);
// Clean all the possible executable names and symlinks.
this->CleanFiles.insert(cleanFiles.begin(), cleanFiles.end());
}
void cmMakefileTargetGenerator::GenerateCustomRuleFile(
cmCustomCommandGenerator const& ccg)
{
// Collect the commands.
std::vector<std::string> commands;
std::string comment = this->LocalGenerator->ConstructComment(ccg);
if (!comment.empty()) {
// add in a progress call if needed
this->NumberOfProgressActions++;
if (!this->NoRuleMessages) {
cmLocalUnixMakefileGenerator3::EchoProgress progress;
this->MakeEchoProgress(progress);
this->LocalGenerator->AppendEcho(
commands, comment, cmLocalUnixMakefileGenerator3::EchoGenerate,
&progress);
}
}
// Now append the actual user-specified commands.
std::ostringstream content;
this->LocalGenerator->AppendCustomCommand(
commands, ccg, this->GeneratorTarget,
this->LocalGenerator->GetBinaryDirectory(), false, &content);
// Collect the dependencies.
std::vector<std::string> depends;
this->LocalGenerator->AppendCustomDepend(depends, ccg);
if (!ccg.GetCC().GetDepfile().empty()) {
// Add dependency over timestamp file for dependencies management
auto dependTimestamp = cmSystemTools::ConvertToOutputPath(
this->LocalGenerator->MaybeRelativeToTopBinDir(
cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.ts")));
depends.push_back(dependTimestamp);
}
// Write the rule.
const std::vector<std::string>& outputs = ccg.GetOutputs();
bool const symbolic = this->WriteMakeRule(*this->BuildFileStream, nullptr,
outputs, depends, commands);
// Symbolic inputs are not expected to exist, so add dummy rules.
if (this->CMP0113New && !depends.empty()) {
std::vector<std::string> no_depends;
std::vector<std::string> no_commands;
for (std::string const& dep : depends) {
if (cmSourceFile* dsf =
this->Makefile->GetSource(dep, cmSourceFileLocationKind::Known)) {
if (dsf->GetPropertyAsBool("SYMBOLIC")) {
this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, nullptr,
dep, no_depends, no_commands,
true);
}
}
}
}
// If the rule has changed make sure the output is rebuilt.
if (!symbolic) {
this->GlobalGenerator->AddRuleHash(ccg.GetOutputs(), content.str());
}
// Setup implicit dependency scanning.
for (auto const& idi : ccg.GetCC().GetImplicitDepends()) {
std::string objFullPath = cmSystemTools::CollapseFullPath(
outputs[0], this->LocalGenerator->GetCurrentBinaryDirectory());
std::string srcFullPath = cmSystemTools::CollapseFullPath(
idi.second, this->LocalGenerator->GetCurrentBinaryDirectory());
this->LocalGenerator->AddImplicitDepends(this->GeneratorTarget, idi.first,
objFullPath, srcFullPath);
}
// Setup implicit depend for depfile if any
if (!ccg.GetCC().GetDepfile().empty()) {
std::string objFullPath = cmSystemTools::CollapseFullPath(
outputs[0], this->LocalGenerator->GetCurrentBinaryDirectory());
this->LocalGenerator->AddImplicitDepends(
this->GeneratorTarget, "CUSTOM", objFullPath, ccg.GetFullDepfile(),
cmDependencyScannerKind::Compiler);
}
this->CustomCommandOutputs.insert(outputs.begin(), outputs.end());
}
void cmMakefileTargetGenerator::MakeEchoProgress(
cmLocalUnixMakefileGenerator3::EchoProgress& progress) const
{
progress.Dir =
cmStrCat(this->LocalGenerator->GetBinaryDirectory(), "/CMakeFiles");
std::ostringstream progressArg;
progressArg << "$(CMAKE_PROGRESS_" << this->NumberOfProgressActions << ")";
progress.Arg = progressArg.str();
}
void cmMakefileTargetGenerator::WriteObjectsVariable(
std::string& variableName, std::string& variableNameExternal,
bool useWatcomQuote)
{
// Write a make variable assignment that lists all objects for the
// target.
variableName = this->LocalGenerator->CreateMakeVariable(
this->GeneratorTarget->GetName(), "_OBJECTS");
*this->BuildFileStream << "# Object files for target "
<< this->GeneratorTarget->GetName() << "\n"
<< variableName << " =";
std::string object;
const auto& lineContinue = this->GlobalGenerator->LineContinueDirective;
cmValue pchExtension = this->Makefile->GetDefinition("CMAKE_PCH_EXTENSION");
for (std::string const& obj : this->Objects) {
if (cmHasSuffix(obj, pchExtension)) {
continue;
}
*this->BuildFileStream << " " << lineContinue;
*this->BuildFileStream
<< cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(
obj, useWatcomQuote);
}
*this->BuildFileStream << "\n";
// Write a make variable assignment that lists all external objects
// for the target.
variableNameExternal = this->LocalGenerator->CreateMakeVariable(
this->GeneratorTarget->GetName(), "_EXTERNAL_OBJECTS");
/* clang-format off */
*this->BuildFileStream
<< "\n"
<< "# External object files for target "
<< this->GeneratorTarget->GetName() << "\n"
<< variableNameExternal << " =";
/* clang-format on */
for (std::string const& obj : this->ExternalObjects) {
object = this->LocalGenerator->MaybeRelativeToCurBinDir(obj);
*this->BuildFileStream << " " << lineContinue;
*this->BuildFileStream
<< cmLocalUnixMakefileGenerator3::ConvertToQuotedOutputPath(
obj, useWatcomQuote);
}
*this->BuildFileStream << "\n"
<< "\n";
}
class cmMakefileTargetGeneratorObjectStrings
{
public:
cmMakefileTargetGeneratorObjectStrings(std::vector<std::string>& strings,
cmOutputConverter* outputConverter,
bool useWatcomQuote,
cmStateDirectory const& stateDir,
std::string::size_type limit)
: Strings(strings)
, OutputConverter(outputConverter)
, UseWatcomQuote(useWatcomQuote)
, StateDir(stateDir)
, LengthLimit(limit)
{
this->Space = "";
}
void Feed(std::string const& obj)
{
// Construct the name of the next object.
this->NextObject = this->OutputConverter->ConvertToOutputFormat(
this->OutputConverter->MaybeRelativeToCurBinDir(obj),
cmOutputConverter::RESPONSE, this->UseWatcomQuote);
// Roll over to next string if the limit will be exceeded.
if (this->LengthLimit != std::string::npos &&
(this->CurrentString.length() + 1 + this->NextObject.length() >
this->LengthLimit)) {
this->Strings.push_back(this->CurrentString);
this->CurrentString.clear();
this->Space = "";
}
// Separate from previous object.
this->CurrentString += this->Space;
this->Space = " ";
// Append this object.
this->CurrentString += this->NextObject;
}
void Done() { this->Strings.push_back(this->CurrentString); }
private:
std::vector<std::string>& Strings;
cmOutputConverter* OutputConverter;
bool UseWatcomQuote;
cmStateDirectory StateDir;
std::string::size_type LengthLimit;
std::string CurrentString;
std::string NextObject;
const char* Space;
};
void cmMakefileTargetGenerator::WriteObjectsStrings(
std::vector<std::string>& objStrings, bool useWatcomQuote,
std::string::size_type limit)
{
cmValue pchExtension = this->Makefile->GetDefinition("CMAKE_PCH_EXTENSION");
cmMakefileTargetGeneratorObjectStrings helper(
objStrings, this->LocalGenerator, useWatcomQuote,
this->LocalGenerator->GetStateSnapshot().GetDirectory(), limit);
for (std::string const& obj : this->Objects) {
if (cmHasSuffix(obj, pchExtension)) {
continue;
}
helper.Feed(obj);
}
for (std::string const& obj : this->ExternalObjects) {
helper.Feed(obj);
}
auto ispcAdditionalObjs =
this->GeneratorTarget->GetGeneratedISPCObjects(this->GetConfigName());
for (std::string const& obj : ispcAdditionalObjs) {
helper.Feed(obj);
}
helper.Done();
}
void cmMakefileTargetGenerator::WriteTargetDriverRule(
const std::string& main_output, bool relink)
{
// Compute the name of the driver target.
std::string dir =
this->LocalGenerator->GetRelativeTargetDirectory(this->GeneratorTarget);
std::string buildTargetRuleName =
cmStrCat(dir, relink ? "/preinstall" : "/build");
buildTargetRuleName =
this->LocalGenerator->MaybeRelativeToTopBinDir(buildTargetRuleName);
// Build the list of target outputs to drive.
std::vector<std::string> depends;
depends.push_back(main_output);
const char* comment = nullptr;
if (relink) {
// Setup the comment for the preinstall driver.
comment = "Rule to relink during preinstall.";
} else {
// Setup the comment for the main build driver.
comment = "Rule to build all files generated by this target.";
// Make sure all custom command outputs in this target are built.
if (this->CustomCommandDriver == OnBuild) {
this->DriveCustomCommands(depends);
}
// Make sure the extra files are built.
cm::append(depends, this->ExtraFiles);
}
// Write the driver rule.
std::vector<std::string> no_commands;
this->LocalGenerator->WriteMakeRule(*this->BuildFileStream, comment,
buildTargetRuleName, depends,
no_commands, true);
}
void cmMakefileTargetGenerator::AppendTargetDepends(
std::vector<std::string>& depends, bool ignoreType)
{
// Static libraries never depend on anything for linking.
if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY &&
!ignoreType) {
return;
}
const std::string& cfg = this->GetConfigName();
if (this->GeneratorTarget->HasLinkDependencyFile(cfg)) {
depends.push_back(
cmStrCat(this->TargetBuildDirectoryFull, "/compiler_depend.ts"));
}
// Loop over all library dependencies.
if (cmComputeLinkInformation* cli =
this->GeneratorTarget->GetLinkInformation(cfg)) {
cm::append(depends, cli->GetDepends());
}
}
void cmMakefileTargetGenerator::AppendObjectDepends(
std::vector<std::string>& depends)
{
// Add dependencies on the compiled object files.
std::string const& relPath =
this->LocalGenerator->GetHomeRelativeOutputPath();
for (std::string const& obj : this->Objects) {
std::string objTarget = cmStrCat(relPath, obj);
depends.push_back(std::move(objTarget));
}
// Add dependencies on the external object files.
cm::append(depends, this->ExternalObjects);
// Add a dependency on the rule file itself.
this->LocalGenerator->AppendRuleDepend(depends,
this->BuildFileNameFull.c_str());
}
void cmMakefileTargetGenerator::AppendLinkDepends(
std::vector<std::string>& depends, const std::string& linkLanguage)
{
this->AppendObjectDepends(depends);
// Add dependencies on targets that must be built first.
this->AppendTargetDepends(depends);
// Add a dependency on the link definitions file, if any.
if (cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
this->GeneratorTarget->GetModuleDefinitionInfo(
this->GetConfigName())) {
for (cmSourceFile const* src : mdi->Sources) {
depends.push_back(src->GetFullPath());
}
}
// Add a dependency on user-specified manifest files, if any.
std::vector<cmSourceFile const*> manifest_srcs;
this->GeneratorTarget->GetManifests(manifest_srcs, this->GetConfigName());
for (cmSourceFile const* manifest_src : manifest_srcs) {
depends.push_back(manifest_src->GetFullPath());
}
// Add user-specified dependencies.
this->GeneratorTarget->GetLinkDepends(depends, this->GetConfigName(),
linkLanguage);
}
std::string cmMakefileTargetGenerator::GetLinkRule(
const std::string& linkRuleVar)
{
std::string linkRule = this->Makefile->GetRequiredDefinition(linkRuleVar);
if (this->GeneratorTarget->HasImplibGNUtoMS(this->GetConfigName())) {
std::string ruleVar =
cmStrCat("CMAKE_",
this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName()),
"_GNUtoMS_RULE");
if (cmValue rule = this->Makefile->GetDefinition(ruleVar)) {
linkRule += *rule;
}
}
return linkRule;
}
void cmMakefileTargetGenerator::CloseFileStreams()
{
this->BuildFileStream.reset();
this->InfoFileStream.reset();
this->FlagFileStream.reset();
}
void cmMakefileTargetGenerator::CreateLinkScript(
const char* name, std::vector<std::string> const& link_commands,
std::vector<std::string>& makefile_commands,
std::vector<std::string>& makefile_depends)
{
// Create the link script file.
std::string linkScriptName =
cmStrCat(this->TargetBuildDirectoryFull, '/', name);
cmGeneratedFileStream linkScriptStream(linkScriptName);
linkScriptStream.SetCopyIfDifferent(true);
for (std::string const& link_command : link_commands) {
// Do not write out empty commands or commands beginning in the
// shell no-op ":".
if (!link_command.empty() && link_command[0] != ':') {
linkScriptStream << link_command << "\n";
}
}
// Create the makefile command to invoke the link script.
std::string link_command =
cmStrCat("$(CMAKE_COMMAND) -E cmake_link_script ",
this->LocalGenerator->ConvertToOutputFormat(
this->LocalGenerator->MaybeRelativeToCurBinDir(linkScriptName),
cmOutputConverter::SHELL),
" --verbose=$(VERBOSE)");
makefile_commands.push_back(std::move(link_command));
makefile_depends.push_back(std::move(linkScriptName));
}
bool cmMakefileTargetGenerator::CheckUseResponseFileForObjects(
std::string const& l) const
{
// Check for an explicit setting one way or the other.
std::string const responseVar =
"CMAKE_" + l + "_USE_RESPONSE_FILE_FOR_OBJECTS";
if (cmValue val = this->Makefile->GetDefinition(responseVar)) {
if (!val->empty()) {
return cmIsOn(val);
}
}
// Check for a system limit.
if (size_t const limit = cmSystemTools::CalculateCommandLineLengthLimit()) {
// Compute the total length of our list of object files with room
// for argument separation and quoting. This does not convert paths
// relative to CMAKE_CURRENT_BINARY_DIR like the final list will be, so
// the actual list will likely be much shorter than this. However, in
// the worst case all objects will remain as absolute paths.
size_t length = 0;
for (std::string const& obj : this->Objects) {
length += obj.size() + 3;
}
for (std::string const& ext_obj : this->ExternalObjects) {
length += ext_obj.size() + 3;
}
// We need to guarantee room for both objects and libraries, so
// if the objects take up more than half then use a response file
// for them.
if (length > (limit / 2)) {
return true;
}
}
// We do not need a response file for objects.
return false;
}
bool cmMakefileTargetGenerator::CheckUseResponseFileForLibraries(
std::string const& l) const
{
// Check for an explicit setting one way or the other.
std::string const responseVar =
"CMAKE_" + l + "_USE_RESPONSE_FILE_FOR_LIBRARIES";
if (cmValue val = this->Makefile->GetDefinition(responseVar)) {
if (!val->empty()) {
return cmIsOn(val);
}
}
// We do not need a response file for libraries.
return false;
}
std::string cmMakefileTargetGenerator::CreateResponseFile(
const std::string& name, std::string const& options,
std::vector<std::string>& makefile_depends, std::string const& language)
{
// FIXME: Find a better way to determine the response file encoding,
// perhaps using tool-specific platform information variables.
// For now, use the makefile encoding as a heuristic.
codecvt::Encoding responseEncoding =
this->GlobalGenerator->GetMakefileEncoding();
// Non-MSVC tooling doesn't understand BOM encoded files.
if (responseEncoding == codecvt::UTF8_WITH_BOM &&
(language == "CUDA" || !this->Makefile->IsOn("MSVC"))) {
responseEncoding = codecvt::UTF8;
}
// Create the response file.
std::string responseFileNameFull =
cmStrCat(this->TargetBuildDirectoryFull, '/', name);
cmGeneratedFileStream responseStream(responseFileNameFull, false,
responseEncoding);
responseStream.SetCopyIfDifferent(true);
responseStream << options << "\n";
// Add a dependency so the target will rebuild when the set of
// objects changes.
makefile_depends.push_back(std::move(responseFileNameFull));
// Construct the name to be used on the command line.
std::string responseFileName =
cmStrCat(this->TargetBuildDirectory, '/', name);
return responseFileName;
}
std::unique_ptr<cmLinkLineComputer>
cmMakefileTargetGenerator::CreateLinkLineComputer(
cmOutputConverter* outputConverter, cmStateDirectory const& stateDir)
{
if (this->Makefile->IsOn("MSVC60")) {
return this->GlobalGenerator->CreateMSVC60LinkLineComputer(outputConverter,
stateDir);
}
return this->GlobalGenerator->CreateLinkLineComputer(outputConverter,
stateDir);
}
void cmMakefileTargetGenerator::CreateLinkLibs(
cmLinkLineComputer* linkLineComputer, std::string& linkLibs,
bool useResponseFile, std::vector<std::string>& makefile_depends,
std::string const& linkLanguage, ResponseFlagFor responseMode)
{
std::string frameworkPath;
std::string linkPath;
cmComputeLinkInformation* pcli =
this->GeneratorTarget->GetLinkInformation(this->GetConfigName());
this->LocalGenerator->OutputLinkLibraries(pcli, linkLineComputer, linkLibs,
frameworkPath, linkPath);
linkLibs = frameworkPath + linkPath + linkLibs;
if (useResponseFile &&
linkLibs.find_first_not_of(' ') != std::string::npos) {
// Lookup the response file reference flag.
std::string responseFlag = this->GetResponseFlag(responseMode);
// Create this response file.
std::string responseFileName =
(responseMode == Link) ? "linkLibs.rsp" : "deviceLinkLibs.rsp";
std::string responseLang = (responseMode == Link) ? linkLanguage : "CUDA";
std::string link_rsp = this->CreateResponseFile(
responseFileName, linkLibs, makefile_depends, responseLang);
// Reference the response file.
linkLibs = cmStrCat(responseFlag,
this->LocalGenerator->ConvertToOutputFormat(
link_rsp, cmOutputConverter::SHELL));
}
}
void cmMakefileTargetGenerator::CreateObjectLists(
bool useLinkScript, bool useArchiveRules, bool useResponseFile,
std::string& buildObjs, std::vector<std::string>& makefile_depends,
bool useWatcomQuote, std::string const& linkLanguage,
ResponseFlagFor responseMode)
{
std::string variableName;
std::string variableNameExternal;
this->WriteObjectsVariable(variableName, variableNameExternal,
useWatcomQuote);
if (useResponseFile) {
// MSVC response files cannot exceed 128K.
std::string::size_type constexpr responseFileLimit = 131000;
// Construct the individual object list strings.
std::vector<std::string> object_strings;
this->WriteObjectsStrings(object_strings, useWatcomQuote,
responseFileLimit);
// Lookup the response file reference flag.
std::string responseFlag = this->GetResponseFlag(responseMode);
// Write a response file for each string.
const char* sep = "";
for (unsigned int i = 0; i < object_strings.size(); ++i) {
// Number the response files.
std::string responseFileName =
(responseMode == Link) ? "objects" : "deviceObjects";
responseFileName += std::to_string(i + 1);
responseFileName += ".rsp";
// Create this response file.
std::string objects_rsp = this->CreateResponseFile(
responseFileName, object_strings[i], makefile_depends, linkLanguage);
// Separate from previous response file references.
buildObjs += sep;
sep = " ";
// Reference the response file.
buildObjs += responseFlag;
buildObjs += this->LocalGenerator->ConvertToOutputFormat(
objects_rsp, cmOutputConverter::SHELL);
}
} else if (useLinkScript) {
if (!useArchiveRules) {
std::vector<std::string> objStrings;
this->WriteObjectsStrings(objStrings, useWatcomQuote);
buildObjs = objStrings[0];
}
} else {
buildObjs =
cmStrCat("$(", variableName, ") $(", variableNameExternal, ')');
}
}
void cmMakefileTargetGenerator::AddIncludeFlags(std::string& flags,
const std::string& lang,
const std::string& /*config*/)
{
std::string responseVar =
cmStrCat("CMAKE_", lang, "_USE_RESPONSE_FILE_FOR_INCLUDES");
bool useResponseFile = this->Makefile->IsOn(responseVar);
std::vector<std::string> includes;
this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
lang, this->GetConfigName());
std::string includeFlags = this->LocalGenerator->GetIncludeFlags(
includes, this->GeneratorTarget, lang, this->GetConfigName(),
useResponseFile);
if (includeFlags.empty()) {
return;
}
if (useResponseFile) {
std::string const responseFlagVar =
"CMAKE_" + lang + "_RESPONSE_FILE_FLAG";
std::string responseFlag =
this->Makefile->GetSafeDefinition(responseFlagVar);
if (responseFlag.empty()) {
responseFlag = "@";
}
std::string name = cmStrCat("includes_", lang, ".rsp");
std::string arg = std::move(responseFlag) +
this->CreateResponseFile(name, includeFlags, this->FlagFileDepends[lang],
lang);
this->LocalGenerator->AppendFlags(flags, arg);
} else {
this->LocalGenerator->AppendFlags(flags, includeFlags);
}
}
void cmMakefileTargetGenerator::GenDefFile(
std::vector<std::string>& real_link_commands)
{
cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
this->GeneratorTarget->GetModuleDefinitionInfo(this->GetConfigName());
if (!mdi || !mdi->DefFileGenerated) {
return;
}
std::string cmd = cmSystemTools::GetCMakeCommand();
cmd = cmStrCat(
this->LocalGenerator->ConvertToOutputFormat(cmd, cmOutputConverter::SHELL),
" -E __create_def ",
this->LocalGenerator->ConvertToOutputFormat(
this->LocalGenerator->MaybeRelativeToCurBinDir(mdi->DefFile),
cmOutputConverter::SHELL),
' ');
std::string objlist_file = mdi->DefFile + ".objs";
cmd += this->LocalGenerator->ConvertToOutputFormat(
this->LocalGenerator->MaybeRelativeToCurBinDir(objlist_file),
cmOutputConverter::SHELL);
cmValue nm_executable = this->Makefile->GetDefinition("CMAKE_NM");
if (cmNonempty(nm_executable)) {
cmd += " --nm=";
cmd += this->LocalCommonGenerator->ConvertToOutputFormat(
*nm_executable, cmOutputConverter::SHELL);
}
real_link_commands.insert(real_link_commands.begin(), cmd);
// create a list of obj files for the -E __create_def to read
cmGeneratedFileStream fout(objlist_file);
if (mdi->WindowsExportAllSymbols) {
for (std::string const& obj : this->Objects) {
if (cmHasLiteralSuffix(obj, ".obj")) {
fout << obj << "\n";
}
}
for (std::string const& obj : this->ExternalObjects) {
fout << obj << "\n";
}
}
for (cmSourceFile const* src : mdi->Sources) {
fout << src->GetFullPath() << "\n";
}
}
std::string cmMakefileTargetGenerator::GetResponseFlag(
ResponseFlagFor mode) const
{
std::string responseFlag = "@";
std::string responseFlagVar;
auto const lang =
this->GeneratorTarget->GetLinkerLanguage(this->GetConfigName());
if (mode == cmMakefileTargetGenerator::ResponseFlagFor::Link) {
responseFlagVar = cmStrCat("CMAKE_", lang, "_RESPONSE_FILE_LINK_FLAG");
} else if (mode == cmMakefileTargetGenerator::ResponseFlagFor::DeviceLink) {
responseFlagVar = "CMAKE_CUDA_RESPONSE_FILE_DEVICE_LINK_FLAG";
}
if (cmValue p = this->Makefile->GetDefinition(responseFlagVar)) {
responseFlag = *p;
}
return responseFlag;
}
|