# Template file for 'mozjs128'
pkgname=mozjs128
version=128.7.0
revision=3
build_wrksrc=js/src
build_style=gnu-configure
build_helper=rust
make_check_target=check-jit-test
configure_args="--disable-jemalloc --disable-strip --disable-tests \
 --disable-optimize --disable-debug --enable-ctypes --enable-readline \
 --enable-shared-js --enable-system-ffi --with-intl-api --with-system-icu \
 --with-system-nspr --with-system-zlib --enable-hardening --enable-release"
hostmakedepends="make pkg-config python3 cbindgen perl m4
 rust cargo llvm19 clang19"
makedepends="icu-devel libffi-devel nspr-devel python3-devel readline-devel
 zlib-devel rust-std"
checkdepends="python3"
short_desc="SpiderMonkey JavaScript interpreter and library (128.x)"
maintainer="oreo639 <oreo6391@gmail.com>"
license="MPL-2.0"
homepage="https://spidermonkey.dev"
distfiles="${MOZILLA_SITE}/firefox/releases/${version}esr/source/firefox-${version}esr.source.tar.xz"
checksum=e0a7bf4eddfa33cbc8797d68ecea8fb4e6404ca197d3b2242cbc8daed03794f7

LDFLAGS="-Wl,-z,stack-size=1048576"

pre_configure() {
	if [ "$CROSS_BUILD" ]; then
		configure_args+=" --host=${XBPS_TRIPLET} --target=${XBPS_CROSS_TRIPLET} --enable-linker=bfd"
	fi
	export M4=m4
	export AWK=awk
	export AC_MACRODIR=${wrksrc}/build/autoconf
}

post_install() {
	# Remove unneeded static library
	rm -f "${DESTDIR}"/usr/lib/*.ajs

	# it has correct soname but not the right file name
	mv "${DESTDIR}"/usr/lib/libmozjs-${version%%.*}.so \
	   "${DESTDIR}"/usr/lib/libmozjs-${version%%.*}.so.0
	ln -rs "${DESTDIR}"/usr/lib/libmozjs-${version%%.*}.so.0 \
	       "${DESTDIR}"/usr/lib/libmozjs-${version%%.*}.so
}

pre_check() {
	export JITTEST_EXTRA_ARGS="--timeout 600"
	if [ "$XBPS_WORDSIZE" = 32 ]; then
		vsed -i jit-test/tests/basic/bug653153.js -e s/65536/65568/g
		vsed -i jit-test/tests/basic/bug653153.js -e s/65537/65569/g
		vsed -i jit-test/tests/bug1782468-ptrdiff-veclen.js -e 's/getBuildConfiguration("pointer-byte-size") == 4/false/g'
	fi
	if [ "$XBPS_TARGET_MACHINE" = i686 ]; then
		export JITTEST_EXTRA_ARGS="${JITTEST_EXTRA_ARGS} \
			-x basic/fdlibm-for-sin-cos-tan-argument.js \
			-x gc/gcparam.js \
			-x ion/dce-with-rinstructions.js \
			-x sunspider/check-3d-cube.js \
			-x sunspider/check-3d-raytrace.js \
			-x sunspider/check-access-nbody.js \
			-x wasm \
			-x resist-fingerprinting/math-fdlibm-sincostan-asm.js \
			-x resist-fingerprinting/math-fdlibm-sincostan.js \
			"
	fi
	if [ "$XBPS_LIBC" = musl ]; then
		export JITTEST_EXTRA_ARGS="${JITTEST_EXTRA_ARGS} \
			-x sunspider/check-date-format-tofte.js \
			-x resist-fingerprinting/math-fdlibm-sincostan.js \
			"
	fi
}

mozjs128-devel_package() {
	depends="nspr-devel ${sourcepkg}>=${version}_${revision}"
	short_desc+=" - development files"
	pkg_install() {
		vmove usr/bin
		vmove usr/include
		vmove "usr/lib/*.so"
		vmove usr/lib/pkgconfig
	}
}
