# Template file for 'perl-bootstrap'
# This is needed to prevent build cycle between libxcrypt and perl
pkgname=perl-bootstrap
version=5.38.2
revision=2
_perl_cross_version=1.5.2
build_style=gnu-configure
makedepends="zlib-devel bzip2-devel gdbm-devel db-devel"
checkdepends="iana-etc perl-AnyEvent perl-Test-Pod procps-ng"
short_desc="Practical Extraction and Report Language"
maintainer="Leah Neukirchen <leah@vuxu.org>"
license="Artistic-1.0-Perl, GPL-1.0-or-later"
homepage="https://www.perl.org"
distfiles="https://www.cpan.org/src/5.0/perl-${version}.tar.gz
 https://github.com/arsv/perl-cross/releases/download/${_perl_cross_version}/perl-cross-${_perl_cross_version}.tar.gz"
checksum="a0a31534451eb7b83c7d6594a497543a54d488bc90ca00f5e34762577f40655e
 584dc54c48dca25e032b676a15bef377c1fed9de318b4fc140292a5dbf326e90"
repository=bootstrap
conflicts="perl>=0"
skiprdeps="/usr/bin/perl"
noverifyrdeps=yes

skip_extraction=perl-cross-${_perl_cross_version}.tar.gz

post_extract() {
	local TAR=$(command -v bsdtar || command -v tar)
	$TAR --strip-components=1 -xf \
		${XBPS_SRCDISTDIR}/${pkgname}-${version}/perl-cross-$_perl_cross_version.tar.gz

	[ -n "${SOURCE_DATE_EPOCH}" ] && touch -h -d @$SOURCE_DATE_EPOCH patchlevel.h
}

do_configure() {
	local _args="-Dusethreads -Duseshrplib -Dusesoname -Dusevendorprefix
		-Dprefix=/usr -Dvendorprefix=/usr
		-Dprivlib=/usr/share/perl5/core_perl
		-Darchlib=/usr/lib/perl5/core_perl
		-Dsitelib=/usr/share/perl5/site_perl
		-Dsitearch=/usr/lib/perl5/site_perl
		-Dvendorlib=/usr/share/perl5/vendor_perl
		-Dvendorarch=/usr/lib/perl5/vendor_perl
		-Dscriptdir=/usr/bin -Dvendorscript=/usr/bin
		-Dinc_version_list=none -Dman1ext=1p -Dman3ext=3p
		-Dman1dir=/usr/share/man/man1
		-Dman3dir=/usr/share/man/man3
		-Dd_sockaddr_in6=define
		-Dd_crypt=undef"

	if [ "$CROSS_BUILD" ]; then
		_args+=" --target=$XBPS_CROSS_TRIPLET"
	fi

	LDFLAGS+=" -Wl,-z,stack-size=2097152 -pthread"
	export HOSTLDFLAGS+=" -pthread"

	if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
		export HOSTCFLAGS+=" -D_GNU_SOURCE"
		export CFLAGS+=" -DNO_POSIX_2008_LOCALE -D_GNU_SOURCE"
	else
		export CFLAGS+=" -D_FILE_OFFSET_BITS=64 -DLARGE_FILE_SUPPORT64"
	fi

	export LD="$CC"
	./configure --prefix=/usr $_args \
		-Dcccdlflags="-fPIC" \
		-Doptimize=" -Wall ${CFLAGS} " -Dccflags=" ${CFLAGS} " \
		-Dlddlflags="-shared ${LDFLAGS}" -Dldflags="${LDFLAGS}" \
		-Dperl_static_inline='static __inline__' -Dd_static_inline ||
	{
		cat ./config.log
		return 42
	}
}

do_check() {
	export LD="$CC"
	export PERL_BUILD_PACKAGING=1
	TEST_JOBS=${XBPS_MAKEJOBS} make test
}

post_install() {
	find ${DESTDIR}/usr/share -type f -exec chmod 644 {} \;
	find ${DESTDIR}/usr/lib -type f -exec chmod 644 {} \;

	# Make a symbolic link from perl to perl${version}.
	ln -sf perl ${DESTDIR}/usr/bin/perl${version}

	# Remove all pod files *except* those under
	# /usr/share/perl5/core_perl/pod/ (FS#16488)
	rm -f ${DESTDIR}/usr/share/perl5/core_perl/*.pod
	for d in ${DESTDIR}/usr/share/perl5/core_perl/*; do
		if [ -d $d -a $(basename $d) != "pod" ]; then
			find $d -name *.pod -delete
		fi
	done
	find ${DESTDIR}/usr/lib -name *.pod -delete
	find ${DESTDIR} -name .packlist -delete

	# Remove references to hardening -specs.
	sed -e "s|-specs=.*hardened-ld||g" -e "s|-specs=.*hardened-cc1||g" \
		-i ${DESTDIR}/usr/lib/perl5/core_perl/Config_heavy.pl

	# HOTFIX
	# ln -sfr ${DESTDIR}/usr/lib/perl5/core_perl/CORE/libperl.so.${version} ${DESTDIR}/usr/lib/
	# ln -sfr ${DESTDIR}/usr/lib/perl5/core_perl/CORE/libperl.so.${version} ${DESTDIR}/usr/lib/libperl.so.
}
