# Template file for 'python'
#
# THIS PKG MUST BE SYNCHRONIZED WITH "srcpkgs/python-tkinter".
#
pkgname=python
version=2.7.15
revision=2
wrksrc="Python-${version}"
hostmakedepends="pkg-config"
makedepends="libffi-devel readline-devel gdbm-devel libressl-devel expat-devel
 sqlite-devel bzip2-devel zlib-devel"
depends="ca-certificates"
pycompile_dirs="usr/lib/python2.7"
short_desc="Interpreted, interactive, object-oriented programming language"
maintainer="Juan RP <xtraeme@voidlinux.eu>"
homepage="https://www.python.org"
license="Python-2.0"
distfiles="https://www.python.org/ftp/python/${version}/Python-${version}.tar.xz"
checksum=22d9b1ac5b26135ad2b8c2901a9413537e08749a753356ee913c84dbd2df5574

alternatives="
 python:idle:/usr/bin/idle2.7
 python:2to3:/usr/bin/2to3-2.7
 python:pydoc:/usr/bin/pydoc2.7
 python:python:/usr/bin/python2.7
 python:python.1:/usr/share/man/man1/python2.7.1"

pre_configure() {
	# Ensure that internal copies of expat, libffi and zlib are not used.
	rm -r Modules/expat
	rm -r Modules/_ctypes/libffi*
	rm -r Modules/zlib
	# Enable loading sqlite extensions.
	sed -i '/SQLITE_OMIT_LOAD_EXTENSION/d' setup.py
}

do_configure() {
	local _args
	unset GCC CC CXX CPP LD AR AS RANLIB
	if [ "$CROSS_BUILD" ]; then
		mkdir -p host-build
		cd host-build
		env -i PATH=$PATH ../configure
		env -i PATH=$PATH make ${makejobs} python
		mkdir -p ../hostpython
		mv python ../hostpython
		cd ..
		patch -Np0 -i ${FILESDIR}/cross.patch
		_args="--build=${XBPS_MACHINE%%-musl}"
	fi
	./configure ${configure_args} ${_args} \
		--enable-shared --enable-ipv6 --with-threads --enable-unicode=ucs4 \
		--with-computed-gotos --with-wctype-functions --with-dbmliborder=gdbm:ndbm \
		--with-system-expat --with-system-ffi --without-ensurepip
}

do_build() {
	export PATH="$PATH:$wrksrc/hostpython"
	make ${makejobs}
}

do_install() {
	export PATH="$PATH:$wrksrc/hostpython"
	make DESTDIR=${DESTDIR} altinstall
	ln -sf pydoc2.7 ${DESTDIR}/usr/bin/pydoc2
	ln -sf python2.7 ${DESTDIR}/usr/bin/python2
	ln -sf python2.7-config ${DESTDIR}/usr/bin/python-config
	ln -sf python2.7-config ${DESTDIR}/usr/bin/python2-config
	ln -sf python2.7.1 ${DESTDIR}/usr/share/man/man1/python2.1
	ln -sf python-2.7.pc ${DESTDIR}/usr/lib/pkgconfig/python2.pc
}

post_install() {
	chmod 755 ${DESTDIR}/usr/lib/libpython*.so*
	vlicense LICENSE
	rm -f ${DESTDIR}/usr/bin/smtpd.py
	mv ${DESTDIR}/usr/bin/2to3{,-2.7}
	mv ${DESTDIR}/usr/bin/pydoc{,2.7}
	# Remove files that belong to python-tkinter and idle-python
	rm ${DESTDIR}/usr/bin/idle
	rm -r ${DESTDIR}/usr/lib/python2.7/idlelib
	rm -r ${DESTDIR}/usr/lib/python2.7/lib-tk
	# Remove test module and tests that fail to be byte-compiled.
	rm -r ${DESTDIR}/usr/lib/python2.7/test
	rm -r ${DESTDIR}/usr/lib/python2.7/lib2to3/tests
	# Remove references to hardening -specs.
	sed -e 's|\(-specs=.*hardened-ld\)||g' -e 's|\(-specs=.*hardened-cc1\)||g' -i \
		${DESTDIR}/usr/lib/python${version%.*}/_sysconfigdata.py \
		${DESTDIR}/usr/lib/python${version%.*}/config/Makefile
	# 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/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/Makefile
	fi
}

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