pkgname=python
pkgver=3.13.7
_pybasever=${pkgver%.*}
pkgdesc="The Python programming language"
license=('PSF-2.0')
homepage="https://www.python.org/"
sources=("Python-${pkgver}.tar.xz"
  "${pkgname}-${pkgver}-docs-html.tar.bz2"
)
urls=("https://www.python.org/ftp/${pkgname}/${pkgver}/${sources[0]}"
  "https://www.python.org/ftp/${pkgname}/doc/${pkgver}/${sources[1]}"
)
md5sums=("256cdb3bbf45cdce7499e52ba6c36ea3"
  "b84c0d81b2758398bb7f5b7411d3d908"
)

src_prepare() {
  tar -xf ${distdir}/${sources[0]} --strip-components=1
  tar --no-same-owner -xf ${distdir}/${sources[1]}
}

src_build() {
  # PGO should be done with -O3
  CFLAGS="${CFLAGS/-O2/-O3}"

  ./configure --prefix=/usr        \
              --enable-shared      \
              --with-system-expat  \
              --without-ensurepip  \
              --enable-optimizations
  make
}

src_check() {
  LD_LIBRARY_PATH="${srcdir}":${LD_LIBRARY_PATH}\
  make test TESTOPTS="--timeout 120"
}

src_install() {
  # PGO should be done with -O3
  CFLAGS="${CFLAGS/-O2/-O3} -ffat-lto-objects"

  make DESTDIR="${pkgdir}" EXTRA_CFLAGS="$CFLAGS" install

  ln -s python3               "${pkgdir}"/usr/bin/python
  ln -s python3-config        "${pkgdir}"/usr/bin/python-config
  ln -s idle3                 "${pkgdir}"/usr/bin/idle
  ln -s pydoc3                "${pkgdir}"/usr/bin/pydoc
  ln -s python${_pybasever}.1 "${pkgdir}"/usr/share/man/man1/python.1

  # doc
  install -v -dm755 "${pkgdir}/usr/share/doc/python-${pkgver}/html"
  cp -R --no-preserve=mode python-${pkgver}-docs-html/* \
    "${pkgdir}/usr/share/doc/python-${pkgver}/html"
}
