Skip to content

Commit

Permalink
New package: emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
truboxl committed Mar 29, 2021
1 parent cbdd1cf commit 4f086f8
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
83 changes: 83 additions & 0 deletions packages/emscripten/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
TERMUX_PKG_HOMEPAGE=https://emscripten.org/
TERMUX_PKG_DESCRIPTION="Emscripten: An LLVM-to-WebAssembly Compiler"
TERMUX_PKG_LICENSE="MIT"
TERMUX_PKG_MAINTAINER="@termux"
TERMUX_PKG_VERSION=2.0.16
TERMUX_PKG_SRCURL=https://github.com/emscripten-core/emscripten/archive/${TERMUX_PKG_VERSION}.tar.gz
TERMUX_PKG_SHA256=6a1732596349637953ba5c403c47ce064c80773816050066a5b130293666f59c
TERMUX_PKG_DEPENDS="python, nodejs, binaryen"
TERMUX_PKG_BUILD_DEPENDS="cmake"
TERMUX_PKG_BUILD_IN_SRC=true
TERMUX_PKG_HOSTBUILD=true

# https://github.com/emscripten-core/emscripten/issues/11362
# switch to stable LLVM and drop building here once above is fixed
# build from main branch for now
LLVM_COMMIT_HASH=a50037aaa6d5df403ff6b709a367164313ffd23f
LLVM_CHECKSUM=2fdcf716633884749758e6c6d239b555b7fe0b75e4b57d81c1fe231e1db16dad

# https://github.com/emscripten-core/emsdk/blob/master/emsdk.py
LLVM_BUILD_ARGS="
-DCMAKE_BUILD_TYPE=Release
-DPYTHON_EXECUTABLE=$(which python3)
-DLLVM_INCLUDE_EXAMPLES=OFF
-DCLANG_INCLUDE_EXAMPLES=OFF
-DLLVM_INCLUDE_TESTS=OFF
-DCLANG_INCLUDE_TESTS=OFF
-DLLVM_ENABLE_ASSERTIONS=OFF
-DLLVM_ENABLE_PROJECTS='clang;clang;lld;lld'
-DLLVM_TABLEGEN=$TERMUX_PKG_HOSTBUILD_DIR/bin/llvm-tblgen
-DCLANG_TABLEGEN=$TERMUX_PKG_HOSTBUILD_DIR/bin/clang-tblgen
"

termux_step_post_get_source() {
termux_download "https://github.com/llvm/llvm-project/archive/$LLVM_COMMIT_HASH.zip" "$TERMUX_PKG_CACHEDIR/llvm.zip" "$LLVM_CHECKSUM"
unzip -qo "$TERMUX_PKG_CACHEDIR/llvm.zip" -d "$TERMUX_PKG_CACHEDIR"
}

termux_step_host_build() {
termux_setup_cmake
cmake $LLVM_BUILD_ARGS "$TERMUX_PKG_CACHEDIR/llvm-project-$LLVM_COMMIT_HASH/llvm"
cmake --build "$TERMUX_PKG_HOSTBUILD_DIR" -j "$TERMUX_MAKE_PROCESSES" --target llvm-tblgen clang-tblgen
}

termux_step_make() {
# from packages/libllvm/build.sh
export LLVM_DEFAULT_TARGET_TRIPLE=${CCTERMUX_HOST_PLATFORM/-/-unknown-}
export LLVM_TARGET_ARCH
case "$TERMUX_ARCH" in
arm) LLVM_TARGET_ARCH=ARM ;;
aarch64) LLVM_TARGET_ARCH=AArch64 ;;
i686|x86_64) LLVM_TARGET_ARCH=X86 ;;
*) termux_error_exit "Invalid arch: $TERMUX_ARCH" ;;
esac

LLVM_BUILD_ARGS+=" -DLLVM_TARGET_ARCH=$LLVM_TARGET_ARCH"
LLVM_BUILD_ARGS+=" -DLLVM_TARGETS_TO_BUILD=WebAssembly;$LLVM_TARGET_ARCH"
LLVM_BUILD_ARGS+=" -DLLVM_HOST_TRIPLE=$LLVM_DEFAULT_TARGET_TRIPLE"
LLVM_BUILD_ARGS+=" -DCMAKE_INSTALL_PREFIX=$TERMUX_PREFIX/lib/emscripten/llvm"

termux_setup_cmake
cmake $LLVM_BUILD_ARGS -S "$TERMUX_PKG_CACHEDIR/llvm-project-$LLVM_COMMIT_HASH/llvm" -B "$TERMUX_PKG_CACHEDIR/build"
cmake --build "$TERMUX_PKG_CACHEDIR/build" -j "$TERMUX_MAKE_PROCESSES" --target install

# first run generates .emscripten and exits immediately
./emcc
sed -i .emscripten -e "s|'EMSCRIPTEN'.*|'EMSCRIPTEN', '$TERMUX_PREFIX/lib/emscripten')) # directory|"
sed -i .emscripten -e "s|'LLVM'.*|'LLVM', '$TERMUX_PREFIX/lib/emscripten/llvm/bin')) # directory|"
sed -i .emscripten -e "s|'NODE'.*|'NODE', '$TERMUX_PREFIX/bin/node')) # executable|"

make DESTDIR="$TERMUX_PKG_TMPDIR/emscripten"
}

termux_step_make_install() {
tar -xvf "$TERMUX_PKG_SRCDIR/emscripten-$TERMUX_PKG_VERSION.tar.bz2" -C "$TERMUX_PREFIX/lib"

echo "#!$TERMUX_PREFIX/bin/sh" > "$TERMUX_PKG_TMPDIR/emscripten.sh"
echo "export PATH=\$PATH:$TERMUX_PREFIX/lib/emscripten" >> "$TERMUX_PKG_TMPDIR/emscripten.sh"

# https://github.com/emscripten-core/emscripten/issues/9098
echo "export EMCC_CORES=1" >> "$TERMUX_PKG_TMPDIR/emscripten.sh"

install -Dm644 "$TERMUX_PKG_TMPDIR/emscripten.sh" "$TERMUX_PREFIX/etc/profile.d/emscripten.sh"
}
11 changes: 11 additions & 0 deletions packages/emscripten/makefile.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- ./Makefile 2021-03-29 15:57:43.340000000 +0000
+++ ./Makefile 2021-03-29 15:57:51.630000000 +0000
@@ -1,5 +1,7 @@
VERSION = $(shell cat emscripten-version.txt | sed s/\"//g)
-DESTDIR ?= ../emscripten-$(VERSION)
+ifeq ($(DESTDIR),)
+ DESTDIR := $(shell mktemp -d)/emscripten-$(VERSION)
+endif
DISTFILE = emscripten-$(VERSION).tar.bz2

dist: $(DISTFILE)
12 changes: 12 additions & 0 deletions packages/emscripten/tools_install.py.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--- ./tools/install.py 2021-03-29 16:03:59.490000000 +0000
+++ ./tools/install.py 2021-03-29 16:04:02.660000000 +0000
@@ -37,6 +37,9 @@


def add_revision_file(target):
+ if not os.path.exists('.git'):
+ print('skipping generate emscripten-revision.txt as .git directory does not exist')
+ return
# text=True would be better than encoding here, but it's only supported in 3.7+
git_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD'], encoding='utf-8').strip()
with open(os.path.join(target, 'emscripten-revision.txt'), 'w') as f:

0 comments on commit 4f086f8

Please sign in to comment.