diff options
| author | ruki <[email protected]> | 2026-02-06 18:46:45 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2026-02-06 18:46:45 +0800 |
| commit | 1128735f502188ec37dad489420ab3e678599157 (patch) | |
| tree | e601caf1dbf488ed5ee8211a3f29a31008eccdc6 | |
| parent | 469ac03541fc0cdf47f804651e227e6f9f6fc6f3 (diff) | |
| parent | 2786adcbd1dfd44a3a2dc5f652abc2fc11241403 (diff) | |
Merge pull request #7293 from jinke18/dev
feat: add support for running wasm target in browser
| -rw-r--r-- | xmake/actions/run/main.lua | 29 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/find_emrun.lua | 64 |
2 files changed, 92 insertions, 1 deletions
diff --git a/xmake/actions/run/main.lua b/xmake/actions/run/main.lua index 6cfe09d3a..effaa8e45 100644 --- a/xmake/actions/run/main.lua +++ b/xmake/actions/run/main.lua @@ -33,6 +33,28 @@ import("private.detect.check_targetname") import("lib.detect.find_tool") import("private.action.utils", {alias = "action_utils"}) +function _run_wasm_target_in_browser(targetfile, opt) + opt = opt or {} + local rundir = opt.rundir + local addenvs = opt.addenvs + local setenvs = opt.setenvs + local emrun = find_tool("emrun") + if emrun then + os.execv(emrun.program, {targetfile}, { + curdir = rundir, detach = option.get("detach"), addenvs = addenvs, setenvs = setenvs}) + else + 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.unix(path.relative(targetfile, rundir)) + 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 +end + -- run target function _do_run_target(target) @@ -59,6 +81,12 @@ function _do_run_target(target) targetfile = wine.program end + -- run wasm target in browser + if target:is_plat("wasm") then + _run_wasm_target_in_browser(targetfile, {rundir = rundir, addenvs = addenvs, setenvs = setenvs}) + return + end + -- debugging? if option.get("debug") then debugger.run(targetfile, args, {curdir = rundir, addenvs = addenvs, setenvs = setenvs}) @@ -249,4 +277,3 @@ function main() -- leave project directory os.cd(oldir) 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 |
