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
|
--!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, TBOOX Open Source Group.
--
-- @author ruki
-- @file find_vstudio.lua
--
-- imports
import("core.project.config")
import("lib.detect.find_file")
import("lib.detect.find_tool")
import("core.cache.global_detectcache")
-- init vc variables
local vcvars = {"path",
"lib",
"libpath",
"include",
"DevEnvdir",
"VSInstallDir",
"VCInstallDir",
"WindowsSdkDir",
"WindowsLibPath",
"WindowsSDKVersion",
"WindowsSdkBinPath",
"WindowsSdkVerBinPath",
"ExtensionSdkDir",
"UniversalCRTSdkDir",
"UCRTVersion",
"VCToolsVersion",
"VCIDEInstallDir",
"VCToolsInstallDir",
"VCToolsRedistDir",
"VisualStudioVersion",
"VSCMD_VER",
"VSCMD_ARG_app_plat",
"VSCMD_ARG_HOST_ARCH",
"VSCMD_ARG_TGT_ARCH"}
-- init vsvers
local vsvers =
{
["17.0"] = "2022"
, ["16.0"] = "2019"
, ["15.0"] = "2017"
, ["14.0"] = "2015"
, ["12.0"] = "2013"
, ["11.0"] = "2012"
, ["10.0"] = "2010"
, ["9.0"] = "2008"
, ["8.0"] = "2005"
, ["7.1"] = "2003"
, ["7.0"] = "7.0"
, ["6.0"] = "6.0"
, ["5.0"] = "5.0"
, ["4.2"] = "4.2"
}
-- init vsenvs
local vsenvs =
{
["17.0"] = "VS170COMNTOOLS"
, ["16.0"] = "VS160COMNTOOLS"
, ["15.0"] = "VS150COMNTOOLS"
, ["14.0"] = "VS140COMNTOOLS"
, ["12.0"] = "VS120COMNTOOLS"
, ["11.0"] = "VS110COMNTOOLS"
, ["10.0"] = "VS100COMNTOOLS"
, ["9.0"] = "VS90COMNTOOLS"
, ["8.0"] = "VS80COMNTOOLS"
, ["7.1"] = "VS71COMNTOOLS"
, ["7.0"] = "VS70COMNTOOLS"
, ["6.0"] = "VS60COMNTOOLS"
, ["5.0"] = "VS50COMNTOOLS"
, ["4.2"] = "VS42COMNTOOLS"
}
-- get all known Visual Studio environment variables
function get_vcvars()
local realvcvars = vcvars
for _, v in pairs(vsenvs) do
table.insert(realvcvars, v)
end
return realvcvars
end
-- load vcvarsall environment variables
function _load_vcvarsall(vcvarsall, vsver, arch, opt)
opt = opt or {}
-- is VsDevCmd.bat?
local is_vsdevcmd = path.basename(vcvarsall):lower() == "vsdevcmd"
-- make the genvcvars.bat
local genvcvars_bat = os.tmpfile() .. "_genvcvars.bat"
local file = io.open(genvcvars_bat, "w")
file:print("@echo off")
-- @note we need to get utf8 output from cmd.exe
-- because some %PATH% and other envs maybe contains unicode characters
if winos.version():gt("winxp") then
file:print("chcp 65001")
end
-- fix error caused by the new vsDevCmd.bat of vs2019
-- @see https://github.com/xmake-io/xmake/issues/549
if vsver and tonumber(vsver) >= 16 then
file:print("set VSCMD_SKIP_SENDTELEMETRY=yes")
end
local host_arch = os.arch()
if is_vsdevcmd then
if vsver and tonumber(vsver) >= 16 then
if opt.vcvars_ver then
file:print("call \"%s\" -host_arch=%s -arch=%s -winsdk=%s -vcvars_ver=%s > nul", vcvarsall, host_arch, arch, opt.sdkver and opt.sdkver or "", opt.vcvars_ver)
else
file:print("call \"%s\" -host_arch=%s -arch=%s -winsdk=%s > nul", vcvarsall, host_arch, arch, opt.sdkver and opt.sdkver or "")
end
else
if opt.vcvars_ver then
file:print("call \"%s\" -arch=%s -winsdk=%s -vcvars_ver=%s > nul", vcvarsall, arch, opt.sdkver and opt.sdkver or "", opt.vcvars_ver)
else
file:print("call \"%s\" -arch=%s -winsdk=%s > nul", vcvarsall, arch, opt.sdkver and opt.sdkver or "")
end
end
else
-- @see https://github.com/xmake-io/xmake/issues/5077
if vsver and tonumber(vsver) >= 16 and host_arch ~= arch then
if host_arch == "x64" then
host_arch = "amd64"
end
arch = host_arch .. "_" .. arch
end
if opt.vcvars_ver then
file:print("call \"%s\" %s %s -vcvars_ver=%s > nul", vcvarsall, arch, opt.sdkver and opt.sdkver or "", opt.vcvars_ver)
else
file:print("call \"%s\" %s %s > nul", vcvarsall, arch, opt.sdkver and opt.sdkver or "")
end
end
for idx, var in ipairs(get_vcvars()) do
file:print("echo " .. var .. " = %%" .. var .. "%%")
end
file:close()
-- run genvcvars.bat
local outdata = try {function () return os.iorun(genvcvars_bat) end}
if not outdata then
return
end
-- load all envirnoment variables
local variables = {}
for _, line in ipairs(outdata:split("\n")) do
local p = line:find('=', 1, true)
if p then
local name = line:sub(1, p - 1):trim()
local value = line:sub(p + 1):trim()
variables[name] = value
end
end
if not variables.path then
return
end
-- remove some empty entries
for _, name in ipairs(vcvars) do
if variables[name] and #variables[name]:trim() == 0 then
variables[name] = nil
end
end
-- fix WindowsSDKVersion
local WindowsSDKVersion = variables["WindowsSDKVersion"]
if WindowsSDKVersion then
WindowsSDKVersion = WindowsSDKVersion:gsub("\\", ""):trim()
if WindowsSDKVersion ~= "" then
variables["WindowsSDKVersion"] = WindowsSDKVersion
end
else
-- sometimes the variable `WindowsSDKVersion` is not available
-- then parse it from `WindowsSdkBinPath`, such as: `C:\\Program Files (x86)\\Windows Kits\\8.1\\bin`
local WindowsSdkBinPath = variables["WindowsSdkBinPath"]
if WindowsSdkBinPath then
WindowsSDKVersion = string.match(WindowsSdkBinPath, "\\(%d+%.%d+)\\bin$")
if WindowsSDKVersion then
variables["WindowsSDKVersion"] = WindowsSDKVersion
end
end
end
-- fix UCRTVersion
--
-- @note vcvarsall.bat maybe detect error if install WDK and SDK at same time (multi-sdk version exists in include directory).
--
local UCRTVersion = variables["UCRTVersion"]
if UCRTVersion and WindowsSDKVersion and UCRTVersion ~= WindowsSDKVersion and WindowsSDKVersion ~= "" then
local lib = variables["lib"]
if lib then
lib = lib:gsub(UCRTVersion, WindowsSDKVersion)
variables["lib"] = lib
end
local include = variables["include"]
if include then
include = include:gsub(UCRTVersion, WindowsSDKVersion)
variables["include"] = include
end
UCRTVersion = WindowsSDKVersion
variables["UCRTVersion"] = UCRTVersion
end
-- convert path/lib/include to PATH/LIB/INCLUDE
variables.PATH = variables.path
variables.LIB = variables.lib
variables.LIBPATH = variables.libpath
variables.INCLUDE = variables.include
variables.path = nil
variables.lib = nil
variables.include = nil
variables.libpath = nil
return variables
end
-- find vstudio for msvc
function _find_vstudio(opt)
opt = opt or {}
-- find the single current MSVC/VS from environment variables
local VCInstallDir = os.getenv("VCInstallDir")
if VCInstallDir and (VCInstallDir ~= "") then
local VisualStudioVersion = os.getenv("VisualStudioVersion")
if not VisualStudioVersion or (VisualStudioVersion == "") then
-- heuristic for VisualStudioVersion value (early MSVC/VS versions don't set VisualStudioVersion)
local VSInstallDir = os.getenv("VSInstallDir") or ""
VisualStudioVersion = VSInstallDir:match('(%d+[.]?%d*)\\?%s*$')
if not VisualStudioVersion then VisualStudioVersion = VCInstallDir:match('(%d+[.]?%d*)\\VC\\?%s*$') end
if not VisualStudioVersion then VisualStudioVersion = "0" end
if not VisualStudioVersion:match('[.]') then VisualStudioVersion = VisualStudioVersion .. '.0' end
-- find highest known version which is less than or equal to VisualStudioVersion
if not vsvers[VisualStudioVersion] then
local versions = {}
local count = 0
for k in pairs(vsvers) do
table.insert(versions, tonumber(k))
count = count + 1
end
table.sort(versions)
local i = 0
local v = tonumber(VisualStudioVersion)
while ((i < count) and (versions[i + 1] <= v)) do
i = i + 1
end
VisualStudioVersion = versions[i] or "0"
end
end
-- find vcvarsall.bat or vcvars32.bat
local paths =
{
path.join(VCInstallDir, "Auxiliary", "Build"),
path.join(VCInstallDir, "bin"),
VCInstallDir
}
local vcvarsall = find_file("vcvarsall.bat", paths) or find_file("vcvars32.bat", paths)
if vcvarsall and os.isfile(vcvarsall) and vsvers[VisualStudioVersion] then
-- load vcvarsall
local vcvarsall_x86 = _load_vcvarsall(vcvarsall, VisualStudioVersion, "x86", opt)
local vcvarsall_x64 = _load_vcvarsall(vcvarsall, VisualStudioVersion, "x64", opt)
local vcvarsall_arm = _load_vcvarsall(vcvarsall, VisualStudioVersion, "arm", opt)
local vcvarsall_arm64 = _load_vcvarsall(vcvarsall, VisualStudioVersion, "arm64", opt)
local vcvarsall_arm64ec = vcvarsall_arm64
-- save results
local results = {}
results[vsvers[VisualStudioVersion]] = {
version = VisualStudioVersion,
vcvarsall_bat = vcvarsall,
vcvarsall = {
x86 = vcvarsall_x86,
x64 = vcvarsall_x64,
arm = vcvarsall_arm,
arm64 = vcvarsall_arm64,
arm64ec = vcvarsall_arm64ec
}
}
return results
end
end
-- find vswhere
local vswhere = find_tool("vswhere")
-- sort vs versions
local order_vsvers = table.keys(vsvers)
table.sort(order_vsvers, function (a, b) return tonumber(a) > tonumber(b) end)
-- find vs2017 -> vs4.2
local results = {}
for _, version in ipairs(order_vsvers) do
-- find VC install path (and aux build path) using `vswhere` (for version >= 15.0)
-- * version > 15.0 eschews registry entries; but `vswhere` (included with version >= 15.2) can be used to find VC install path
-- ref: https://github.com/Microsoft/vswhere/blob/master/README.md @@ https://archive.is/mEmdu
local vswhere_VCAuxiliaryBuildDir = nil
local vswhere_Common7ToolsDir = nil
if (tonumber(version) >= 15) and vswhere then
local vswhere_vrange = format("%s,%s)", version, (version + 1))
-- build tools: https://github.com/microsoft/vswhere/issues/22 @@ https://aka.ms/vs/workloads
local result = os.iorunv(vswhere.program, {"-products", "*", "-prerelease", "-property", "installationpath", "-version", vswhere_vrange})
if result then
vswhere_VCAuxiliaryBuildDir = path.join(result:trim(), "VC", "Auxiliary", "Build")
vswhere_Common7ToolsDir = path.join(result:trim(), "Common7", "Tools")
end
end
-- init paths
local paths = {
format("$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7;%s)\\VC", version),
format("$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7;%s)\\VC7\\bin", version),
format("$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VS7;%s)\\VC", version),
format("$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7;%s)\\VC\\Auxiliary\\Build", version),
format("$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VS7;%s)\\VC\\Auxiliary\\Build", version)
}
if vsenvs[version] then
table.insert(paths, format("$(env %s)\\..\\..\\VC", vsenvs[version]))
end
if vswhere_VCAuxiliaryBuildDir and os.isdir(vswhere_VCAuxiliaryBuildDir) then
table.insert(paths, 1, vswhere_VCAuxiliaryBuildDir)
end
if version == "6.0" and os.arch() == "x64" then
table.insert(paths, "$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\DevStudio\\6.0\\Products\\Microsoft Visual C++;ProductDir)\\Bin")
table.insert(paths, "$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\Microsoft\\VisualStudio\\6.0\\Setup\\Microsoft Visual C++;ProductDir)\\Bin")
end
-- find vcvarsall.bat, vcvars32.bat for vs7.1
local vcvarsall = find_file("vcvarsall.bat", paths) or find_file("vcvars32.bat", paths)
if not vcvarsall then
-- find vs from some logical drives paths
paths = {}
local logical_drives = winos.logical_drives()
-- we attempt to find vs from wdk directory
-- wdk: E:\Program Files\Windows Kits\10
-- vcvarsall: E:\Program Files\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build
local wdk = config.get("wdk")
if wdk and os.isdir(wdk) then
local p = wdk:find("Program Files")
if p then
table.insert(logical_drives, wdk:sub(1, p - 1))
end
end
for _, logical_drive in ipairs(logical_drives) do
if os.isdir(path.join(logical_drive, "Program Files (x86)")) then
table.insert(paths, path.join(logical_drive, "Program Files (x86)", "Microsoft Visual Studio", vsvers[version], "*", "VC", "Auxiliary", "Build"))
table.insert(paths, path.join(logical_drive, "Program Files (x86)", "Microsoft Visual Studio " .. version, "VC"))
end
table.insert(paths, path.join(logical_drive, "Program Files", "Microsoft Visual Studio", vsvers[version], "*", "VC", "Auxiliary", "Build"))
table.insert(paths, path.join(logical_drive, "Program Files", "Microsoft Visual Studio " .. version, "VC"))
if version == "6.0" then
table.insert(paths, path.join(logical_drive, "Program Files", "Microsoft Visual Studio", "VC98", "Bin"))
table.insert(paths, path.join(logical_drive, "Program Files (x86)", "Microsoft Visual Studio", "VC98", "Bin"))
end
end
vcvarsall = find_file("vcvarsall.bat", paths) or find_file("vcvars32.bat", paths)
end
if not vcvarsall then
local paths = {
format("$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7;%s)\\Common7\\Tools", version),
format("$(reg HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VS7;%s)\\Common7\\Tools", version)
}
if vswhere_Common7ToolsDir and os.isdir(vswhere_Common7ToolsDir) then
table.insert(paths, 1, vswhere_Common7ToolsDir)
end
vcvarsall = find_file("VsDevCmd.bat", paths)
end
if vcvarsall then
-- load vcvarsall
local vcvarsall_x86 = _load_vcvarsall(vcvarsall, version, "x86", opt)
local vcvarsall_x64 = _load_vcvarsall(vcvarsall, version, "x64", opt)
local vcvarsall_arm = _load_vcvarsall(vcvarsall, version, "arm", opt)
local vcvarsall_arm64 = _load_vcvarsall(vcvarsall, version, "arm64", opt)
local vcvarsall_arm64ec = vcvarsall_arm64
-- save results
results[vsvers[version]] = {
version = version,
vcvarsall_bat = vcvarsall,
vcvarsall = {
x86 = vcvarsall_x86,
x64 = vcvarsall_x64,
arm = vcvarsall_arm,
arm64 = vcvarsall_arm64,
arm64ec = vcvarsall_arm64ec
}
}
end
end
return results
end
-- get last mtime of msvc
-- @see https://github.com/xmake-io/xmake/issues/3652
function _get_last_mtime_of_msvc(msvc)
local mtime = -1
for arch, envs in pairs(msvc.vcvarsall) do
if envs.PATH then
local pathenv = path.splitenv(envs.PATH)
for _, dir in ipairs(pathenv) do
local cl = path.join(dir, "cl.exe")
if os.isfile(cl) then
local t = os.mtime(cl)
if t > mtime then
mtime = t
end
end
end
end
local winsdk = envs.WindowsSdkDir
if winsdk and os.isdir(winsdk) then
local t = os.mtime(winsdk)
if t > mtime then
mtime = t
end
end
end
return mtime
end
-- get last mtime of vstudio
function _get_last_mtime(vstudio)
local mtime = -1
for _, msvc in pairs(vstudio) do
local vcvarsall_bat = msvc.vcvarsall_bat
if vcvarsall_bat and os.isfile(vcvarsall_bat) then
local t = os.mtime(vcvarsall_bat)
if t > mtime then
mtime = t
end
t = _get_last_mtime_of_msvc(msvc)
if t > mtime then
mtime = t
end
else
mtime = -1
break
end
end
return mtime
end
-- find vstudio environment
--
-- @param opt the options, e.g. {vcvars_ver = 14.0, sdkver = "10.0.15063.0"}
--
-- @return { 2008 = {version = "9.0", vcvarsall = {x86 = {path = .., lib = .., include = ..}}}
-- , 2017 = {version = "15.0", vcvarsall = {x64 = {path = .., lib = ..}}}}
--
function main(opt)
opt = opt or {}
-- only for windows
if not is_host("windows") then
return
end
local key = "vstudio"
if opt.vcvars_ver then
key = key .. opt.vcvars_ver
end
if opt.sdkver then
key = key .. opt.sdkver
end
-- attempt to get it from the global cache first
local vstudio = global_detectcache:get2(key, "msvc")
if vstudio then
local mtime = _get_last_mtime(vstudio)
local mtimeprev = global_detectcache:get2(key, "mtime")
if mtime and mtimeprev and mtime > 0 and mtimeprev > 0 and mtime == mtimeprev then
return vstudio
end
end
-- find and cache result
vstudio = _find_vstudio(opt)
if vstudio then
local mtime = _get_last_mtime(vstudio)
global_detectcache:set2(key, "msvc", vstudio)
global_detectcache:set2(key, "mtime", mtime)
global_detectcache:save()
end
return vstudio
end
|