From 1180d360196457e57920fd0e6d866ddf654c488e Mon Sep 17 00:00:00 2001 From: vkensou Date: Tue, 5 Mar 2024 23:16:29 +0800 Subject: optimize wasi-sdk detect --- xmake/modules/detect/sdks/find_wasisdk.lua | 94 ++++++++++++++++++++++++++++++ xmake/platforms/wasm/xmake.lua | 4 ++ xmake/toolchains/wasi/xmake.lua | 9 +++ 3 files changed, 107 insertions(+) create mode 100644 xmake/modules/detect/sdks/find_wasisdk.lua diff --git a/xmake/modules/detect/sdks/find_wasisdk.lua b/xmake/modules/detect/sdks/find_wasisdk.lua new file mode 100644 index 000000000..fee876716 --- /dev/null +++ b/xmake/modules/detect/sdks/find_wasisdk.lua @@ -0,0 +1,94 @@ +-- +-- @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) + + -- 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")) + if sdk and sdk.sdkdir and sdk.bindir then + config.set("wasi_sdk", sdk.sdkdir, {force = true, readonly = true}) + 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/platforms/wasm/xmake.lua b/xmake/platforms/wasm/xmake.lua index 885233cad..840d6f5d4 100644 --- a/xmake/platforms/wasm/xmake.lua +++ b/xmake/platforms/wasm/xmake.lua @@ -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" } } , global = { {category = "Emscripten Configuration" } , {nil, "emsdk", "kv", nil, "The emsdk directory" } + , {category = "WASI-SDK Configuration" } + , {nil, "wasi_sdk", "kv", nil, "The wasi-sdk directory" } } } 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) -- cgit v1.3.1 From e30f82bc4321d475e2346debc4d2469ad3cc3b05 Mon Sep 17 00:00:00 2001 From: vkensou Date: Tue, 5 Mar 2024 23:56:27 +0800 Subject: delete config --- xmake/modules/detect/sdks/find_wasisdk.lua | 3 +-- xmake/platforms/wasm/xmake.lua | 4 ---- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/xmake/modules/detect/sdks/find_wasisdk.lua b/xmake/modules/detect/sdks/find_wasisdk.lua index fee876716..c4feac76f 100644 --- a/xmake/modules/detect/sdks/find_wasisdk.lua +++ b/xmake/modules/detect/sdks/find_wasisdk.lua @@ -73,9 +73,8 @@ function main(sdkdir, opt) end -- find sdk - local sdk = _find_wasisdk(sdkdir or config.get("wasi_sdk") or global.get("wasi_sdk")) + local sdk = _find_wasisdk(sdkdir) if sdk and sdk.sdkdir and sdk.bindir then - config.set("wasi_sdk", sdk.sdkdir, {force = true, readonly = true}) if opt.verbose or option.get("verbose") then cprint("checking for wasi-sdk directory ... ${color.success}%s", sdk.sdkdir) end diff --git a/xmake/platforms/wasm/xmake.lua b/xmake/platforms/wasm/xmake.lua index 840d6f5d4..885233cad 100644 --- a/xmake/platforms/wasm/xmake.lua +++ b/xmake/platforms/wasm/xmake.lua @@ -36,15 +36,11 @@ 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" } } , global = { {category = "Emscripten Configuration" } , {nil, "emsdk", "kv", nil, "The emsdk directory" } - , {category = "WASI-SDK Configuration" } - , {nil, "wasi_sdk", "kv", nil, "The wasi-sdk directory" } } } -- cgit v1.3.1 From a22c87fc9455fb384d2257cb937567d06d4f439b Mon Sep 17 00:00:00 2001 From: vkensou Date: Tue, 5 Mar 2024 23:56:51 +0800 Subject: add copyright --- xmake/modules/detect/sdks/find_wasisdk.lua | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/xmake/modules/detect/sdks/find_wasisdk.lua b/xmake/modules/detect/sdks/find_wasisdk.lua index c4feac76f..cfc5343e7 100644 --- a/xmake/modules/detect/sdks/find_wasisdk.lua +++ b/xmake/modules/detect/sdks/find_wasisdk.lua @@ -1,3 +1,18 @@ +--!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 -- cgit v1.3.1 From e93061694b27445a80cd596d0d3d15939cda21e1 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 6 Mar 2024 09:52:31 +0800 Subject: Update find_wasisdk.lua --- xmake/modules/detect/sdks/find_wasisdk.lua | 3 --- 1 file changed, 3 deletions(-) diff --git a/xmake/modules/detect/sdks/find_wasisdk.lua b/xmake/modules/detect/sdks/find_wasisdk.lua index cfc5343e7..916f528ea 100644 --- a/xmake/modules/detect/sdks/find_wasisdk.lua +++ b/xmake/modules/detect/sdks/find_wasisdk.lua @@ -76,8 +76,6 @@ end -- @endcode -- function main(sdkdir, opt) - - -- init arguments opt = opt or {} -- attempt to load cache first @@ -93,7 +91,6 @@ function main(sdkdir, opt) 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}") -- cgit v1.3.1