summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2026-03-14 23:02:18 +0800
committerruki <[email protected]>2026-03-14 23:02:18 +0800
commit22976f06349a3af4b520ae5796ab20edc95bfe0c (patch)
treeeb40e0b3395d1083802159031e79691f17676385
parenta86bde866d5f65d5fbd31f1474190dcc40aa2876 (diff)
improve find_dotnet
-rw-r--r--xmake/modules/detect/sdks/find_dotnet.lua27
-rw-r--r--xmake/toolchains/dotnet/xmake.lua33
2 files changed, 39 insertions, 21 deletions
diff --git a/xmake/modules/detect/sdks/find_dotnet.lua b/xmake/modules/detect/sdks/find_dotnet.lua
index 3430d2626..f64acacd3 100644
--- a/xmake/modules/detect/sdks/find_dotnet.lua
+++ b/xmake/modules/detect/sdks/find_dotnet.lua
@@ -28,13 +28,21 @@ import("core.project.config")
import("core.cache.detectcache")
-- find dotnet sdk info from dotnet cli
-function _find_dotnet_cli()
- local dotnet = find_tool("dotnet", {version = true})
+function _find_dotnet_cli(sdkdir)
+
+ -- find dotnet program
+ local paths = {}
+ if sdkdir then
+ table.insert(paths, path.join(sdkdir, "bin"))
+ table.insert(paths, sdkdir)
+ end
+ local dotnet = find_tool("dotnet", {version = true, paths = #paths > 0 and paths or nil})
if not dotnet then
return nil
end
- local result = {program = dotnet.program, version = dotnet.version}
+ local bindir = path.directory(dotnet.program)
+ local result = {bindir = bindir, version = dotnet.version}
-- get sdk list, e.g. "8.0.100 [/usr/share/dotnet/sdk]"
local sdklist = try { function () return os.iorunv(dotnet.program, {"--list-sdks"}) end }
@@ -54,6 +62,11 @@ function _find_dotnet_cli()
end
end
+ -- set sdkdir from bindir if not found from sdk list
+ if not result.sdkdir then
+ result.sdkdir = sdkdir or path.directory(bindir)
+ end
+
-- get runtime list, e.g. "Microsoft.NETCore.App 8.0.0 [/usr/share/dotnet/shared/Microsoft.NETCore.App]"
local runtimelist = try { function () return os.iorunv(dotnet.program, {"--list-runtimes"}) end }
if runtimelist then
@@ -129,14 +142,14 @@ function main(sdkdir, opt)
opt = opt or {}
-- attempt to load cache first
- local key = "detect.sdks.find_dotnet." .. (sdkdir or "")
+ local key = "detect.sdks.find_dotnet"
local cacheinfo = detectcache:get(key) or {}
- if not opt.force and cacheinfo.dotnet then
+ if not opt.force and cacheinfo.dotnet and cacheinfo.dotnet.sdkdir and os.isdir(cacheinfo.dotnet.sdkdir) then
return cacheinfo.dotnet
end
-- find dotnet cli sdk
- local dotnet = _find_dotnet_cli()
+ local dotnet = _find_dotnet_cli(sdkdir or config.get("dotnet") or global.get("dotnet"))
-- find .NET Framework SDK on Windows
if is_host("windows") then
@@ -151,6 +164,7 @@ function main(sdkdir, opt)
end
if dotnet then
+
-- save to config
if dotnet.sdkdir then
config.set("dotnet", dotnet.sdkdir, {force = true, readonly = true})
@@ -169,6 +183,7 @@ function main(sdkdir, opt)
end
end
else
+
-- trace
if opt.verbose or option.get("verbose") then
cprint("checking for .NET SDK directory ... ${color.nothing}${text.nothing}")
diff --git a/xmake/toolchains/dotnet/xmake.lua b/xmake/toolchains/dotnet/xmake.lua
index 9c1fe40c3..6a02b0a0a 100644
--- a/xmake/toolchains/dotnet/xmake.lua
+++ b/xmake/toolchains/dotnet/xmake.lua
@@ -28,30 +28,33 @@ toolchain("dotnet")
set_toolset("cssh", "dotnet")
on_check(function (toolchain)
- import("lib.detect.find_tool")
import("detect.sdks.find_dotnet")
- -- find dotnet program
- local dotnet = find_tool("dotnet", {version = true})
- if not dotnet then
- return false
+ -- find dotnet sdk from packages first
+ local sdkinfo
+ for _, package in ipairs(toolchain:packages()) do
+ local installdir = package:installdir()
+ if installdir and os.isdir(installdir) then
+ sdkinfo = find_dotnet(installdir, {force = true})
+ if sdkinfo then
+ break
+ end
+ end
end
- toolchain:config_set("dotnet", dotnet.program)
- toolchain:config_set("dotnet_version", dotnet.version)
- -- find dotnet sdk info
- local sdkinfo = find_dotnet()
- if sdkinfo then
- toolchain:config_set("sdkinfo", sdkinfo)
+ -- find dotnet sdk from system
+ if not sdkinfo then
+ sdkinfo = find_dotnet()
+ end
+ if not sdkinfo or not sdkinfo.bindir then
+ return false
end
+ toolchain:config_set("bindir", sdkinfo.bindir)
+ toolchain:config_set("sdkdir", sdkinfo.sdkdir)
return true
end)
on_load(function (toolchain)
- local dotnet = toolchain:config("dotnet") or "dotnet"
- toolchain:set("toolset", "cs", dotnet)
- toolchain:set("toolset", "csld", dotnet)
- toolchain:set("toolset", "cssh", dotnet)
-- set default environment variables
toolchain:add("runenvs", "DOTNET_NOLOGO", "1")