Skip to content
This repository has been archived by the owner on Aug 4, 2024. It is now read-only.

support running in browsers (with emscripten) #28

Draft
wants to merge 72 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
d4bb1a7
fixup: README: fixup emsdk link style
Apr 12, 2024
aa95adb
CI: rename NodeJS build artifacts
Apr 12, 2024
bcd1a39
CI: emscripten: upgrade mymindstorm/setup-emsdk to v14
Apr 13, 2024
90f73b6
web: start working on a frontend wrapper for Wasm build
Apr 12, 2024
4493bc6
web-shims: add hint texts for argument input box
Apr 13, 2024
73159dc
web-shims: use pico.css theme
Apr 13, 2024
eb0e848
web-shims: fix monospace font
Apr 13, 2024
cc3de9f
web-shims: rename script
Apr 13, 2024
2ba793d
web-shims: fix mobile browsers
Apr 13, 2024
f2b1905
web-shims: fix old browsers
Apr 14, 2024
b165a86
web-shims: tweak default status text for unsupported browsers
Apr 14, 2024
3758710
web-shims: clean up
Apr 14, 2024
30f1077
web-shims: add a missing comment
Apr 14, 2024
15556c1
fixup: web-shims: abort runtime when failed to load wasm
Apr 14, 2024
da949eb
web-shims: support quoted arguments
Apr 14, 2024
205f813
web-shims: set initial CWD
Apr 14, 2024
018720f
web-shims: make ENTER key fire program on desktop
Apr 14, 2024
f696bbf
web-shims: remove "Run" button
Apr 14, 2024
d3b57b4
web-shims: 'Status: Ready' -> 'Status: Idle'
Apr 14, 2024
adcd4d1
web-shims: display correct argv[0]
Apr 14, 2024
32fb1d6
web-shims: add a little delay before auto-scrolling console output
Apr 14, 2024
51f6156
web-shims: add drop down menu for switching between screens
Apr 14, 2024
e744924
web-shims: update hint texts for input boxes
Apr 14, 2024
f099f5e
web-shims: add a little delay before showing exit code
Apr 15, 2024
4e7f372
web-shims: implement env vars setting
Apr 15, 2024
a2aaea4
CI: emscripten: downgrade emsdk to 3.1.34
Apr 15, 2024
c05f532
emscripten: stop shipping dead code in builds
Apr 15, 2024
9c963e5
fixup: web-shims: enable system environ for NodeJS builds
Apr 15, 2024
ad51e97
fixup: README: correct emscripten CMake toolchain path
Apr 15, 2024
371e1b1
web-shims: hide page before reloading tab
Apr 15, 2024
e1cf56d
web-shims: add a clear button to environ screen
Apr 15, 2024
b592496
web-shims: rename "Command" screen to "TTY"
Apr 15, 2024
a2a9aab
web-shims: add filesystem screen
Apr 15, 2024
05b253b
web-shims: reformat code
Apr 15, 2024
d40971b
README: WebAssembly -> Web
Apr 15, 2024
246e8eb
README: add emscripten doc link
Apr 15, 2024
8ca6723
CI: add workflow for Cloudflare Pages
Apr 15, 2024
45ba005
web-shims: add link preview
Apr 16, 2024
29080a0
web-shims: fix spaces after label
Apr 16, 2024
3b69609
web-shims: add CWD display
Apr 17, 2024
81eb1c6
web-shims: use a lighter way to switch between screens
Apr 17, 2024
1fa3e49
web-shims: tweak button sizes
Apr 17, 2024
605aeed
web-shims: wrap long lines in HTML code
Apr 17, 2024
2b9f1ff
web-shims: fix autocomplete
Apr 17, 2024
007bc5a
web-shims: add table to show directory entries
Apr 17, 2024
8567cab
web-shims: impl directory entry listing
Apr 17, 2024
3348fa4
web-shims: display CWD path in monospace
Apr 17, 2024
6eadcc5
web-shims: add mkdir and upload button
Apr 17, 2024
8eb41af
web-shims: disable button while magiskboot is running
Apr 17, 2024
57d50af
web-shims: catch FS error
Apr 17, 2024
265fea3
web-shims: simplify list appending
Apr 17, 2024
7144a5e
web-shims: impl file selection
Apr 17, 2024
4e04f25
web-shims: add a delete button
Apr 17, 2024
a17b7e2
web-shims: fix trim
Apr 17, 2024
464ad83
web-shims: use lstat() instead of stat()
Apr 17, 2024
2fb6723
web-shims: vfs screen: add a friendly hint
Apr 17, 2024
853dd9a
web-shims: disable button when no selection
Apr 17, 2024
1409489
web-shims: impl directory navigating
Apr 17, 2024
2a60784
web-shims: impl file exporting
Apr 17, 2024
2464332
web-shims: prevent default click event on table
Apr 17, 2024
662bac1
web-shims: fix confusing variable names
Apr 17, 2024
a2c2c1b
web-shims: add a rename button
Apr 17, 2024
0d34da0
web-shims: add a copy button
Apr 17, 2024
d6dd4da
web-shims: lint
Apr 17, 2024
f644cda
web-shims: fix errno detection in release build
Apr 17, 2024
66c1d81
HACK: web-shims: allow UI to update on console output
Apr 18, 2024
5b28c1e
web-shims: use JS library instead of CMake configure_file()
Apr 18, 2024
108a695
web-shims: scroll environ edit to bottom on startup
Apr 18, 2024
98f1d0a
web-shims: make Asyncify optional
Apr 18, 2024
6f1b23d
web-shims: allow UI update when doing write() on console IO
Apr 18, 2024
70dfa34
fixup: CI: disable -Oz for Web build again
Apr 18, 2024
3871851
web-shims: reduce unnecessary console io sleep
Apr 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/__release_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,4 @@ For debug builds, please download from [here](../../releases/last-debug-ci) inst

OS release number is minimal required version to run the binary, binaries from the previous OS release(s) should work.

#### About Web builds

- `noderawfs`: using NodeJS's host filesystem API (requires NodeJS to run, see the *WebAssembly*->*Emscripten* section in [README.md](README.md#emscripten) for more details), build with this tag is intended for running on desktop OS that is not yet supported by magiskboot_build

[Magisk]: https://github.com/topjohnwu/Magisk/releases
36 changes: 30 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ on:
- main
paths-ignore:
- '.github/workflows/release.yaml'
- '.github/workflows/deploy.yaml'
- '.github/ISSUE_TEMPLATE/**'
- 'patches-contrib/**'
- 'LICENSE'
Expand All @@ -23,6 +24,7 @@ on:
pull_request:
paths-ignore:
- '.github/workflows/release.yaml'
- '.github/workflows/deploy.yaml'
- '.github/__release_template.txt'
- 'patches-contrib/**'
- 'LICENSE'
Expand Down Expand Up @@ -780,12 +782,24 @@ jobs:
permissions:
actions: write
needs: [vars]
strategy:
matrix:
include:
- upload_prefix: "node"
ldflags: "-sENVIRONMENT=node -sEXIT_RUNTIME=1 -sNODERAWFS"
check: YES

- upload_prefix: "web"
ldflags: "-sENVIRONMENT=web -sASYNCIFY"
keep_shell_file: YES
opt_size: YES

env:
vcpkg_triplet: "wasm32-emscripten"
emsdk_ver: 3.1.37
emsdk_ver: 3.1.34
emsdk_cache_dir: "emsdk-cache"
rust_target: "wasm32-unknown-emscripten"
upload_suffix: "emscripten-wasm32-noderawfs-static"
upload_suffix: "emscripten-wasm32-static"

steps:
- name: Prepare (Host)
Expand All @@ -802,7 +816,7 @@ jobs:
key: emsdk-${{ env.emsdk_ver }}-${{ runner.os }}

- name: Prepare (Emscripten)
uses: mymindstorm/setup-emsdk@v13
uses: mymindstorm/setup-emsdk@v14
with:
version: ${{ env.emsdk_ver }}
actions-cache-folder: ${{ env.emsdk_cache_dir }}
Expand Down Expand Up @@ -848,7 +862,7 @@ jobs:

echo -e "\n### configure ###\n"
. "$HOME/.cargo/env"
emcmake cmake -G Ninja -B build -DCMAKE_EXE_LINKER_FLAGS=" ${{ needs.vars.outputs.lto_ldflags }} -sNODERAWFS" \
emcmake cmake -G Ninja -B build -DCMAKE_EXE_LINKER_FLAGS=" ${{ needs.vars.outputs.lto_ldflags }} ${{ matrix.ldflags }}" \
-DRust_CARGO_TARGET=${{ env.rust_target }} -DFULL_RUST_LTO=${{ needs.vars.outputs.full_lto }} \
-DCMAKE_BUILD_TYPE=${{ needs.vars.outputs.cmake_build_type }} -DPREFER_STATIC_LINKING=ON \
-DCMAKE_TOOLCHAIN_FILE=$VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake \
Expand All @@ -859,19 +873,29 @@ jobs:
cmake --build build -j $(nproc) -v
emstrip build/magiskboot*.wasm

if [[ "${{ matrix.keep_shell_file }}" != 'YES' ]]; then
rm -f build/magiskboot*.html
fi

file build/magiskboot*
$EMSDK_NODE ./build/magiskboot*.js || true
du -h build/magiskboot*

if [[ "${{ matrix.check }}" == "YES" ]]; then
$EMSDK_NODE ./build/magiskboot*.js || true
fi

rm -rf $GITHUB_WORKSPACE/${{ env.progname }}-out && mkdir -p $GITHUB_WORKSPACE/${{ env.progname }}-out
cp -afv build/magiskboot* $GITHUB_WORKSPACE/${{ env.progname }}-out

- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
if-no-files-found: error
name: ${{ env.progname }}-${{ needs.vars.outputs.short_sha }}-${{ needs.vars.outputs.build_type }}-${{ env.upload_suffix }}
name: ${{ env.progname }}-${{ needs.vars.outputs.short_sha }}-${{ needs.vars.outputs.build_type }}-${{ matrix.upload_prefix }}-${{ env.upload_suffix }}
path: ${{ github.workspace }}/${{ env.progname }}-out/magiskboot*

- name: Check
if: matrix.check == 'YES'
run: |
cd magiskboot_*-src/
EMULATOR=$EMSDK_NODE ./scripts/magiskboot_test.sh $GITHUB_WORKSPACE/${{ env.progname }}-out/magiskboot*.js
Expand Down
41 changes: 41 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Deploy
on:
workflow_dispatch:
inputs:
ci_run_id:
description: "Run ID of the desired CI run to download artifacts from"
required: true

env:
projname: "magiskboot-build-wasm"

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
steps:
- name: Download artifacts
id: download-artifact
uses: dawidd6/action-download-artifact@v3
with:
workflow: ci.yaml
workflow_conclusion: success
run_id: ${{ github.event.inputs.ci_run_id }}
path: artifacts
skip_unpack: true

- name: Extract artifacts
run: |
unzip -j -d dists artifacts/magiskboot-*-*-web-emscripten-wasm32-static.zip 'magiskboot*.*'

- name: Rename files
run: |
cd dists
mv magiskboot*.html index.html

- name: Deploy to Cloudflare Pages
uses: cloudflare/wrangler-action@v3
with:
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
command: pages deploy dists --project-name=${{ env.projname }}
2 changes: 2 additions & 0 deletions CMakeLists.compat.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ elseif (WIN32)
elseif (CYGWIN)
add_compile_definitions(__USE_LINUX_IOCTL_DEFS)
elseif (EMSCRIPTEN)
set(CMAKE_EXECUTABLE_SUFFIX ".html")

# exists in musl but not in rust libc crate
list(APPEND RUSTFLAGS --cfg mbb_stubs_SYS_dup3)
else()
Expand Down
17 changes: 17 additions & 0 deletions CMakeLists.stub.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ if (WIN32)
endif()

if (EMSCRIPTEN)
target_sources(magiskboot_exe PRIVATE
src/web-shims/conio_hack.cc)
set_source_files_properties(src/Magisk/native/src/boot/main.cpp PROPERTIES
COMPILE_FLAGS -Dmain=__mbb_main)
target_link_libraries(magiskboot_exe embind)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--wrap=printf -Wl,--wrap=iprintf -Wl,--wrap=write")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--wrap=fprintf -Wl,--wrap=fiprintf -Wl,--wrap=exit")

message(STATUS "Using custom HTML wrapper")

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -sFORCE_FILESYSTEM -sEXPORTED_RUNTIME_METHODS=ENV,FS,TTY,callMain,abort,mbb_get_wasm_name,mbb_set_this_prog")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --pre-js=${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/impl.pre.js")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --js-library=${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/utils.lib.js")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --shell-file=${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/shfile.in.html")
set_target_properties(magiskboot_exe PROPERTIES LINK_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/impl.pre.js;${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/utils.lib.js;${CMAKE_CURRENT_SOURCE_DIR}/src/web-shims/shfile.in.html")

message(STATUS "Using Emscripten mmap stubs")

target_sources(platform_stubs PRIVATE
Expand Down
24 changes: 9 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,33 +178,27 @@ On arch-cygwin, set `CYGWIN_CC` and `CYGWIN_CXX` to `x86_64-pc-cygwin-clang` and

</details>

<details><summary>WebAssembly</summary>
<details><summary>Web</summary>

#### Emscripten

> **Note**
>
> Currently this port only support running with NodeJS.
>
> A web frontend wrapper must be written to handle the argument passing and file system management in browsers.
>
> This should be implemented soon in the future ([#19](../../issues/19)), but unfortunately I am not a web developer, any help on this will be welcome QwQ

Please read the [Cross compiling](#cross-compiling) instructions first.

Install the [Emscripten][Emscripten] SDK and also a Rust compiler with Emscripten target (probably via [rustup][rustup]).
Install the [Emscripten SDK][emsdk] and also a Rust compiler with Emscripten target (probably via [rustup][rustup]).

> **Warning**
>
> emsdk version 3.1.37 is recommended, you might run into weird problems with other versions.
> An emsdk with version between 3.1.31 and 3.1.34 is required, this project is known to not build or work correctly with other SDK versions.

Use [vcpkg][vcpkg] to install the [depended libraries](#requirements), the triplet is called `wasm32-emscripten`.

When configuring, use `emcmake cmake` instead of `cmake` (but don't use it for `cmake --build` and other CMake commands) , and use `/path/to/your/emsdk/emscripten/cmake/Modules/Platform/Emscripten.cmake` as the toolchain file for vcpkg.
When configuring, use `emcmake cmake` instead of `cmake` (but don't use it for `cmake --build` and other CMake commands) , and use `/path/to/your/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake` as the toolchain file for vcpkg.

To target web browsers, set `-sENVIRONMENT=web` in `CMAKE_EXE_LINKER_FLAGS` (recommended step, for reducing JS size). If you want real-time console output, set `-sASYNCIFY` as well (Note this will increase the binary size significantly).

For NodeJS, make sure to set `CMAKE_EXE_LINKER_FLAGS` to `-sNODERAWFS` to allow using the host filesystem.
For NodeJS, set `-sENVIRONMENT=node` instead. **Make sure** to also set `-sEXIT_RUNTIME=1` and `-sNODERAWFS`. And then you will be able to run the end result with [NodeJS][NodeJS] like this: `node magiskboot.js`

finally, you can run the result with [NodeJS][NodeJS] using: `node magiskboot.js`
You can find more details about these linker flags in [Emscripten documentation](https://emscripten.org/docs/tools_reference/settings_reference.html).

</details>

Expand Down Expand Up @@ -409,6 +403,6 @@ For more details about these licenses, please see [LICENSE](LICENSE) and [LICENS
[fedora-cygwin]: https://copr.fedorainfracloud.org/coprs/yselkowitz/cygwin/
[cmake-toolchains]: https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html
[vcpkg]: https://vcpkg.io/
[Emscripten]: https://github.com/emscripten-core/emsdk
[emsdk]: https://github.com/emscripten-core/emsdk
[NodeJS]: https://nodejs.org/
[ONDK]: https://github.com/topjohnwu/ondk
Loading