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

pkgbase=msys2-runtime
pkgname=('msys2-runtime' 'msys2-runtime-devel')
pkgver=3.5.3
pkgrel=5
pkgdesc="Cygwin POSIX emulation engine"
arch=('x86_64')
url="https://www.cygwin.com/"
license=('GPL')
msys2_references=(
  'cygwin: cygwin'
  "cpe: cpe:/a:cygwin:cygwin"
)
makedepends=('cocom'
             'git'
             'perl'
             'gcc'
             'mingw-w64-cross-crt'
             'mingw-w64-cross-gcc'
             'mingw-w64-cross-zlib'
             'zlib-devel'
             'gettext-devel'
             'libiconv-devel'
             'autotools'
             'xmlto'
             'docbook-xsl')
# re zipman: https://github.com/msys2/MSYS2-packages/pull/2687#issuecomment-965714874
options=('!zipman')
source=('msys2-runtime'::git://sourceware.org/git/newlib-cygwin.git#tag=cygwin-${pkgver}
        0001-Add-MSYS2-triplet.patch
        0002-Fix-msys-library-name-in-import-libraries.patch
        0003-Rename-dll-from-cygwin-to-msys.patch
        0004-Add-functionality-for-converting-UNIX-paths-in-argum.patch
        0005-Add-functionality-for-changing-OS-name-via-MSYSTEM-e.patch
        0006-Move-root-to-usr.-Change-sorting-mount-points.-By-de.patch
        0007-Instead-of-creating-Cygwin-symlinks-use-deep-copy-by.patch
        0008-Automatically-rewrite-TERM-msys-to-TERM-cygwin.patch
        0009-Do-not-convert-environment-for-strace.patch
        0010-strace.cc-Don-t-set-MSYS-noglob.patch
        0011-Add-debugging-for-strace-make_command_line.patch
        0012-strace-quiet-be-really-quiet.patch
        0013-path_conv-special-case-root-directory-to-have-traili.patch
        0014-When-converting-to-a-Unix-path-avoid-double-trailing.patch
        0015-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch
        0016-path-conversion-Introduce-ability-to-switch-off-conv.patch
        0017-dcrt0.cc-Untangle-allow_glob-from-winshell.patch
        0018-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch
        0019-Add-debugging-for-build_argv.patch
        0020-environ.cc-New-facility-environment-variable-MSYS2_E.patch
        0021-Fix-native-symbolic-link-spawn-passing-wrong-arg0.patch
        0022-Introduce-the-enable_pcon-value-for-MSYS.patch
        0023-popen-call-usr-bin-sh-instead-of-bin-sh.patch
        0024-Expose-full-command-lines-to-other-Win32-processes-b.patch
        0025-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch
        0026-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch
        0027-kill-kill-Win32-processes-more-gently.patch
        0028-Cygwin-make-option-for-native-inner-link-handling.patch
        0029-docs-skip-building-texinfo-and-PDF-files.patch
        0030-install-libs-depend-on-the-toollibs.patch
        0031-POSIX-ify-the-SHELL-variable.patch
        0032-Handle-ORIGINAL_PATH-just-like-PATH.patch
        0033-uname-allow-setting-the-system-name-to-CYGWIN.patch
        0034-Pass-environment-variables-with-empty-values.patch
        0035-Optionally-disallow-empty-environment-values-again.patch
        0036-build_env-respect-the-MSYS-environment-variable.patch
        0037-Revert-Cygwin-Enable-dynamicbase-on-the-Cygwin-DLL-b.patch
        0038-proc-fix-error-the-address-of-iso15924-will-never-be.patch
        0039-Cygwin-disable-high-entropy-VA-for-ldh.patch
        0040-Avoid-sharing-cygheaps-across-Cygwin-versions.patch
        0041-uname-report-msys2-runtime-commit-hash-too.patch)
sha256sums=('e395966060e0cf901f3cd4190b0b189bface69a2fec6ba93748387b61aa7dd5c'
            'a0de51b420ad7fc67b007adf4159e065a5beefc56e66735827fc9f39364e045f'
            '857830ffd5fe820e90e0f3b49b72c1f31c3c929be845e178eb3e6c7352181e18'
            '35723eb517f034508d09e0974e70db99a003aa863509bb8b69d3feec87e47fb9'
            '97ebf77b9d8977594c6b0cda1696ee3e6b7651bc44e1ab8feeea0ba6ba00296d'
            '4822b632fc9827468ab0bd69ef609168ee5909f16efc78f50572f4f82ef0242b'
            'f15fe4ac450ab59c9ae37fe4f1536961e4caa3b906ebf149512da6c34ec6f406'
            '691c8001ff79a8b814f8feb3b94956d49abb4f209e4ec87ea1da49435eb839af'
            '09a19369f97ef6de3c0b05667af125b6dbdae738815b495fc0010cff9dcedf83'
            '372575f8e7c1dbb942c5cf6f4b1598adf2079eb3474e0cd0de84777dfff9a101'
            '055121314086990ba31ba61d1c200cd084b4e27a2751530401c897d7621017e8'
            '65e2015054cdb54da4ee7b617b3d2578350ea5664ccdd055ebc635bb05c65ad4'
            '47f05d9720fc7607cd62758d1885627385afa0e143570a2a6366e97499370cec'
            'a68503a5ec629c0f67fdeb9d665f1542f24f33288a6338102f4b4e7b89977668'
            'e34d872cabe95124f6624efbd6f4683d78cd5fc36f35fd2ec8a86db8935691d6'
            '38f33b085a237737b35dd168213d98c3ca2c33fcace18d793fda338b5094c9f7'
            '7bcbec199fc009d986ba37e9b32cb491d4bc87fe6c2d0c35806072cf1ca113f4'
            '65816fd36ae04af58c1b6595a75600f0a3d8f92c8d76065be1759af8a84c9528'
            '174dd3a782a50d18c67954a5f6b7177259487319ab70833e9afefedd690a29b5'
            '5e0510cbe2043a348f554583ade18b1b6496c23ec459ff1ae0228377ae77432f'
            'c637342b6656c946901641170bb0b1adf470e63589ddeffa69d3b1c0f46d6b14'
            '8757f2f27e95c27a289a0d3e425c5ec33877dd01c3e27273cf73529836522b1c'
            '12bb20d78a9a88e548fa538e9dee1f0e7d0bc75909e2af73d3e61e8c268f10c4'
            '249ba3beb4a4b469e0812996b6684fb6c6f6542a056350c210f719a68da835f8'
            '81d9fe00d981ee75acd4f58a4cc344cb5da77bf948e09653b76e2106ed83d2ff'
            'd25286caaca6fb3597e4e1d44fe16d0a13a71c598bc8064637a3fdf16b4a3ec7'
            '35f761b27f73e78c5c363e2a33c035eb16f01d8a6a9fe0c4dc6a4f6f29da9afd'
            'ce2dfb9b194055d4383a4f38c11136cbf39cbc2bf55bb1a25afc9bb68ee15131'
            'ad7c77da4980742211e6c3d09a9b29cabf0cb1255c707836531f47f93267980c'
            'ff7093cead563b300df20e7505b0efbb8b1c67a01bacf20b13c7aae9e7f743f8'
            '5f25db396b9657c9a8b2165ebb102deec7279ce809ae6103d8e68b2e939c0c29'
            'fa69f5d4cf8baa42018dc0b34b0075440a104805c87b753cae12ef3c9fca6a4d'
            '3697ac83ab3e0226ca0d44c231d58111a50a1b5425a967ca7511d7891dfcda09'
            '485964804b19716615541151b0f89ec7ff8e433db00177b049c35eaeb3ac56f9'
            '65aebf69b7f2f5d19e6c8a666db3392416de39afcf21cd955b637984e8d30d8a'
            '39fd77a2968bd39fd13daea5cd3b30e1cf3aa714e24aa3408ef634f81d39467a'
            'd472029276932e7df3da56347d1d09d5bd422f9f96f6f607fe8a28362ebe4a1b'
            '4683b3dca58af0fef7f704b3bcb199da1ddbce874e5fc69bfaaced9cc76d6be9'
            '853c6c41da7e188811dae2b3df9251ce84dbb81b39c8632173556132fcbd96c1'
            'd038eec988844df56ac2ed183dddc73c3f4296c2606c818f2521d40eb2fa7500'
            'de9d29b6e889b64be316445529d03821473ad780e2950c72e1692975219483ae'
            '26effe81ff2405d444845b484520a50ac6bdb1625474151b01835ba814a51a64')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

apply_git_am_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    git apply "${srcdir}/${_patch}"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"/msys2-runtime
  if test true != "$(git config core.symlinks)"
  then
    git config core.symlinks true &&
    /usr/bin/git reset --hard
  fi
  del_file_exists winsup/cygwin/msys2_path_conv.cc \
    winsup/cygwin/msys2_path_conv.h
 apply_git_am_with_msg 0001-Add-MSYS2-triplet.patch \
  0002-Fix-msys-library-name-in-import-libraries.patch \
  0003-Rename-dll-from-cygwin-to-msys.patch \
  0004-Add-functionality-for-converting-UNIX-paths-in-argum.patch \
  0005-Add-functionality-for-changing-OS-name-via-MSYSTEM-e.patch \
  0006-Move-root-to-usr.-Change-sorting-mount-points.-By-de.patch \
  0007-Instead-of-creating-Cygwin-symlinks-use-deep-copy-by.patch \
  0008-Automatically-rewrite-TERM-msys-to-TERM-cygwin.patch \
  0009-Do-not-convert-environment-for-strace.patch \
  0010-strace.cc-Don-t-set-MSYS-noglob.patch \
  0011-Add-debugging-for-strace-make_command_line.patch \
  0012-strace-quiet-be-really-quiet.patch \
  0013-path_conv-special-case-root-directory-to-have-traili.patch \
  0014-When-converting-to-a-Unix-path-avoid-double-trailing.patch \
  0015-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch \
  0016-path-conversion-Introduce-ability-to-switch-off-conv.patch \
  0017-dcrt0.cc-Untangle-allow_glob-from-winshell.patch \
  0018-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch \
  0019-Add-debugging-for-build_argv.patch \
  0020-environ.cc-New-facility-environment-variable-MSYS2_E.patch \
  0021-Fix-native-symbolic-link-spawn-passing-wrong-arg0.patch \
  0022-Introduce-the-enable_pcon-value-for-MSYS.patch \
  0023-popen-call-usr-bin-sh-instead-of-bin-sh.patch \
  0024-Expose-full-command-lines-to-other-Win32-processes-b.patch \
  0025-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch \
  0026-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch \
  0027-kill-kill-Win32-processes-more-gently.patch \
  0028-Cygwin-make-option-for-native-inner-link-handling.patch \
  0029-docs-skip-building-texinfo-and-PDF-files.patch \
  0030-install-libs-depend-on-the-toollibs.patch \
  0031-POSIX-ify-the-SHELL-variable.patch \
  0032-Handle-ORIGINAL_PATH-just-like-PATH.patch \
  0033-uname-allow-setting-the-system-name-to-CYGWIN.patch \
  0034-Pass-environment-variables-with-empty-values.patch \
  0035-Optionally-disallow-empty-environment-values-again.patch \
  0036-build_env-respect-the-MSYS-environment-variable.patch \
  0037-Revert-Cygwin-Enable-dynamicbase-on-the-Cygwin-DLL-b.patch \
  0038-proc-fix-error-the-address-of-iso15924-will-never-be.patch \
  0039-Cygwin-disable-high-entropy-VA-for-ldh.patch \
  0040-Avoid-sharing-cygheaps-across-Cygwin-versions.patch \
  0041-uname-report-msys2-runtime-commit-hash-too.patch
}

build() {
  [[ -d "${srcdir}"/build-${CHOST} ]] && rm -rf "${srcdir}"/build-${CHOST}
  mkdir -p "${srcdir}"/build-${CHOST} && cd "${srcdir}"/build-${CHOST}

  # Gives more verbose compile output when debugging.
  local -a extra_config
  if check_option "debug" "y"; then
    export CCWRAP_VERBOSE=1
    OPTIM="-O0"
    extra_config+=(--enable-debugging)
  else
    OPTIM="-O2"
  fi

  CFLAGS="$OPTIM -pipe -ggdb"
  CXXFLAGS="$OPTIM -pipe -ggdb -Wno-error=stringop-truncation -Wno-error=array-bounds -Wno-error=overloaded-virtual -Wno-narrowing -Wno-use-after-free -Wno-error=maybe-uninitialized"

  # otherwise it asks git which appends "-dirty" because of our uncommited patches
  CFLAGS+=" -DCYGPORT_RELEASE_INFO=${pkgver}"

  (cd "${srcdir}/msys2-runtime/winsup" && ./autogen.sh)

  "${srcdir}"/msys2-runtime/configure \
    --prefix=/usr \
    --build=${CHOST} \
    --sysconfdir=/etc \
    "${extra_config[@]}"
  LC_ALL=C make
  LC_ALL=C make -j1 DESTDIR="${srcdir}"/dest install

  rm -rf "${srcdir}"/dest/etc

  # split debug info from msys-2.0.dll
  cd "${srcdir}"/dest
  objcopy --add-gnu-debuglink=/dev/null --only-keep-debug usr/bin/msys-2.0.dll usr/bin/msys-2.0.dbg
  objcopy -g --add-gnu-debuglink=usr/bin/msys-2.0.dbg usr/bin/msys-2.0.dll usr/bin/msys-2.0.dll.new
  mv -f usr/bin/msys-2.0.dll.new usr/bin/msys-2.0.dll
}

package_msys2-runtime() {
  pkgdesc="Posix emulation engine for Windows"
  conflicts=('catgets' 'libcatgets' 'msys2-runtime-3.4' 'msys2-runtime-3.5')
  replaces=('catgets' 'libcatgets' 'msys2-runtime-3.5')

  mkdir -p "${pkgdir}"/usr
  cp -rf "${srcdir}"/dest/usr/bin "${pkgdir}"/usr/
  cp -rf "${srcdir}"/dest/usr/libexec "${pkgdir}"/usr/
  rm -f "${pkgdir}"/usr/bin/msys-2.0.dbg
  rm -f "${pkgdir}"/usr/bin/cyglsa-config
  rm -f "${pkgdir}"/usr/bin/cyglsa.dll
  rm -f "${pkgdir}"/usr/bin/cyglsa64.dll
  rm -f "${pkgdir}"/usr/bin/cygserver-config
  cp -rf "${srcdir}"/dest/usr/share "${pkgdir}"/usr/
}

package_msys2-runtime-devel() {
  pkgdesc="MSYS2 headers and libraries"
  depends=("msys2-runtime=${pkgver}")
  conflicts=('libcatgets-devel' 'msys2-runtime-3.4-devel' 'msys2-runtime-3.5-devel')
  replaces=('libcatgets-devel' 'msys2-runtime-3.5-devel')
  # strip breaks the split debug info.  msys2/msys2-pacman#52
  options=('!strip')

  mkdir -p "${pkgdir}"/usr/bin
  cp -f "${srcdir}"/dest/usr/bin/msys-2.0.dbg "${pkgdir}"/usr/bin/
  cp -rLf "${srcdir}"/dest/usr/${CHOST}/include "${pkgdir}"/usr/
  rm -f "${pkgdir}"/usr/include/iconv.h
  rm -f "${pkgdir}"/usr/include/unctrl.h
  # provided by libtirpc
  rm -fr "${pkgdir}"/usr/include/rpc/

  cp -rLf "${srcdir}"/dest/usr/${CHOST}/lib "${pkgdir}"/usr/
}
