# Template file for 'nodejs'
pkgname=nodejs
version=24.14.1
revision=2
build_style=configure
configure_args="
 --prefix=/usr
 --enable-lto
 --shared-zlib
 --shared-ada
 --shared-sqlite
 --shared-zstd
 --shared-http-parser
 $(vopt_if icu --with-intl=system-icu)
 $(vopt_if ssl --shared-openssl)
 $(vopt_if libuv --shared-libuv)
 $(vopt_if nghttp2 --shared-nghttp2)
 $(vopt_if nghttp3 --shared-nghttp3)
 $(vopt_if ngtcp2 --shared-ngtcp2)
 $(vopt_if cares --shared-cares)
 $(vopt_if brotli --shared-brotli)"
make_build_args="PORTABLE=1 V=1 LD=$CXX"
hostmakedepends="pkg-config python3-setuptools"
_make_depends="
 zlib-devel
 ada-devel
 sqlite-devel
 libzstd-devel
 llhttp-devel
 $(vopt_if icu icu-devel)
 $(vopt_if ssl openssl-devel)
 $(vopt_if libuv libuv-devel)
 $(vopt_if nghttp2 nghttp2-devel)
 $(vopt_if nghttp3 nghttp3-devel)
 $(vopt_if ngtcp2 ngtcp2-devel)
 $(vopt_if cares c-ares-devel)
 $(vopt_if brotli brotli-devel)"
makedepends="${_make_depends}"
checkdepends="procps-ng iana-etc"
short_desc="Evented I/O for V8 javascript"
maintainer="Orphaned <orphan@voidlinux.org>"
license="MIT"
homepage="https://nodejs.org/"
changelog="https://raw.githubusercontent.com/nodejs/node/main/doc/changelogs/CHANGELOG_V${version%%.*}.md"
distfiles="https://nodejs.org/dist/v${version}/node-v${version}.tar.xz"
checksum=7822507713f202cf2a551899d250259643f477b671706db421a6fb55c4aa0991
python_version=3

build_options="ssl libuv icu nghttp2 cares brotli"
desc_option_ssl="Enable shared openssl"
desc_option_libuv="Enable shared libuv"
desc_option_icu="Enable shared icu"
desc_option_nghttp2="Enable shared nghttp2"
desc_option_nghttp3="Use system nghttp3"
desc_option_ngtcp2="Use system ngtcp2"
desc_option_cares="Enable shared c-ares"
desc_option_brotli="Enable shared brotli"
build_options_default="ssl libuv icu nghttp2 nghttp3 ngtcp2 cares brotli"

replaces="iojs>=0 nodejs-lts>=0"
provides="nodejs-runtime-0_1"

if [ "$CROSS_BUILD" ]; then
	# Need these for host v8 for torque, see https://github.com/nodejs/node/pull/21079
	hostmakedepends+=" ${_make_depends}"
fi

# https://build.voidlinux.org/builders/i686_builder/builds/27325/steps/shell_3/logs/stdio
if [ "$XBPS_WORDSIZE" = 32 ]; then
	LDFLAGS="-Wl,--no-keep-memory"
fi

if [ "$XBPS_WORDSIZE" -ne "$XBPS_TARGET_WORDSIZE" ]; then
	nocross="host and target must have the same pointer size"
fi

case "$XBPS_TARGET_MACHINE" in
	ppc64*) ;;
	ppc*) broken="Node 18.x does not support 32-bit ppc" ;;
esac

CFLAGS="-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"
CXXFLAGS="-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64"

post_patch() {
	vsed -e 's/install: all/install:/' -i Makefile
}

pre_configure() {
	export LD="$CXX"
	if [ "$CROSS_BUILD" ]; then
		case "$XBPS_TARGET_MACHINE" in
			arm*) configure_args+=" --dest-cpu=arm" ;;
			aarch64*) configure_args+=" --dest-cpu=arm64" ;;
			ppc64*) configure_args+=" --dest-cpu=ppc64" ;;
			ppc*) configure_args+=" --dest-cpu=ppc" ;;
			mipsel*) configure_args+=" --dest-cpu=mipsel" ;;
			mips*) configure_args+=" --dest-cpu=mips" ;;
			i686*) configure_args+=" --dest-cpu=x86" ;;
			x86_64*) configure_args+=" --dest-cpu=x86_64" ;;
			riscv64*) configure_args+=" --dest-cpu=riscv64" ;;
			*) msg_error "$pkgver: cannot be cross compiled for ${XBPS_TARGET_MACHINE}.\n" ;;
		esac
		# this is necessary - for example, normally compiling from ppc64le
		# to ppc64 or from glibc to musl is considered non-cross-compiling
		# by the build system, because it's technically the same architecture
		#
		# that results in the toolset built for target only, and attempting
		# to execute it within the build system fails
		configure_args+=" --cross-compiling"
	fi

	CFLAGS=${CFLAGS/-O2/}
	CXXFLAGS=${CXXFLAGS/-O2/}
	if [ "$XBPS_WORDSIZE" = 32 ]; then
		CFLAGS=${CFLAGS/-g /-g1 }
		CXXFLAGS=${CFLAGS/-g /-g1 }
	fi
}

pre_build() {
	CFLAGS=${CFLAGS/-O2/}
	CXXFLAGS=${CXXFLAGS/-O2/}
	if [ "$XBPS_WORDSIZE" = 32 ]; then
		CFLAGS=${CFLAGS/-g /-g1 }
		CXXFLAGS=${CFLAGS/-g /-g1 }
	fi
}

_gen_csv() {
	printf '%s\n' "$@" | paste -sd,
}

do_check() {
	pre_build
	local CI_SKIP_TESTS=$( {
		cat <<-EOF
		test-crypto-dh.js
		test-child-process-uid-gid.js
		test-cluster-primary-error.js
		test-cluster-primary-kill.js
		test-dns.js
		test-http2-https-fallback.js
		test-http2-server-unknown-protocol.js
		test-https-client-checkServerIdentity.js
		test-https-strict.js
		test-process-euid-egid.js
		test-process-initgroups.js
		test-process-setgroups.js
		test-process-uid-gid.js
		test-tls-alert-handling.js
		test-tls-alpn-server-client.js
		test-tls-cert-regression.js
		test-tls-client-auth.js
		test-tls-client-getephemeralkeyinfo.js
		test-tls-client-mindhsize.js
		test-tls-client-renegotiation-13.js
		test-tls-client-verify.js
		test-tls-dhe.js
		test-tls-empty-sni-context.js
		test-tls-enable-trace-cli.js
		test-tls-enable-trace.js
		test-tls-getcipher.js
		test-tls-junk-closes-server.js
		test-tls-junk-server.js
		test-tls-multi-key.js
		test-tls-multiple-cas-as-string.js
		test-tls-peer-certificate-encoding.js
		test-tls-psk-circuit.js
		test-tls-server-verify.js
		test-tls-set-ciphers.js
		test-tls-sni-option.js
		test-tls-sni-server-client.js
		# https://github.com/nodejs/node/issues/50741
		test-dns-resolveany.js
		test-dns-resolveany-bad-ancount.js
		EOF

		if [ "$XBPS_TARGET_MACHINE" = i686 ]; then
			# https://github.com/nodejs/node/issues/45906
			echo test-fs-utimes-y2K38.js
		fi
	} | grep -v '^#' | paste -sd,)

	make CI_SKIP_TESTS="$CI_SKIP_TESTS" ${makejobs} ${make_build_args} test-only
}

post_install() {
	rm -rf $DESTDIR/usr/include/node/openssl
	vlicense LICENSE
}

nodejs-devel_package() {
	short_desc+=" (development files)"
	replaces="nodejs-lts-10-devel>=0 nodejs-lts-devel>=0"
	pkg_install() {
		vmove usr/include
	}
}

nodejs-lts_package() {
	depends="${sourcepkg}>=${version}_${revision}"
	short_desc+=" LTS"
	metapackage=yes
}

nodejs-lts-devel_package() {
	depends="${sourcepkg}-devel>=${version}_${revision}"
	short_desc+=" LTS (development files)"
	metapackage=yes
}
