Skip to content

Commit 9a313f3

Browse files
authored
Merge pull request #27 from Birch-san/update-build-rebased
Build with newer Emscripten and different flags
2 parents cec8659 + 92bdf21 commit 9a313f3

File tree

3 files changed

+80
-46
lines changed

3 files changed

+80
-46
lines changed

box2d-wasm/README.dev.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Ensure that you have a recent [emscripten](https://emscripten.org/) installed.
44

5-
This build process is known to work with emscripten 2.0.5.
5+
**Requires Emscripten 2.0.16 or higher.**
66

77
### Overview
88

@@ -12,8 +12,6 @@ Navigate to `<repository root>/box2d-wasm`, then:
1212
export TARGET_TYPE=Debug
1313
export EMSCRIPTEN="${EMSCRIPTEN:-"$(realpath "$(dirname $(realpath "$(which emcc)"))/../libexec")"}"
1414
export PYTHON="${PYTHON:-"$(which python3)"}"
15-
# if you're developing in this monorepo, you'll be testing your changes with integration-test, which doesn't use the UMD output
16-
export SKIP_UMD_BUILD=1
1715
./build_all.sh
1816
```
1917

box2d-wasm/build_makefile.sh

+2-23
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,9 @@ fi
1313

1414
>&2 echo -e '\nGenerating Makefile with emcmake'
1515

16-
# if we were to pass -DCMAKE_BUILD_TYPE="$TARGET_TYPE" to emcmake,
17-
# emcmake would enforce the following overrides (see build/CMakeCache.txt after running emcmake)
18-
# CMAKE_CXX_FLAGS_DEBUG:STRING=-g
19-
# CMAKE_CXX_FLAGS_MINSIZEREL:STRING=-DNDEBUG -Os
20-
# CMAKE_CXX_FLAGS_RELEASE:STRING=-DNDEBUG -O2
21-
# CMAKE_CXX_FLAGS_RELWITHDEBINFO:STRING=-DNDEBUG -O2
22-
# these flags would be applied *after* ours, which means they would have precedence.
23-
# as such: we do *not* set CMAKE_BUILD_TYPE, because we wish for higher optimization (O3) on Release builds
24-
# see how the flags get resolved in build/src/CMakeFiles/box2d.dir/flags.make
25-
CMAKE_CXX_FLAGS=()
26-
RELEASE_FLAGS_NOMINAL=(-DNDEBUG -O3)
27-
2816
case "$TARGET_TYPE" in
29-
RelWithDebInfo)
30-
# -flto can succeed here, but causes the emcc after this to fail during wasm-emscripten-finalize (possibly due to source maps)
31-
CMAKE_CXX_FLAGS=(${RELEASE_FLAGS_NOMINAL[@]} -g)
32-
;;
33-
Release)
34-
CMAKE_CXX_FLAGS=(${RELEASE_FLAGS_NOMINAL[@]} -flto)
17+
RelWithDebInfo | Release | Debug)
3518
;;
36-
Debug)
37-
CMAKE_CXX_FLAGS=(-g)
38-
;;
39-
4019
*)
4120
>&2 echo -e "${Red}TARGET_TYPE not set.${NC}"
4221
>&2 echo -e "Please set TARGET_TYPE to 'Debug', 'Release', or 'RelWithDebInfo'. For example, with:"
@@ -47,4 +26,4 @@ esac
4726
>&2 echo -e "TARGET_TYPE is $TARGET_TYPE"
4827

4928
set -x
50-
emcmake cmake -DCMAKE_CXX_FLAGS="$CMAKE_CXX_FLAGS" ../../box2d -DBOX2D_BUILD_UNIT_TESTS=OFF -DBOX2D_BUILD_DOCS=OFF -DBOX2D_BUILD_TESTBED=OFF
29+
emcmake cmake -DCMAKE_BUILD_TYPE="$TARGET_TYPE" ../../box2d -DBOX2D_BUILD_UNIT_TESTS=OFF -DBOX2D_BUILD_DOCS=OFF -DBOX2D_BUILD_TESTBED=OFF

box2d-wasm/build_wasm.sh

+77-20
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,48 @@ if ! [[ "$PWD" -ef "$DIR/build" ]]; then
1414
fi
1515

1616
# we used to use -s ENVIRONMENT=web for a slightly smaller build, until Node.js compatibility was requested in https://github.com/Birch-san/box2d-wasm/issues/8
17-
EMCC_OPTS=(-s MODULARIZE=1 -s EXPORT_NAME=Box2D -s EXPORT_BINDINGS=1 -s RESERVED_FUNCTION_POINTERS=20 --post-js box2d_glue.js --memory-init-file 0 -s NO_EXIT_RUNTIME=1 -s NO_FILESYSTEM=1 -s EXPORTED_RUNTIME_METHODS=[] -s EXPORTED_FUNCTIONS="['_malloc','_free']" -fno-rtti -s ALLOW_MEMORY_GROWTH=1)
18-
19-
RELEASE_OPTS_NOMINAL=(-O3)
17+
EMCC_OPTS=(
18+
-fno-rtti
19+
-s MODULARIZE=1
20+
-s EXPORT_NAME=Box2D
21+
-s ALLOW_TABLE_GROWTH=1
22+
--post-js box2d_glue.js
23+
--memory-init-file 0
24+
-s FILESYSTEM=0
25+
-s SUPPORT_LONGJMP=0
26+
-s EXPORTED_FUNCTIONS=_malloc,_free
27+
-s ALLOW_MEMORY_GROWTH=1
28+
)
29+
RELEASE_OPTS=(-O3)
2030

2131
case "$TARGET_TYPE" in
2232
Debug)
23-
FLAVOUR_EMCC_OPTS=(-g4 -s ASSERTIONS=2 -s DEMANGLE_SUPPORT=1)
33+
EMCC_OPTS=(
34+
${EMCC_OPTS[@]}
35+
-g4
36+
-s ASSERTIONS=2
37+
-s DEMANGLE_SUPPORT=1
38+
)
2439
;;
2540

2641
RelWithDebInfo)
2742
# consider setting --source-map-base if you know where
2843
# Box2D will be served from.
29-
FLAVOUR_EMCC_OPTS=(-g4 ${RELEASE_OPTS_NOMINAL[@]})
44+
EMCC_OPTS=(
45+
${EMCC_OPTS[@]}
46+
${RELEASE_OPTS[@]}
47+
-g4
48+
)
3049
;;
3150

3251
Release)
33-
FLAVOUR_EMCC_OPTS=(-flto --closure 1 -s IGNORE_CLOSURE_COMPILER_ERRORS=1 ${RELEASE_OPTS_NOMINAL[@]})
52+
EMCC_OPTS=(
53+
${EMCC_OPTS[@]}
54+
${RELEASE_OPTS[@]}
55+
-flto
56+
--closure 1
57+
-s IGNORE_CLOSURE_COMPILER_ERRORS=1
58+
)
3459
;;
3560

3661
*)
@@ -42,28 +67,60 @@ case "$TARGET_TYPE" in
4267
esac
4368
>&2 echo -e "TARGET_TYPE is $TARGET_TYPE"
4469

45-
EMCC_COMMAND_NOMINAL=("${EMCC_OPTS[@]}" "${FLAVOUR_EMCC_OPTS[@]}" -I "$DIR/../box2d/include" --post-js "$DIR/glue_stub.js" "$DIR/glue_stub.cpp" bin/libbox2d.a)
4670

4771
BASENAME='Box2D'
72+
BARE_WASM="$BASENAME.bare.wasm"
73+
74+
>&2 echo -e "${Blue}Building bare WASM${NC}"
75+
set -x
76+
emcc "$DIR/glue_stub.cpp" bin/libbox2d.a -I "$DIR/../box2d/include" "${EMCC_OPTS[@]}" --oformat=bare -o "$BARE_WASM"
77+
{ set +x; } 2>&-
78+
>&2 echo -e "${Green}Successfully built $BARE_WASM${NC}\n"
4879

4980
UMD_DIR='umd'
5081
ES_DIR='es'
5182
mkdir -p "$UMD_DIR" "$ES_DIR"
5283

53-
if [ "$SKIP_UMD_BUILD" = "1" ]; then
54-
>&2 echo -e "${Green}Skipped UMD build because we gotta go fast${NC}"
55-
else
56-
UMD_FILE="$UMD_DIR/$BASENAME.js"
57-
>&2 echo -e "${Blue}Building UMD module, $UMD_FILE${NC}"
58-
set -x
59-
emcc "${EMCC_COMMAND_NOMINAL[@]}" -o "$UMD_FILE"
60-
{ set +x; } 2>&-
61-
>&2 echo -e "${Green}Successfully built $UMD_FILE${NC}\n"
62-
fi
84+
>&2 echo -e "${Blue}Building post-link targets${NC}"
85+
86+
LINK_OPTS=(--post-link "$BARE_WASM" --post-js "$DIR/glue_stub.js" ${EMCC_OPTS[@]})
6387

6488
ES_FILE="$ES_DIR/$BASENAME.js"
65-
>&2 echo -e "${Blue}Building ES module, $ES_FILE${NC}"
89+
>&2 echo -e "${Blue}Building ES module, $ES_DIR/$BASENAME.{js,wasm}${NC}"
6690
set -x
67-
emcc "${EMCC_COMMAND_NOMINAL[@]}" -s EXPORT_ES6=1 -o "$ES_FILE"
91+
emcc "${LINK_OPTS[@]}" -s EXPORT_ES6=1 -o "$ES_FILE"
6892
{ set +x; } 2>&-
69-
>&2 echo -e "${Green}Successfully built $ES_FILE${NC}"
93+
>&2 echo -e "${Green}Successfully built $ES_DIR/$BASENAME.{js,wasm}${NC}\n"
94+
95+
UMD_FILE="$UMD_DIR/$BASENAME.js"
96+
if [ "$BUILD_UMD_FROM_SCRATCH" = "1" ]; then
97+
>&2 echo -e "${Blue}Building UMD module, $UMD_DIR/$BASENAME.{js,wasm} from scratch${NC}"
98+
set -x
99+
emcc "${LINK_OPTS[@]}" -o "$UMD_FILE"
100+
{ set +x; } 2>&-
101+
else
102+
>&2 echo -e "${Blue}Building UMD module, $UMD_DIR/$BASENAME.{js,wasm} by replacing header & footer of ES module${NC}"
103+
escape_for_sed_replace () {
104+
echo "$1" | sed -e 's/&/\\\&/g' -e '$!s/$/\\n/' | tr -d '\n'
105+
}
106+
107+
ES6_HEADER=' var _scriptDir = import.meta.url;'
108+
UMD_HEADER=" var _scriptDir = typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined;
109+
if (typeof __filename !== 'undefined') _scriptDir = _scriptDir || __filename;"
110+
UMD_HEADER_ESCAPED=`escape_for_sed_replace "$UMD_HEADER"`
111+
112+
ES6_FOOTER='export default Box2D;'
113+
UMD_FOOTER="if (typeof exports === 'object' && typeof module === 'object')
114+
module.exports = Box2D;
115+
else if (typeof define === 'function' && define['amd'])
116+
define([], function() { return Box2D; });
117+
else if (typeof exports === 'object')
118+
exports['Box2D'] = Box2D;
119+
120+
"
121+
UMD_FOOTER_ESCAPED=`escape_for_sed_replace "$UMD_FOOTER"`
122+
123+
sed -e "s/^$ES6_HEADER$/$UMD_HEADER_ESCAPED/" -e "s/^$ES6_FOOTER$/$UMD_FOOTER_ESCAPED/" "$ES_FILE" > "$UMD_FILE"
124+
cp "$ES_DIR/$BASENAME.wasm" "$UMD_DIR"
125+
fi
126+
>&2 echo -e "${Green}Successfully built $UMD_DIR/$BASENAME.{js,wasm}${NC}\n"

0 commit comments

Comments
 (0)