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

pkgbase=msys2-runtime-3.3
pkgname=('msys2-runtime-3.3' 'msys2-runtime-3.3-devel')
pkgver=3.3.6
pkgrel=9
pkgdesc="Cygwin POSIX emulation engine"
arch=('i686' 'x86_64')
url="https://www.cygwin.com/"
license=('GPL')
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//./_}-release
        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-With-.patch
        0009-Do-not-convert-environment-for-strace.patch
        0010-Special-case-for-converting-root-directory-to-have-t.patch
        0011-dcrt0.cc-Untangle-allow_glob-from-winshell.patch
        0012-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch
        0013-strace.cc-Don-t-set-MSYS-noglob.patch
        0014-Add-debugging-for-build_argv.patch
        0015-Add-debugging-for-strace-make_command_line.patch
        0016-environ.cc-New-facility-environment-variable-MSYS2_E.patch
        0017-Fix-native-symbolic-link-spawn-passing-wrong-arg0.patch
        0018-QueryUnbiasedInterruptTime-must-be-load-from-kernel3.patch
        0019-strace-quiet-be-really-quiet.patch
        0020-Default-to-disable_pcon.patch
        0021-Introduce-the-enable_pcon-value-for-MSYS.patch
        0022-popen-call-usr-bin-sh-instead-of-bin-sh.patch
        0023-CI-add-a-GHA-for-doing-a-basic-build-test.patch
        0024-Set-up-a-GitHub-Action-to-keep-in-sync-with-Cygwin.patch
        0025-Expose-full-command-lines-to-other-Win32-processes-b.patch
        0026-Disable-the-cygwin-GitHub-workflow.patch
        0027-Do-not-show-Error-dialogs-by-default.patch
        0028-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch
        0029-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch
        0030-kill-kill-Win32-processes-more-gently.patch
        0031-Cygwin-make-option-for-native-inner-link-handling.patch
        0032-docs-skip-building-texinfo-and-PDF-files.patch
        0033-install-libs-depend-on-the-toollibs.patch
        0034-POSIX-ify-the-SHELL-variable.patch
        0035-Handle-ORIGINAL_PATH-just-like-PATH.patch
        0036-uname-allow-setting-the-system-name-to-CYGWIN.patch
        0037-fixup-Revert-Default-to-disable_pcon.patch
        0038-Pass-environment-variables-with-empty-values.patch
        0039-Optionally-disallow-empty-environment-values-again.patch
        0040-build_env-respect-the-MSYS-environment-variable.patch
        0041-Cygwin-pty-Fix-Bad-address-error-when-running-cmd.ex.patch
        0042-ci-avoid-using-Node.js-12-Actions.patch
        0043-When-converting-to-a-Unix-path-avoid-double-trailing.patch
        0044-amend-Special-case-for-converting-root-directory-to-.patch
        0045-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch
        0046-Cygwin-Implicitly-support-the-dev-fd-symlink-and-fri.patch
        0047-dumper-avoid-linker-problem-when-libbfd-depends-on-l.patch
        0048-CI-build-with-disable-dependency-tracking.patch
        0049-Stop-assuming-that-there-are-no-spaces-in-POSIX-styl.patch
        0050-Cygwin-Fix-compiling-with-w32api-headers-v11.0.0.patch
        0051-Cygwin-console-Fix-hangup-of-less-on-quit-after-the-.patch
        0052-Adjust-CWD-magic-to-accommodate-for-the-latest-Windo.patch
        0053-CI-fix-the-build-with-gcc-13.patch
        0054-fixup-Add-functionality-for-converting-UNIX-paths-in.patch
        0055-fixup-Add-functionality-for-converting-UNIX-paths-in.patch
        0056-nlsfuncs-work-around-an-overzealous-GCC-warning.patch
        0057-Move-_cygheap_start-into-.cygheap-section.patch
        0058-Cygwin-dumper-also-link-with-libzstd-as-libbfd-may-r.patch
        0059-Cygwin-configure-Add-option-to-disable-building-dump.patch
        0060-Work-around-fragile-include-in-binutils.patch)
sha256sums=('SKIP'
            'c375315e58181ee5589b7966101aa095de3f864a579c3c3f0f0683595d4e428d'
            '01ea2b131cf5a3b27fdbc458019eac14e45a36782ce3ce33e62328eefcd2d02e'
            '475ddea4d86605ab097f98ec764951a9d647ea3100dea0e1620f57044ea4a114'
            '6775247d7a213268bbb2315f764ebc9faf9967b70a95dbc6e9de53fe04142433'
            '91e2a72a980aeefadc215cd36147c56666679bbcd68c60120278b6270454b710'
            '54cd198f3ce3931884220c8938bd0f348e0889c20b36d110ded9302ec7364c34'
            '31a4bcb985e03c45f16644a1604ebc649dc50586968050f8819035b84f80bcf3'
            'e9cfe4d79ac294e19e648f51a8bb491231204d8eb9c6a2e998a7cc2506ca47ea'
            '816e98ee5ad15d3cfb75598e2e20e11272ac6867a7bd9b0a3fea0ac0ba4daf69'
            'e895d76936f4668bb7b67581ef642e845968dfb86e21d654dde98727f0ebabb2'
            'f416f109d323618ff3d7240ea7845d5ab3221a8a6c6074e36a34e62fbaabdbfd'
            'e710a9eed8bd767d97acb2d2cdc56eef880e02b20f1b14fdf462678954e3a896'
            'ce72bc18fcf101d14c0623c1d459b141ba883934939c70d198d69932ec4873c7'
            '5f5850cd725c4df6ce6cc86d07da1ed696e119d8bb80602380c4941aa10d0784'
            '8df334362506dbe9de56017c0df1d3459b5c6e29860b73a5e9ebd0421b2f3a6a'
            '988a2d365302c73ab482bd1f0d4edea8e687de7fdf068e5bec3aafaf33d63a58'
            'f9aa893686405b8b92b3d585b7a7dff35f9345a64a282f75d8962e92f86bea25'
            'b3073a6b2acf836f5b3286ff8be66633ed4f93280d50186635a3fcfb4cdc3745'
            '52cd166461fc2cc3c880c174160516a93d456dad44f382aed518aa9bfa36303f'
            'cee973f136152e6a5cd987bba85937584c9459aff1c2777134a99526b2d5512d'
            '221dacc354d4f27443665bfe32b114235ef7305c55c9189c77f636c46522ee80'
            'bd985989895e51688072d7751976ecb83720c8e0786126d7025ba355b64f6edc'
            '0f9e96fab22bb5ea0d4d6dc4a943ab56f28c0dab675e57a75b892c491d51808b'
            '61a9684bde1165f4afbf1ace3ddb873e6c8cc92e65930aff0c45e20b4d12a86a'
            'a02f701e524e0508149e3f16296db4f76382482ff114b170a1cc136d3ad65b2d'
            '205a6b56ab3bfa9209c3c1f89aac1491a75b6700e7656252f9afd84800bb3bc1'
            '8f817df7130fdec6477a69ba77394183c0dd7d8c2edb133b680f3be542e2896f'
            '5e7661cb53a050bc5470ea2d749d5e7524b02ae89dc929d17a9272508ddc6d4c'
            '13bbe25b3b60791b327163109e697ba160a70f58d13684aa52ab9bac00d5a555'
            '0b2c75b5f7e04c479d0a5ec7170e430e641b5449aa24232b5c3f073767a8fdbc'
            '53e11ea902756768f8601fa243ed6602b54c30df514a652c0d8e15de245a46c3'
            '1f03ce6418812425494695290711b7076c8678aeca36b2683ef0ec1875d1e0c3'
            '25c1e568b9f03772ca7a1bce1c90b6a9e22d49e15b7b372a0921dcd0e66c98d2'
            'bf0ff70c2ee6a4b9db7ed52154f5ba02d4a1d92cbfc2618ac86723c49156034f'
            '658ee069e968c7def37300bb7058bb17cd6e079eb789fe86b3af0fe1ae86a4cb'
            '59836bc012a3cf8dd245615f4cbeb4c3cb141434a30226c5e9647f265b589faf'
            'de0e56d2f01f02f1d4014cade92b4c78a90fb85f2409846a932b57b3c6d0b213'
            '30a3f2c7fba69aa9942f18f98c4289a8d89d6a6187f84a57895dbaa65f54d30e'
            'fcf8caa3cab42f0ea228b8e3213238ce2354f13dea27289c6899b0a209ffd204'
            '0e5ba38fa125822330b1842d487aee78126bf7ea503eb1fe970440cf1504f25f'
            'd5f6913d0d1439973a3687b3ef8948b6859ec302c0f810e9343c7e44f9146176'
            '65af62e9ca7870930b5d2b673f8eadfe3ce72c9672b8554790bd7dc65c0039dd'
            '6d062c34feca04b5dbd38273f8bb7d270947a368ade550fdfc8b11f0daa1a17c'
            '0454442f2ad3312df83ec86bf872b5ad0bc1c014ed13dad1fbac7dd8e8de8f7a'
            '61276aec4c9b7132487fd1d91e6c5991a24909f24a6de0312f62f1f99608e9c0'
            '202f3aaf82e001f7972d5d73152c424858d193108372453a52dbe6ab05b08922'
            '5647a4675b1f1f7c0a221f077d1d892c3e6bdb0635b2b23563838fe8ebfe0637'
            'c209bb6147094390e71d024367859f04e112eb2c90cbb289b24c4903224b2ae8'
            '5e690151d40a180138e198b19200b2e9840a9d4510da21896b027dc6b8e47f7d'
            'b37dbd1f70b3843a9917d4ec19bb57b1003893ccba79bbc5881ef3753838d8e4'
            '37019d648c72147173a847212d77241d3a9e6e4ae458f50587f60106a761061e'
            '9b0e31682c86d4f61677413f7a536bed04d631471d009dd4351c53e0b2f5c9e5'
            '8024edf0bcce8900061fae947bb46b4c3fbdea09d327a3c6048e0af292c8e991'
            'bf366ac49fb1c4e9d6d16eb38267cf04cf12a1ca510bd7d1ddfc0b834146c295'
            '168025561b2d0150e40509bd028545dc0e7af4ae1dbca7aa9b8f37da801e37e3'
            '6d985b7f92307cbe376af31fa1fc8010e2dc56e1354938b13423d4da06bc1d6c'
            '658288b4dc8306fc56bfd37b32326cf9d05aba1960205c4ba73821c0341a56f9'
            '1cdf4bff5af6b71c0710e608439f5fda3a097a895a565ad4c2542f301c108d3b'
            '66193f7b677f46edfba8d054365d58a79b5f141354c9614adb788f89a7360d53'
            'b6a286617f1c3278c2303db8e0bc7d94c455e1fb47e8bb8fd55aa9c5f477ccf0')

# 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-With-.patch \
  0009-Do-not-convert-environment-for-strace.patch \
  0010-Special-case-for-converting-root-directory-to-have-t.patch \
  0011-dcrt0.cc-Untangle-allow_glob-from-winshell.patch \
  0012-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch \
  0013-strace.cc-Don-t-set-MSYS-noglob.patch \
  0014-Add-debugging-for-build_argv.patch \
  0015-Add-debugging-for-strace-make_command_line.patch \
  0016-environ.cc-New-facility-environment-variable-MSYS2_E.patch \
  0017-Fix-native-symbolic-link-spawn-passing-wrong-arg0.patch \
  0018-QueryUnbiasedInterruptTime-must-be-load-from-kernel3.patch \
  0019-strace-quiet-be-really-quiet.patch \
  0020-Default-to-disable_pcon.patch \
  0021-Introduce-the-enable_pcon-value-for-MSYS.patch \
  0022-popen-call-usr-bin-sh-instead-of-bin-sh.patch \
  0023-CI-add-a-GHA-for-doing-a-basic-build-test.patch \
  0024-Set-up-a-GitHub-Action-to-keep-in-sync-with-Cygwin.patch \
  0025-Expose-full-command-lines-to-other-Win32-processes-b.patch \
  0026-Disable-the-cygwin-GitHub-workflow.patch \
  0027-Do-not-show-Error-dialogs-by-default.patch \
  0028-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch \
  0029-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch \
  0030-kill-kill-Win32-processes-more-gently.patch \
  0031-Cygwin-make-option-for-native-inner-link-handling.patch \
  0032-docs-skip-building-texinfo-and-PDF-files.patch \
  0033-install-libs-depend-on-the-toollibs.patch \
  0034-POSIX-ify-the-SHELL-variable.patch \
  0035-Handle-ORIGINAL_PATH-just-like-PATH.patch \
  0036-uname-allow-setting-the-system-name-to-CYGWIN.patch \
  0037-fixup-Revert-Default-to-disable_pcon.patch \
  0038-Pass-environment-variables-with-empty-values.patch \
  0039-Optionally-disallow-empty-environment-values-again.patch \
  0040-build_env-respect-the-MSYS-environment-variable.patch \
  0041-Cygwin-pty-Fix-Bad-address-error-when-running-cmd.ex.patch \
  0042-ci-avoid-using-Node.js-12-Actions.patch \
  0043-When-converting-to-a-Unix-path-avoid-double-trailing.patch \
  0044-amend-Special-case-for-converting-root-directory-to-.patch \
  0045-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch \
  0046-Cygwin-Implicitly-support-the-dev-fd-symlink-and-fri.patch \
  0047-dumper-avoid-linker-problem-when-libbfd-depends-on-l.patch \
  0048-CI-build-with-disable-dependency-tracking.patch \
  0049-Stop-assuming-that-there-are-no-spaces-in-POSIX-styl.patch \
  0050-Cygwin-Fix-compiling-with-w32api-headers-v11.0.0.patch \
  0051-Cygwin-console-Fix-hangup-of-less-on-quit-after-the-.patch \
  0052-Adjust-CWD-magic-to-accommodate-for-the-latest-Windo.patch \
  0053-CI-fix-the-build-with-gcc-13.patch \
  0054-fixup-Add-functionality-for-converting-UNIX-paths-in.patch \
  0055-fixup-Add-functionality-for-converting-UNIX-paths-in.patch \
  0056-nlsfuncs-work-around-an-overzealous-GCC-warning.patch \
  0057-Move-_cygheap_start-into-.cygheap-section.patch \
  0058-Cygwin-dumper-also-link-with-libzstd-as-libbfd-may-r.patch \
  0059-Cygwin-configure-Add-option-to-disable-building-dump.patch \
  0060-Work-around-fragile-include-in-binutils.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 -Wno-error=deprecated -Wno-error=stringop-truncation -Wno-error=missing-attributes -Wno-error=maybe-uninitialized -Wno-error=format-overflow" #-Wno-error=class-memaccess
  CXXFLAGS="$OPTIM -pipe -ggdb -Wno-error=deprecated -Wno-error=stringop-truncation -Wno-error=missing-attributes -Wno-error=maybe-uninitialized -Wno-error=format-overflow -Wno-error=overloaded-virtual -Wno-narrowing" #-Wno-error=class-memaccess

  (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
}

package_msys2-runtime-3.3() {
  pkgdesc="Posix emulation engine for Windows"
  options=('!strip')
  provides=("msys2-runtime=${pkgver}")
  conflicts=('catgets' 'libcatgets' 'msys2-runtime' 'msys2-runtime-3.5')
  replaces=('catgets' 'libcatgets')

  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-3.3-devel() {
  pkgdesc="MSYS2 headers and libraries"
  depends=("msys2-runtime-3.3=${pkgver}")
  provides=("msys2-runtime-devel=${pkgver}")
  options=('staticlibs' '!strip')
  conflicts=('libcatgets-devel' 'msys2-runtime-devel' 'msys2-runtime-3.5-devel')
  replaces=('libcatgets-devel')

  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/
}
