Skip to content

Instantly share code, notes, and snippets.

@surma

surma/build.sh Secret

Last active May 22, 2021 17:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save surma/8884a1e66b006c48e1ecc57bd1f36011 to your computer and use it in GitHub Desktop.
Save surma/8884a1e66b006c48e1ecc57bd1f36011 to your computer and use it in GitHub Desktop.
Example for a Emscripten project boilerplate
#!/bin/bash
set -e
export OPTIMIZE="-Os"
export LDFLAGS="${OPTIMIZE}"
export CFLAGS="${OPTIMIZE}"
export CPPFLAGS="${OPTIMIZE}"
eval $@
echo "============================================="
echo "Compiling libvpx"
echo "============================================="
test -n "$SKIP_LIBVPX" || (
rm -rf build-vpx || true
mkdir build-vpx
cd build-vpx
emconfigure ../node_modules/libvpx/configure \
--target=generic-gnu
emmake make
)
echo "============================================="
echo "Compiling libvpx done"
echo "============================================="
echo "============================================="
echo "Compiling wasm bindings"
echo "============================================="
(
# Compile C/C++ code
emcc \
${OPTIMIZE} \
--bind \
-s STRICT=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-s ASSERTIONS=0 \
-s MALLOC=emmalloc \
-s MODULARIZE=1 \
-s EXPORT_ES6=1 \
-o ./my-module.js \
-I ./node_modules/libvpx \
my-module.cpp \
build-vpx/libvpx.a
# Create output folder
mkdir -p dist
# Move artifacts
mv my-module.{js,wasm} dist
)
echo "============================================="
echo "Compiling wasm bindings done"
echo "============================================="
<!doctype html>
Open the console to see the output from the wasm module.
<script type="module">
import wasmModule from "./my-module.js";
const instance = wasmModule({
onRuntimeInitialized() {
instance.sayHello();
}
});
</script>
#include "vpxenc.h"
#include <emscripten/bind.h>
using namespace emscripten;
int say_hello() {
printf("Hello from your wasm module with libvpx %d\n", VPX_CODEC_ABI_VERSION);
return 0;
}
EMSCRIPTEN_BINDINGS(my_module) {
function("sayHello", &say_hello);
}
{
"name": "my-worldchanging-project",
"scripts": {
"kek": "echo hai",
"install": "napa",
"build:emscripten": "docker run --rm -v $(pwd):/src trzeci/emscripten ./build.sh",
"build:app": "cp index.html dist/index.html",
"build": "npm run build:emscripten && npm run build:app",
"serve": "http-server -c0 dist"
},
"devDependencies": {
"http-server": "*"
},
"dependencies": {
"napa": "^3.0.0"
},
"napa": {
"libvpx": "git+https://github.com/webmproject/libvpx"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment