diff options
| author | ruki <[email protected]> | 2022-12-11 23:50:57 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-12-11 23:50:57 +0800 |
| commit | 2347d4ab4bf5d9bb3e5a9f67423df51313553b7e (patch) | |
| tree | 1959adb8cf0d39c603ec491eea8c0717e094f018 | |
| parent | 52cb0ae50fe816c8b04b48320064b5910c180b0b (diff) | |
| parent | 74698cc483c68972ab9487e08b3c6ab05b410585 (diff) | |
Merge pull request #3143 from SirLynix/find_emcc
Add emsdk and emscripten find scripts and config
| -rw-r--r-- | xmake/modules/detect/sdks/find_emsdk.lua | 121 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/find_emar.lua | 9 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/find_emcc.lua | 9 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/find_emxx.lua | 9 | ||||
| -rw-r--r-- | xmake/modules/package/tools/cmake.lua | 13 | ||||
| -rw-r--r-- | xmake/modules/private/action/trybuild/cmake.lua | 13 | ||||
| -rw-r--r-- | xmake/platforms/wasm/xmake.lua | 15 |
7 files changed, 173 insertions, 16 deletions
diff --git a/xmake/modules/detect/sdks/find_emsdk.lua b/xmake/modules/detect/sdks/find_emsdk.lua new file mode 100644 index 000000000..52f47079d --- /dev/null +++ b/xmake/modules/detect/sdks/find_emsdk.lua @@ -0,0 +1,121 @@ +--!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 SirLynix +-- @file find_emsdk.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 sdk directory +function _find_emsdkdir(sdkdir) + + -- try to find emsdk.py + local paths = {} + if sdkdir then + table.insert(paths, sdkdir) + end + table.insert(paths, "$(env EMSDK)") + local emsdk = find_file("emsdk.py", paths, {suffixes = subdirs}) + + -- get sdk directory + if emsdk then + return path.directory(emsdk) + end +end + +-- find emsdk +function _find_emsdk(sdkdir) + + -- find sdk root directory + sdkdir = _find_emsdkdir(sdkdir) + if not sdkdir then + return {} + end + + -- find emscripten toolchain directory + local emscripten + + local subdirs = {} + table.insert(subdirs, path.join("upstream", "emscripten")) + + local emcc = find_file("emcc", sdkdir, {suffixes = subdirs}) + if emcc then + emscripten = path.directory(emcc) + end + + -- ok? + return {sdkdir = sdkdir, emscripten = emscripten} + +end + +-- find emsdk directory +-- +-- @param sdkdir the emsdk directory +-- @param opt the argument options, e.g. {force = true} +-- +-- @return the sdk toolchains. e.g. {sdkdir = ..} +-- +-- @code +-- +-- local sdk = find_emsdk("~/emsdk") +-- +-- @endcode +-- +function main(sdkdir, opt) + + -- init arguments + opt = opt or {} + + -- attempt to load cache first + local key = "detect.sdks.find_emsdk" + 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_emsdk(sdkdir or config.get("emsdk") or global.get("emsdk")) + if sdk and sdk.sdkdir then + + -- save to config + config.set("emsdk", sdk.sdkdir, {force = true, readonly = true}) + + -- trace + if opt.verbose or option.get("verbose") then + cprint("checking for emsdk directory ... ${color.success}%s", sdk.sdkdir) + end + + else + + -- trace + if opt.verbose or option.get("verbose") then + cprint("checking for emsdk 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/modules/detect/tools/find_emar.lua b/xmake/modules/detect/tools/find_emar.lua index 612c2f3da..5c09f9952 100644 --- a/xmake/modules/detect/tools/find_emar.lua +++ b/xmake/modules/detect/tools/find_emar.lua @@ -21,6 +21,7 @@ -- imports import("core.tool.compiler") import("lib.detect.find_program") +import("detect.sdks.find_emsdk") -- check function _check(program) @@ -62,6 +63,14 @@ function main(opt) opt = opt or {} opt.check = opt.check or _check + -- init the search directories + local emsdk = find_emsdk() + if emsdk and emsdk.emscripten then + local paths = {} + table.insert(paths, emsdk.emscripten) + opt.paths = paths + end + -- find program return find_program(opt.program or (is_host("windows") and "emar.bat" or "emar"), opt) end diff --git a/xmake/modules/detect/tools/find_emcc.lua b/xmake/modules/detect/tools/find_emcc.lua index 372864fab..020ed29b7 100644 --- a/xmake/modules/detect/tools/find_emcc.lua +++ b/xmake/modules/detect/tools/find_emcc.lua @@ -21,6 +21,7 @@ -- imports import("lib.detect.find_program") import("lib.detect.find_programver") +import("detect.sdks.find_emsdk") -- find emcc -- @@ -40,6 +41,14 @@ function main(opt) -- init options opt = opt or {} + -- init the search directories + local emsdk = find_emsdk() + if emsdk and emsdk.emscripten then + local paths = {} + table.insert(paths, emsdk.emscripten) + opt.paths = paths + end + -- find program local program = find_program(opt.program or (is_host("windows") and "emcc.bat" or "emcc"), opt) diff --git a/xmake/modules/detect/tools/find_emxx.lua b/xmake/modules/detect/tools/find_emxx.lua index f5609d72d..4018b9b13 100644 --- a/xmake/modules/detect/tools/find_emxx.lua +++ b/xmake/modules/detect/tools/find_emxx.lua @@ -21,6 +21,7 @@ -- imports import("lib.detect.find_program") import("lib.detect.find_programver") +import("detect.sdks.find_emsdk") -- find emxx -- @@ -40,6 +41,14 @@ function main(opt) -- init options opt = opt or {} + -- init the search directories + local emsdk = find_emsdk() + if emsdk and emsdk.emscripten then + local paths = {} + table.insert(paths, emsdk.emscripten) + opt.paths = paths + end + -- find program local program = find_program(opt.program or (is_host("windows") and "em++.bat" or "em++"), opt) diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua index 3304cf90b..7e9b9d6c9 100644 --- a/xmake/modules/package/tools/cmake.lua +++ b/xmake/modules/package/tools/cmake.lua @@ -27,6 +27,7 @@ import("core.tool.compiler") import("lib.detect.find_file") import("lib.detect.find_tool") import("package.tools.ninja") +import("detect.sdks.find_emsdk") -- get the number of parallel jobs function _get_parallel_njobs(opt) @@ -474,15 +475,11 @@ end -- get configs for wasm function _get_configs_for_wasm(package, configs, opt) - local emsdk = os.getenv("EMSDK") - local emscripten_cmakefile - if emsdk and os.isdir(emsdk) then - emscripten_cmakefile = find_file("Emscripten.cmake", path.join(emsdk, "*", "emscripten/cmake/Modules/Platform")) - end - if emscripten_cmakefile then - table.insert(configs, "-DCMAKE_TOOLCHAIN_FILE=" .. emscripten_cmakefile) - end + local emsdk = find_emsdk() + assert(emsdk and emsdk.emscripten, "emscripten not found!") + local emscripten_cmakefile = find_file("Emscripten.cmake", path.join(emsdk.emscripten, "cmake/Modules/Platform")) assert(emscripten_cmakefile, "Emscripten.cmake not found!") + table.insert(configs, "-DCMAKE_TOOLCHAIN_FILE=" .. emscripten_cmakefile) if is_subhost("windows") then local mingw = assert(package:build_getenv("mingw") or package:build_getenv("sdk"), "mingw not found!") table.insert(configs, "-DCMAKE_MAKE_PROGRAM=" .. _translate_paths(path.join(mingw, "bin", "mingw32-make.exe"))) diff --git a/xmake/modules/private/action/trybuild/cmake.lua b/xmake/modules/private/action/trybuild/cmake.lua index bd4d7b5c0..8247141d7 100644 --- a/xmake/modules/private/action/trybuild/cmake.lua +++ b/xmake/modules/private/action/trybuild/cmake.lua @@ -151,14 +151,11 @@ end -- get configs for wasm function _get_configs_for_wasm(configs) - local emsdk = os.getenv("EMSDK") - local emscripten_cmakefile - if emsdk and os.isdir(emsdk) then - emscripten_cmakefile = find_file("Emscripten.cmake", path.join(emsdk, "*", "emscripten/cmake/Modules/Platform")) - end - if emscripten_cmakefile then - table.insert(configs, "-DCMAKE_TOOLCHAIN_FILE=" .. emscripten_cmakefile) - end + local emsdk = find_emsdk() + assert(emsdk and emsdk.emscripten, "emscripten not found!") + local emscripten_cmakefile = find_file("Emscripten.cmake", path.join(emsdk.emscripten, "cmake/Modules/Platform")) + assert(emscripten_cmakefile, "Emscripten.cmake not found!") + table.insert(configs, "-DCMAKE_TOOLCHAIN_FILE=" .. emscripten_cmakefile) assert(emscripten_cmakefile, "Emscripten.cmake not found!") _get_configs_for_generic(configs) end diff --git a/xmake/platforms/wasm/xmake.lua b/xmake/platforms/wasm/xmake.lua index 915b73498..578620384 100644 --- a/xmake/platforms/wasm/xmake.lua +++ b/xmake/platforms/wasm/xmake.lua @@ -40,3 +40,18 @@ platform("wasm") -- set toolchains set_toolchains("emcc") + -- set menu + set_menu { + config = + { + {category = "Emscripten Configuration" } + , {nil, "emsdk", "kv", nil, "emsdk directory" } + } + , global = + { + {category = "Emscripten Configuration" } + , {nil, "emsdk", "kv", nil, "emsdk directory" } + } + } + + |
