# packages/Python/auxbuild
#

# download and unpack tarball
TARVERSION="2.5.1"
TARDIR="${BUILD_PACKAGE}-${TARVERSION}"
TARBALL="${TARDIR}.tar.bz2"
build_download "http://www.python.org/ftp/python/${TARVERSION}/${TARBALL}"
build_unpack "${TARBALL}"
PKGDIR="${TARDIR}"

# apply patches matching pattern (repository, patch prefix)
build_patch "${PKGDIR}" "${BUILD_PACKAGE}-ALL-"
build_patch "${PKGDIR}" "${BUILD_PACKAGE}-${TARVERSION}-"

# so we get symlinks, not hardlinks
export LN="ln -s"

# kill these modules
export PYTHON_DISABLE_MODULES="gdbm dbm bsddb _tkinter _sqlite3"

# build the host tools
#  http://whatschrisdoing.com/blog/2006/10/06/howto-cross-compile-python-25/
#  http://www.ailis.de/~k/archives/19-ARM-cross-compiling-howto.html
#  also using Gentoo's 2.5.1-r3 patchset
(
    echo "Building local host tools..."
    cd "Distn/${PKGDIR}"

    # this comes from the Gentoo patchset, but we don't need multilib support
    sed -i -e "s:@@GENTOO_LIBDIR@@:lib:g" \
        Lib/distutils/command/install.py \
        Lib/distutils/sysconfig.py \
        Lib/site.py \
        Makefile.pre.in \
        Modules/Setup.dist \
        Modules/getpath.c \
        setup.py || die

    ./configure
    make python Parser/pgen
    mv python hostpython
    mv Parser/pgen Parser/hostpgen
    make distclean
)
if [ $? -ne 0 ]
then
    echo "Building host Python tools failed"
    exit 1
fi

# invoke configure (directory, prefix, extra arguments)
build_autoconf "${PKGDIR}" "" "--with-fpectl --enable-shared --enable-ipv6"

# run make, make install (directory, extra arguments)
build_make "${PKGDIR}" "HOSTPYTHON=hostpython" "HOSTPGEN=Parser/hostpgen"
build_make "${PKGDIR}" "HOSTPYTHON=hostpython" "HOSTPGEN=Parser/hostpgen" "install"

# kill dangling symlink of a config script (see static config script)
rm "${BUILDROOT}/usr/bin/python-config"

# copy across static content
cp -a static/* "${BUILDROOT}"

# now tidy up (~35Mb -> ~10Mb), since we really can't justify doubling the size of our root fs 
# solely for Python
chmod 0755 "${BUILDROOT}/usr/lib/"libpython*
"${ARCH_CC_PREFIX}strip" "${BUILDROOT}/usr/lib/"libpython*
find "${BUILDROOT}/usr/lib/python2.5" -name '*.so' -exec "${ARCH_CC_PREFIX}strip" {} \;
rm -rf "${BUILDROOT}/usr/lib/python2.5/test" # 10.5MiB
rm -rf "${BUILDROOT}/usr/lib/python2.5/lib-tk" # 431kiB
rm -rf "${BUILDROOT}/usr/lib/python2.5/idlelib" # 895kiB
rm -rf "${BUILDROOT}/usr/lib/python2.5/sqlite3" # 106kiB (lib not in our image)
rm -f "${BUILDROOT}/usr/lib/python2.5/lib-dynload/_bsddb.so" # (lib not in our image)

# remove uncompiled and stripped python sources, leaving only compiled
find "${BUILDROOT}/usr/lib/python2.5" -name '*.py' -exec rm {} \; # 7.6MiB
find "${BUILDROOT}/usr/lib/python2.5" -name '*.pyo' -exec rm {} \;

true
# vim: ts=4:sw=4:expandtab:syntax=sh
