@@ -14,23 +14,48 @@ if ! [[ "$PWD" -ef "$DIR/build" ]]; then
14
14
fi
15
15
16
16
# 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)
20
30
21
31
case " $TARGET_TYPE " in
22
32
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
+ )
24
39
;;
25
40
26
41
RelWithDebInfo)
27
42
# consider setting --source-map-base if you know where
28
43
# 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
+ )
30
49
;;
31
50
32
51
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
+ )
34
59
;;
35
60
36
61
* )
@@ -42,28 +67,60 @@ case "$TARGET_TYPE" in
42
67
esac
43
68
>&2 echo -e " TARGET_TYPE is $TARGET_TYPE "
44
69
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)
46
70
47
71
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"
48
79
49
80
UMD_DIR=' umd'
50
81
ES_DIR=' es'
51
82
mkdir -p " $UMD_DIR " " $ES_DIR "
52
83
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[@]} )
63
87
64
88
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} "
66
90
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 "
68
92
{ 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