From 76e2800c3c7422e4db4334a9b2c03d1df2a66937 Mon Sep 17 00:00:00 2001 From: paradust7 <102263465+paradust7@users.noreply.github.com> Date: Wed, 30 Mar 2022 03:04:49 +0000 Subject: [PATCH] Initial build scripts --- .gitignore | 5 + README.md | 30 ++++ build_all.sh | 23 +++ build_freetype.sh | 27 ++++ build_fsroot.sh | 54 +++++++ build_libjpeg.sh | 24 +++ build_libogg.sh | 30 ++++ build_libpng.sh | 21 +++ build_libvorbis.sh | 29 ++++ build_minetest.sh | 83 +++++++++++ build_sqlite3.sh | 21 +++ build_webshims.sh | 20 +++ build_zlib.sh | 20 +++ build_zstd.sh | 30 ++++ common.sh | 34 +++++ incremental.sh | 4 + pull_minetest.sh | 15 ++ static/.htaccess | 2 + static/index.html | 357 +++++++++++++++++++++++++++++++++++++++++++++ 19 files changed, 829 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 build_all.sh create mode 100755 build_freetype.sh create mode 100755 build_fsroot.sh create mode 100755 build_libjpeg.sh create mode 100755 build_libogg.sh create mode 100755 build_libpng.sh create mode 100755 build_libvorbis.sh create mode 100755 build_minetest.sh create mode 100755 build_sqlite3.sh create mode 100755 build_webshims.sh create mode 100755 build_zlib.sh create mode 100755 build_zstd.sh create mode 100644 common.sh create mode 100755 incremental.sh create mode 100755 pull_minetest.sh create mode 100644 static/.htaccess create mode 100644 static/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..141c3ad --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +minetest/ +build/ +sources/ +install/ +www/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..517b0ed --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +Minetest-wasm +============= + +This is an experimental port of Minetest to the web using emscripten/WebAssembly. + + +System Requirements +------------------- +This has only been tested on Ubuntu 20.04. + +* Ubuntu: apt-get install -y build-essential cmake tclsh + +Pre-requisites +-------------- +The Emscripten SDK (emsdk) must be installed, activated, and in the PATH. +It is assumed to be installed in $HOME/emsdk (edit `common.sh` to change this). + +Building +--------- + + cd minetest-wasm + ./build_all.sh + +Installation +------------ + +If the build completes successfully, the www/ directory will contain the entire application. This +includes an `.htaccess` file which sets headers that are required (by browsers) to load the app. +If your webserver does not recognize `.htaccess` files, you may need to set the headers in +another way. diff --git a/build_all.sh b/build_all.sh new file mode 100755 index 0000000..91d423e --- /dev/null +++ b/build_all.sh @@ -0,0 +1,23 @@ +#!/bin/bash -eux + +rm -rf build install www + +# Emscripten comes with ports for most of these, but they don't compile +# with pthread support. Wipe the cache and build them ourselves. +emcc --clear-cache --clear-ports + +# Dependencies +./build_zlib.sh +./build_libjpeg.sh +./build_libpng.sh # uses zlib +./build_libogg.sh +./build_libvorbis.sh # uses ogg +./build_freetype.sh # uses zlib, libpng +./build_zstd.sh +./build_sqlite3.sh +./build_webshims.sh + +# Minetest +./pull_minetest.sh +./build_fsroot.sh +./build_minetest.sh diff --git a/build_freetype.sh b/build_freetype.sh new file mode 100755 index 0000000..3223abf --- /dev/null +++ b/build_freetype.sh @@ -0,0 +1,27 @@ +#!/bin/bash -eux + +source common.sh + +if [ ! -d src/freetype ]; then + pushd sources + git clone https://gitlab.freedesktop.org/freetype/freetype.git freetype + popd +fi + +pushd build +rm -rf freetype +mkdir freetype + +pushd freetype + +emcmake cmake \ + -DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" \ + -DZLIB_LIBRARY="$INSTALL_DIR/lib/libz.a" \ + -DZLIB_INCLUDE_DIR="$INSTALL_DIR/include" \ + -DPNG_LIBRARY="$INSTALL_DIR/lib/libpng.a" \ + -DPNG_PNG_INCLUDE_DIR="$INSTALL_DIR/include" \ + "$SRC_DIR/freetype" +emmake make +emmake make install + +echo "freetype OK" diff --git a/build_fsroot.sh b/build_fsroot.sh new file mode 100755 index 0000000..f41956e --- /dev/null +++ b/build_fsroot.sh @@ -0,0 +1,54 @@ +#!/bin/bash -eux + +# Build virtual file system +# +# All the files minetest needs to function correctly. +# +# Shaders, fonts, games, etc +# +# The only source for this seems to be the official distribution. +# There must be a script to build it somewhere, but I haven't found it. + +source common.sh + +UPSTREAM="https://github.com/minetest/minetest/releases/download/5.5.0/minetest-5.5.0-win64.zip" +ZIPFILE="minetest-5.5.0-win64.zip" +ZIPDIR="minetest-5.5.0-win64" + +if [ ! -d "$MINETEST_REPO" ] || [ ! -d "$IRRLICHT_REPO" ; then + echo "Minetest source not found" + exit 1 +fi + +if [ ! -f sources/"$ZIPFILE" ]; then + pushd sources + wget "$UPSTREAM" + popd +fi + +pushd build +rm -rf "$ZIPDIR" +unzip "$SRC_DIR"/"$ZIPFILE" + +rm -rf fsroot +mkdir fsroot +mv "$ZIPDIR" fsroot/minetest + +pushd fsroot/minetest + +# Don't need the Windows exe/dlls +rm -rf bin + +# Emscripten strips empty directories. But bin/ needs to be present so that +# realpath() works on relative paths starting with bin/../ +mkdir bin +echo "This is here to ensure bin exists" > bin/readme.txt + +# Replace these directories with the ones from the source directory +for I in client builtin clientmods games/devtest textures; do + rm -rf "$I" + cp -r "$MINETEST_REPO/$I" "$I" +done + +# Copy the irrlicht shaders +cp -r "$IRRLICHT_REPO/media/Shaders" client/shaders/Irrlicht diff --git a/build_libjpeg.sh b/build_libjpeg.sh new file mode 100755 index 0000000..a6ce04a --- /dev/null +++ b/build_libjpeg.sh @@ -0,0 +1,24 @@ +#!/bin/bash -eux + +source common.sh + +if [ ! -d sources/libjpeg ]; then + pushd sources + git clone "https://github.com/libjpeg-turbo/libjpeg-turbo.git" libjpeg + popd +fi + +pushd build +rm -rf libjpeg +mkdir -p libjpeg + +pushd libjpeg +emcmake cmake \ +-DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" \ +-DWITH_SIMD=0 \ +"$SRC_DIR/libjpeg" + +emmake make +emmake make install + +echo "libjpeg OK" diff --git a/build_libogg.sh b/build_libogg.sh new file mode 100755 index 0000000..bbb56bd --- /dev/null +++ b/build_libogg.sh @@ -0,0 +1,30 @@ +#!/bin/bash -eux + +source common.sh + +TARNAME="libogg-1.3.5" +TARBALL="libogg-1.3.5.tar.gz" + +if [ ! -f sources/"$TARBALL" ]; then + pushd sources + wget "https://downloads.xiph.org/releases/ogg/$TARBALL" + popd +fi + +if ! sha256sum sources/"$TARBALL" | grep -q 0eb4b4b9420a0f51db142ba3f9c64b333f826532dc0f48c6410ae51f4799b664; then + echo "Wrong checksum for $TARNAME" + exit 1 +fi + +pushd build + +rm -rf "$TARNAME" +tar -zxvf "$SRC_DIR/$TARBALL" + +pushd "$TARNAME" + +emconfigure ./configure --disable-shared --prefix="$INSTALL_DIR" +emmake make +emmake make install + +echo "libogg OK" diff --git a/build_libpng.sh b/build_libpng.sh new file mode 100755 index 0000000..b8a877d --- /dev/null +++ b/build_libpng.sh @@ -0,0 +1,21 @@ +#!/bin/bash -eux + +source common.sh + +if [ ! -d sources/libpng ]; then + pushd sources + git clone https://git.code.sf.net/p/libpng/code libpng + popd +fi + +pushd build +rm -rf libpng +cp -a "$SRC_DIR/libpng" libpng + +pushd libpng +# For zlib +export CPPFLAGS="-I${INSTALL_DIR}/include" +export LDFLAGS="-L${INSTALL_DIR}/lib" +emconfigure ./configure --disable-shared --prefix="${INSTALL_DIR}" +emmake make +emmake make install diff --git a/build_libvorbis.sh b/build_libvorbis.sh new file mode 100755 index 0000000..200b312 --- /dev/null +++ b/build_libvorbis.sh @@ -0,0 +1,29 @@ +#!/bin/bash -eux + +source common.sh + +TARNAME="libvorbis-1.3.7" +TARBALL="libvorbis-1.3.7.tar.gz" + +if [ ! -f "sources/$TARBALL" ]; then + pushd sources + wget "https://downloads.xiph.org/releases/vorbis/$TARBALL" + popd +fi + +if ! sha256sum "sources/$TARBALL" | grep -q 0e982409a9c3fc82ee06e08205b1355e5c6aa4c36bca58146ef399621b0ce5ab; then + echo "Wrong checksum" + exit 1 +fi + +pushd build +rm -rf "$TARNAME" +tar -zxvf "$SRC_DIR/$TARBALL" + +pushd "$TARNAME" + +emconfigure ./configure --disable-shared --prefix="$INSTALL_DIR" --with-ogg="$INSTALL_DIR" +emmake make +emmake make install + +echo "libvorbis OK" diff --git a/build_minetest.sh b/build_minetest.sh new file mode 100755 index 0000000..010b620 --- /dev/null +++ b/build_minetest.sh @@ -0,0 +1,83 @@ +#!/bin/bash -eux + +source common.sh + +INCREMENTAL=${INCREMENTAL:-false} + +pushd build +if ! $INCREMENTAL; then + rm -rf minetest +fi +mkdir -p minetest +pushd minetest + +export EMSDK_EXTRA="-sUSE_SDL=2" +export CFLAGS="$CFLAGS $EMSDK_EXTRA" +export CXXFLAGS="$CXXFLAGS $EMSDK_EXTRA" +export LDFLAGS="$LDFLAGS $EMSDK_EXTRA -sALLOW_MEMORY_GROWTH=1 -sPTHREAD_POOL_SIZE=20 -s EXPORTED_RUNTIME_METHODS=ccall,cwrap -L$INSTALL_DIR/lib -lemsocket" + +# Used by CMakeFiles.txt in the webport +export FSROOT_DIR="$BUILD_DIR/fsroot" + +# Create a dummy .o file to use as a substitute for the OpenGLES2 / EGL libraries, +# since Emscripten doesn't actually provide those. (the symbols are resolved through +# javascript stubs). +echo > dummy.c +emcc -c dummy.c -o dummy.o +DUMMY_OBJECT="$(pwd)/dummy.o" + +if ! $INCREMENTAL; then + emcmake cmake \ + -DENABLE_SYSTEM_GMP=OFF \ + -DRUN_IN_PLACE=TRUE \ + -DENABLE_GLES=TRUE \ + -DCMAKE_BUILD_TYPE="$BUILD_KIND" \ + -DZLIB_INCLUDE_DIR="$INSTALL_DIR/include" \ + -DZLIB_LIBRARY="$INSTALL_DIR/lib/libz.a" \ + -DJPEG_INCLUDE_DIR="$INSTALL_DIR/include" \ + -DJPEG_LIBRARY="$INSTALL_DIR/lib/libjpeg.a" \ + -DPNG_PNG_INCLUDE_DIR="$INSTALL_DIR/include" \ + -DPNG_LIBRARY="$INSTALL_DIR/lib/libpng.a" \ + -DOGG_INCLUDE_DIR="$INSTALL_DIR/include" \ + -DVORBIS_INCLUDE_DIR="$INSTALL_DIR/include" \ + -DOGG_LIBRARY="$INSTALL_DIR/lib/libogg.a" \ + -DVORBIS_LIBRARY="$INSTALL_DIR/lib/libvorbis.a" \ + -DVORBISFILE_LIBRARY="$INSTALL_DIR/lib/libvorbisfile.a" \ + -DFREETYPE_LIBRARY="$INSTALL_DIR/lib/libfreetype.a" \ + -DFREETYPE_INCLUDE_DIRS="$INSTALL_DIR/include/freetype2" \ + -DOPENGLES2_INCLUDE_DIR="$EMSDK_SYSINCLUDE" \ + -DOPENGLES2_LIBRARY="$DUMMY_OBJECT" \ + -DSQLITE3_LIBRARY="$INSTALL_DIR/lib/libsqlite3.a" \ + -DSQLITE3_INCLUDE_DIR="$INSTALL_DIR/include" \ + -DZSTD_LIBRARY="$INSTALL_DIR/lib/libzstd.a" \ + -DZSTD_INCLUDE_DIR="$INSTALL_DIR/include" \ + -DEGL_LIBRARY="$DUMMY_OBJECT" \ + -DEGL_INCLUDE_DIR="$EMSDK_SYSINCLUDE" \ + -G "Unix Makefiles" \ + "$BASE_DIR/minetest" +fi + +if $INCREMENTAL; then + emmake make -j1 +else + emmake make -j4 +fi + +echo "Installing into www/" +rm -rf "$WWW_DIR" +mkdir "$WWW_DIR" + +FILES="minetest.data minetest.js minetest.wasm minetest.worker.js" + +for I in $FILES; do + cp src/"$I" "$WWW_DIR" +done + +if [ -f src/minetest.wasm.map ]; then + cp src/minetest.wasm.map "$WWW_DIR" +fi + +cp "$BASE_DIR/static/index.html" "$WWW_DIR" +cp "$BASE_DIR/static/.htaccess" "$WWW_DIR" + +echo "DONE" diff --git a/build_sqlite3.sh b/build_sqlite3.sh new file mode 100755 index 0000000..7e95c2a --- /dev/null +++ b/build_sqlite3.sh @@ -0,0 +1,21 @@ +#!/bin/bash -eux + +source common.sh + +if [ ! -f sources/sqlite.tar.gz ]; then + pushd sources + wget "https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release" -O sqlite.tar.gz + popd +fi + +pushd build +rm -rf sqlite +tar -zxvf "$SRC_DIR/sqlite.tar.gz" + +pushd sqlite +export BUILD_CC="gcc" +emconfigure ./configure --disable-shared --prefix="$INSTALL_DIR" cross_compiling=yes +emmake make +emmake make install + +echo "sqlite3 OK" diff --git a/build_webshims.sh b/build_webshims.sh new file mode 100755 index 0000000..922758d --- /dev/null +++ b/build_webshims.sh @@ -0,0 +1,20 @@ +#!/bin/bash -eux + +source common.sh + +if [ ! -d sources/webshims ]; then + pushd sources + git clone "https://github.com/paradust7/webshims.git" webshims + popd +fi + +pushd build +rm -rf webshims +mkdir webshims +pushd webshims + +emcmake cmake -DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" "$SRC_DIR/webshims" +emmake make +emmake make install + +echo "webshims OK" diff --git a/build_zlib.sh b/build_zlib.sh new file mode 100755 index 0000000..d0406db --- /dev/null +++ b/build_zlib.sh @@ -0,0 +1,20 @@ +#!/bin/bash -eux + +source common.sh + +if [ ! -d sources/zlib ]; then + pushd sources + git clone "https://github.com/madler/zlib.git" zlib + popd +fi + +pushd build +rm -rf zlib +cp -a "$SRC_DIR/zlib" zlib + +pushd zlib +emconfigure ./configure --static --prefix="$INSTALL_DIR" +emmake make +emmake make install + +echo "ZLIB OK" diff --git a/build_zstd.sh b/build_zstd.sh new file mode 100755 index 0000000..5613e80 --- /dev/null +++ b/build_zstd.sh @@ -0,0 +1,30 @@ +#!/bin/bash -eux + +source common.sh + +if [ ! -d sources/zstd ]; then + pushd sources + git clone https://github.com/facebook/zstd.git zstd + + pushd zstd + git checkout v1.5.2 + popd + + popd +fi + +pushd build +rm -rf zstd +mkdir zstd + +pushd zstd +export CFLAGS="-D_POSIX_SOURCE=1" +export CXXFLAGS="-D_POSIX_SOURCE=1" +emcmake cmake \ + -DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" \ + "$SRC_DIR/zstd/build/cmake" + +emmake make +emmake make install + +echo "zstd OK" diff --git a/common.sh b/common.sh new file mode 100644 index 0000000..a931541 --- /dev/null +++ b/common.sh @@ -0,0 +1,34 @@ +BASE_DIR="$(dirname -- "$(readlink -f -- "$0")")" + +cd "$BASE_DIR" +mkdir -p sources build install +SRC_DIR="$BASE_DIR/sources" +BUILD_DIR="$BASE_DIR/build" +INSTALL_DIR="$BASE_DIR/install" +WWW_DIR="$BASE_DIR/www" + +test -d "$SRC_DIR" +test -d "$BUILD_DIR" +test -d "$INSTALL_DIR" + +# Debug / Release +export BUILD_KIND=Release + +if [ $BUILD_KIND == Debug ]; then + export BUILD_CFLAGS="-g -gsource-map -O0 --source-map-base=/dev/" + export BUILD_LDFLAGS="-sSAFE_HEAP=1 -sASSERTIONS=2 -sDEMANGLE_SUPPORT=1" +else + export BUILD_CFLAGS="-O2" + export BUILD_LDFLAGS="" +fi + +export CFLAGS="$BUILD_CFLAGS -pthread -sUSE_PTHREADS=1 -fexceptions" +export CXXFLAGS="$BUILD_CFLAGS -pthread -sUSE_PTHREADS=1 -fexceptions" +export LDFLAGS="$BUILD_LDFLAGS -pthread -sUSE_PTHREADS=1 -fexceptions -sEXIT_RUNTIME" + +export EMSDK_ROOT="$HOME/emsdk" +export EMSDK_SYSLIB="${EMSDK_ROOT}/upstream/emscripten/cache/sysroot/lib/wasm32-emscripten" +export EMSDK_SYSINCLUDE="${EMSDK_ROOT}/upstream/emscripten/cache/sysroot/include" + +export MINETEST_REPO="$BASE_DIR/minetest" +export IRRLICHT_REPO="$BASE_DIR/minetest/lib/irrlichtmt" diff --git a/incremental.sh b/incremental.sh new file mode 100755 index 0000000..dfc1325 --- /dev/null +++ b/incremental.sh @@ -0,0 +1,4 @@ +#!/bin/bash -eux + +export INCREMENTAL=true +./build_minetest.sh diff --git a/pull_minetest.sh b/pull_minetest.sh new file mode 100755 index 0000000..fe0a671 --- /dev/null +++ b/pull_minetest.sh @@ -0,0 +1,15 @@ +#!/bin/bash -eux + +source common.sh + + +# If there's an existing source directory, leave it alone +if [ -d minetest ]; then + exit +fi + +git clone -b webport "https://github.com/paradust7/minetest.git" + +pushd minetest/lib/ + +git clone -b webport "https://github.com/paradust7/irrlicht.git" irrlichtmt diff --git a/static/.htaccess b/static/.htaccess new file mode 100644 index 0000000..7e31924 --- /dev/null +++ b/static/.htaccess @@ -0,0 +1,2 @@ +Header set Cross-Origin-Embedder-Policy "require-corp" +Header set Cross-Origin-Opener-Policy "same-origin" diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..d85b2fb --- /dev/null +++ b/static/index.html @@ -0,0 +1,357 @@ + + +
+ + +