Skip to content

Commit

Permalink
start making multi-flavour build with feature-detection/fallback for …
Browse files Browse the repository at this point in the history
…SIMD support
  • Loading branch information
Birch-san committed Jul 4, 2021
1 parent cd3baf1 commit 6ec863d
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 46 deletions.
5 changes: 4 additions & 1 deletion box2d-wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/build/
/node_modules/
/node_modules/
# distributions made with npm pack
*.tgz
*.tar.gz
2 changes: 1 addition & 1 deletion box2d-wasm/Box2DModule.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference types="emscripten" />
/// <reference path="Box2DModuleAugmentations.d.ts" />
/// <reference path="build/Box2D.d.ts" />
/// <reference path="build/common/Box2D.d.ts" />
declare module "box2d-wasm" {
const Box2DFactory: EmscriptenModuleFactory<typeof Box2D & EmscriptenModule>;
export = Box2DFactory;
Expand Down
2 changes: 1 addition & 1 deletion box2d-wasm/Box2DModuleAugmentations.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// <reference path="build/Box2D.d.ts" />
/// <reference path="build/common/Box2D.d.ts" />
declare namespace Box2D {
/**
* merge in an extra {@link b2RopeDef} property; we were unable to describe this (pointer to array of floats)
Expand Down
19 changes: 19 additions & 0 deletions box2d-wasm/LICENSE.zlib.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Zlib License

Copyright (c) 2020 Alex Birch

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
65 changes: 46 additions & 19 deletions box2d-wasm/build_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,58 @@ if ! [[ "$PWD" -ef "$DIR" ]]; then
exit 1
fi

mkdir -p build
pushd build > /dev/null

set -x
../build_makefile.sh
{ set +x; } 2>&-

>&2 echo -e '\nCompiling C++ to LLVM IR (creates ./build/bin/libbox2d.a archive)'
set -x
emmake make
{ set +x; } 2>&-
>&2 echo
mkdir -p "build/common"
pushd "build/common"

# use Box2D.idl to create ./box2d_glue.{js,cpp} for invoking functionality from libbox2d
set -x
../build_idl_bindings.sh
"$DIR/build_idl_bindings.sh"
{ set +x; } 2>&-
>&2 echo

# generate Box2D_*.{wasm,js} from glue code + libbox2d.a
set -x
../build_wasm.sh
"$DIR/build_typings.sh"
{ set +x; } 2>&-
>&2 echo

set -x
../build_typings.sh
{ set +x; } 2>&-
popd

FLAVOUR_NAMES=(vanilla SIMD)
FLAVOUR_DIRNAMES=(standard simd)
SIMD_ENABLED=('' 1)

for i in {0..1}
do
FLAVOUR_NAME="${FLAVOUR_NAMES["$i"]}"
FLAVOUR_DIRNAME="${FLAVOUR_DIRNAMES["$i"]}"

# use JUST_SIMD=1 to save time when prototyping
if [[ "$JUST_SIMD" = "1" && "$FLAVOUR_NAME" != "simd" ]]; then
>&2 echo -e "\nSkipping '$FLAVOUR_NAME' flavour build because JUST_SIMD is set."
continue
fi


FLAVOUR_DIR="build/flavour/$FLAVOUR_DIRNAME"
>&2 echo -e "\nMaking '$FLAVOUR_NAME' flavour in ./$FLAVOUR_DIR directory"
mkdir -p "$FLAVOUR_DIR"
pushd "$FLAVOUR_DIR"

set -x
SIMD_ENABLED="$SIMD_ENABLED" "$DIR/build_makefile.sh"
{ set +x; } 2>&-

>&2 echo -e '\nCompiling C++ to LLVM IR (creates ./build/bin/libbox2d.a archive)'
set -x
emmake make
{ set +x; } 2>&-
>&2 echo

# generate Box2D_*.{wasm,js} from glue code + libbox2d.a
set -x
SIMD_ENABLED="$SIMD_ENABLED" "$DIR/build_wasm.sh"
{ set +x; } 2>&-

>&2 echo -e "Completed '$FLAVOUR_NAME' flavour"

popd
done
4 changes: 2 additions & 2 deletions box2d-wasm/build_idl_bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Red='\033[0;31m'
Purple='\033[0;35m'
NC='\033[0m' # No Color

if ! [[ "$PWD" -ef "$DIR/build" ]]; then
>&2 echo -e "${Red}This script is meant to be run from <repository_root>/box2d-wasm/build${NC}"
if ! [[ "$PWD" -ef "$DIR/build/common" ]]; then
>&2 echo -e "${Red}This script is meant to be run from <repository_root>/box2d-wasm/build/common${NC}"
exit 1
fi

Expand Down
11 changes: 8 additions & 3 deletions box2d-wasm/build_makefile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Red='\033[0;31m'
Purple='\033[0;35m'
NC='\033[0m' # No Color

if ! [[ "$PWD" -ef "$DIR/build" ]]; then
>&2 echo -e "${Red}This script is meant to be run from <repository_root>/box2d-wasm/build${NC}"
if ! [[ "$(dirname "$PWD")" -ef "$DIR/build/flavour" ]]; then
>&2 echo -e "${Red}This script is meant to be run from <repository_root>/box2d-wasm/build/flavour/\$FLAVOUR_DIRNAME${NC}"
exit 1
fi

Expand All @@ -25,5 +25,10 @@ case "$TARGET_TYPE" in
esac
>&2 echo -e "TARGET_TYPE is $TARGET_TYPE"

CMAKE_CXX_FLAGS=()
if [[ "$SIMD_ENABLED" = "1" ]]; then
CMAKE_CXX_FLAGS=(${CMAKE_CXX_FLAGS[@]} -msimd128)
fi

set -x
emcmake cmake -DCMAKE_BUILD_TYPE="$TARGET_TYPE" ../../box2d -DBOX2D_BUILD_UNIT_TESTS=OFF -DBOX2D_BUILD_DOCS=OFF -DBOX2D_BUILD_TESTBED=OFF
emcmake cmake -DCMAKE_CXX_FLAGS="${CMAKE_CXX_FLAGS[@]}" "$DIR/../box2d" -DBOX2D_BUILD_UNIT_TESTS=OFF -DBOX2D_BUILD_DOCS=OFF -DBOX2D_BUILD_TESTBED=OFF
8 changes: 4 additions & 4 deletions box2d-wasm/build_typings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ Red='\033[0;31m'
Purple='\033[0;35m'
NC='\033[0m' # No Color

if ! [[ "$PWD" -ef "$DIR/build" ]]; then
>&2 echo -e "${Red}This script is meant to be run from <repository_root>/box2d-wasm/build${NC}"
if ! [[ "$PWD" -ef "$DIR/build/common" ]]; then
>&2 echo -e "${Red}This script is meant to be run from <repository_root>/box2d-wasm/build/common${NC}"
exit 1
fi

: "${WEBIDL_TO_TS:=../node_modules/webidl-to-ts}"
: "${WEBIDL_TO_TS:="$DIR/node_modules/webidl-to-ts"}"
if test -d "${WEBIDL_TO_TS-}"; then
>&2 echo -e "webidl-to-ts found at: $WEBIDL_TO_TS"
else
Expand All @@ -23,4 +23,4 @@ fi

set -x
# requires Node 14.0.0 for running ES modules
exec node --experimental-specifier-resolution=node --harmony "$WEBIDL_TO_TS/dist/index.js" -f ../Box2D.idl -n Box2D -o Box2D.d.ts
exec node --experimental-specifier-resolution=node --harmony "$WEBIDL_TO_TS/dist/index.js" -f "$DIR/Box2D.idl" -n Box2D -o Box2D.d.ts
13 changes: 7 additions & 6 deletions box2d-wasm/build_wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Blue='\033[0;34m'
Purple='\033[0;35m'
NC='\033[0m' # No Color

if ! [[ "$PWD" -ef "$DIR/build" ]]; then
>&2 echo -e "${Red}This script is meant to be run from <repository_root>/box2d-wasm/build${NC}"
if ! [[ "$(dirname "$PWD")" -ef "$DIR/build/flavour" ]]; then
>&2 echo -e "${Red}This script is meant to be run from <repository_root>/box2d-wasm/build/flavour/\$FLAVOUR_DIRNAME${NC}"
exit 1
fi

Expand All @@ -25,6 +25,9 @@ EMCC_OPTS=(
-s EXPORTED_FUNCTIONS=_malloc,_free
-s ALLOW_MEMORY_GROWTH=1
)
if [[ "$SIMD_ENABLED" = "1" ]]; then
EMCC_OPTS=(${EMCC_OPTS[@]} -msimd128)
fi
DEBUG_OPTS=(
-g3
-gsource-map
Expand Down Expand Up @@ -70,7 +73,6 @@ case "$TARGET_TYPE" in
esac
>&2 echo -e "TARGET_TYPE is $TARGET_TYPE"


BASENAME='Box2D'
BARE_WASM="$BASENAME.bare.wasm"

Expand All @@ -86,7 +88,7 @@ mkdir -p "$UMD_DIR" "$ES_DIR"

>&2 echo -e "${Blue}Building post-link targets${NC}"

LINK_OPTS=(--post-link "$BARE_WASM" --post-js box2d_glue.js --post-js "$DIR/glue_stub.js" ${EMCC_OPTS[@]})
LINK_OPTS=(--post-link "$BARE_WASM" --post-js "$DIR/build/common/box2d_glue.js" --post-js "$DIR/glue_stub.js" ${EMCC_OPTS[@]})

ES_FILE="$ES_DIR/$BASENAME.js"
>&2 echo -e "${Blue}Building ES module, $ES_DIR/$BASENAME.{js,wasm}${NC}"
Expand Down Expand Up @@ -119,8 +121,7 @@ else
define([], function() { return Box2D; });
else if (typeof exports === 'object')
exports['Box2D'] = Box2D;
"
"
UMD_FOOTER_ESCAPED=`escape_for_sed_replace "$UMD_FOOTER"`

sed -e "s/^$ES6_HEADER$/$UMD_HEADER_ESCAPED/" -e "s/^$ES6_FOOTER$/$UMD_FOOTER_ESCAPED/" "$ES_FILE" > "$UMD_FILE"
Expand Down
11 changes: 11 additions & 0 deletions box2d-wasm/entry/es/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { simd } from 'wasm-feature-detect'

export default (async () => {
const hasSIMD = await simd();
const module = await (
hasSIMD
? import('../../build/flavour/simd/es/Box2D.simd.js')
: import('../../build/flavour/standard/es/Box2D.js')
);
return module;
})();
9 changes: 9 additions & 0 deletions box2d-wasm/entry/umd/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
parserOptions: {
ecmaVersion: 2019,
},
env: {
commonjs: true,
es6: true
}
}
12 changes: 12 additions & 0 deletions box2d-wasm/entry/umd/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// this file will be copied into the build/es folder
const { simd } = require('wasm-feature-detect');

module.exports = (async () => {
const hasSIMD = await simd();
const module = (
hasSIMD
? require('../../build/flavour/simd/umd/Box2D.simd.js')
: require('../../build/flavour/standard/umd/Box2D.js')
);
return module;
})();
2 changes: 1 addition & 1 deletion box2d-wasm/glue_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class b2DestructionListenerWrapper : b2DestructionListener {
}
};

#include "build/box2d_glue.cpp"
#include "build/common/box2d_glue.cpp"

extern "C" {
// member functions that we weren't able to describe in WebIDL (e.g. pointer-to-float params)
Expand Down
23 changes: 15 additions & 8 deletions box2d-wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@
"name": "box2d-wasm",
"version": "4.1.0",
"description": "Box2D compiled to WebAssembly",
"module": "build/es/Box2D.js",
"main": "build/umd/Box2D.js",
"module": "entry/es/entry.js",
"main": "entry/umd/entry.js",
"files": [
"Box2DModule.d.ts",
"Box2DModuleAugmentations.d.ts",
"build/Box2D.d.ts",
"build/es/Box2D.js",
"build/es/Box2D.wasm",
"build/umd/Box2D.js",
"build/umd/Box2D.wasm"
"entry/es/entry.js",
"entry/umd/entry.js",
"build/common/Box2D.d.ts",
"build/flavour/standard/es/Box2D.js",
"build/flavour/standard/es/Box2D.wasm",
"build/flavour/standard/umd/Box2D.js",
"build/flavour/standard/umd/Box2D.wasm",
"build/flavour/simd/es/Box2D.simd.js",
"build/flavour/simd/es/Box2D.simd.wasm",
"build/flavour/simd/umd/Box2D.simd.js",
"build/flavour/simd/umd/Box2D.simd.wasm"
],
"types": "Box2DModule.d.ts",
"repository": {
Expand All @@ -26,7 +32,8 @@
"webidl-to-ts": "workspace:*"
},
"dependencies": {
"@types/emscripten": "^1.39.4"
"@types/emscripten": "^1.39.4",
"wasm-feature-detect": "^1.2.11"
},
"author": "Alex Birch",
"license": "Zlib"
Expand Down

0 comments on commit 6ec863d

Please sign in to comment.