# Description: Python interpreter, version 3.
# URL: https://www.python.org/
# Maintainer: CRUX MUSL Team, crux-musl at libera dot chat
# Depends on: bzip2 expat gdbm libffi libnsl libtirpc mpdecimal sqlite3 xz

name=python3
version=3.12.13
release=1
source=(https://www.python.org/ftp/python/${version%rc*}/Python-$version.tar.xz
    fix-run_fileexflags-test.patch
    musl-find_library.patch
    test_posix-nodev-disable.patch
    musl-skip-tests.patch)

build() {
    # Stack patch from Alpine:
    # set thread stack size to 2MB so we don't segfault before we hit
    # sys.getrecursionlimit()
    # note: raised from 1 as we ran into some stack limit on x86_64 too
    # sometimes, but not recursion
    local stacksize=0x200000

    cd Python-$version

    # Apply Alpine patches
    patch -Np1 -i "$SRC"/fix-run_fileexflags-test.patch
    patch -Np1 -i "$SRC"/musl-find_library.patch
    patch -Np1 -i "$SRC"/test_posix-nodev-disable.patch
    # Apply Crux patch
    patch -Np1 -i "$SRC"/musl-skip-tests.patch

    # From Alpine:
    # Force system libs
    rm -r Modules/expat

    # sed -i -e "s|-flto |-flto=4 |g" configure configure.ac
    sed -i -e "s|^#.* /usr/local/bin/python|#!/usr/bin/python3|" Lib/cgi.py

    # From Alpine:
    export CFLAGS_NODIST="$CFLAGS -O2 -DTHREAD_STACK_SIZE=$stacksize"
    export CXXFLAGS_NODIST="$CXXFLAGS -O2"
    export LDFLAGS_NODIST="$LDFLAGS -Wl,-z,stack-size=$stacksize"

    # sed -i -e "s:-j0:-j$(nproc):" Makefile.pre.in
    # local _pgojobs=$(nproc)
    # export PROFILE_TASK="-m test -j${_pgojobs} --pgo-extended -x test_gdb -u-network"
    # PROFILE_TASK+=" -x test_socket -x test_asyncio -x test_httpservers -x test_logging -x test_multiprocessing_fork -x test_xmlrpc"
    # local -x LC_ALL=C.UTF-8
    # export SETUPTOOLS_USE_DISTUTILS=stdlib
    # local -x COLUMNS=80
    # local -x PYTHONDONTWRITEBYTECODE=

    ## Remove tests
    #rm -r Lib/{test,{ctypes,tkinter,unittest}/test}
    #rm -r Lib/{{distutils,lib2to3}/tests,idlelib/idle_test}

    # From Alpine:
    # we set them via NODIST to not propagate them and duplicate them to modules
    unset LDFLAGS CFLAGS CXXFLAGS CPPFLAGS

    # From Void:
    # 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

    ./configure \
        --prefix=/usr \
        --enable-shared \
        --enable-optimizations \
        --enable-ipv6 \
        --enable-loadable-sqlite-extensions \
        --with-computed-gotos \
        --with-dbmliborder=gdbm:ndbm \
        --with-lto \
        --with-system-expat \
        --with-system-ffi \
        --with-system-libmpdec \
        --with-tzpath=/usr/share/zoneinfo \
        --without-ensurepip

    make
    make -j1 EXTRA_CFLAGS="$CFLAGS" DESTDIR="$PKG" altinstall maninstall

    ln -s 2to3-${version%.*}                        "$PKG"/usr/bin/2to3
    ln -s python${version%.*}                     "$PKG"/usr/bin/python3
    ln -s python${version%.*}-config        "$PKG"/usr/bin/python3-config
    ln -s idle${version%.*}                         "$PKG"/usr/bin/idle3
    ln -s pydoc${version%.*}                        "$PKG"/usr/bin/pydoc3
    ln -s python${version%.*}                     "$PKG"/usr/lib/$name
    ln -s python-${version%.*}.pc             "$PKG"/usr/lib/pkgconfig/python3.pc
    ln -s python-${version%.*}-embed.pc "$PKG"/usr/lib/pkgconfig/python3-embed.pc

    _pybasever=${version%.*}
    install -dm755 "$PKG"/usr/lib/python${_pybasever}/Tools/{i18n,scripts}
    install -m755 Tools/i18n/{msgfmt,pygettext}.py \
        "$PKG"/usr/lib/python${_pybasever}/Tools/i18n/
    install -m755 Tools/scripts/{README,*py} \
        "$PKG"/usr/lib/python${_pybasever}/Tools/scripts/

    find "$PKG"/usr/lib/python${version%.*} \( \
        -iname "*README*" -o \
        -iname "*ChangeLog*" -o \
        -iname "*NEWS*" -o \
        -iname "*TODO*" -o \
        -iname "*CREDITS*" -o \
        -iname "*HISTORY*" \) \
        -type f -delete
}
