# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=setuptools
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=80.9.0
pkgrel=2
pkgdesc="Easily download, build, install, upgrade, and uninstall Python packages (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/pypa/setuptools"
msys2_references=(
  'purl: pkg:pypi/setuptools'
  "cpe: cpe:/a:python:setuptools"
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer")
if [[ ${CARCH} != i686 ]]; then
checkdepends=(
  "${MINGW_PACKAGE_PREFIX}-python-filelock"
  #"${MINGW_PACKAGE_PREFIX}-python-ini2toml"
  #"${MINGW_PACKAGE_PREFIX}-python-jaraco.develop"
  "${MINGW_PACKAGE_PREFIX}-python-jaraco.envs"
  "${MINGW_PACKAGE_PREFIX}-python-jaraco.path"
  #"${MINGW_PACKAGE_PREFIX}-python-jaraco.test"
  "${MINGW_PACKAGE_PREFIX}-python-packaging"
  "${MINGW_PACKAGE_PREFIX}-python-pip"
  "${MINGW_PACKAGE_PREFIX}-python-pyproject-hooks"
  "${MINGW_PACKAGE_PREFIX}-python-pytest"
  #"${MINGW_PACKAGE_PREFIX}-python-pytest-home"
  #"${MINGW_PACKAGE_PREFIX}-python-pytest-perf"
  "${MINGW_PACKAGE_PREFIX}-python-pytest-subprocess"
  "${MINGW_PACKAGE_PREFIX}-python-pytest-timeout"
  "${MINGW_PACKAGE_PREFIX}-python-pytest-xdist"
  "${MINGW_PACKAGE_PREFIX}-python-tomli-w"
  "${MINGW_PACKAGE_PREFIX}-python-virtualenv"
  "${MINGW_PACKAGE_PREFIX}-python-wheel"
  'git'
)
fi
options=(!strip)
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        '0002-Allow-usr-bin-env-in-script.patch'
        '0003-MinGW-w64-Look-in-same-dir-as-script-for-exe.patch'
        '0005-execv-warning.patch'
        '0006-fix-tests-path.patch'
        '0007-windows-arm64.patch'
        '0009-launcher-revert-symlink-resolve.patch'
        '0010-support-py_limited_api.patch'
        '0011-add-support-for-windmc.patch')
sha256sums=('f36b47402ecde768dbfafc46e8e4207b4360c654f1f3bb84475f0a28628fb19c'
            'fa54581e3dddb9f4edd332dedbc101f48939a9ca5878e13cf9cf9b3408bc8c22'
            '39503256652c7c23ce07e26539e8123d269eb5c09f5d9b07e5784b2d7fb8c96f'
            'a356b0663f67a296624d1b178b42437b380b48a4bbe05a560d3bf29e93a4c623'
            'dbdd96a7ead797b2db9f02dfe19ce853d3775337ccf8fd836377fe3c2a9d1c24'
            'fbf6903e88c0fe0b307365db997505c3d9f07184792aea1442616a3d97f037d9'
            '391d682a8b8832d7e4be3ac0c3327cfe3c8e520f16e25b43d4441df8036bf147'
            'ccb276f9d2d26587d29ecfd4d47a3d4e96fb58738cfaf3a600a8b942c79fc439'
            '5732980fe5d94f3371ccd0609b160e3b74f88ab71829d0e8b37f6039f4508b79')

prepare() {
  cd "setuptools-${pkgver}"

  patch -p1 -i "${srcdir}"/0002-Allow-usr-bin-env-in-script.patch
  patch -p1 -i "${srcdir}"/0003-MinGW-w64-Look-in-same-dir-as-script-for-exe.patch
  patch -p1 -i "${srcdir}"/0005-execv-warning.patch
  patch -p1 -i "${srcdir}"/0006-fix-tests-path.patch
  patch -p1 -i "${srcdir}"/0007-windows-arm64.patch

  # Partially reverts https://github.com/pypa/setuptools/commit/4bf6f127f4056c42dbcbc723ecf40dbb91569f7f
  # GetFinalPathNameByHandle converts the script path to a length-extended path (starting with \\?\)
  # and our Python can't deal with that for some reason
  # (from what I see the launcher code itself deals with it fine)
  patch -R -p1 -i "${srcdir}"/0009-launcher-revert-symlink-resolve.patch

  # https://github.com/msys2-contrib/cpython-mingw/issues/149
  patch -p1 -i "${srcdir}"/0010-support-py_limited_api.patch

  # From https://github.com/msys2-contrib/cpython-mingw/commit/b96319d3d9384c6facb2fda1c82dcd1a1b9564e3
  # Needed for building pywin32
  patch -p1 -i "${srcdir}"/0011-add-support-for-windmc.patch

  # https://github.com/pypa/setuptools/pull/5047
  mv "setuptools/command/launcher manifest.xml" "setuptools"

  cd "${srcdir}"
  rm -rf python-build-${MSYSTEM} | true
  cp -r "${_realname}-$pkgver" "python-build-${MSYSTEM}"
}

build() {
  cd "python-build-${MSYSTEM}"

  rm -f setuptools/{gui,cli}{-32,-64,-arm64,}.exe
  local _gui_args="$CFLAGS -DGUI=1 -mwindows"
  local _cli_args="$CFLAGS -DGUI=0"
  if check_option "strip" "y"; then
    _gui_args+=" -s"
    _cli_args+=" -s"
  fi
  case "${MINGW_CHOST}" in
    i686-w64-mingw32)
      cc $_cli_args -o setuptools/cli-32.exe launcher.c
      cc $_gui_args -o setuptools/gui-32.exe launcher.c
      # In theory these are not used, but upstream has 32bit launchers
      # under both names, so we keep them for compatibility
      cp setuptools/cli-32.exe setuptools/cli.exe
      cp setuptools/gui-32.exe setuptools/gui.exe
      ;;
    x86_64-w64-mingw32)
      cc $_cli_args -o setuptools/cli-64.exe launcher.c
      cc $_gui_args -o setuptools/gui-64.exe launcher.c
      ;;
    aarch64-w64-mingw32)
      cc $_cli_args -o setuptools/cli-arm64.exe launcher.c
      cc $_gui_args -o setuptools/gui-arm64.exe launcher.c
      ;;
    *)
      echo "Unsupported CHOST ${MINGW_CHOST}" && false
      ;;
  esac

  PYTHONPATH="${srcdir}/python-build-${MSYSTEM}" \
    python -m build --wheel --skip-dependency-check --no-isolation
}

check() {
  cd "python-build-${MSYSTEM}"

  python -m pytest || true
}

package() {
  cd "python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
      --destdir="${pkgdir}" dist/*.whl

  install -Dm644 LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE
}
