# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=wasm-micro-runtime
pkgver=2.3.0
pkgrel=0
pkgdesc="WebAssembly Micro Runtime (WAMR)"
url="https://github.com/bytecodealliance/wasm-micro-runtime"
# loongarch64,ppc64le,s390x: not supported by upstream
arch="all !loongarch64 !ppc64le !s390x"
license="Apache-2.0"
makedepends="
	cmake
	llvm-dev
	llvm-static
	llvm-gtest
	samurai
	"
checkdepends="bash"
subpackages="
	iwasm
	iwasm-gc:_iwasm_gc
	wamrc
	"
_asmjit_rev=7bed2b0e1427fab185eea2da8ab4b9fb5b1f45a9
# Keep in sync with core/iwasm/libraries/simde/simde.cmake
_simde_ver=0.8.2
source="https://github.com/bytecodealliance/wasm-micro-runtime/archive/WAMR-$pkgver/wasm-micro-runtime-WAMR-$pkgver.tar.gz
	https://github.com/asmjit/asmjit/archive/$_asmjit_rev/asmjit-$_asmjit_rev.tar.gz
	https://github.com/simd-everywhere/simde/archive/v$_simde_ver/simde-$_simde_ver.tar.gz
	test-unit-mindirect-branch-register.patch
	fix-incompatible-pointer-types.patch
	iwasm
	"
builddir="$srcdir/$pkgname-WAMR-$pkgver"
options="!check"  # TODO: run tests

# NOTE: We don't build iwasm with LLVM JIT because LLVM is huge and this WASM
#  runtime is supposed to be "micro".

case "$CARCH" in
# NOTE: asmjit supports aarch64 and x86, but WAMR doesn't
#  (https://github.com/bytecodealliance/wasm-micro-runtime/issues/3870).
x86_64) _fastjit=1; subpackages="$subpackages iwasm-jit:_iwasm_jit";;
*) _fastjit=0;;
esac

prepare() {
	default_prepare

	local llvmlibs="$(llvm-config --libs)"
	find . -name CMakeLists.txt -exec sed -i "s/\${LLVM_AVAILABLE_LIBS}/$llvmlibs/g" {} \;
}

build() {
	local warm_target="$(echo "$CARCH" | tr 'a-z' 'A-Z')"
	case "$CARCH" in
		arm*) warm_target="ARM";;
		x86) warm_target="X86_32";;
	esac

	local common_args="\
		-G Ninja \
		-W no-dev \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=OFF \
		-DFETCHCONTENT_FULLY_DISCONNECTED=ON \
		-DLLVM_DIR=$(llvm-config --cmakedir) \
		-DWAMR_BUILD_TARGET=$warm_target \
		-DWAMR_BUILD_WITH_CUSTOM_LLVM=1 \
		"

	local core_args="\
		-DFETCHCONTENT_SOURCE_DIR_ASMJIT=$srcdir/asmjit-$_asmjit_rev \
		-DFETCHCONTENT_SOURCE_DIR_SIMDE=$srcdir/simde-$_simde_ver \
		-DWAMR_BUILD_SHARED=0
		-DWAMR_BUILD_AOT=1 \
		-DWAMR_BUILD_JIT=0 \
		-DWAMR_BUILD_MULTI_MODULE=1 \
		-DWAMR_BUILD_THREAD_MGR=1 \
		-DWAMR_BUILD_LIB_PTHREAD=0 \
		-DWAMR_BUILD_LIB_WASI_THREADS=1 \
		-DWAMR_BUILD_TAIL_CALL=1 \
		-DWAMR_BUILD_REF_TYPES=1 \
		"

	export CFLAGS="${CFLAGS/-Os/-O2} -flto=auto"
	export CXXLAGS="${CXXLAGS/-Os/-O2} -flto=auto"

	cmake -B build/iwasm-gc -S product-mini/platforms/linux \
		$common_args \
		$core_args \
		-DWAMR_BUILD_GC=1

	# FastJIT is not compatible with GC.
	[ "$_fastjit" -eq 1 ] && cmake -B build/iwasm-jit -S product-mini/platforms/linux \
		$common_args \
		$core_args \
		-DWAMR_BUILD_FAST_JIT=1

	cmake -B build/compiler -S wamr-compiler \
		$common_args

	cmake --build build/iwasm-gc
	[ "$_fastjit" -eq 1 ] && cmake --build build/iwasm-jit
	cmake --build build/compiler
}

package() {
	mkdir -p "$pkgdir"
}

iwasm() {
	pkgdesc="$pkgdesc - CLI"

	install -D -m755 "$srcdir"/iwasm -t "$subpkgdir"/usr/bin/
}

_iwasm_gc() {
	pkgdesc="$pkgdesc - CLI (with GC)"
	install_if="iwasm"

	cd "$builddir"
	DESTDIR="$subpkgdir" cmake --install build/iwasm-gc

	cd "$subpkgdir"
	mv usr/bin/iwasm-$pkgver usr/bin/iwasm-gc
	rm usr/bin/iwasm  # symlink

	# libiwasm.so doesn't set SONAME.
	# See https://github.com/bytecodealliance/wasm-micro-runtime/issues/3932
	rm -rf usr/lib
}

_iwasm_jit() {
	pkgdesc="$pkgdesc - CLI (with FastJIT)"
	license="$license AND Zlib"

	cd "$builddir"
	DESTDIR="$subpkgdir" cmake --install build/iwasm-jit

	cd "$subpkgdir"
	mv usr/bin/iwasm-$pkgver usr/bin/iwasm-jit
	rm usr/bin/iwasm  # symlink

	# libiwasm.so doesn't set SONAME.
	# See https://github.com/bytecodealliance/wasm-micro-runtime/issues/3932
	rm -rf usr/lib
}

wamrc() {
	pkgdesc="$pkgdesc - AOT compiler"

	cd "$builddir"
	DESTDIR="$subpkgdir" cmake --install build/compiler

	cd "$subpkgdir"
	mv usr/bin/wamrc-$pkgver usr/bin/wamrc
}

sha512sums="
47b52026ef5c8a590b070fc29f184c91c88d73c574085e77b383ff2008c7c22889073ca3821311a2b7a54be1d4d17e1c2b2198ed2c12453aeaf0cf4551f4d2ae  wasm-micro-runtime-WAMR-2.3.0.tar.gz
f67e315bb39a996b90cb3d2dd0780c6147db42d1cfda794c450a70aaef07f1baec681b88fcec59fb70752fbf113c38a929b71ea5e337aa219c864843649badba  asmjit-7bed2b0e1427fab185eea2da8ab4b9fb5b1f45a9.tar.gz
4e42d7140c0afae507773527c6c0c07e6f0cdad59a1d42ebcf4bd223fc9f71e91a2e3db7746aca3c0c5ad2a13333c2322ce1e384c7d699ddfe33bed6f107aec5  simde-0.8.2.tar.gz
c12b8a87cbf329dd5e3b98cc0a922ffece8cb4f4ad3275cd7493ba8973c43f11a1ed635941ca90a6265c9eaf48c005fa8915ada61f0d59015d50c9069d9352ce  test-unit-mindirect-branch-register.patch
bb53fbf9101a0d9ad0f35c0887d2be81bc5223b9fe03e31ef96b19c05072dade0333b206ad78797da28143ff8b876d4063a3c2152d0dd56960e8ded01a79f9b5  fix-incompatible-pointer-types.patch
db83a78a45bb77c3dac5f4506fb951f3b60249d4d76329bdc0278d09e03c122086775169763bfcdf1221a5f0ded6126f418d49df0081231710ba28bd9d83593d  iwasm
"
