Skip to content

Instantly share code, notes, and snippets.

@kleisauke
Last active May 4, 2024 08:51
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save kleisauke/acfa1c09522705efa5eb0541d2d00887 to your computer and use it in GitHub Desktop.
Save kleisauke/acfa1c09522705efa5eb0541d2d00887 to your computer and use it in GitHub Desktop.
git clone https://gist.github.com/acfa1c09522705efa5eb0541d2d00887.git glib-emscripten
cd glib-emscripten
docker build -t glib-emscripten .
docker run -it --rm -v $(pwd):/src glib-emscripten
#!/usr/bin/env bash
set -e
SOURCE_DIR=$PWD
# Working directories
DEPS=$SOURCE_DIR/deps
TARGET=$SOURCE_DIR/target
rm -rf $DEPS/
mkdir $DEPS
mkdir -p $TARGET
# Define default arguments
# JS BigInt to Wasm i64 integration, enabled by default
WASM_BIGINT=true
# Parse arguments
while [ $# -gt 0 ]; do
case $1 in
--disable-wasm-bigint) WASM_BIGINT=false ;;
*) echo "ERROR: Unknown parameter: $1" >&2; exit 1 ;;
esac
shift
done
# Configure the ENABLE_* and DISABLE_* expansion helpers
for arg in WASM_BIGINT; do
if [ "${!arg}" = "true" ]; then
declare ENABLE_$arg=true
else
declare DISABLE_$arg=true
fi
done
# Common compiler flags
export CFLAGS="-O3"
export CXXFLAGS="$CFLAGS"
export LDFLAGS="-L$TARGET/lib -O3"
if [ "$WASM_BIGINT" = "true" ]; then export LDFLAGS+=" -sWASM_BIGINT"; fi
# Build paths
export CPATH="$TARGET/include"
export PKG_CONFIG_PATH="$TARGET/lib/pkgconfig"
export EM_PKG_CONFIG_PATH="$PKG_CONFIG_PATH"
# Specific variables for cross-compilation
export CHOST="wasm32-unknown-linux" # wasm32-unknown-emscripten
export MESON_CROSS="$SOURCE_DIR/emscripten-crossfile.meson"
# Run as many parallel jobs as there are available CPU cores
export MAKEFLAGS="-j$(nproc)"
# Ensure we link against internal/private dependencies
export PKG_CONFIG="pkg-config --static"
# Dependency version numbers
VERSION_ZLIB=1.3
VERSION_FFI=3.4.6
VERSION_GLIB=2.80.0
# Remove patch version component
without_patch() {
echo "${1%.[[:digit:]]*}"
}
mkdir $DEPS/zlib
curl -Ls https://github.com/madler/zlib/releases/download/v$VERSION_ZLIB/zlib-$VERSION_ZLIB.tar.xz | tar xJC $DEPS/zlib --strip-components=1
cd $DEPS/zlib
emconfigure ./configure --prefix=$TARGET --static
make install
mkdir $DEPS/ffi
curl -Ls https://github.com/libffi/libffi/releases/download/v$VERSION_FFI/libffi-$VERSION_FFI.tar.gz | tar xzC $DEPS/ffi --strip-components=1
cd $DEPS/ffi
# TODO(kleisauke): Discuss this patch upstream
curl -Ls https://github.com/libffi/libffi/compare/v$VERSION_FFI...kleisauke:cleanup${ENABLE_WASM_BIGINT:+-bigint}.patch | patch -p1
# Compile without -fexceptions
sed -i 's/ -fexceptions//g' configure
emconfigure ./configure --host=$CHOST --prefix=$TARGET --enable-static --disable-shared --disable-dependency-tracking \
--disable-builddir --disable-multi-os-directory --disable-raw-api --disable-structs --disable-docs
make install SUBDIRS='include'
mkdir $DEPS/glib
curl -Lks https://download.gnome.org/sources/glib/$(without_patch $VERSION_GLIB)/glib-$VERSION_GLIB.tar.xz | tar xJC $DEPS/glib --strip-components=1
cd $DEPS/glib
# TODO(kleisauke): Discuss these patches upstream
curl -Ls https://github.com/GNOME/glib/compare/$VERSION_GLIB...kleisauke:wasm-vips-$VERSION_GLIB.patch | patch -p1
# Propagate -pthread into CFLAGS to ensure GObject/GIO is compiled with the atomics/bulk-memory features
CFLAGS="$CFLAGS -pthread" meson setup _build --prefix=$TARGET --cross-file=$MESON_CROSS --default-library=static --buildtype=release \
--force-fallback-for=pcre2,gvdb -Dintrospection=disabled -Dselinux=disabled -Dxattr=false -Dlibmount=disabled -Dnls=disabled \
-Dtests=false -Dglib_assert=false -Dglib_checks=false
meson install -C _build --tag devel
# https://github.com/emscripten-core/emsdk
FROM docker.io/emscripten/emsdk:3.1.59
RUN apt-get update \
&& apt-get install -qqy \
build-essential \
libglib2.0-dev \
pkgconf \
# needed for Meson
ninja-build \
python3-pip \
# needed by GLib
python3-packaging \
&& pip3 install meson
CMD ["./build.sh"]
[binaries]
c = 'emcc'
cpp = 'em++'
ar = 'emar'
ranlib = 'emranlib'
pkg-config = ['pkg-config', '--static']
exe_wrapper = 'node'
[properties]
needs_exe_wrapper = true
# Ensure that `-sPTHREAD_POOL_SIZE=4` is not injected into .pc files
[built-in options]
c_thread_count = 0
cpp_thread_count = 0
[host_machine]
system = 'emscripten'
cpu_family = 'wasm32'
cpu = 'wasm32'
endian = 'little'
@t-paul
Copy link

t-paul commented Sep 6, 2023

Thanks for the script, it's very useful for making https://github.com/DSchroer/openscad-wasm possible!

Could you please update... zlib 1.2.13 is not available anymore, current version is now 1.3 (could be downloaded via https too).

@kleisauke
Copy link
Author

Thanks for the script, it's very useful for making https://github.com/DSchroer/openscad-wasm possible!

No problem, I'm glad it's useful.

Could you please update... zlib 1.2.13 is not available anymore, current version is now 1.3 (could be downloaded via https too).

The latest revision of this gist should fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment