# Template file for 'python3.11'
pkgname=python3.11
version=3.11.11
revision=1
build_style="gnu-configure"
configure_args="--enable-shared --enable-ipv6 --enable-optimizations
 --enable-loadable-sqlite-extensions --with-computed-gotos
 --with-dbmliborder=gdbm:ndbm --with-system-expat --with-system-ffi
 --without-ensurepip ac_cv_working_tzset=yes"
pycompile_dirs="usr/lib/python${version%.*}"
hostmakedepends="pkgconf"
makedepends="libffi-devel readline-devel gdbm-devel openssl-devel
 expat-devel sqlite-devel bzip2-devel zlib-devel liblzma-devel"
depends="ca-certificates"
checkdepends="$depends iana-etc"
short_desc="Python ${version%.*} (limited install; not for regular use)"
maintainer="Andrew J. Hesford <ajh@sideband.org>"
license="Python-2.0"
homepage="https://www.python.org"
_bluez="bluez-5.64"
distfiles="
 https://www.python.org/ftp/python/${version%rc*}/Python-${version}.tar.xz
 ${KERNEL_SITE}/bluetooth/${_bluez}.tar.xz
"
checksum="2a9920c7a0cd236de33644ed980a13cbbc21058bfdc528febb6081575ed73be3
 ae437e65b6b3070c198bc5b0109fe9cdeb9eaa387380e2072f9de65fe8a1de34"
skip_extraction="${_bluez}.tar.xz"

if [ "$CROSS_BUILD" ]; then
	hostmakedepends+=" python3.11"
	configure_args+=" --with-build-python=python${version%.*}"
	configure_args+=" ac_cv_broken_sem_getvalue=no"
fi

post_extract() {
	# Ensure that the internal copy of expat is not used
	rm -r Modules/expat

	# Extract Bluetooth headers
	local _srcdistdir="${XBPS_SRCDISTDIR}/${sourcepkg}-${version}"
	mkdir External
	bsdtar -C External --strip-components=1 -x \
		-f "${_srcdistdir}/${_bluez}.tar.xz" "${_bluez}/lib/*.h"
	mv External/lib External/bluetooth
}

post_patch() {
	if [ -n "$CROSS_BUILD" ]; then
		patch -Np1 -i ${FILESDIR}/cross.patch
	fi
}

do_configure() {
	# If these are set, distutils will default to cc and attempt to pass
	# `-R<path>` to the compiler for every compiled extension that sets an
	# rpath rather than the '-Wl,-R<path>' argument that GCC requires
	unset GCC CC CXX CPP LD AR AS RANLIB

	# Make sure configure can find Bluetooth headers
	CFLAGS+=" -I./External"
	CPPFLAGS+=" -I./External"
	./configure ${configure_args}
}

do_check() {
	local opts

	# relies on sane group membership not found in xbps-src
	opts="-i test_chown_*"
	# expects shadow passwd db unreadable by user
	opts+=" -i test_getspnam_exception"
	# expects functionality patched out for musl
	opts+=" -i test_find_library_with_*"
	# SSL advertises unexpected ciphers
	opts+=" -i test_shared_ciphers"
	# requires in-tree expat, which we removed
	opts+=" -i test_freeze_simple_script"
	# anomalies in SSL session handling
	opts+=" -i test_session*"
	# overflow in datetime.time.mktime
	opts+=" -i test_localtime_daylight_*_dst_true"
	# https://github.com/python/cpython/issues/115133
	opts+=" -i test_simple_xml"

	if [ "${XBPS_TARGET_LIBC}" = "musl" ]; then
		# musl doesn't work with locales
		opts+=" -i test_locale* -i test_c_locale* -i test__locale"
		opts+=" -i test_fpathconf"
	else
		# ctypes libm imports misbehave with glibc linker scripts
		opts+=" -i test_issue18060_*"
	fi

	make ${makejobs} EXTRATESTOPTS="${opts}" quicktest
}

do_install() {
	make DESTDIR=${DESTDIR} install maninstall

	# Put the license in the usual location and remove from the libdir
	# to prevent the XBPS trigger from attempting to byte-compile it
	vlicense LICENSE
	rm ${DESTDIR}/usr/lib/python${version%.*}/LICENSE.txt

	# Remove major-only components that conflict with python3 packages
	rm ${DESTDIR}/usr/bin/{pydoc3,python3,2to3,python3-config}
	rm ${DESTDIR}/usr/lib/libpython3.so
	rm ${DESTDIR}/usr/lib/pkgconfig/python3{,-embed}.pc
	rm ${DESTDIR}/usr/share/man/man1/python3.1

	# Remove files that belong to idle
	rm ${DESTDIR}/usr/bin/idle3*
	rm -r ${DESTDIR}/usr/lib/python${version%.*}/idlelib

	# Remove test module and tests that fail to be byte-compiled.
	rm -r ${DESTDIR}/usr/lib/python${version%.*}/test
	rm -r ${DESTDIR}/usr/lib/python${version%.*}/lib2to3/tests

	# Remove references to the install(1) wrapper.
	sed -e "s,${XBPS_WRAPPERDIR},/usr/bin,g" -i \
		${DESTDIR}/usr/lib/python${version%.*}/_sysconfigdata_*_*.py \
		${DESTDIR}/usr/lib/python${version%.*}/config-${version%.*}*/Makefile

	if [ "$CROSS_BUILD" ]; then
		# Remove references to cross toolchain.
		sed -i "s/$XBPS_CROSS_TRIPLET-//g" \
			${DESTDIR}/usr/lib/python${version%.*}/_sysconfigdata_*_*.py \
			${DESTDIR}/usr/lib/python${version%.*}/config-${version%.*}*/Makefile
		sed -i -e "s,$XBPS_CROSS_BASE,,g" \
			${DESTDIR}/usr/bin/python${version%.*}-config
	fi

	# https://peps.python.org/pep-0668/
	vinstall ${FILESDIR}/EXTERNALLY-MANAGED 644 usr/lib/python${version%.*}
}

python3.11-devel_package() {
	short_desc+=" - development files"
	depends="${sourcepkg}-${version}_${revision}"
	pkg_install() {
		vmove usr/bin/python*-config
		vmove usr/lib/pkgconfig
		vmove usr/include
		mv ${DESTDIR}/usr/lib/python${version%.*}/config-${version%.*}*/libpython${version%.*}*.a \
			${PKGDESTDIR}/usr/lib
		mkdir -p ${DESTDIR}/usr/include/python${version%.*}
		mv ${PKGDESTDIR}/usr/include/python${version%.*}/pyconfig.h \
			${DESTDIR}/usr/include/python${version%.*}
	}
}
