summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-02-28 11:29:21 +0800
committerruki <[email protected]>2025-02-28 11:29:21 +0800
commit0fb17801c211b0ec7d0dc6757b0e604b4f707d36 (patch)
tree34ad7d7169778d50f8523ceedebb702285ddf44d
parentd796790592ba9413d7d17877da6fe7806ee410e6 (diff)
improve to select latest toolset
-rw-r--r--xmake/modules/detect/sdks/find_vstudio.lua51
1 files changed, 43 insertions, 8 deletions
diff --git a/xmake/modules/detect/sdks/find_vstudio.lua b/xmake/modules/detect/sdks/find_vstudio.lua
index 501f8d79b..25af40ff9 100644
--- a/xmake/modules/detect/sdks/find_vstudio.lua
+++ b/xmake/modules/detect/sdks/find_vstudio.lua
@@ -20,6 +20,7 @@
-- imports
import("core.base.option")
+import("core.base.semver")
import("core.project.config")
import("lib.detect.find_file")
import("lib.detect.find_tool")
@@ -113,9 +114,16 @@ function find_build_tools(opt)
if vs_toolset and os.isdir(path.join(sdkdir, "VC/Tools/MSVC", vs_toolset)) then
VCToolsVersion = vs_toolset
else
- local dir = find_directory("14*", path.join(sdkdir, "VC/Tools/MSVC"))
- if dir then
- VCToolsVersion = path.filename(dir)
+ -- https://github.com/xmake-io/xmake/issues/6159
+ local latest_toolset
+ for _, dir in ipairs(os.dirs(path.join(sdkdir, "VC/Tools/MSVC/*"))) do
+ local toolset = path.filename(dir)
+ if not latest_toolset or semver.compare(toolset, latest_toolset) > 0 then
+ latest_toolset = toolset
+ end
+ end
+ if latest_toolset then
+ VCToolsVersion = latest_toolset
else
return
end
@@ -235,13 +243,13 @@ function _load_vcvarsall_impl(vcvarsall, vsver, arch, opt)
if is_vsdevcmd then
if vsver and tonumber(vsver) >= 16 then
if opt.toolset then
- file:print("call \"%s\" -host_arch=%s -arch=%s -winsdk=%s -vcvars_ver=%s > nul", vcvarsall, host_arch, arch, opt.sdkver or "", opt.toolset or opt.vcvars_ver or "")
+ file:print("call \"%s\" -host_arch=%s -arch=%s -winsdk=%s -vcvars_ver=%s > nul", vcvarsall, host_arch, arch, opt.sdkver or "", opt.toolset or "")
else
- file:print("call \"%s\" -host_arch=%s -arch=%s -winsdk=%s > nul", vcvarsall, host_arch, arch, opt.sdkver and opt.sdkver or "")
+ file:print("call \"%s\" -host_arch=%s -arch=%s -winsdk=%s > nul", vcvarsall, host_arch, arch, opt.sdkver or "")
end
else
if opt.toolset then
- file:print("call \"%s\" -arch=%s -winsdk=%s -vcvars_ver=%s > nul", vcvarsall, arch, opt.sdkver or "", opt.toolset or opt.vcvars_ver or "")
+ file:print("call \"%s\" -arch=%s -winsdk=%s -vcvars_ver=%s > nul", vcvarsall, arch, opt.sdkver or "", opt.toolset or "")
else
file:print("call \"%s\" -arch=%s -winsdk=%s > nul", vcvarsall, arch, opt.sdkver or "")
end
@@ -255,7 +263,7 @@ function _load_vcvarsall_impl(vcvarsall, vsver, arch, opt)
arch = host_arch .. "_" .. arch
end
if opt.toolset then
- file:print("call \"%s\" %s %s -vcvars_ver=%s > nul", vcvarsall, arch, opt.sdkver or "", opt.toolset or opt.vcvars_ver or "")
+ file:print("call \"%s\" %s %s -vcvars_ver=%s > nul", vcvarsall, arch, opt.sdkver or "", opt.toolset or "")
else
file:print("call \"%s\" %s %s > nul", vcvarsall, arch, opt.sdkver or "")
end
@@ -346,13 +354,40 @@ function _load_vcvarsall_impl(vcvarsall, vsver, arch, opt)
return variables
end
+-- strip toolset version, e.g. 14.16.27023 -> 14.16
+function _strip_toolset_ver(vs_toolset)
+ local version = semver.new(vs_toolset)
+ if version then
+ return version:major() .. "." .. version:minor()
+ end
+ return vs_toolset
+end
+
function _load_vcvarsall(vcvarsall, vsver, arch, opt)
opt = opt or {}
local vs_toolset = opt.toolset or opt.vcvars_ver
+ if vs_toolset then
+ opt.toolset = _strip_toolset_ver(vs_toolset)
+ end
local result = _load_vcvarsall_impl(vcvarsall, vsver, arch, opt)
if result and not vs_toolset then
-- if no vs toolset version is specified, we default to the latest version.
- print(result)
+ -- https://github.com/xmake-io/xmake/issues/6159
+ local latest_toolset
+ local VCToolsVersion = result.VCToolsVersion
+ local VCInstallDir = result.VCInstallDir
+ if VCToolsVersion and VCInstallDir then
+ for _, dir in ipairs(os.dirs(path.join(VCInstallDir, "Tools/MSVC/*"))) do
+ local toolset = path.filename(dir)
+ if not latest_toolset or semver.compare(toolset, latest_toolset) > 0 then
+ latest_toolset = toolset
+ end
+ end
+ end
+ if latest_toolset and VCToolsVersion and semver.compare(latest_toolset, VCToolsVersion) > 0 then
+ opt.toolset = _strip_toolset_ver(latest_toolset)
+ result = _load_vcvarsall_impl(vcvarsall, vsver, arch, opt)
+ end
end
return result
end