-
-
Notifications
You must be signed in to change notification settings - Fork 651
Expand file tree
/
Copy pathmsc.lua
More file actions
579 lines (494 loc) · 12.8 KB
/
Copy pathmsc.lua
File metadata and controls
579 lines (494 loc) · 12.8 KB
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
---
-- msc.lua
-- Interface for the MS C/C++ compiler.
-- Author Jess Perkins
-- Modified by Manu Evans
-- Copyright (c) 2009-2015 Jess Perkins and the Premake project
---
local p = premake
p.tools.msc = {}
local msc = p.tools.msc
local project = p.project
local config = p.config
local string = require("string")
-- string comparison `toolset >= "msc-v142"` won't work with "msc-v80"
local function isVersionGreaterOrEqualTo(lhs, rhs)
if lhs == nil or rhs == nil then
return false
end
lhs = _G.tonumber(string.match(lhs, "^msc%-v([0-9]+)$"))
rhs = _G.tonumber(string.match(rhs, "^msc%-v([0-9]+)$"))
if lhs == nil or rhs == nil then
return false
end
return lhs >= rhs
end
--
-- Returns list of C preprocessor flags for a configuration.
--
function msc.getcppflags(cfg)
return {}
end
--
-- Returns list of C compiler flags for a configuration.
--
local function getRuntimeFlag(cfg, isstatic)
local rt = cfg.runtime
local flag = iif(isstatic, "/MT", "/MD")
if (rt == "Debug") or (rt == nil and config.isDebugBuild(cfg)) then
flag = flag .. "d"
end
return flag
end
msc.shared = {
clr = {
On = "/clr",
Unsafe = "/clr",
Pure = "/clr:pure",
Safe = "/clr:safe",
},
compileas = {
["C"] = "/TC",
["C++"] = "/TP",
},
fatalwarnings = {
All = "/WX"
},
floatingpoint = {
Fast = "/fp:fast",
Strict = "/fp:strict",
Precise = "/fp:precise",
},
floatingpointexceptions = {
On = "/fp:except",
Off = "/fp:except-",
},
functionlevellinking = {
On = "/Gy",
Off = "/Gy-",
},
callingconvention = {
Cdecl = "/Gd",
FastCall = "/Gr",
StdCall = "/Gz",
VectorCall = "/Gv",
},
dynamicdebugging = {
On = "/dynamicdeopt",
},
intrinsics = {
On = "/Oi",
},
linktimeoptimization = {
On = "/GL",
Fast = "/GL",
},
multiprocessorcompile = {
On = "/MP",
},
minimalrebuild = {
Off = "/Gm-",
},
optimize = {
Off = "/Od",
On = "/Ot",
Debug = "/Od",
Full = "/Ox",
Size = "/O1",
Speed = "/O2",
},
vectorextensions = {
AVX = "/arch:AVX",
AVX2 = "/arch:AVX2",
SSE = "/arch:SSE",
SSE2 = "/arch:SSE2",
SSE3 = "/arch:SSE2",
SSSE3 = "/arch:SSE2",
["SSE4.1"] = "/arch:SSE2",
["SSE4.2"] = "/arch:SSE2",
},
warnings = {
Off = "/W0",
High = "/W4",
Extra = "/W4",
Everything = "/Wall",
},
externalwarnings = {
Off = "/external:W0",
Default = "/external:W3",
High = "/external:W4",
Extra = "/external:W4",
Everything = "/external:W4",
},
externalanglebrackets = {
On = "/external:anglebrackets",
},
nodefaultlib = {
On = "/Zl",
},
staticruntime = {
-- runtime defaults to dynamic in VS
Default = function(cfg) return getRuntimeFlag(cfg, false) end,
On = function(cfg) return getRuntimeFlag(cfg, true) end,
Off = function(cfg) return getRuntimeFlag(cfg, false) end,
},
stringpooling = {
On = "/GF",
Off = "/GF-",
},
structmemberalign = {
[1] = "/Zp1",
[2] = "/Zp2",
[4] = "/Zp4",
[8] = "/Zp8",
[16] = "/Zp16",
},
symbols = {
On = "/Z7"
},
unsignedchar = {
On = "/J",
},
omitframepointer = {
On = "/Oy"
},
justmycode = {
On = "/JMC",
Off = "/JMC-"
},
openmp = {
On = "/openmp",
Off = "/openmp-"
},
usestandardpreprocessor = {
On = "/Zc:preprocessor",
Off = "/Zc:preprocessor-"
}
}
function msc.getsharedflags(cfg)
local shared = config.mapFlags(cfg, msc.shared)
-- D9007: '/external:I' requires '/external:W'
if (#cfg.externalincludedirs > 0 or #cfg.includedirsafter > 0)
and cfg.externalwarnings == nil
and isVersionGreaterOrEqualTo(cfg.toolset, "msc-v142")
then
table.insert(shared, msc.shared.externalwarnings.Default)
end
return shared
end
msc.cflags = {
cdialect = {
["C11"] = "/std:c11",
["C17"] = "/std:c17",
["C23"] = "/std:clatest"
}
}
function msc.getcflags(cfg)
local shared = msc.getsharedflags(cfg)
local cflags = config.mapFlags(cfg, msc.cflags)
local flags = table.join(shared, cflags, msc.getwarnings(cfg))
return flags
end
--
-- Returns list of C++ compiler flags for a configuration.
--
msc.cxxflags = {
cppdialect = {
["C++14"] = "/std:c++14",
["C++17"] = "/std:c++17",
["C++20"] = "/std:c++20",
["C++23"] = "/std:c++23preview",
["C++26"] = "/std:c++latest",
["C++latest"] = "/std:c++latest",
},
exceptionhandling = {
Default = "/EHsc",
On = "/EHsc",
SEH = "/EHa",
},
rtti = {
Off = "/GR-"
},
sanitize = {
Address = "/fsanitize=address",
Fuzzer = "/fsanitize=fuzzer",
}
}
function msc.getcxxflags(cfg)
local shared = msc.getsharedflags(cfg)
local cxxflags = config.mapFlags(cfg, msc.cxxflags)
local flags = table.join(shared, cxxflags, msc.getwarnings(cfg))
return flags
end
--
-- Decorate defines for the MSVC command line.
--
msc.defines = {
characterset = {
Default = { '/D_UNICODE', '/DUNICODE' },
MBCS = '/D_MBCS',
Unicode = { '/D_UNICODE', '/DUNICODE' },
ASCII = { },
}
}
function msc.getdefines(defines, cfg)
local result
-- HACK: I need the cfg to tell what the character set defines should be. But
-- there's lots of legacy code using the old getdefines(defines) signature.
-- For now, detect one or two arguments and apply the right behavior; will fix
-- it properly when the I roll out the adapter overhaul
if cfg and defines then
result = config.mapFlags(cfg, msc.defines)
else
result = {}
end
for _, define in ipairs(defines) do
table.insert(result, '/D' .. p.esc(define))
end
if cfg and cfg.exceptionhandling == p.OFF then
table.insert(result, "/D_HAS_EXCEPTIONS=0")
end
return result
end
function msc.getundefines(undefines)
return table.translate(undefines, function (undefine) return '/U' .. p.esc(undefine) end)
end
--
-- Returns a list of forced include files, decorated for the compiler
-- command line.
--
-- @param cfg
-- The project configuration.
-- @return
-- An array of force include files with the appropriate flags.
--
function msc.getforceincludes(cfg)
local result = {}
table.foreachi(cfg.forceincludes, function(value)
local fn = p.tools.getrelative(cfg.project, value)
table.insert(result, "/FI" .. p.quoted(fn))
end)
return result
end
function msc.getpch(cfg)
-- TODO: Add support for MSC precompiled headers in gmake
return nil
end
function msc.getrunpathdirs()
return {}
end
function msc.getSections()
return {}
end
--
-- Decorate include file search paths for the MSVC command line.
--
function msc.getstructuredincludedirs(cfg, dirs, extdirs, frameworkdirs, includedirsafter)
local result = {}
for _, dir in ipairs(dirs) do
dir = p.tools.getrelative(cfg.project, dir)
table.insert(result, { flag = '/I', value = p.quoted(dir) })
end
for _, dir in ipairs(extdirs or {}) do
dir = p.tools.getrelative(cfg.project, dir)
if isVersionGreaterOrEqualTo(cfg.toolset, "msc-v142") then
table.insert(result, { flag = '/external:I', value = p.quoted(dir) })
else
table.insert(result, { flag = '/I', value = p.quoted(dir) })
end
end
for _, dir in ipairs(includedirsafter or {}) do
dir = p.tools.getrelative(cfg.project, dir)
if isVersionGreaterOrEqualTo(cfg.toolset, "msc-v142") then
table.insert(result, { flag = '/external:I', value = p.quoted(dir) })
else
table.insert(result, { flag = '/I', value = p.quoted(dir) })
end
end
return result
end
function msc.getincludedirs(cfg, dirs, extdirs, frameworkdirs, includedirsafter)
local result = msc.getstructuredincludedirs(cfg, dirs, extdirs, frameworkdirs, includedirsafter)
return table.flatten(table.translate(result, function(kv)
return kv.flag .. kv.value
end))
end
function msc.getstructuredimplicitincludedirs(toolname, language)
return {}
end
--
-- Return a list of linker flags for a specific configuration.
--
msc.linkerFlags = {
linkerfatalwarnings = {
All = "/WX",
},
manifest = {
Off = "/MANIFEST:NO",
},
incrementallink = {
Off = "/INCREMENTAL:NO",
},
kind = {
SharedLib = "/DLL",
WindowedApp = "/SUBSYSTEM:WINDOWS"
},
linktimeoptimization = {
On = "/LTCG",
Fast = "/LTCG:incremental",
},
nodefaultlib = {
On = "/NODEFAULTLIB",
},
symbols = {
On = "/DEBUG"
},
dynamicdebugging = {
On = "/dynamicdeopt",
},
}
msc.librarianFlags = {
linkerfatalwarnings = {
All = "/WX",
}
}
function msc.wholearchive(cfg)
return table.translate(config.getwholearchive(cfg), function(libraryname) return "/WHOLEARCHIVE:" .. libraryname end)
end
function msc.getldflags(cfg)
local map = iif(cfg.kind ~= p.STATICLIB, msc.linkerFlags, msc.librarianFlags)
local flags = config.mapFlags(cfg, map)
if cfg.entrypoint then
-- /ENTRY requires that /SUBSYSTEM is set.
if cfg.kind == "ConsoleApp" then
table.insert(flags, "/SUBSYSTEM:CONSOLE")
elseif cfg.kind ~= "WindowedApp" then -- already set by above map
table.insert(flags, "/SUBSYSTEM:NATIVE") -- fallback
end
table.insert(flags, '/ENTRY:' .. cfg.entrypoint)
end
table.insert(flags, 1, "/NOLOGO")
-- Ignore default libraries
for i, ignore in ipairs(cfg.ignoredefaultlibraries) do
-- Add extension if required
if not msc.getLibraryExtensions()[ignore:match("[^.]+$")] then
ignore = path.appendextension(ignore, ".lib")
end
table.insert(flags, '/NODEFAULTLIB:' .. ignore)
end
if cfg.kind == "ConsoleApp" or cfg.kind == "WindowedApp" or cfg.kind == "SharedLib" then
if cfg.profile then
table.insert(flags, "/PROFILE")
end
end
flags = table.join(flags, msc.wholearchive(cfg))
return flags
end
--
-- Build a list of additional library directories for a particular
-- project configuration, decorated for the tool command line.
--
-- @param cfg
-- The project configuration.
-- @return
-- An array of decorated additional library directories.
--
function msc.getLibraryDirectories(cfg)
local flags = {}
local dirs = table.join(cfg.libdirs, cfg.syslibdirs)
for i, dir in ipairs(dirs) do
dir = p.tools.getrelative(cfg.project, dir)
table.insert(flags, '/LIBPATH:"' .. dir .. '"')
end
return flags
end
--
-- Return a list of valid library extensions
--
function msc.getLibraryExtensions()
return {
["lib"] = true,
["obj"] = true,
}
end
--
-- Return the list of libraries to link, decorated with flags as needed.
--
function msc.getlinks(cfg, systemonly, nogroups)
local links = {}
-- If we need sibling projects to be listed explicitly, grab them first
if not systemonly then
links = config.getlinks(cfg, "siblings", "fullpath", nil, true)
end
-- Then the system libraries, which come undecorated
local system = config.getlinks(cfg, "system", "fullpath", nil, true)
for i = 1, #system do
-- Add extension if required
local link = system[i]
if not p.tools.msc.getLibraryExtensions()[link:match("[^.]+$")] then
link = path.appendextension(link, ".lib")
end
table.insert(links, link)
end
return links
end
--
-- Returns makefile-specific configuration rules.
--
function msc.getmakesettings(cfg)
return nil
end
--
-- Retrieves the executable command name for a tool, based on the
-- provided configuration and the operating environment.
--
-- @param cfg
-- The configuration to query.
-- @param tool
-- The tool to fetch, one of "cc" for the C compiler, "cxx" for
-- the C++ compiler, or "ar" for the static linker.
-- @return
-- The executable command name for a tool, or nil if the system's
-- default value should be used.
--
msc.tools = {
cc = "cl",
cxx = "cl",
ar = "lib",
rc = "rc"
}
function msc.gettoolname(cfg, tool)
local toolset, version = p.tools.canonical(cfg.toolset or p.MSC)
-- TODO: Support versioning?
return msc.tools[tool]
end
function msc.gettooloutputext(tool)
return iif(tool == "rc", ".res", ".obj")
end
function msc.gettoolflags(cfg, tool, input, output, depfile)
if tool == "rc" then
return string.format('/nologo /fo%s %s', output, input)
end
local lang = iif(tool == "cc", "/Tc", "/Tp")
return string.format('/nologo /Fo%s /c %s%s', output, lang, input)
end
function msc.getlinkcommand(cfg, linker, output, objects, resources, ldflags, libs)
if cfg.kind == p.STATICLIB then
return string.format('%s /nologo /OUT:%s %s', linker, output, objects)
end
local shared = iif(cfg.kind == p.SHAREDLIB, " /LD", "")
return string.format('%s%s /nologo /Fe%s %s %s %s /link %s', linker, shared, output, objects, resources, libs, ldflags)
end
function msc.getwarnings(cfg)
local result = {}
for _, enable in ipairs(cfg.enablewarnings) do
table.insert(result, '/w1"' .. enable .. '"')
end
for _, disable in ipairs(cfg.disablewarnings) do
table.insert(result, '/wd"' .. disable .. '"')
end
for _, fatal in ipairs(p.filterFatalWarnings(cfg.fatalwarnings)) do
table.insert(result, '/we"' .. fatal .. '"')
end
return result
end