Skip to content

Commit 1ffc277

Browse files
committed
feat: WIP emscripten build script
1 parent cd39c11 commit 1ffc277

File tree

2 files changed

+127
-1
lines changed

2 files changed

+127
-1
lines changed

platforms/build-web.sh

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
if [ ! -d "toolchains" ]; then
6+
mkdir -p toolchains
7+
fi
8+
9+
export EMSCRIPTEN_ROOT="$(pwd)/toolchains/emsdk"
10+
11+
if [ ! -d "$EMSCRIPTEN_ROOT" ]; then
12+
git clone https://github.com/emscripten-core/emsdk.git "$EMSCRIPTEN_ROOT"
13+
fi
14+
15+
"$EMSCRIPTEN_ROOT/emsdk" install latest >/dev/null
16+
"$EMSCRIPTEN_ROOT/emsdk" activate latest >/dev/null
17+
18+
# shellcheck disable=SC1091
19+
EMSDK_QUIET=1 source "$EMSCRIPTEN_ROOT/emsdk_env.sh" >/dev/null
20+
21+
export EMSCRIPTEN_SYS_ROOT="$EMSCRIPTEN_ROOT/upstream/emscripten/cache/sysroot"
22+
23+
export BUILD_DIR="build-emcc"
24+
25+
export CC="emcc"
26+
export CXX="em++"
27+
export AR="emar"
28+
export RANLIB="emranlib"
29+
export STRIP="emstrip"
30+
31+
export ARCH="wasm32"
32+
export CPU_ARCH="wasm32"
33+
export ENDIANESS="little"
34+
35+
export COMMON_EMSCRIPTEN_OPTIONS="'-fexceptions', '-sEXCEPTION_CATCHING_ALLOWED=[..]'"
36+
37+
# 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
38+
export LINK_EMSCRIPTEN_OPTIONS="$COMMON_EMSCRIPTEN_OPTIONS, '-sEXPORT_ALL=1', '-sUSE_GLFW=3', '-sUSE_WEBGPU=1', '-sWASM=1', '-sALLOW_MEMORY_GROWTH=1', '-sNO_EXIT_RUNTIME=0', '-sASSERTIONS=1'"
39+
export COMPILE_EMSCRIPTEN_OPTIONS="$COMMON_EMSCRIPTEN_OPTIONS"
40+
41+
export CROSS_FILE="./platforms/crossbuild-web.ini"
42+
43+
cat <<EOF >"$CROSS_FILE"
44+
[host_machine]
45+
system = 'emscripten'
46+
cpu_family = '$ARCH'
47+
cpu = '$CPU_ARCH'
48+
endian = '$ENDIANESS'
49+
50+
[target_machine]
51+
system = 'emscripten'
52+
cpu_family = '$ARCH'
53+
cpu = '$CPU_ARCH'
54+
endian = '$ENDIANESS'
55+
56+
[constants]
57+
emscripten_root = '$(pwd)/emsdk'
58+
59+
[binaries]
60+
c = '$CC'
61+
cpp = '$CXX'
62+
ar = '$AR'
63+
ranlib = '$RANLIB'
64+
strip = '$STRIP'
65+
pkg-config = ['emmake', 'env', 'PKG_CONFIG_PATH=PREFIX_GOES_HERE/lib/pkgconfig', 'pkg-config']
66+
cmake = ['emmake', 'cmake']
67+
sdl2-config = ['emconfigure', 'sdl2-config']
68+
69+
exe_wrapper = '$EMSDK_NODE'
70+
71+
[built-in options]
72+
c_std = 'c11'
73+
cpp_std = 'c++20'
74+
c_args = [$COMPILE_EMSCRIPTEN_OPTIONS]
75+
c_link_args = [$LINK_EMSCRIPTEN_OPTIONS]
76+
cpp_args = [$COMPILE_EMSCRIPTEN_OPTIONS]
77+
cpp_link_args = [$LINK_EMSCRIPTEN_OPTIONS]
78+
79+
[properties]
80+
needs_exe_wrapper = true
81+
sys_root = '$EMSCRIPTEN_SYS_ROOT'
82+
83+
84+
EOF
85+
86+
## options: "smart, complete_rebuild"
87+
export COMPILE_TYPE="smart"
88+
89+
export BUILDTYPE="debug"
90+
91+
if [ "$#" -eq 0 ]; then
92+
# nothing
93+
echo "Using compile type '$COMPILE_TYPE'"
94+
elif [ "$#" -eq 1 ]; then
95+
COMPILE_TYPE="$1"
96+
elif [ "$#" -eq 2 ]; then
97+
COMPILE_TYPE="$1"
98+
BUILDTYPE="$2"
99+
else
100+
echo "Too many arguments given, expected 1 or 2"
101+
exit 1
102+
fi
103+
104+
if [ "$COMPILE_TYPE" == "smart" ]; then
105+
: # noop
106+
elif [ "$COMPILE_TYPE" == "complete_rebuild" ]; then
107+
: # noop
108+
else
109+
echo "Invalid COMPILE_TYPE, expected: 'smart' or 'complete_rebuild'"
110+
exit 1
111+
fi
112+
113+
if [ "$COMPILE_TYPE" == "complete_rebuild" ] || [ ! -e "$BUILD_DIR" ]; then
114+
115+
meson setup "$BUILD_DIR" \
116+
"--wipe" \
117+
--cross-file "$CROSS_FILE" \
118+
"-Dbuildtype=$BUILDTYPE" \
119+
-Dcpp_args=-DAUDIO_PREFER_MP3 \
120+
-Ddefault_library=static \
121+
-Dfreetype2:zlib=disabled # TODI, since it's statically linked no duplicates are allowed, solve that
122+
123+
fi
124+
125+
meson compile -C "$BUILD_DIR"

tools/options/meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ elif cpp.get_id() == 'clang'
9494
# TODO: once clang with libstdc++ (gcc c++ stdlib) supports std::expected, remove this special behaviour
9595
allow_tl_expected_fallback = true
9696
endif
97-
97+
elif cpp.get_id() == 'emscripten'
98+
allow_tl_expected_fallback = true
9899
endif
99100

100101
build_application = true

0 commit comments

Comments
 (0)