summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-03-06 12:12:04 +0800
committerGitHub <[email protected]>2024-03-06 12:12:04 +0800
commitb3b3bf69d648ed2aed3725b91872a1165e3601e7 (patch)
tree80fd932c531e378300e6ec63ada252cb72132c59
parent77c86917235630813f5b463aba3fe4c0ceb6c7f4 (diff)
parente93061694b27445a80cd596d0d3d15939cda21e1 (diff)
Merge pull request #4799 from vkensou/dev
optimize wasi-sdk detect
-rw-r--r--xmake/modules/detect/sdks/find_wasisdk.lua105
-rw-r--r--xmake/toolchains/wasi/xmake.lua9
2 files changed, 114 insertions, 0 deletions
diff --git a/xmake/modules/detect/sdks/find_wasisdk.lua b/xmake/modules/detect/sdks/find_wasisdk.lua
new file mode 100644
index 000000000..916f528ea
--- /dev/null
+++ b/xmake/modules/detect/sdks/find_wasisdk.lua
@@ -0,0 +1,105 @@
+--!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 vkensou
+-- @file find_wasisdk.lua
+--
+
+-- 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)
+ 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)
+ if sdk and sdk.sdkdir and sdk.bindir then
+ if opt.verbose or option.get("verbose") then
+ cprint("checking for wasi-sdk directory ... ${color.success}%s", sdk.sdkdir)
+ end
+ 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
diff --git a/xmake/toolchains/wasi/xmake.lua b/xmake/toolchains/wasi/xmake.lua
index 00ad3465a..758579b6e 100644
--- a/xmake/toolchains/wasi/xmake.lua
+++ b/xmake/toolchains/wasi/xmake.lua
@@ -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)