diff options
| author | jinke18 <[email protected]> | 2026-02-05 20:20:20 +0800 |
|---|---|---|
| committer | jinke18 <[email protected]> | 2026-02-05 20:20:20 +0800 |
| commit | b366835683953d6be0600e2b9c7ee4dea7968c61 (patch) | |
| tree | 1017c32f7c86c5af07c2cfc35b2ecd63eb2b56c5 | |
| parent | 9579dc6742e85f4a78f788e806fe5b51f26459d4 (diff) | |
feat: prefer emrun for running wasm target
| -rw-r--r-- | xmake/actions/run/main.lua | 20 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/find_emrun.lua | 64 |
2 files changed, 79 insertions, 5 deletions
diff --git a/xmake/actions/run/main.lua b/xmake/actions/run/main.lua index 080804ad6..161e8ccae 100644 --- a/xmake/actions/run/main.lua +++ b/xmake/actions/run/main.lua @@ -61,11 +61,21 @@ function _do_run_target(target) -- run wasm target in browser if target:is_plat("wasm") then - local python = assert(find_tool("python3"), "python not found! python is required for running wasm target in browser!") - local url = "http://localhost:8000/" .. path.relative(targetfile, rundir):gsub("\\", "/") - print("please open the url in browser") - cprint("${color.success}%s${clear}", url) - os.execv(python.program, {"-m", "http.server"}, {curdir = rundir, detach = option.get("detach"), addenvs = addenvs, setenvs = setenvs}) + -- try to run with emrun + local emrun = find_tool("emrun") + if emrun then + os.execv(emrun.program, {targetfile}, {curdir = rundir, detach = option.get("detach"), addenvs = addenvs, setenvs = setenvs}) + else + -- try to run with python + local python = find_tool("python3") + if not python then + raise("emrun or python not found, which is required for running wasm target in browser!") + end + local url = "http://localhost:8000/" .. path.relative(targetfile, rundir):gsub("\\", "/") + print("please open the url in browser") + cprint("${color.success}%s${clear}", url) + os.execv(python.program, {"-m", "http.server", "--bind", "127.0.0.1", "8000"}, {curdir = rundir, detach = option.get("detach"), addenvs = addenvs, setenvs = setenvs}) + end return end diff --git a/xmake/modules/detect/tools/find_emrun.lua b/xmake/modules/detect/tools/find_emrun.lua new file mode 100644 index 000000000..ac210c4ca --- /dev/null +++ b/xmake/modules/detect/tools/find_emrun.lua @@ -0,0 +1,64 @@ +--!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, Xmake Open Source Community. +-- +-- @author jinke18 +-- @file find_emrun.lua +-- + +-- imports +import("lib.detect.find_program") +import("lib.detect.find_programver") +import("detect.sdks.find_emsdk") + +-- find emrun +-- +-- @param opt the argument options, e.g. {version = true} +-- +-- @return program, version +-- +-- @code +-- +-- local emrun = find_emrun() +-- local emrun, version = find_emrun({version = true}) +-- +-- @endcode +-- +function main(opt) + + -- init options + opt = opt or {} + opt.check = opt.check or function (program) + os.runv(program, {"--help"}, {envs = opt.envs}) + end + + -- 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 "emrun.bat" or "emrun"), opt) + + -- find program version + local version = nil + if program and opt and opt.version then + version = find_programver(program, opt) + end + return program, version +end |
