Skip to content

Commit

Permalink
New package: emscripten
Browse files Browse the repository at this point in the history
  • Loading branch information
truboxl committed Apr 1, 2021
1 parent cbdd1cf commit 7a402d5
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
111 changes: 111 additions & 0 deletions packages/emscripten/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
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_IN_SRC=true
TERMUX_PKG_HOSTBUILD=true

# https://github.com/emscripten-core/emscripten/blob/main/docs/packaging.md

# https://github.com/emscripten-core/emscripten/issues/11362
# switch to stable LLVM and drop building here once above is fixed
# build from tip-of-tree main branch for now

# https://chromium.googlesource.com/emscripten-releases/+/refs/heads/master/DEPS
# refer llvm_project_revision above
LLVM_COMMIT_HASH=a1d83776bfa186422ec2e1b19eb9d12c96c33348
LLVM_CHECKSUM=dba8f95f55f1dbe45a7408056a5135a5c739f6504f5a29c13bf740db50a6f933

# https://github.com/emscripten-core/emsdk/blob/master/emsdk.py
LLVM_BUILD_ARGS="
-DCMAKE_BUILD_TYPE=Release
-DPython3_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
-DGENERATOR_IS_MULTI_CONFIG=ON
"

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
if [ $TERMUX_ARCH = "arm" ]; then
LLVM_TARGET_ARCH=ARM
elif [ $TERMUX_ARCH = "aarch64" ]; then
LLVM_TARGET_ARCH=AArch64
elif [ $TERMUX_ARCH = "i686" ] || [ $TERMUX_ARCH = "x86_64" ]; then
LLVM_TARGET_ARCH=X86
else
termux_error_exit "Invalid arch: $TERMUX_ARCH"
fi

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
if [ -z $(grep "$LLVM_COMMIT_HASH" "$TERMUX_PKG_CACHEDIR/build/TERMUX_BUILT_FOR_${TERMUX_PKG_VERSION}" 2>/dev/null) ]; then
rm -fr "$TERMUX_PKG_CACHEDIR/build"
fi
cmake $LLVM_BUILD_ARGS -S "$TERMUX_PKG_CACHEDIR/llvm-project-$LLVM_COMMIT_HASH/llvm" -B "$TERMUX_PKG_CACHEDIR/build"
echo "$LLVM_COMMIT_HASH" > "$TERMUX_PKG_CACHEDIR/build/TERMUX_BUILT_FOR_${TERMUX_PKG_VERSION}"
cmake --build "$TERMUX_PKG_CACHEDIR/build" -j "$TERMUX_MAKE_PROCESSES" --target install
}

termux_step_make_install() {
# skip using Makefile which does host npm install, tar archive and extract steps
rm -fr "$TERMUX_PREFIX/lib/emscripten"
./tools/install.py "$TERMUX_PREFIX/lib/emscripten"

# 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|'BINARYEN'.*|'BINARYEN', '$TERMUX_PREFIX')) # directory|"
sed -i .emscripten -e "s|'NODE'.*|'NODE', '$TERMUX_PREFIX/bin/node')) # executable|"
install -Dm644 "$TERMUX_PKG_SRCDIR/.emscripten" "$TERMUX_PREFIX/lib/emscripten/.emscripten"

# https://github.com/emscripten-core/emscripten/issues/9098
echo "#!$TERMUX_PREFIX/bin/sh" > "$TERMUX_PKG_TMPDIR/emscripten.sh"
echo "export PATH=\$PATH:$TERMUX_PREFIX/lib/emscripten" >> "$TERMUX_PKG_TMPDIR/emscripten.sh"
echo "export EMCC_CORES=1" >> "$TERMUX_PKG_TMPDIR/emscripten.sh"
install -Dm644 "$TERMUX_PKG_TMPDIR/emscripten.sh" "$TERMUX_PREFIX/etc/profile.d/emscripten.sh"
}

termux_step_create_debscripts() {
echo "#!$TERMUX_PREFIX/bin/sh" > ./postinst
echo "echo \"Running \"npm ci\" in $TERMUX_PREFIX/lib/emscripten ...\"" >> ./postinst
echo "cd \"$TERMUX_PREFIX/lib/emscripten\"" >> ./postinst
echo "npm ci" >> ./postinst

echo "#!$TERMUX_PREFIX/bin/sh" > ./postrm
echo "case \"\$1\" in" >> ./postrm
echo "purge|remove)" >> ./postrm
echo " rm -fr \"$TERMUX_PREFIX/lib/emscripten\" ;;" >> ./postrm
echo "esac" >> ./postrm
}
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 @@
--- a/tools/install.py
+++ b/tools/install.py
@@ -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 7a402d5

Please sign in to comment.