From 7d2e23f065373ebc7c171bd27f0e958ee716fea1 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 4 Feb 2026 00:38:12 +0800 Subject: improve vcvars --- xmake/modules/detect/sdks/find_vstudio.lua | 101 +++++++++++++---------------- 1 file changed, 44 insertions(+), 57 deletions(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index 22f5dea6b..ee4563bc5 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -28,32 +28,6 @@ import("lib.detect.find_tool") import("lib.detect.find_directory") 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 = { @@ -99,6 +73,32 @@ local _env_orgs = {} -- get all known Visual Studio environment variables function get_vcvars() + 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"} + local realvcvars = vcvars for _, v in pairs(vsenvs) do table.insert(realvcvars, v) @@ -310,39 +310,36 @@ function _load_vcvarsall_impl(vcvarsall, vsver, arch, opt) 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 + if name and #name > 0 then + local value = line:sub(p + 1):trim() + if value and #value > 0 then + variables[name] = value + end + end end end -- check if the environment variables are truncated _check_vcvarsall_env(variables) - if not variables.path then + 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"] + local WindowsSDKVersion = variables.WindowsSDKVersion if WindowsSDKVersion then WindowsSDKVersion = WindowsSDKVersion:gsub("\\", ""):trim() if WindowsSDKVersion ~= "" then - variables["WindowsSDKVersion"] = WindowsSDKVersion + 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"] + local WindowsSdkBinPath = variables.WindowsSdkBinPath if WindowsSdkBinPath then WindowsSDKVersion = string.match(WindowsSdkBinPath, "\\(%d+%.%d+)\\bin$") if WindowsSDKVersion then - variables["WindowsSDKVersion"] = WindowsSDKVersion + variables.WindowsSDKVersion = WindowsSDKVersion end end end @@ -351,31 +348,21 @@ function _load_vcvarsall_impl(vcvarsall, vsver, arch, opt) -- -- @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"] + local UCRTVersion = variables.UCRTVersion if UCRTVersion and WindowsSDKVersion and UCRTVersion ~= WindowsSDKVersion and WindowsSDKVersion ~= "" then - local lib = variables["lib"] + local lib = variables.LIB if lib then lib = lib:gsub(UCRTVersion, WindowsSDKVersion) - variables["lib"] = lib + variables.LIB = lib end - local include = variables["include"] + local include = variables.INCLUDE if include then include = include:gsub(UCRTVersion, WindowsSDKVersion) - variables["include"] = include + variables.INCLUDE = include end UCRTVersion = WindowsSDKVersion - variables["UCRTVersion"] = UCRTVersion + 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 @@ -406,7 +393,7 @@ function _check_vcvarsall_env(vars) end value_org = _env_orgs[name] end - local value_new = vars[name] or vars[name:lower()] + local value_new = vars[name] if value_org and value_new and #value_org > 0 then local values_new = hashset.from(path.splitenv(value_new)) for _, p in ipairs(value_org) do -- cgit v1.3.1 From 9abb15ab15b59d5c3376a39b1f6934f6f4f02233 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 4 Feb 2026 00:38:25 +0800 Subject: format code --- xmake/modules/detect/sdks/find_vstudio.lua | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index ee4563bc5..43809aa53 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -29,8 +29,7 @@ import("lib.detect.find_directory") import("core.cache.global_detectcache") -- init vsvers -local vsvers = -{ +local vsvers = { ["18.0"] = "2026" , ["17.0"] = "2022" , ["16.0"] = "2019" @@ -49,8 +48,7 @@ local vsvers = } -- init vsenvs -local vsenvs = -{ +local vsenvs = { ["18.0"] = "VS180COMNTOOLS" , ["17.0"] = "VS170COMNTOOLS" , ["16.0"] = "VS160COMNTOOLS" -- cgit v1.3.1 From 9f76385286b3efe1e7978c584201abeb21ec8068 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 4 Feb 2026 00:43:42 +0800 Subject: improve tips --- xmake/modules/detect/sdks/find_vstudio.lua | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index 43809aa53..1395ec274 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -547,7 +547,7 @@ function _find_vstudio(opt) if vsenvs[version] then table.insert(paths, format("$(env %s)\\..\\..\\VC", vsenvs[version])) end - + if vswhere_VCAuxiliaryBuildDir then for _, vc_path in ipairs(vswhere_VCAuxiliaryBuildDir) do if os.isdir(vc_path) then @@ -680,6 +680,19 @@ function _get_last_mtime(vstudio) return mtime end +function _show_vstudio_diagnosis_result(vstudio) + local found = {} + for vsver, msvc in pairs(vstudio) do + if type(msvc) == "table" and msvc.version then + table.insert(found, tostring(vsver) .. "(" .. tostring(msvc.version) .. ")") + else + table.insert(found, tostring(vsver)) + end + end + table.sort(found, function (a, b) return a > b end) + cprint("${dim}detecting vstudio environment ... found: %s", table.concat(found, ", ")) +end + -- find vstudio environment -- -- @param opt the options, e.g. {toolset = 14.0, sdkver = "10.0.15063.0"} @@ -714,6 +727,10 @@ function main(opt) end -- find and cache result + if option.get("diagnosis") then + wprint("Detecting Visual Studio environment (first run uses cache later). If VS is updated, run `xmake global --clean` to refresh.") + cprint("${dim}detecting vstudio environment (toolset: %s, sdk: %s) ...", opt.toolset or "", opt.sdkver or "") + end vstudio = _find_vstudio(opt) if vstudio then local mtime = _get_last_mtime(vstudio) @@ -721,6 +738,12 @@ function main(opt) global_detectcache:set2(key, "mtime", mtime) global_detectcache:save() end + if option.get("diagnosis") then + if vstudio and not table.empty(vstudio) then + _show_vstudio_diagnosis_result(vstudio) + else + cprint("${dim}detecting vstudio environment ... not found") + end + end return vstudio end - -- cgit v1.3.1 From 61a16be436e712b2227dba3ead097a5f821c65db Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 4 Feb 2026 00:46:27 +0800 Subject: improve tips --- xmake/modules/detect/sdks/find_vstudio.lua | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index 1395ec274..920b9fad2 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -684,7 +684,21 @@ function _show_vstudio_diagnosis_result(vstudio) local found = {} for vsver, msvc in pairs(vstudio) do if type(msvc) == "table" and msvc.version then - table.insert(found, tostring(vsver) .. "(" .. tostring(msvc.version) .. ")") + local toolset + local vcvarsall = msvc.vcvarsall + if type(vcvarsall) == "table" then + for _, envs in pairs(vcvarsall) do + if type(envs) == "table" and envs.VCToolsVersion then + toolset = _strip_toolset_ver(envs.VCToolsVersion) + break + end + end + end + if toolset then + table.insert(found, tostring(vsver) .. "(" .. tostring(msvc.version) .. ", toolset:" .. tostring(toolset) .. ")") + else + table.insert(found, tostring(vsver) .. "(" .. tostring(msvc.version) .. ")") + end else table.insert(found, tostring(vsver)) end @@ -727,10 +741,7 @@ function main(opt) end -- find and cache result - if option.get("diagnosis") then - wprint("Detecting Visual Studio environment (first run uses cache later). If VS is updated, run `xmake global --clean` to refresh.") - cprint("${dim}detecting vstudio environment (toolset: %s, sdk: %s) ...", opt.toolset or "", opt.sdkver or "") - end + cprint("${color.warning}detecting vstudio environment, it will be cached; run `xmake global --clean` if vs updated.") vstudio = _find_vstudio(opt) if vstudio then local mtime = _get_last_mtime(vstudio) -- cgit v1.3.1 From c3d3aecca53110ac17c32c4a70da6a7e3b20b837 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 4 Feb 2026 00:47:28 +0800 Subject: improve tips --- xmake/modules/detect/sdks/find_vstudio.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index 920b9fad2..a92872199 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -704,7 +704,7 @@ function _show_vstudio_diagnosis_result(vstudio) end end table.sort(found, function (a, b) return a > b end) - cprint("${dim}detecting vstudio environment ... found: %s", table.concat(found, ", ")) + cprint("${dim}detecting vstudio environment ... ${color.success}found: %s", table.concat(found, ", ")) end -- find vstudio environment @@ -741,7 +741,7 @@ function main(opt) end -- find and cache result - cprint("${color.warning}detecting vstudio environment, it will be cached; run `xmake global --clean` if vs updated.") + cprint("${color.warning}Detecting Visual Studio environment (cached); run `xmake global --clean` first if vs updated.") vstudio = _find_vstudio(opt) if vstudio then local mtime = _get_last_mtime(vstudio) -- cgit v1.3.1 From 3445a14dda3cceb13ead089ecd03387012446cac Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 4 Feb 2026 00:48:00 +0800 Subject: improve tips --- xmake/modules/detect/sdks/find_vstudio.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index a92872199..be0c08ad8 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -704,7 +704,7 @@ function _show_vstudio_diagnosis_result(vstudio) end end table.sort(found, function (a, b) return a > b end) - cprint("${dim}detecting vstudio environment ... ${color.success}found: %s", table.concat(found, ", ")) + cprint("${dim}detecting vstudio environment ... ${color.success}%s", table.concat(found, ", ")) end -- find vstudio environment @@ -753,7 +753,7 @@ function main(opt) if vstudio and not table.empty(vstudio) then _show_vstudio_diagnosis_result(vstudio) else - cprint("${dim}detecting vstudio environment ... not found") + cprint("${dim}detecting vstudio environment ...${color.failure}${text.failure}") end end return vstudio -- cgit v1.3.1 From eea6cdd0be89085cf72b8277ea9dba31af07ad3b Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 4 Feb 2026 00:48:49 +0800 Subject: fix review issues --- xmake/modules/detect/sdks/find_vstudio.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua index be0c08ad8..8a805aae6 100644 --- a/xmake/modules/detect/sdks/find_vstudio.lua +++ b/xmake/modules/detect/sdks/find_vstudio.lua @@ -308,9 +308,9 @@ function _load_vcvarsall_impl(vcvarsall, vsver, arch, opt) local p = line:find('=', 1, true) if p then local name = line:sub(1, p - 1):trim() - if name and #name > 0 then + if #name > 0 then local value = line:sub(p + 1):trim() - if value and #value > 0 then + if #value > 0 then variables[name] = value end end -- cgit v1.3.1