Skip to content

Instantly share code, notes, and snippets.

@Wizermil
Last active February 23, 2024 03:27
Show Gist options
  • Save Wizermil/1c6c7b505b760c9fd6c47a15d6bf665a to your computer and use it in GitHub Desktop.
Save Wizermil/1c6c7b505b760c9fd6c47a15d6bf665a to your computer and use it in GitHub Desktop.
Script to compile harfbuzz + freetype static library for emscripten
#!/bin/bash
FREETYPE_VERSION="2.10.0"
HARFBUZZ_VERSION="2.4.0"
ROOT_DIR=$(pwd)
WORKER=$(getconf _NPROCESSORS_ONLN)
BUILD_DIR_FREETYPE2="build/emscripten/freetype2"
BUILD_DIR_HARFBUZZ="build/emscripten/harfbuzz"
error() {
echo -e "$@" 1>&2
}
fail() {
error "$@"
exit 1
}
function build_freetype() {
HAS_HARFBUZZ=$1
cd "$ROOT_DIR" || exit 1
if $HAS_HARFBUZZ; then
echo " "
BUILD_DIR_FREETYPE2="build/emscripten/freetype2_harfbuzz"
fi
LOG_FILE="$ROOT_DIR/$BUILD_DIR_FREETYPE2/build.log"
if [ -d "$BUILD_DIR_FREETYPE2/source" ]; then
rm -rf "$BUILD_DIR_FREETYPE2/source"
fi
if [ -f "$LOG_FILE" ]; then
rm "$LOG_FILE"
touch "$LOG_FILE"
fi
mkdir -p "$BUILD_DIR_FREETYPE2/download"
mkdir -p "$BUILD_DIR_FREETYPE2/source"
if [ ! -f "$BUILD_DIR_FREETYPE2/download/freetype-$FREETYPE_VERSION.tar.gz" ]; then
echo "Downloading: freetype"
curl -Lo "$BUILD_DIR_FREETYPE2/download/freetype-$FREETYPE_VERSION.tar.gz" "http://download.savannah.gnu.org/releases/freetype/freetype-$FREETYPE_VERSION.tar.gz" >> "$LOG_FILE" 2>&1 || fail "Error download freetype"
echo "Downloaded: freetype"
fi
echo "Uncompressing: freetype"
tar zxf "$BUILD_DIR_FREETYPE2/download/freetype-$FREETYPE_VERSION.tar.gz" -C "$BUILD_DIR_FREETYPE2/source" >> "$LOG_FILE" 2>&1 || fail "Error uncompress freetype"
echo "Uncompressed: freetype"
echo " "
cd "$ROOT_DIR" || exit 1
if [ -d "$BUILD_DIR_FREETYPE2/output" ]; then
rm -rf "$BUILD_DIR_FREETYPE2/output"
fi
mkdir -p "$BUILD_DIR_FREETYPE2/output"
cp -R "$BUILD_DIR_FREETYPE2/source/freetype-$FREETYPE_VERSION/" "$BUILD_DIR_FREETYPE2/output"
if [ -d "$BUILD_DIR_FREETYPE2/install" ]; then
rm -rf "$BUILD_DIR_FREETYPE2/install"
fi
mkdir -p "$BUILD_DIR_FREETYPE2/install"
export CC="emcc"
export CXX="em++"
export AR="emar"
export LDFLAGS="-Os"
export CFLAGS="-m32 -Os"
export CPPFLAGS="$CFLAGS"
export CXXFLAGS="${CFLAGS} -fno-exceptions -fno-rtti"
if $HAS_HARFBUZZ; then
export HARFBUZZ_CFLAGS="-I$ROOT_DIR/$BUILD_DIR_HARFBUZZ/install/include/harfbuzz"
export HARFBUZZ_LIBS="-L$ROOT_DIR/$BUILD_DIR_HARFBUZZ/install/lib -lharfbuzz"
fi
cd "$ROOT_DIR/$BUILD_DIR_FREETYPE2/output" || exit 1
echo "Configuring: freetype emscripten"
FREETYPE_CONFIGURE_CMD="emconfigure ./configure --host=\"i386-apple-darwin\" \\
--prefix=$ROOT_DIR/$BUILD_DIR_FREETYPE2/install \\
--enable-static \\
--disable-shared \\
--without-zlib \\
--without-png \\
--without-bzip2 \\"
if $HAS_HARFBUZZ; then
FREETYPE_CONFIGURE_CMD="$FREETYPE_CONFIGURE_CMD--with-harfbuzz=yes"
else
FREETYPE_CONFIGURE_CMD="$FREETYPE_CONFIGURE_CMD--without-harfbuzz"
fi
eval "$FREETYPE_CONFIGURE_CMD" >> "$LOG_FILE" 2>&1 || fail "Error configuration freetype emscripten"
# Patch from http://zarprime.blogspot.com/2006/08/how-to-cross-compile-freetype.html
cd src/tools || exit 1
clang apinames.c -o apinames
cp apinames ../../objs
cd "$ROOT_DIR/$BUILD_DIR_FREETYPE2/output" || exit 1
echo "Configured: freetype emscripten"
echo "Compiling: freetype emscripten"
emmake make -j "$WORKER" >> "$LOG_FILE" 2>&1 || fail "Error compilation freetype emscripten"
echo "Compiled: freetype emscripten"
echo "Installing: freetype emscripten"
emmake make install >> "$LOG_FILE" 2>&1 || fail echo "Error installation freetype emscripten"
echo "Installed: freetype emscripten"
}
build_freetype false
cd "$ROOT_DIR" || exit 1
LOG_FILE="$ROOT_DIR/$BUILD_DIR_HARFBUZZ/build.log"
if [ -d "$BUILD_DIR_HARFBUZZ/source" ]; then
rm -rf "$BUILD_DIR_HARFBUZZ/source"
fi
if [ -f "$LOG_FILE" ]; then
rm "$LOG_FILE"
touch "$LOG_FILE"
fi
mkdir -p "$BUILD_DIR_HARFBUZZ/download"
mkdir -p "$BUILD_DIR_HARFBUZZ/source"
echo " "
if [ ! -f "$BUILD_DIR_HARFBUZZ/download/harfbuzz-$HARFBUZZ_VERSION.tar.bz2" ]; then
echo "Downloading: harfbuzz"
curl -Lo "$BUILD_DIR_HARFBUZZ/download/harfbuzz-$HARFBUZZ_VERSION.tar.bz2" "https://github.com/harfbuzz/harfbuzz/releases/download/$HARFBUZZ_VERSION/harfbuzz-$HARFBUZZ_VERSION.tar.bz2" >> "$LOG_FILE" 2>&1 || fail "Error download harfbuzz"
echo "Downloaded: harfbuzz"
fi
echo "Uncompressing: harfbuzz"
tar jxf "$BUILD_DIR_HARFBUZZ/download/harfbuzz-$HARFBUZZ_VERSION.tar.bz2" -C "$BUILD_DIR_HARFBUZZ/source" >> "$LOG_FILE" 2>&1 || fail "Error uncompress harfbuzz"
echo "Uncompressed: harfbuzz"
echo " "
cd "$ROOT_DIR" || exit 1
if [ -d "$BUILD_DIR_HARFBUZZ/output" ]; then
rm -rf "$BUILD_DIR_HARFBUZZ/output"
fi
mkdir -p "$BUILD_DIR_HARFBUZZ/output"
cp -R "$BUILD_DIR_HARFBUZZ/source/harfbuzz-$HARFBUZZ_VERSION/" "$BUILD_DIR_HARFBUZZ/output" || exit 1
if [ -d "$BUILD_DIR_HARFBUZZ/install" ]; then
rm -rf "$BUILD_DIR_HARFBUZZ/install"
fi
mkdir -p "$BUILD_DIR_HARFBUZZ/install"
export FREETYPE_CFLAGS="-I$ROOT_DIR/$BUILD_DIR_FREETYPE2/install/include/freetype2"
export FREETYPE_LIBS="-L$ROOT_DIR/$BUILD_DIR_FREETYPE2/install/lib -lfreetype"
cd "$ROOT_DIR/$BUILD_DIR_HARFBUZZ/output" || exit 1
if [ -d "$BUILD_DIR_HARFBUZZ/download" ]; then
rm -rf "$BUILD_DIR_HARFBUZZ/download"
fi
export CC="emcc"
export CXX="em++"
export AR="emar"
export LDFLAGS="-Os"
export CFLAGS="-m32 -Os"
export CPPFLAGS="$CFLAGS"
export CXXFLAGS="${CFLAGS} -fno-exceptions -fno-rtti"
echo "Configuring: harfbuzz emscripten"
emconfigure ./configure --host="i386-apple-darwin" \
--prefix="$ROOT_DIR/$BUILD_DIR_HARFBUZZ/install" \
--enable-static \
--disable-shared \
--disable-dependency-tracking \
--disable-gtk-doc-html \
--enable-introspection \
--without-fontconfig \
--without-glib \
--without-gobject \
--without-icu \
--without-cairo \
--with-ucdn=yes \
--with-freetype=yes >> "$LOG_FILE" 2>&1 || fail "Error configuration harfbuzz emscripten"
echo "Configured: harfbuzz emscripten"
echo "Compiling: harfbuzz emscripten"
emmake make -j "$WORKER" >> "$LOG_FILE" 2>&1 || fail "Error compilation harfbuzz emscripten"
echo "Compiled: harfbuzz emscripten"
echo "Installing: harfbuzz emscripten"
emmake make install >> "$LOG_FILE" 2>&1 || fail "Error installation harfbuzz emscripten"
echo "Installed: harfbuzz emscripten"
build_freetype true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment