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
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author ruki
-- @file vs201x_vcxproj.lua
--
-- imports
import("core.base.hashset")
import("core.project.rule")
import("core.project.config")
import("core.project.project")
import("core.language.language")
import("core.tool.toolchain")
import("private.utils.batchcmds")
import("detect.sdks.find_cuda")
import("vsfile")
import("vsutils")
import("private.utils.toolchain", {alias = "toolchain_utils"})
import("rules.c++.modules.support", {rootdir = os.programdir()})
function _make_dirs(dir, vcxprojdir)
dir = dir:trim()
if #dir == 0 then
return ""
end
dir = path.translate(dir)
if not path.is_absolute(dir) then
dir = path.relative(path.absolute(dir), vcxprojdir)
end
return dir
end
-- check for CUDA
function _check_cuda(target)
local cuda
for _, targetinfo in ipairs(target.info) do
if targetinfo.sourcekinds and table.contains(targetinfo.sourcekinds, "cu") then
cuda = find_cuda()
break
end
end
if cuda then
if cuda.msbuildextensionsdir and cuda.version and os.isfile(path.join(cuda.msbuildextensionsdir, format("CUDA %s.props", cuda.version))) then
return cuda
else
os.raise("The Visual Studio Integration for CUDA %s is not found. Please check your CUDA installation.", cuda.version)
end
end
end
-- get toolset version
function _get_toolset_ver(targetinfo, vsinfo)
-- get toolset version from vs version
local vs_toolset = toolchain.load("msvc"):config("vs_toolset") or config.get("vs_toolset")
local toolset_ver = toolchain_utils.get_vs_toolset_ver(vs_toolset)
if not toolset_ver then
toolset_ver = vsinfo.toolset_version
end
return toolset_ver
end
-- get platform sdk version from vcvars.WindowsSDKVersion
function _get_platform_sdkver(target, vsinfo)
local sdkver = nil
for _, targetinfo in ipairs(target.info) do
sdkver = targetinfo.sdkver
if sdkver then
break
end
end
return sdkver or vsinfo.sdk_version
end
-- combine two successive flags
function _combine_flags(flags, patterns)
local newflags = {}
local temparg
for _, arg in ipairs(flags) do
if temparg then
table.insert(newflags, temparg .. " " .. arg)
temparg = nil
else
for _, pattern in ipairs(patterns) do
if arg:match(pattern) then
temparg = arg
end
end
if not temparg then
table.insert(newflags, arg)
end
end
end
return newflags
end
-- exclude patterns from flags
function _exclude_flags(flags, excludes)
local newflags = {}
for _, flag in ipairs(flags) do
local excluded = false
for _, exclude in ipairs(excludes) do
if flag:find("^[%-/]" .. exclude) then
excluded = true
break
end
end
if not excluded then
table.insert(newflags, vsutils.escape(flag))
end
end
return newflags
end
-- try split from nvcc -code flag
-- e.g. nvcc -arch=compute_86 -code=\"sm_86,compute_86\"
-- nvcc -gencode arch=compute_86,code=[sm_86,compute_86]
function _split_gpucodes(flag)
flag = flag:gsub("[%[\"]?(.-)[%]\"]?", "%1")
return flag:split(",")
end
-- make compiling command
function _make_compcmd(compargv, sourcefile, objectfile, vcxprojdir)
local argv = {}
for i, v in ipairs(compargv) do
if i == 1 then
v = path.filename(v) -- C:\xxx\ml.exe -> ml.exe
end
v = v:gsub("__sourcefile__", sourcefile)
v = v:gsub("__objectfile__", objectfile)
-- -Idir or /Idir
-- handle external includes as well
for _, pattern in ipairs({"[%-/](I)(.*)", "[%-/](external:I)(.*)"}) do
v = v:gsub(pattern, function (flag, dir)
dir = _make_dirs(dir, vcxprojdir)
return "/" .. flag .. dir
end)
end
table.insert(argv, v)
end
return table.concat(argv, " ")
end
-- make compiling flags
function _make_compflags(sourcefile, targetinfo, vcxprojdir)
-- translate path for -Idir or /Idir
local flags = {}
for _, flag in ipairs(targetinfo.compflags[sourcefile]) do
for _, pattern in ipairs({"[%-/](I)(.*)", "[%-/](external:I)(.*)"}) do
-- -Idir or /Idir
flag = flag:gsub(pattern, function (flag, dir)
dir = _make_dirs(dir, vcxprojdir)
return "/" .. flag .. dir
end)
end
table.insert(flags, flag)
end
-- add -D__config_$(mode)__ and -D__config_$(arch)__ for the config header
table.insert(flags, "-D__config_" .. targetinfo.mode .. "__")
table.insert(flags, "-D__config_" .. targetinfo.arch .. "__")
return flags
end
-- make linking flags
function _make_linkflags(targetinfo, vcxprojdir)
-- replace -libpath:dir or /libpath:dir
local flags = {}
for _, flag in ipairs(targetinfo.linkflags) do
-- replace -libpath:dir or /libpath:dir
flag = flag:gsub(string.ipattern("[%-/]libpath:(.*)"), function (dir)
dir = _make_dirs(dir, vcxprojdir)
return "/libpath:" .. dir
end)
-- replace -def:dir or /def:dir
flag = flag:gsub(string.ipattern("[%-/]def:(.*)"), function (dir)
dir = _make_dirs(dir, vcxprojdir)
return "/def:" .. dir
end)
-- save flag
table.insert(flags, flag)
end
-- ok?
return flags
end
-- make header
function _make_header(vcxprojfile, vsinfo)
vcxprojfile:print("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
vcxprojfile:enter("<Project DefaultTargets=\"Build\" ToolsVersion=\"%s.0\" xmlns=\"http://schemas.microsoft.com/developer/msbuild/2003\">", assert(vsinfo.project_version))
end
-- make references
function _make_references(vcxprojfile, vsinfo, target)
vcxprojfile:print("<ItemGroup>")
for dep_name, dep_vcxprojfile in pairs(target.deps) do
vcxprojfile:print("<ProjectReference Include=\"%s\">", dep_vcxprojfile)
vcxprojfile:print("<Project>{%s}</Project>", hash.uuid4(dep_name))
vcxprojfile:print("</ProjectReference>")
end
vcxprojfile:print("</ItemGroup>")
end
-- make tailer
function _make_tailer(vcxprojfile, vsinfo, target)
vcxprojfile:print("<Import Project=\"%$(VCTargetsPath)\\Microsoft.Cpp.targets\" />")
vcxprojfile:enter("<ImportGroup Label=\"ExtensionTargets\">")
local cuda = _check_cuda(target)
if cuda then
vcxprojfile:print("<Import Project=\"%s\" />", path.join(cuda.msbuildextensionsdir, format("CUDA %s.targets", cuda.version)))
end
vcxprojfile:leave("</ImportGroup>")
vcxprojfile:leave("</Project>")
end
-- make Configurations
function _make_configurations(vcxprojfile, vsinfo, target)
-- the target name
local targetname = target.name
-- init configuration type
local configuration_types =
{
binary = "Application"
, shared = "DynamicLibrary"
, static = "StaticLibrary"
, moduleonly = "StaticLibrary" -- emulate moduleonly with staticlib
}
-- make ProjectConfigurations
vcxprojfile:enter("<ItemGroup Label=\"ProjectConfigurations\">")
for _, targetinfo in ipairs(target.info) do
vcxprojfile:enter("<ProjectConfiguration Include=\"%s|%s\">", targetinfo.mode, targetinfo.arch)
vcxprojfile:print("<Configuration>%s</Configuration>", targetinfo.mode)
vcxprojfile:print("<Platform>%s</Platform>", targetinfo.arch)
vcxprojfile:leave("</ProjectConfiguration>")
end
vcxprojfile:leave("</ItemGroup>")
-- make Globals
vcxprojfile:enter("<PropertyGroup Label=\"Globals\">")
vcxprojfile:print("<ProjectGuid>{%s}</ProjectGuid>", hash.uuid4(targetname))
vcxprojfile:print("<RootNamespace>%s</RootNamespace>", targetname)
if vsinfo.vstudio_version >= "2015" then
vcxprojfile:print("<WindowsTargetPlatformVersion>%s</WindowsTargetPlatformVersion>", _get_platform_sdkver(target, vsinfo))
end
vcxprojfile:leave("</PropertyGroup>")
-- import Microsoft.Cpp.Default.props
vcxprojfile:print("<Import Project=\"%$(VCTargetsPath)\\Microsoft.Cpp.Default.props\" />")
-- make Configuration
for _, targetinfo in ipairs(target.info) do
vcxprojfile:enter("<PropertyGroup Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s|%s\'\" Label=\"Configuration\">", targetinfo.mode, targetinfo.arch)
vcxprojfile:print("<ConfigurationType>%s</ConfigurationType>", configuration_types[target.kind] or "Unknown")
vcxprojfile:print("<PlatformToolset>%s</PlatformToolset>", _get_toolset_ver(targetinfo, vsinfo))
vcxprojfile:print("<CharacterSet>%s</CharacterSet>", targetinfo.unicode and "Unicode" or "MultiByte")
if targetinfo.usemfc then
vcxprojfile:print("<UseOfMfc>%s</UseOfMfc>", targetinfo.usemfc)
end
vcxprojfile:leave("</PropertyGroup>")
end
-- import Microsoft.Cpp.props
vcxprojfile:print("<Import Project=\"%$(VCTargetsPath)\\Microsoft.Cpp.props\" />")
-- make ExtensionSettings
vcxprojfile:enter("<ImportGroup Label=\"ExtensionSettings\">")
local cuda = _check_cuda(target)
if cuda then
vcxprojfile:print("<Import Project=\"%s\" />", path.join(cuda.msbuildextensionsdir, format("CUDA %s.props", cuda.version)))
end
vcxprojfile:leave("</ImportGroup>")
-- make PropertySheets
for _, targetinfo in ipairs(target.info) do
vcxprojfile:enter("<ImportGroup Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s|%s\'\" Label=\"PropertySheets\">", targetinfo.mode, targetinfo.arch)
vcxprojfile:print("<Import Project=\"%$(UserRootDir)\\Microsoft.Cpp.%$(Platform).user.props\" Condition=\"exists(\'%$(UserRootDir)\\Microsoft.Cpp.%$(Platform).user.props\')\" Label=\"LocalAppDataPlatform\" />")
vcxprojfile:leave("</ImportGroup>")
end
-- make UserMacros
vcxprojfile:print("<PropertyGroup Label=\"UserMacros\" />")
if not configuration_types[target.kind] then
return
end
-- make OutputDirectory and IntermediateDirectory
for _, targetinfo in ipairs(target.info) do
vcxprojfile:enter("<PropertyGroup Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s|%s\'\">", targetinfo.mode, targetinfo.arch)
vcxprojfile:print("<OutDir>%s\\</OutDir>", _make_dirs(targetinfo.targetdir, target.project_dir))
vcxprojfile:print("<IntDir>%s\\</IntDir>", _make_dirs(targetinfo.objectdir, target.project_dir))
if targetinfo.targetfile then
vcxprojfile:print("<TargetName>%s</TargetName>", path.basename(targetinfo.targetfile))
vcxprojfile:print("<TargetExt>%s</TargetExt>", path.extension(targetinfo.targetfile))
end
if target.kind == "binary" then
vcxprojfile:print("<LinkIncremental>true</LinkIncremental>")
end
if targetinfo.manifest_embed ~= nil then
vcxprojfile:print("<EmbedManifest>%s</EmbedManifest>", targetinfo.manifest_embed)
end
-- handle ExternalIncludePath (should we handle IncludePath here too?)
local externaldirs = {}
for _, flag in ipairs(targetinfo.commonflags.cl) do
flag:gsub("[%-/]external:I(.*)", function (dir) table.insert(externaldirs, dir) end)
end
if #externaldirs > 0 then
vcxprojfile:print("<ExternalIncludePath>%s;$(VC_IncludePath);$(WindowsSDK_IncludePath);</ExternalIncludePath>", table.concat(externaldirs, ";"))
end
vcxprojfile:leave("</PropertyGroup>")
end
-- make Debugger
for _, targetinfo in ipairs(target.info) do
vcxprojfile:enter("<PropertyGroup Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s|%s\'\" Label=\"Debugger\">", targetinfo.mode, targetinfo.arch)
vcxprojfile:print("<LocalDebuggerWorkingDirectory>%s</LocalDebuggerWorkingDirectory>", _make_dirs(targetinfo.rundir, target.project_dir))
-- @note we use writef to avoid escape $() in runenvs, e.g. $([System.Environment]::Get ..)
vcxprojfile:writef("<LocalDebuggerEnvironment>%s;%%(LocalDebuggerEnvironment)</LocalDebuggerEnvironment>\n", targetinfo.runenvs)
vcxprojfile:leave("</PropertyGroup>")
end
end
-- make source options for cl
function _make_source_options_cl(vcxprojfile, flags, condition)
-- exists condition?
condition = condition or ""
-- get flags string
local flagstr = os.args(flags)
-- make Optimization
if flagstr:find("[%-/]Os") or flagstr:find("[%-/]O1") then
vcxprojfile:print("<Optimization%s>MinSpace</Optimization>", condition)
elseif flagstr:find("[%-/]O2") or flagstr:find("[%-/]Ot") then
vcxprojfile:print("<Optimization%s>MaxSpeed</Optimization>", condition)
elseif flagstr:find("[%-/]Ox") then
vcxprojfile:print("<Optimization%s>Full</Optimization>", condition)
else
vcxprojfile:print("<Optimization%s>Disabled</Optimization>", condition)
end
-- make FloatingPointModel
if flagstr:find("[%-/]fp:fast") then
vcxprojfile:print("<FloatingPointModel%s>Fast</FloatingPointModel>", condition)
elseif flagstr:find("[%-/]fp:strict") then
vcxprojfile:print("<FloatingPointModel%s>Strict</FloatingPointModel>", condition)
elseif flagstr:find("[%-/]fp:precise") then
vcxprojfile:print("<FloatingPointModel%s>Precise</FloatingPointModel>", condition)
end
-- make WarningLevel
if flagstr:find("[%-/]W1") then
vcxprojfile:print("<WarningLevel%s>Level1</WarningLevel>", condition)
elseif flagstr:find("[%-/]W2") then
vcxprojfile:print("<WarningLevel%s>Level2</WarningLevel>", condition)
elseif flagstr:find("[%-/]W3") then
vcxprojfile:print("<WarningLevel%s>Level3</WarningLevel>", condition)
elseif flagstr:find("[%-/]W4") then
vcxprojfile:print("<WarningLevel%s>Level4</WarningLevel>", condition)
elseif flagstr:find("[%-/]Wall") then
vcxprojfile:print("<WarningLevel%s>EnableAllWarnings</WarningLevel>", condition)
else
vcxprojfile:print("<WarningLevel%s>TurnOffAllWarnings</WarningLevel>", condition)
end
if flagstr:find("[%-/]WX") then
vcxprojfile:print("<TreatWarningAsError%s>true</TreatWarningAsError>", condition)
end
-- make ExternalWarningLevel
if flagstr:find("[%-/]external:W1") then
vcxprojfile:print("<ExternalWarningLevel%s>Level1</ExternalWarningLevel>", condition)
elseif flagstr:find("[%-/]external:W2") then
vcxprojfile:print("<ExternalWarningLevel%s>Level2</ExternalWarningLevel>", condition)
elseif flagstr:find("[%-/]external:W3") then
vcxprojfile:print("<ExternalWarningLevel%s>Level3</ExternalWarningLevel>", condition)
elseif flagstr:find("[%-/]external:W4") then
vcxprojfile:print("<ExternalWarningLevel%s>Level4</ExternalWarningLevel>", condition)
else
vcxprojfile:print("<ExternalWarningLevel%s>TurnOffAllWarnings</ExternalWarningLevel>", condition)
end
-- make ExternalTemplatesDiagnostics
if flagstr:find("[%-/]external:templates%-") then
vcxprojfile:print("<ExternalTemplatesDiagnostics%s>true</ExternalTemplatesDiagnostics>", condition)
else
vcxprojfile:print("<ExternalTemplatesDiagnostics%s>false</ExternalTemplatesDiagnostics>", condition)
end
-- make DisableSpecificWarnings
local disabledwarnings = {}
for _, flag in ipairs(flags) do
flag:gsub("[%-/]wd(%d+)", function (warn) table.insert(disabledwarnings, warn) end)
end
if #disabledwarnings > 0 then
vcxprojfile:print("<DisableSpecificWarnings>%s;%%(DisableSpecificWarnings)</DisableSpecificWarnings>", table.concat(disabledwarnings, ";"))
end
-- make PreprocessorDefinitions
local defstr = ""
for _, flag in ipairs(flags) do
flag:gsub("^[%-/]D(.*)",
function (def)
defstr = defstr .. vsutils.escape(def) .. ";"
end
)
end
defstr = defstr .. "%%(PreprocessorDefinitions)"
vcxprojfile:print("<PreprocessorDefinitions%s>%s</PreprocessorDefinitions>", condition, defstr)
-- make DebugInformationFormat
if flagstr:find("[%-/]Zi") then
vcxprojfile:print("<DebugInformationFormat%s>ProgramDatabase</DebugInformationFormat>", condition)
elseif flagstr:find("[%-/]ZI") then
vcxprojfile:print("<DebugInformationFormat%s>EditAndContinue</DebugInformationFormat>", condition)
elseif flagstr:find("[%-/]Z7") then
vcxprojfile:print("<DebugInformationFormat%s>OldStyle</DebugInformationFormat>", condition)
else
vcxprojfile:print("<DebugInformationFormat%s>None</DebugInformationFormat>", condition)
end
-- make RuntimeLibrary
if flagstr:find("[%-/]MDd") then
vcxprojfile:print("<RuntimeLibrary%s>MultiThreadedDebugDLL</RuntimeLibrary>", condition)
elseif flagstr:find("[%-/]MD") then
vcxprojfile:print("<RuntimeLibrary%s>MultiThreadedDLL</RuntimeLibrary>", condition)
elseif flagstr:find("[%-/]MTd") then
vcxprojfile:print("<RuntimeLibrary%s>MultiThreadedDebug</RuntimeLibrary>", condition)
else
vcxprojfile:print("<RuntimeLibrary%s>MultiThreaded</RuntimeLibrary>", condition)
end
-- make RuntimeTypeInfo
if flagstr:find("[%-/]GR%-") then
vcxprojfile:print("<RuntimeTypeInfo%s>false</RuntimeTypeInfo>", condition)
elseif flagstr:find("[%-/]GR") then
vcxprojfile:print("<RuntimeTypeInfo%s>true</RuntimeTypeInfo>", condition)
end
-- handle multi processor compilation
if flagstr:find("[%-/]Gm%-") or not flagstr:find("[%-/]Gm") then
vcxprojfile:print("<MinimalRebuild%s>false</MinimalRebuild>", condition)
if not flagstr:find("[%-/]MP1") then
vcxprojfile:print("<MultiProcessorCompilation%s>true</MultiProcessorCompilation>", condition)
end
end
-- make AdditionalIncludeDirectories
if flagstr:find("[%-/]I") then
local dirs = {}
for _, flag in ipairs(flags) do
flag:gsub("^[%-/]I(.*)", function (dir) table.insert(dirs, vsutils.escape(dir)) end)
end
if #dirs > 0 then
vcxprojfile:print("<AdditionalIncludeDirectories%s>%s</AdditionalIncludeDirectories>", condition, table.concat(dirs, ";"))
end
end
-- compile as c++ if exists flag: /TP
if flagstr:find("[%-/]TP") then
vcxprojfile:print("<CompileAs%s>CompileAsCpp</CompileAs>", condition)
end
-- make SDLCheck flag: /sdl
if flagstr:find("[%-/]sdl") then
if flagstr:find("[%-/]sdl%-") then
vcxprojfile:print("<SDLCheck%s>false</SDLCheck>", condition)
else
vcxprojfile:print("<SDLCheck%s>true</SDLCheck>", condition)
end
end
-- make RemoveUnreferencedCodeData flag: Zc:inline
if flagstr:find("[%-/]Zc:inline") then
if flagstr:find("[%-/]Zc:inline%-") then
vcxprojfile:print("<RemoveUnreferencedCodeData%s>false</RemoveUnreferencedCodeData>", condition)
else
vcxprojfile:print("<RemoveUnreferencedCodeData%s>true</RemoveUnreferencedCodeData>", condition)
end
end
-- make ExceptionHandling flag:
if flagstr:find("[%-/]EH[asc]+%-?") then
local args = flagstr:match("[%-/]EH([asc]+%-?)")
-- remove the last arg if flag endwith `-`
if args and args:endswith("-") then
args = args:sub(1, -2)
end
if args and args:find("a", 1, true) then
-- a will overwrite s and c
vcxprojfile:print("<ExceptionHandling%s>Async</ExceptionHandling>", condition)
elseif args == "sc" or args == "cs" then
vcxprojfile:print("<ExceptionHandling%s>Sync</ExceptionHandling>", condition)
elseif args == "s" then
vcxprojfile:print("<ExceptionHandling%s>SyncCThrow</ExceptionHandling>", condition)
else
-- if args == "c"
-- c is ignored without s or a, do nothing here
end
end
-- make AdditionalOptions
local excludes = {
"Od", "Os", "O0", "O1", "O2", "Ot", "Ox", "W0", "W1", "W2", "W3", "W4", "WX", "Wall", "Zi", "ZI", "Z7", "MT", "MTd", "MD", "MDd", "TP",
"Fd", "fp", "I", "D", "Gm%-", "Gm", "GR%-", "GR", "MP", "external:W0", "external:W1", "external:W2", "external:W3", "external:W4", "external:templates%-?", "external:I",
"std:c11", "std:c17", "std:c%+%+11", "std:c%+%+14", "std:c%+%+17", "std:c%+%+20", "std:c%+%+latest", "nologo", "wd(%d+)", "sdl%-?", "Zc:inline%-?", "EH[asc]+%-?"
}
local additional_flags = _exclude_flags(flags, excludes)
if #additional_flags > 0 then
vcxprojfile:print("<AdditionalOptions%s>%s %%(AdditionalOptions)</AdditionalOptions>", condition, os.args(additional_flags))
end
end
-- make source options for cl
function _make_resource_options_cl(vcxprojfile, flags)
-- get flags string
local flagstr = os.args(flags)
-- make PreprocessorDefinitions
local defstr = ""
for _, flag in ipairs(flags) do
flag:gsub("^[%-/]D(.*)",
function (def)
defstr = defstr .. vsutils.escape(def) .. ";"
end
)
end
defstr = defstr .. "%%(PreprocessorDefinitions)"
vcxprojfile:print("<PreprocessorDefinitions>%s</PreprocessorDefinitions>", defstr)
-- make AdditionalIncludeDirectories
if flagstr:find("[%-/]I") then
local dirs = {}
for _, flag in ipairs(flags) do
flag:gsub("^[%-/]I(.*)", function (dir) table.insert(dirs, vsutils.escape(dir)) end)
end
if #dirs > 0 then
vcxprojfile:print("<AdditionalIncludeDirectories>%s</AdditionalIncludeDirectories>", table.concat(dirs, ";"))
end
end
end
-- make source options for cuda
function _make_source_options_cuda(vcxprojfile, flags, opt)
-- exists condition?
condition = (opt and opt.condition) or ""
-- combine successive commands
flags = _combine_flags(flags, {"^%-gencode$", "^%-arch$", "^%-code$", "^%-%-machine$", "^%-rdc$", "^%-cudart$", "^%-%-keep%-dir$"})
-- get flags string
local flagstr = os.args(flags)
if not (opt and opt.link) then
-- make Optimization
if flagstr:find("[%-/]Od") then
vcxprojfile:print("<Optimization%s>Od</Optimization>", condition)
elseif flagstr:find("[%-/]O1") then
vcxprojfile:print("<Optimization%s>O1</Optimization>", condition)
elseif flagstr:find("[%-/]O2") then
vcxprojfile:print("<Optimization%s>O2</Optimization>", condition)
elseif flagstr:find("[%-/]O3") or flagstr:find("[%-/]Ox") then
vcxprojfile:print("<Optimization%s>O3</Optimization>", condition)
end
-- make Warning
if flagstr:find("[%-/]W[1234]") then
local wlevel = flagstr:match("[%-/](W[1234])")
vcxprojfile:print("<Warning%s>%s</Warning>", condition, wlevel)
elseif flagstr:find("[%-/]Wall") then
vcxprojfile:print("<Warning%s>Wall</Warning>", condition)
end
-- make Defines
local defstr = ""
for _, flag in ipairs(flags) do
flag:gsub("^[%-/]D(.*)",
function (def)
defstr = defstr .. vsutils.escape(def) .. ";"
end
)
end
defstr = defstr .. "%%(Defines)"
vcxprojfile:print("<Defines%s>%s</Defines>", condition, defstr)
-- make Include
if flagstr:find("[%-/]I") then
local dirs = {}
for _, flag in ipairs(flags) do
flag:gsub("^[%-/]I(.*)", function (dir) table.insert(dirs, vsutils.escape(dir)) end)
end
if #dirs > 0 then
vcxprojfile:print("<Include%s>%s</Include>", condition, table.concat(dirs, ";"))
end
end
end
-- make TargetMachinePlatform
local machinebitwidth
for _, flag in ipairs(flags) do
flag:gsub("^%-m(.+)", function (value) machinebitwidth = value end)
flag:gsub("^%-%-machine[ =](.+)", function (value) machinebitwidth = value end)
end
if machinebitwidth and (machinebitwidth == "32" or machinebitwidth == "64") then
vcxprojfile:print("<TargetMachinePlatform%s>%s</TargetMachinePlatform>", condition, machinebitwidth)
end
-- make CodeGeneration
local gpucode_patterns = {
"%-gencode[ =]arch=(.+),code=(.+)",
"%-%-generate%-code[ =]arch=(.+),code=(.+)",
"%-arch",
"%-%-gpu%-architecture",
"%-code",
"%-%-gpu%-code"
}
local has_gpucode = false
for _, pattern in ipairs(gpucode_patterns) do
if flagstr:find(pattern) then
has_gpucode = true
break
end
end
if has_gpucode then
local arch
local codes = {}
local gencodes = {}
for _, flag in ipairs(flags) do
flag:gsub("^%-gencode[ =]arch=(.+),code=(.+)$", function (garch, gcodes)
for _, gcode in ipairs(_split_gpucodes(gcodes)) do
table.insert(gencodes, garch .. "," .. gcode)
end
end)
flag:gsub("^%-%-generate%-code[ =]arch=(.+),code=(.+)", function (garch, gcodes)
for _, gcode in ipairs(_split_gpucodes(gcodes)) do
table.insert(gencodes, garch .. "," .. gcode)
end
end)
flag:gsub("^%-arch[ =](.+)", function (garch) arch = garch end)
flag:gsub("^%-%-gpu%-architecture[ =](.+)", function (garch) arch = garch end)
flag:gsub("^%-code[ =](.+)", function (gcodes) table.join2(codes, _split_gpucodes(gcodes)) end)
flag:gsub("^%-%-gpu%-code[ =](.+)", function (gcodes) table.join2(codes, _split_gpucodes(gcodes)) end)
end
if arch then
if #codes == 0 then
table.insert(codes, arch)
arch = arch:gsub("sm", "compute")
table.insert(gencodes, arch .. "," .. arch)
end
for _, code in ipairs(codes) do
table.insert(gencodes, arch .. "," .. code)
end
end
if #gencodes > 0 then
gencodes = table.unique(gencodes)
vcxprojfile:print("<CodeGeneration%s>%s</CodeGeneration>", condition, table.concat(gencodes, ";"))
end
end
if not (opt and opt.link) then
-- make CudaRuntime
local cudart
local cudaruntime = {
none = "None",
static = "Static",
shared = "Shared"
}
for _, flag in ipairs(flags) do
flag:gsub("%-cudart[ =](.+)", function (value) cudart = value end)
end
if cudart and cudaruntime[cudart] then
vcxprojfile:print("<CudaRuntime%s>%s</CudaRuntime>", condition, cudaruntime[cudart])
end
-- handle GPU debug info
if flagstr:find("%-G") then
vcxprojfile:print("<GPUDebugInfo%s>true</GPUDebugInfo>", condition)
end
-- handle fast math
if flagstr:find("%-use_fast_math") then
vcxprojfile:print("<FastMath%s>true</FastMath>", condition)
end
-- handle relocatable device code
local rdc
for _, flag in ipairs(flags) do
flag:gsub("%-rdc[ =](.+)", function (value) rdc = value end)
end
if rdc then
vcxprojfile:print("<GenerateRelocatableDeviceCode%s>%s</GenerateRelocatableDeviceCode>", condition, rdc)
end
-- handle keep preprocessed files or directories
if flagstr:find("%-%-keep") then
vcxprojfile:print("<Keep%s>true</Keep>", condition)
end
if flagstr:find("%-%-keep%-dir") then
local dirs = {}
for _, flag in ipairs(flags) do
flag:gsub("%-%-keep%-dir[ =](.*)", function (dir) table.insert(dirs, dir) end)
end
if #dirs > 0 then
vcxprojfile:print("<KeepDir%s>%s</KeepDir>", condition, table.concat(dirs, ";"))
end
end
end
-- make AdditionalOptions
local excludes = {
"Od", "O1", "O2", "O3", "Ox", "W1", "W2", "W3", "W4", "Wall", "I", "D", "L", "l", "m", "%-machine", "gencode", "arch", "code", "cudart", "G", "use_fast_math", "rdc", "%-keep", "%-keep%-dir"
}
local additional_flags = _exclude_flags(flags, excludes)
if #additional_flags > 0 then
vcxprojfile:print("<AdditionalOptions%s>%s %%(AdditionalOptions)</AdditionalOptions>", condition, os.args(additional_flags))
end
end
-- make custom commands item
function _make_custom_commands_item(vcxprojfile, commands, suffix)
if suffix == "after" or suffix == "after_link" then
vcxprojfile:print("<PostBuildEvent>")
elseif suffix == "before" then
vcxprojfile:print("<PreBuildEvent>")
elseif suffix == "before_link" then
vcxprojfile:print("<PreLinkEvent>")
end
vcxprojfile:print("<Message></Message>")
local cmdstr = "setlocal"
for _, command in ipairs(commands) do
cmdstr = cmdstr .. "\n" .. command
cmdstr = cmdstr .. "\nif %errorlevel% neq 0 goto :xmEnd"
end
cmdstr = cmdstr .. "\n" .. [[:xmEnd
endlocal & call :xmErrorLevel %errorlevel% & goto :xmDone
:xmErrorLevel
exit /b %1
:xmDone
if %errorlevel% neq 0 goto :VCEnd]]
vcxprojfile:print("<Command>%s</Command>", cmdstr:replace("<", " <"):replace(">", ">"):replace("/Fo ", "/Fo"))
if suffix == "after" or suffix == "after_link" then
vcxprojfile:print("</PostBuildEvent>")
elseif suffix == "before" then
vcxprojfile:print("</PreBuildEvent>")
elseif suffix == "before_link" then
vcxprojfile:print("</PreLinkEvent>")
end
end
-- make custom commands
function _make_custom_commands(vcxprojfile, target)
for suffix, cmds in pairs(target.commands) do
_make_custom_commands_item(vcxprojfile, cmds, suffix)
end
end
-- make common item
function _make_common_item(vcxprojfile, vsinfo, target, targetinfo)
-- init the linker kinds
local linkerkinds =
{
binary = "Link"
, static = "Lib"
, moduleonly = "Lib" -- emulate moduleonly with staticlib
, shared = "Link"
}
if not linkerkinds[targetinfo.targetkind] then
return
end
-- enter ItemDefinitionGroup
vcxprojfile:enter("<ItemDefinitionGroup Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s|%s\'\">", targetinfo.mode, targetinfo.arch)
-- for linker?
vcxprojfile:enter("<%s>", linkerkinds[targetinfo.targetkind])
-- save subsystem
local subsystem = "Console"
-- save profile
local profile = false
-- make linker flags
local flags = {}
local excludes = {
"nologo", "machine:%w+", "pdb:.+%.pdb", "debug"
}
local libdirs = {}
local links = {}
for _, flag in ipairs(_make_linkflags(targetinfo, target.project_dir)) do
local flag_lower = flag:lower()
-- remove "-subsystem:windows"
if flag_lower:find("[%-/]subsystem:windows") then
subsystem = "Windows"
elseif flag_lower:find("[%-/]libpath") then
-- link dir
flag:gsub("[%-/]libpath:(.*)", function (dir) table.insert(libdirs, vsutils.escape(dir)) end)
elseif flag_lower:find("[^%-/].+%.lib") then
-- link file
table.insert(links, flag)
elseif flag_lower:find("[%-/]profile") then
profile = true
else
local excluded = false
for _, exclude in ipairs(excludes) do
if flag:find("[%-/]" .. exclude) then
excluded = true
break
end
end
if not excluded then
table.insert(flags, flag)
end
end
end
-- make AdditionalLibraryDirectories
if #libdirs > 0 then
vcxprojfile:print("<AdditionalLibraryDirectories>%s;%%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>", table.concat(libdirs, ";"))
end
-- make AdditionalDependencies
if #links > 0 then
vcxprojfile:print("<AdditionalDependencies>%s;%%(AdditionalDependencies)</AdditionalDependencies>", table.concat(links, ";"))
end
-- make AdditionalOptions
if #flags > 0 then
flags = os.args(flags)
vcxprojfile:print("<AdditionalOptions>%s %%(AdditionalOptions)</AdditionalOptions>", vsutils.escape(flags))
end
-- generate debug infomation?
if linkerkinds[targetinfo.targetkind] == "Link" then
-- enable profile?
vcxprojfile:print("<Profile>%s</Profile>", tostring(profile))
-- enable debug infomation?
local debug = false
for _, symbol in ipairs(targetinfo.symbols) do
if symbol == "debug" then
debug = true
break
end
end
vcxprojfile:print("<GenerateDebugInformation>%s</GenerateDebugInformation>", tostring(debug))
end
-- make SubSystem
if targetinfo.targetkind == "binary" then
vcxprojfile:print("<SubSystem>%s</SubSystem>", subsystem)
end
-- make TargetMachine
vcxprojfile:print("<TargetMachine>%s</TargetMachine>", (targetinfo.arch == "x64" and "MachineX64" or "MachineX86"))
vcxprojfile:leave("</%s>", linkerkinds[targetinfo.targetkind])
-- for C/C++ compiler?
vcxprojfile:enter("<ClCompile>")
-- make source options
_make_source_options_cl(vcxprojfile, targetinfo.commonflags.cl)
-- add c and c++ standard
local clangflags = {
c11 = "stdc11",
c17 = "stdc17",
clatest = "stdc17",
gnu11 = "stdc11",
gnu17 = "stdc17",
gnulatest = "stdc17",
}
local cxxlangflags = {
cxx11 = "stdcpp11",
cxx14 = "stdcpp14",
cxx17 = "stdcpp17",
cxx1z = "stdcpp17",
cxx20 = "stdcpp20",
cxx2a = "stdcpplatest",
cxx23 = "stdcpplatest",
cxx2b = "stdcpplatest",
cxxlatest = "stdcpplatest",
gnuxx11 = "stdcpp11",
gnuxx14 = "stdcpp14",
gnuxx17 = "stdcpp17",
gnuxx1z = "stdcpp20",
gnux20 = "stdcpp20",
gnux2a = "stdcpplatest",
}
local cstandard
local cxxstandard
for _, lang in pairs(targetinfo.languages) do
lang = lang:replace("c++", "cxx", {plain = true})
if cxxlangflags[lang] then
cxxstandard = cxxlangflags[lang]
elseif clangflags[lang] then
cstandard = clangflags[lang]
end
end
if cxxstandard then
vcxprojfile:print("<LanguageStandard>%s</LanguageStandard>", cxxstandard)
end
if cstandard then
vcxprojfile:print("<LanguageStandard_C>%s</LanguageStandard_C>", cstandard)
end
if targetinfo.has_modules then
vcxprojfile:enter("<ScanSourceForModuleDependencies>true</ScanSourceForModuleDependencies>")
end
-- use c or c++ precompiled header
local pcheader = target.pcxxheader or target.pcheader
if pcheader then
-- make precompiled header and outputfile
vcxprojfile:print("<PrecompiledHeader>Use</PrecompiledHeader>")
vcxprojfile:print("<PrecompiledHeaderFile>%s</PrecompiledHeaderFile>", vsutils.escape(path.filename(pcheader)))
local pcoutputfile = targetinfo.pcxxoutputfile or targetinfo.pcoutputfile
if pcoutputfile then
vcxprojfile:print("<PrecompiledHeaderOutputFile>%s</PrecompiledHeaderOutputFile>", vsutils.escape(path.relative(path.absolute(pcoutputfile), target.project_dir)))
end
vcxprojfile:print("<ForcedIncludeFiles>%s;%%(ForcedIncludeFiles)</ForcedIncludeFiles>", vsutils.escape(path.filename(pcheader)))
end
vcxprojfile:leave("</ClCompile>")
vcxprojfile:enter("<ResourceCompile>")
-- make resource options
_make_resource_options_cl(vcxprojfile, targetinfo.commonflags.cl)
vcxprojfile:leave("</ResourceCompile>")
local cuda = _check_cuda(target)
if cuda then
-- for CUDA linker?
vcxprojfile:enter("<CudaLink>")
-- make cuda link flags
_make_source_options_cuda(vcxprojfile, targetinfo.culinkflags, {link = true})
-- make devlink
if targetinfo.cudevlink then
vcxprojfile:print("<PerformDeviceLink>%s</PerformDeviceLink>", targetinfo.cudevlink)
end
vcxprojfile:leave("</CudaLink>")
-- for CUDA compiler?
vcxprojfile:enter("<CudaCompile>")
-- make source options
_make_source_options_cuda(vcxprojfile, targetinfo.commonflags.cuda)
vcxprojfile:leave("</CudaCompile>")
end
-- make custom commands
_make_custom_commands(vcxprojfile, targetinfo)
-- leave ItemDefinitionGroup
vcxprojfile:leave("</ItemDefinitionGroup>")
end
-- build common items (doesn't print anything)
function _build_common_items(vsinfo, target)
-- for each mode and arch
for _, targetinfo in ipairs(target.info) do
-- make source flags
local flags_stats = {cl = {}, cuda = {}}
local files_count = {cl = 0, cuda = 0}
local first_flags = {}
targetinfo.sourceflags = {}
for _, sourcebatch in pairs(targetinfo.sourcebatches) do
local sourcekind = sourcebatch.sourcekind
local rulename = sourcebatch.rulename
if (rulename == "c.build" or rulename == "c++.build" or rulename == "c++.build.modules" or rulename == "asm.build" or sourcekind == "mrc") then
for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
-- make compiler flags
local flags = _make_compflags(sourcefile, targetinfo, target.project_dir)
-- no common flags for asm/rc
if sourcekind ~= "as" and sourcekind ~= "mrc" then
for _, flag in ipairs(table.unique(flags)) do
flags_stats.cl[flag] = (flags_stats.cl[flag] or 0) + 1
end
-- update files count
files_count.cl = files_count.cl + 1
-- save first flags
if first_flags.cl == nil then
first_flags.cl = flags
end
end
-- save source flags
targetinfo.sourceflags[sourcefile] = flags
end
elseif sourcekind == "cu" then
for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
-- make compiler flags
local flags = _make_compflags(sourcefile, targetinfo, target.project_dir)
-- count flags
for _, flag in ipairs(table.unique(flags)) do
flags_stats.cuda[flag] = (flags_stats.cuda[flag] or 0) + 1
end
-- update files count
files_count.cuda = files_count.cuda + 1
-- save first flags
if first_flags.cuda == nil then
first_flags.cuda = flags
end
-- save source flags
targetinfo.sourceflags[sourcefile] = flags
end
end
end
-- make common flags
targetinfo.commonflags = {cl = {}, cuda = {}}
for _, comp in ipairs({"cl", "cuda"}) do
for _, flag in ipairs(first_flags[comp]) do
if flags_stats[comp][flag] >= files_count[comp] then
table.insert(targetinfo.commonflags[comp], flag)
end
end
end
-- remove common flags from source flags
local sourceflags = {}
for _, sourcebatch in pairs(targetinfo.sourcebatches) do
local sourcekind = sourcebatch.sourcekind
local rulename = sourcebatch.rulename
if (sourcekind == "as" or sourcekind == "mrc") then
-- no common flags for as/mrc files
for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
sourceflags[sourcefile] = targetinfo.sourceflags[sourcefile]
end
elseif rulename == "c.build" or rulename == "c++.build" or rulename == "c++.build.modules" then -- sourcekind maybe bind multiple rules, e.g. c++modules
for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
local flags = targetinfo.sourceflags[sourcefile]
local otherflags = {}
for _, flag in ipairs(flags) do
if flags_stats.cl[flag] < files_count.cl then
table.insert(otherflags, flag)
end
end
sourceflags[sourcefile] = otherflags
end
elseif sourcekind == "cu" then
for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
local flags = targetinfo.sourceflags[sourcefile]
local otherflags = {}
for _, flag in ipairs(flags) do
if flags_stats.cuda[flag] < files_count.cuda then
table.insert(otherflags, flag)
end
end
sourceflags[sourcefile] = otherflags
end
end
end
targetinfo.sourceflags = sourceflags
end
end
-- make common items
function _make_common_items(vcxprojfile, vsinfo, target)
-- for each mode and arch
for _, targetinfo in ipairs(target.info) do
-- make common item
_make_common_item(vcxprojfile, vsinfo, target, targetinfo)
end
end
-- make header file
function _make_include_file(vcxprojfile, includefile, vcxprojdir)
vcxprojfile:print("<ClInclude Include=\"%s\" />", path.relative(path.absolute(includefile), vcxprojdir))
end
-- make source file for all modes
function _make_source_file_forall(vcxprojfile, vsinfo, target, sourcefile, sourceinfo)
-- get object file and source kind
local sourcekind
for _, info in ipairs(sourceinfo) do
sourcekind = info.sourcekind
break
end
-- enter it
local nodename
if sourcekind == "as" then nodename = "CustomBuild"
elseif sourcekind == "mrc" then nodename = "ResourceCompile"
elseif sourcekind == "cu" then nodename = "CudaCompile"
elseif sourcekind == "cc" or sourcekind == "cxx" then nodename = "ClCompile"
end
sourcefile = path.relative(path.absolute(sourcefile), target.project_dir)
vcxprojfile:enter("<%s Include=\"%s\">", nodename, sourcefile)
-- for *.asm files
if sourcekind == "as" then
vcxprojfile:print("<ExcludedFromBuild>false</ExcludedFromBuild>")
vcxprojfile:print("<FileType>Document</FileType>")
for _, info in ipairs(sourceinfo) do
local objectfile = path.relative(path.absolute(info.objectfile), target.project_dir)
local compcmd = _make_compcmd(info.compargv, sourcefile, objectfile, target.project_dir)
vcxprojfile:print("<Outputs Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s\'\">%s</Outputs>", info.mode .. '|' .. info.arch, objectfile)
vcxprojfile:print("<Command Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s\'\">%s</Command>", info.mode .. '|' .. info.arch, compcmd)
end
vcxprojfile:print("<Message>%s</Message>", path.filename(sourcefile))
-- for *.rc files
elseif sourcekind == "mrc" then
for _, info in ipairs(sourceinfo) do
local objectfile = path.relative(path.absolute(info.objectfile), target.project_dir)
vcxprojfile:print("<ResourceOutputFileName Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s|%s\'\">%s</ResourceOutputFileName>",
info.mode, info.arch, objectfile)
end
-- for *.c/cpp/cu files
else
-- compile as c++ modules
if support.has_module_extension(sourcefile) then
vcxprojfile:print("<CompileAs>CompileAsCppModule</CompileAs>")
end
-- we need to use different object directory and allow parallel building
--
-- @see https://github.com/xmake-io/xmake/issues/2016
-- https://github.com/xmake-io/xmake/issues/1062
for _, info in ipairs(sourceinfo) do
local objectname = path.filename(info.objectfile)
local targetinfo = info.targetinfo
if not targetinfo.objectnames then
targetinfo.objectnames = hashset:new()
end
if targetinfo.objectnames:has(objectname) then
local outputnode = (sourcekind == "cu" and "CompileOut" or "ObjectFileName")
local objectfile = path.relative(path.absolute(info.objectfile), target.project_dir)
vcxprojfile:print("<%s Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s|%s\'\">%s</%s>",
outputnode, info.mode, info.arch, objectfile, outputnode)
else
targetinfo.objectnames:insert(objectname)
end
end
-- init items
local items =
{
AdditionalOptions =
{
key = function (info) return os.args(info.flags) end
, value = function (key) return key .. " %%(AdditionalOptions)" end
}
}
-- make items
for itemname, iteminfo in pairs(items) do
-- make merge keys
local mergekeys = {}
for _, info in ipairs(sourceinfo) do
local key = iteminfo.key(info)
mergekeys[key] = mergekeys[key] or {}
mergekeys[key][info.mode .. '|' .. info.arch] = true
end
for key, mergeinfos in pairs(mergekeys) do
-- merge mode and arch first
local count = 0
for _, mode in ipairs(vsinfo.modes) do
if mergeinfos[mode .. "|Win32"] and mergeinfos[mode .. "|x64"] then
mergeinfos[mode .. "|Win32"] = nil
mergeinfos[mode .. "|x64"] = nil
mergeinfos[mode] = true
end
if mergeinfos[mode] then
count = count + 1
end
end
-- disable the precompiled header if sourcekind ~= headerkind
local pcheader = target.pcxxheader or target.pcheader
local pcheader_disable = false
if sourcekind == "cu" or (pcheader and language.sourcekind_of(sourcefile) ~= (target.pcxxheader and "cxx" or "cc")) then
pcheader_disable = true
end
-- all modes and archs exist?
if count == #vsinfo.modes then
if #key > 0 then
vcxprojfile:print("<%s>%s</%s>", itemname, iteminfo.value(key), itemname)
if pcheader_disable then
vcxprojfile:print("<PrecompiledHeader>NotUsing</PrecompiledHeader>")
end
end
else
for cond, _ in pairs(mergeinfos) do
if cond:find('|', 1, true) then
-- for mode | arch
if #key > 0 then
vcxprojfile:print("<%s Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s\'\">%s</%s>", itemname, cond, iteminfo.value(key), itemname)
if pcheader_disable then
vcxprojfile:print("<PrecompiledHeader Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s\'\">NotUsing</PrecompiledHeader>", cond)
end
end
else
-- only for mode
if #key > 0 then
vcxprojfile:print("<%s Condition=\"\'%$(Configuration)\'==\'%s\'\">%s</%s>", itemname, cond, iteminfo.value(key), itemname)
if pcheader_disable then
vcxprojfile:print("<PrecompiledHeader Condition=\"\'%$(Configuration)\'==\'%s\'\">NotUsing</PrecompiledHeader>", cond)
end
end
end
end
end
end
end
end
-- leave it
vcxprojfile:leave("</%s>", nodename)
end
-- make source file for specific modes
function _make_source_file_forspec(vcxprojfile, vsinfo, target, sourcefile, sourceinfo)
-- add source file
sourcefile = path.relative(path.absolute(sourcefile), target.project_dir)
for _, info in ipairs(sourceinfo) do
-- enter it
local nodename
if info.sourcekind == "as" then nodename = "CustomBuild"
elseif info.sourcekind == "mrc" then nodename = "ResourceCompile"
elseif info.sourcekind == "cu" then nodename = "CudaCompile"
elseif info.sourcekind == "cc" or info.sourcekind == "cxx" then nodename = "ClCompile"
end
vcxprojfile:enter("<%s Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s|%s\'\" Include=\"%s\">",
nodename, info.mode, info.arch, sourcefile)
-- for *.asm files
local objectfile = path.relative(path.absolute(info.objectfile), target.project_dir)
if info.sourcekind == "as" then
local compcmd = _make_compcmd(info.compargv, sourcefile, objectfile, target.project_dir)
vcxprojfile:print("<ExcludedFromBuild>false</ExcludedFromBuild>")
vcxprojfile:print("<FileType>Document</FileType>")
vcxprojfile:print("<Outputs>%s</Outputs>", objectfile)
vcxprojfile:print("<Command>%s</Command>", compcmd)
-- for *.rc files
elseif info.sourcekind == "mrc" then
vcxprojfile:print("<ResourceOutputFileName Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s|%s\'\">%s</ResourceOutputFileName>",
info.mode, info.arch, objectfile)
-- for *.c/cpp/cu files
else
-- compile as c++ modules
if support.has_module_extension(sourcefile) then
vcxprojfile:print("<CompileAs>CompileAsCppModule</CompileAs>")
end
-- we need to use different object directory and allow parallel building
--
-- @see https://github.com/xmake-io/xmake/issues/2016
-- https://github.com/xmake-io/xmake/issues/1062
local objectname = path.filename(objectfile)
local targetinfo = info.targetinfo
if not targetinfo.objectnames then
targetinfo.objectnames = hashset:new()
end
local targetinfo = info.targetinfo
local outputnode = (info.sourcekind == "cu" and "CompileOut" or "ObjectFileName")
if targetinfo.objectnames:has(objectname) then
vcxprojfile:print("<%s Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s|%s\'\">%s</%s>",
outputnode, info.mode, info.arch, objectfile, outputnode)
else
targetinfo.objectnames:insert(objectname)
end
-- disable the precompiled header if sourcekind ~= headerkind
local pcheader = target.pcxxheader or target.pcheader
if pcheader and info.sourcekind ~= "cu" and language.sourcekind_of(sourcefile) ~= (target.pcxxheader and "cxx" or "cc") then
vcxprojfile:print("<PrecompiledHeader>NotUsing</PrecompiledHeader>")
end
vcxprojfile:print("<AdditionalOptions>%s %%(AdditionalOptions)</AdditionalOptions>", os.args(info.flags))
end
-- leave it
vcxprojfile:leave("</%s>", nodename)
end
end
-- make source file for precompiled header
function _make_source_file_forpch(vcxprojfile, vsinfo, target)
-- add precompiled source file
local pcheader = target.pcxxheader or target.pcheader
if pcheader then
local sourcefile = path.relative(path.absolute(pcheader), target.project_dir)
vcxprojfile:enter("<ClCompile Include=\"%s\">", sourcefile)
vcxprojfile:print("<PrecompiledHeader>Create</PrecompiledHeader>")
vcxprojfile:print("<PrecompiledHeaderFile></PrecompiledHeaderFile>")
vcxprojfile:print("<AdditionalOptions> %%(AdditionalOptions)</AdditionalOptions>")
for _, info in ipairs(target.info) do
-- compile as c/c++
local compileas = (target.pcxxheader and "CompileAsCpp" or "CompileAsC")
vcxprojfile:print("<CompileAs Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s|%s\'\">%s</CompileAs>", info.mode, info.arch, compileas)
-- add object file
local pcoutputfile = info.pcxxoutputfile or info.pcoutputfile
if pcoutputfile then
local objectfile = path.relative(path.absolute(pcoutputfile .. ".obj"), target.project_dir)
vcxprojfile:print("<ObjectFileName Condition=\"\'%$(Configuration)|%$(Platform)\'==\'%s|%s\'\">%s</ObjectFileName>", info.mode, info.arch, objectfile)
end
end
vcxprojfile:leave("</ClCompile>")
end
end
-- make source files
function _make_source_files(vcxprojfile, vsinfo, target)
-- add source files
vcxprojfile:enter("<ItemGroup>")
-- make source file infos
local sourceinfos = {}
for _, targetinfo in ipairs(target.info) do
for _, sourcebatch in pairs(targetinfo.sourcebatches) do
local sourcekind = sourcebatch.sourcekind
local rulename = sourcebatch.rulename
if (rulename == "c.build" or rulename == "c++.build" or sourcekind == "as" or sourcekind == "mrc" or sourcekind == "cu") then
local objectfiles = sourcebatch.objectfiles
for idx, sourcefile in ipairs(sourcebatch.sourcefiles) do
local objectfile = objectfiles[idx]
local flags = targetinfo.sourceflags[sourcefile]
sourceinfos[sourcefile] = sourceinfos[sourcefile] or {}
table.insert(sourceinfos[sourcefile], {targetinfo = targetinfo, mode = targetinfo.mode, arch = targetinfo.arch, sourcekind = sourcekind, objectfile = objectfile, flags = flags, compargv = targetinfo.compargvs[sourcefile]})
end
elseif rulename == "c++.build.modules" then
local builder_batch = targetinfo.sourcebatches["c++.build.modules.builder"]
table.sort(builder_batch.objectfiles)
local objectfiles = builder_batch.objectfiles
for idx, sourcefile in ipairs(sourcebatch.sourcefiles) do
local is_named_module = table.contains(builder_batch.sourcefiles, sourcefile)
if is_named_module then
local objectfile = objectfiles[idx]
local flags = targetinfo.sourceflags[sourcefile]
sourceinfos[sourcefile] = sourceinfos[sourcefile] or {}
table.insert(sourceinfos[sourcefile], {targetinfo = targetinfo, mode = targetinfo.mode, arch = targetinfo.arch, sourcekind = "cxx", objectfile = objectfile, flags = flags, compargv = targetinfo.compargvs[sourcefile]})
end
end
end
end
end
-- make source files
for sourcefile, sourceinfo in table.orderpairs(sourceinfos) do
if #sourceinfo == #target.info then
_make_source_file_forall(vcxprojfile, vsinfo, target, sourcefile, sourceinfo)
else
_make_source_file_forspec(vcxprojfile, vsinfo, target, sourcefile, sourceinfo)
end
end
-- make precompiled source file
_make_source_file_forpch(vcxprojfile, vsinfo, target)
vcxprojfile:leave("</ItemGroup>")
-- add include files
local pcheader = target.pcxxheader or target.pcheader
vcxprojfile:enter("<ItemGroup>")
for _, includefile in ipairs(table.join(target.headerfiles or {}, target.extrafiles)) do
-- we need to ignore pcheader file to fix https://github.com/xmake-io/xmake/issues/1171
if not pcheader or includefile ~= pcheader then
_make_include_file(vcxprojfile, includefile, target.project_dir)
end
end
vcxprojfile:leave("</ItemGroup>")
end
-- make vcxproj
function make(vsinfo, target)
-- the target name
local targetname = target.name
-- the vcxproj directory
local vcxprojdir = target.project_dir
-- build common flags
_build_common_items(vsinfo, target)
-- open vcxproj file
local vcxprojpath = path.join(vcxprojdir, targetname .. ".vcxproj")
local vcxprojfile = vsfile.open(vcxprojpath, "w")
-- init indent character
vsfile.indentchar(' ')
-- make header
_make_header(vcxprojfile, vsinfo)
-- make Configurations
_make_configurations(vcxprojfile, vsinfo, target)
-- make common items
_make_common_items(vcxprojfile, vsinfo, target)
-- make source files
_make_source_files(vcxprojfile, vsinfo, target)
-- make deps references
_make_references(vcxprojfile, vsinfo, target)
-- make tailer
_make_tailer(vcxprojfile, vsinfo, target)
-- exit solution file
vcxprojfile:close()
end
|