Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize wasi-sdk detect #4799

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions xmake/modules/detect/sdks/find_wasisdk.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
--
-- @author vkensou
-- @file find_wasisdk.lua
--
vkensou marked this conversation as resolved.
Show resolved Hide resolved

-- imports
import("core.base.semver")
import("core.base.option")
import("core.base.global")
import("core.project.config")
import("core.cache.detectcache")
import("lib.detect.find_file")

-- find wasm-ld directory
function _find_wasm_ld(sdkdir)
local subdirs = {}
table.insert(subdirs, "bin")

local paths = {}
if sdkdir then
table.insert(paths, sdkdir)
end
table.insert(paths, "$(env WASI_SDK_PATH)")

local wasm_ldname = (is_host("windows") and "wasm-ld.exe" or "wasm-ld")
local wasm_ld = find_file(wasm_ldname, paths, {suffixes = subdirs})
if wasm_ld then
return path.directory(wasm_ld)
end
end

-- find wasi-sdk
function _find_wasisdk(sdkdir)

-- find bin directory
local bindir = _find_wasm_ld(sdkdir)
if not bindir or path.filename(bindir) ~= "bin" then
return {}
end

-- find sdk root directory
sdkdir = path.directory(bindir)
if not sdkdir then
return {}
end

return {sdkdir = sdkdir, bindir = bindir}
end

-- find wasi-sdk directory
--
-- @param sdkdir the wasi-sdk directory
-- @param opt the argument options, e.g. {force = true}
--
-- @return the sdk toolchains. e.g. {sdkdir = ..}
--
-- @code
--
-- local sdk = find_wasisdk("~/wasi-sdk")
--
-- @endcode
--
function main(sdkdir, opt)

-- init arguments
opt = opt or {}

-- attempt to load cache first
local key = "detect.sdks.find_wasisdk"
local cacheinfo = detectcache:get(key) or {}
if not opt.force and cacheinfo.sdk and cacheinfo.sdk.sdkdir and os.isdir(cacheinfo.sdk.sdkdir) then
return cacheinfo.sdk
end

-- find sdk
local sdk = _find_wasisdk(sdkdir or config.get("wasi_sdk") or global.get("wasi_sdk"))
vkensou marked this conversation as resolved.
Show resolved Hide resolved
if sdk and sdk.sdkdir and sdk.bindir then
config.set("wasi_sdk", sdk.sdkdir, {force = true, readonly = true})
vkensou marked this conversation as resolved.
Show resolved Hide resolved
if opt.verbose or option.get("verbose") then
cprint("checking for wasi-sdk directory ... ${color.success}%s", sdk.sdkdir)
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

空行去了

else
if opt.verbose or option.get("verbose") then
cprint("checking for wasi-sdk directory ... ${color.nothing}${text.nothing}")
end
end

-- save to cache
cacheinfo.sdk = sdk or false
detectcache:set(key, cacheinfo)
detectcache:save()
return sdk
end
4 changes: 4 additions & 0 deletions xmake/platforms/wasm/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ platform("wasm")
{
{category = "Emscripten Configuration" }
, {nil, "emsdk", "kv", nil, "The emsdk directory" }
, {category = "WASI-SDK Configuration" }
, {nil, "wasi_sdk", "kv", nil, "The wasi-sdk directory" }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前 参数已经太多了,不建议再去对每个 toolchain 添加 option,尽可能收敛。否则以后支持上百 toolchain,不得加上百个 xxxsdk 参数

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那就只支持环境变量?我是想着都支持了比较方便用户。。。

}
, global =
{
{category = "Emscripten Configuration" }
, {nil, "emsdk", "kv", nil, "The emsdk directory" }
, {category = "WASI-SDK Configuration" }
, {nil, "wasi_sdk", "kv", nil, "The wasi-sdk directory" }
}
}

Expand Down
9 changes: 9 additions & 0 deletions xmake/toolchains/wasi/xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ toolchain("wasi")

-- check toolchain
on_check(function (toolchain)
import("lib.detect.find_tool")
import("detect.sdks.find_wasisdk")
local wasisdk = find_wasisdk(toolchain:sdkdir())
if wasisdk then
toolchain:config_set("bindir", wasisdk.bindir)
toolchain:config_set("sdkdir", wasisdk.sdkdir)
toolchain:configs_save()
return wasisdk
end
return import("lib.detect.find_tool")("clang", {paths = toolchain:bindir()})
end)

Expand Down