Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web build with emscripten #90

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cd39c11
fix: android guard toolchains folder creation
Totto16 Nov 14, 2024
1ffc277
feat: WIP emscripten build script
Totto16 Nov 14, 2024
fa3612c
build: adjust build for web
Totto16 Nov 14, 2024
f0b61bd
fix: fix android build script
Totto16 Nov 14, 2024
e37d5f2
fix: remove unused and invalid cpp_args from android build script
Totto16 Nov 14, 2024
df00493
fix: fix some more web build errors
Totto16 Nov 15, 2024
ce88ad8
fix: more emscripten errors + add platform specific API
Totto16 Nov 15, 2024
83393b8
fix: use sdl2 with threads, so that emscripten links successfully
Totto16 Nov 15, 2024
68d8c80
fix: add web support for emscripten, trough the fetch API
Totto16 Nov 15, 2024
1eb539b
fix: emscripten: use mulithreaded libraries and embind-rtti, so that …
Totto16 Nov 15, 2024
0f8c91f
feat: add simple server for emscripten html, so that it works with co…
Totto16 Nov 15, 2024
9740070
fix: build js and html files after another, so that nothing gets part…
Totto16 Nov 15, 2024
8900695
fix: use correct game loop implementation for emscripten
Totto16 Nov 16, 2024
42fdf76
fix: fix a few more emscripten things
Totto16 Nov 16, 2024
bab268c
fix: fix linking order issue on emscripten
Totto16 Nov 16, 2024
db7f287
ci: add web assembly
Totto16 Nov 16, 2024
247542b
ci: update windows build tools version
Totto16 Nov 16, 2024
98d2bbb
fix: use correct namespace for utility function
Totto16 Nov 16, 2024
43d6b16
fix: use correct dependencies names in emscripten
Totto16 Nov 17, 2024
2c1cf65
wrap: update icu to 76.1
Totto16 Nov 17, 2024
d354844
wrap: update magic_enum to 0.9.7
Totto16 Nov 17, 2024
2d0f123
wrap: update utfcpp to 4.0.6
Totto16 Nov 17, 2024
2898d62
fix: only use png and svgs, since jpeg isn't available with mt support
Totto16 Nov 17, 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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ jobs:
uses: TheMrMilchmann/setup-msvc-dev@v3
with:
arch: x64
toolset: '14.41'
toolset: '14.42'

- name: Setup MYSYS2 (Windows)
if: matrix.config.os == 'windows' && ( matrix.config.environment == 'mingw' || matrix.config.environment == 'ucrt' )
Expand Down
40 changes: 40 additions & 0 deletions .github/workflows/web_build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Web CI

on:
release:
types: [published]
push:
branches: ['main']
pull_request:
workflow_dispatch:

jobs:
web-build:
name: Build for the web
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: '0'

# NOTE: meson has no dependencies, so --break-system-packages doesn't really break anything!
- name: Setup Meson
run: |
pip install meson --break-system-packages

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install ninja-build pkg-config build-essential wabt -y --no-install-recommends

- name: Build
run: |
bash ./platforms/build-web.sh
meson test -C build-web

# TODO upload page to gh-pages!
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: oopetris-assets
path: build-web/src/executables/
2 changes: 1 addition & 1 deletion .github/workflows/windows_installer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
uses: TheMrMilchmann/setup-msvc-dev@v3
with:
arch: x64
toolset: '14.41'
toolset: '14.42'

- name: Setup meson
run: |
Expand Down
Binary file removed assets/authors/mgerhold.jpg
Binary file not shown.
Binary file added assets/authors/mgerhold.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 20 additions & 12 deletions platforms/build-android.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

set -e

mkdir -p toolchains
if [ ! -d "toolchains" ]; then
mkdir -p toolchains
fi

export NDK_VER_DOWNLOAD="r28-beta1"
export NDK_VER_DESC="r28-beta1"
Expand Down Expand Up @@ -291,6 +293,10 @@ for INDEX in "${ARCH_KEYS_INDEX[@]}"; do
MESON_CPU_FAMILY="aarch64"
fi

export COMPILE_FLAGS="'--sysroot=${SYS_ROOT:?}','-fPIE','-fPIC','--target=$ARM_COMPILER_TRIPLE','-DAUDIO_PREFER_MP3'"

export LINK_FLAGS="'-fPIE','-L$SYS_ROOT/usr/lib'"

cat <<EOF >"./platforms/crossbuild-android-$ARM_TARGET_ARCH.ini"
[host_machine]
system = 'android'
Expand Down Expand Up @@ -318,10 +324,11 @@ llvm-config = '$LLVM_CONFIG'
[built-in options]
c_std = 'gnu11'
cpp_std = 'c++23'
c_args = ['--sysroot=${SYS_ROOT:?}','-fPIE','-fPIC','--target=$ARM_COMPILER_TRIPLE','-DHAVE_USR_INCLUDE_MALLOC_H','-D_MALLOC_H','-D__BITNESS=$BITNESS']
cpp_args = ['--sysroot=${SYS_ROOT:?}','-fPIE','-fPIC','--target=$ARM_COMPILER_TRIPLE','-D__BITNESS=$BITNESS']
c_link_args = ['-fPIE','-L$SYS_ROOT/usr/lib']
cpp_link_args = ['-fPIE','-L$SYS_ROOT/usr/lib']
c_args = [$COMPILE_FLAGS]
cpp_args = [$COMPILE_FLAGS]
c_link_args = [$LINK_FLAGS]
cpp_link_args = [$LINK_FLAGS]

prefix = '$SYS_ROOT'
libdir = '$LIB_PATH'

Expand All @@ -331,12 +338,14 @@ sys_root = '${SYS_ROOT}'

EOF

if [ ! -d "$PWD/subprojects/cpu-features" ]; then
mkdir -p "$PWD/subprojects/cpu-features/src/"
mkdir -p "$PWD/subprojects/cpu-features/include/"
ln -s "$BASE_PATH/sources/android/cpufeatures/cpu-features.c" "$PWD/subprojects/cpu-features/src/cpu-features.c"
ln -s "$BASE_PATH/sources/android/cpufeatures/cpu-features.h" "$PWD/subprojects/cpu-features/include/cpu-features.h"
cat <<EOF >"$PWD/subprojects/cpu-features/meson.build"
CPU_FUTURES_ROOT="$PWD/subprojects/cpu-features"

if [ ! -d "$CPU_FUTURES_ROOT" ]; then
mkdir -p "$CPU_FUTURES_ROOT/src/"
mkdir -p "$CPU_FUTURES_ROOT/include/"
ln -s "$BASE_PATH/sources/android/cpufeatures/cpu-features.c" "$CPU_FUTURES_ROOT/src/cpu-features.c"
ln -s "$BASE_PATH/sources/android/cpufeatures/cpu-features.h" "$CPU_FUTURES_ROOT/include/cpu-features.h"
cat <<EOF >"$CPU_FUTURES_ROOT/meson.build"
project('cpu-features','c')

meson.override_dependency(
Expand Down Expand Up @@ -367,7 +376,6 @@ EOF
--cross-file "./platforms/crossbuild-android-$ARM_TARGET_ARCH.ini" \
"-Dbuildtype=$BUILDTYPE" \
-Dsdl2:use_hidapi=enabled \
-Dcpp_args=-DAUDIO_PREFER_MP3 \
-Dclang_libcpp=disabled

fi
Expand Down
143 changes: 143 additions & 0 deletions platforms/build-web.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#!/usr/bin/env bash

set -e

if [ ! -d "toolchains" ]; then
mkdir -p toolchains
fi

export EMSCRIPTEN_ROOT="$(pwd)/toolchains/emsdk"

if [ ! -d "$EMSCRIPTEN_ROOT" ]; then
git clone https://github.com/emscripten-core/emsdk.git "$EMSCRIPTEN_ROOT"
fi

"$EMSCRIPTEN_ROOT/emsdk" install latest
"$EMSCRIPTEN_ROOT/emsdk" activate latest

# shellcheck disable=SC1091
EMSDK_QUIET=1 source "$EMSCRIPTEN_ROOT/emsdk_env.sh" >/dev/null

## build theneeded dependencies
embuilder build sdl2-mt harfbuzz-mt freetype zlib sdl2_ttf mpg123 sdl2_mixer_mp3 libpng-mt "sdl2_image:formats=png,svg" icu-mt

export EMSCRIPTEN_SYS_ROOT="$EMSCRIPTEN_ROOT/upstream/emscripten/cache/sysroot"

export BUILD_DIR="build-web"

export CC="emcc"
export CXX="em++"
export AR="emar"
export RANLIB="emranlib"
export STRIP="emstrip"
export NM="emnm"

export ARCH="wasm32"
export CPU_ARCH="wasm32"
export ENDIANESS="little"

export ROMFS="platforms/romfs"

export PACKAGE_FLAGS="'--use-port=sdl2', '--use-port=harfbuzz', '--use-port=freetype', '--use-port=zlib', '--use-port=sdl2_ttf', '--use-port=mpg123', '--use-port=sdl2_mixer', '-sSDL2_MIXER_FORMATS=[\"mp3\"]','--use-port=libpng', '--use-port=sdl2_image','-sSDL2_IMAGE_FORMATS=[\"png\",\"svg\"]', '--use-port=icu'"

export COMMON_FLAGS="'-fexceptions', '-pthread', '-sEXCEPTION_CATCHING_ALLOWED=[..]', $PACKAGE_FLAGS"

# TODO see if ALLOW_MEMORY_GROWTH is needed, but if we load ttf's and music it likely is and we don't have to debug OOm crahses, that aren't handled by some thrid party library, which is painful
export LINK_FLAGS="$COMMON_FLAGS, '-sEXPORT_ALL=1', '-sUSE_WEBGPU=1', '-sWASM=1', '-sALLOW_MEMORY_GROWTH=1', '-sASSERTIONS=1','-sERROR_ON_UNDEFINED_SYMBOLS=1', '-sFETCH=1'"
export COMPILE_FLAGS="$COMMON_FLAGS ,'-DAUDIO_PREFER_MP3'"

export CROSS_FILE="./platforms/crossbuild-web.ini"

cat <<EOF >"$CROSS_FILE"
[host_machine]
system = 'emscripten'
cpu_family = '$ARCH'
cpu = '$CPU_ARCH'
endian = '$ENDIANESS'

[target_machine]
system = 'emscripten'
cpu_family = '$ARCH'
cpu = '$CPU_ARCH'
endian = '$ENDIANESS'

[constants]
emscripten_root = '$(pwd)/emsdk'

[binaries]
c = '$CC'
cpp = '$CXX'
ar = '$AR'
ranlib = '$RANLIB'
strip = '$STRIP'
nm = '$NM'

pkg-config = ['emmake', 'pkg-config']
cmake = ['emmake', 'cmake']
sdl2-config = ['emconfigure', 'sdl2-config']

exe_wrapper = '$EMSDK_NODE'

[built-in options]
c_std = 'c11'
cpp_std = 'c++23'
c_args = [$COMPILE_FLAGS]
cpp_args = [$COMPILE_FLAGS]
c_link_args = [$LINK_FLAGS]
cpp_link_args = [$LINK_FLAGS]

[properties]
needs_exe_wrapper = true
sys_root = '$EMSCRIPTEN_SYS_ROOT'

APP_ROMFS='$ROMFS'

EOF

## options: "smart, complete_rebuild"
export COMPILE_TYPE="smart"

export BUILDTYPE="debug"

if [ "$#" -eq 0 ]; then
# nothing
echo "Using compile type '$COMPILE_TYPE'"
elif [ "$#" -eq 1 ]; then
COMPILE_TYPE="$1"
elif [ "$#" -eq 2 ]; then
COMPILE_TYPE="$1"
BUILDTYPE="$2"
else
echo "Too many arguments given, expected 1 or 2"
exit 1
fi

if [ "$COMPILE_TYPE" == "smart" ]; then
: # noop
elif [ "$COMPILE_TYPE" == "complete_rebuild" ]; then
: # noop
else
echo "Invalid COMPILE_TYPE, expected: 'smart' or 'complete_rebuild'"
exit 1
fi

if [ ! -d "$ROMFS" ]; then

mkdir -p "$ROMFS"

cp -r assets "$ROMFS/"

fi

if [ "$COMPILE_TYPE" == "complete_rebuild" ] || [ ! -e "$BUILD_DIR" ]; then

meson setup "$BUILD_DIR" \
"--wipe" \
--cross-file "$CROSS_FILE" \
"-Dbuildtype=$BUILDTYPE" \
-Ddefault_library=static \
-Dtests=false

fi

meson compile -C "$BUILD_DIR"
53 changes: 53 additions & 0 deletions platforms/emscripten/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer, test
import os


class CORSRequestHandler(SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
self.send_header("Cross-Origin-Opener-Policy", "same-origin")

SimpleHTTPRequestHandler.end_headers(self)


# from: https://github.com/python/cpython/blob/3.13/Lib/http/server.py
if __name__ == "__main__":
import argparse

parser = argparse.ArgumentParser()
parser.add_argument(
"-b",
"--bind",
metavar="ADDRESS",
help="bind to this address " "(default: all interfaces)",
)
parser.add_argument(
"-d",
"--directory",
default=os.getcwd(),
help="serve this directory " "(default: current directory)",
)
parser.add_argument(
"port",
default=8000,
type=int,
nargs="?",
help="bind to this port " "(default: %(default)s)",
)
args = parser.parse_args()

# ensure dual-stack is not disabled; ref #38907
class DualStackServer(ThreadingHTTPServer):
def finish_request(self, request, client_address):
self.RequestHandlerClass(
request, client_address, self, directory=args.directory
)

test(
HandlerClass=CORSRequestHandler,
ServerClass=DualStackServer,
port=args.port,
bind=args.bind,
)
Loading
Loading