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

pkgbase=msys2-runtime
pkgname=('msys2-runtime' 'msys2-runtime-devel')
pkgver=3.6.10
pkgrel=1
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'
             'libzstd-devel'
             'gettext-devel'
             'libiconv-devel'
             'autotools'
             'xmlto'
             'docbook-xsl')
source=('msys2-runtime'::git+https://github.com/cygwin/cygwin#tag=cygwin-${pkgver}
        msys2-runtime.commit
        0001-Fix-msys-library-name-in-import-libraries.patch
        0002-Rename-dll-from-cygwin-to-msys.patch
        0003-Convert-Unix-paths-in-args-env-to-Windows-form-for-n.patch
        0004-Add-functionality-for-changing-OS-name-via-MSYSTEM-e.patch
        0005-Move-root-to-usr.-Change-sorting-mount-points.-By-de.patch
        0006-Instead-of-creating-Cygwin-symlinks-use-deep-copy-by.patch
        0007-Automatically-rewrite-TERM-msys-to-TERM-cygwin.patch
        0008-Do-not-convert-environment-for-strace.patch
        0009-strace.cc-Don-t-set-MSYS-noglob.patch
        0010-Add-debugging-for-strace-make_command_line.patch
        0011-strace-quiet-be-really-quiet.patch
        0012-path_conv-special-case-root-directory-to-have-traili.patch
        0013-When-converting-to-a-Unix-path-avoid-double-trailing.patch
        0014-dcrt0.cc-Untangle-allow_glob-from-winshell.patch
        0015-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch
        0016-Add-debugging-for-build_argv.patch
        0017-environ.cc-New-facility-environment-variable-MSYS2_E.patch
        0018-Introduce-the-enable_pcon-value-for-MSYS.patch
        0019-popen-call-usr-bin-sh-instead-of-bin-sh.patch
        0020-Expose-full-command-lines-to-other-Win32-processes-b.patch
        0021-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch
        0022-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch
        0023-kill-kill-Win32-processes-more-gently.patch
        0024-Cygwin-make-option-for-native-inner-link-handling.patch
        0025-docs-skip-building-texinfo-and-PDF-files.patch
        0026-install-libs-depend-on-the-toollibs.patch
        0027-POSIX-ify-the-SHELL-variable.patch
        0028-Handle-ORIGINAL_PATH-just-like-PATH.patch
        0029-uname-allow-setting-the-system-name-to-CYGWIN.patch
        0030-Pass-environment-variables-with-empty-values.patch
        0031-Optionally-disallow-empty-environment-values-again.patch
        0032-build_env-respect-the-MSYS-environment-variable.patch
        0033-Revert-Cygwin-Enable-dynamicbase-on-the-Cygwin-DLL-b.patch
        0034-Avoid-sharing-cygheaps-across-Cygwin-versions.patch
        0035-uname-report-msys2-runtime-commit-hash-too.patch
        0036-Cygwin-Adjust-CWD-magic-to-accommodate-for-the-lates.patch
        0037-Cygwin-Fix-segfault-when-XSAVE-area-sizes-are-unalig.patch
        0038-cygcheck-remove-an-unused-variable-causing-a-build-e.patch
        0039-fixup-Convert-Unix-paths-in-args-env-to-Windows-form.patch
        0040-Cygwin-pty-detect-pcon-backed-pty-for-non-Cygwin-spa.patch
        0041-Cygwin-pty-Discard-pcon-input-buffer-when-discard_in.patch
        0042-Cygwin-pty-Fixup-pty-state-after-a-cygwin-app-exits.patch
        0043-Cygwin-pty-grow-GetConsoleProcessList-buffer-in-find.patch
        0044-Cygwin-pty-bound-the-cursor-sync-round-trip-so-an-ex.patch
        0045-Cygwin-pty-do-not-leak-nat-handles-when-adopting-the.patch
        0046-Cygwin-pty-validate-the-cursor-position-reply-before.patch
        0047-Cygwin-pty-keep-interactive-console-input-for-native.patch
        0048-Mention-the-extremely-useful-small_printf-function.patch
        0049-Allow-native-symlinks-to-non-existing-targets-in-nat.patch
        0050-WIP-Handle-8-bit-characters-under-LOCALE-C.patch
        0051-Make-paths-WCS-MBS-conversion-explicit.patch
        0052-msys2-runtime-restore-fast-path-for-current-user-pri.patch
        0053-Change-the-default-base-address-for-x86_64.patch
        0054-dumper-allow-compiling-with-binutils-2.47.patch)
sha256sums=('7eac3518c0225e991a00a7ad36009090b54abead9dc802236771cde3d9332f7c'
            'add6be70f5e518ec09b05ed626b5743d9754cafeb626ceb22e0134a10433f1ae'
            '7c83791a32dbc139159802b7349fe50f69610863feea99bddcbed7d8d449b34f'
            '961fdf373c882fdec852453a6db368c2817fa4c422a5f786f51e277be1a68621'
            '92cdf141ffdd4142fb209da4d5b3cb7264e9fc55e70d2a88df8e2971a920b1ea'
            '3a4bb9f4c814bf3654f4a0dedf5dbcf1a42778c968fa754ed3cdcaba73a73f4b'
            'c5d80e2182cab9e4fc2913fcaacafe0bfbca2a702a145fd8028fd9e194acc723'
            'fd86635e9db2616b923768cc05ba5142c449cdfb9a4487069041df6d280e4a63'
            '2e687cadeef3063b9aeda09e78cb24458ea89cc3cb9430ed2cd28841b0baaa64'
            '1259e2378aedc791110dca771a9a8b4a17e13a0cb56bd6df41a4a9185b330421'
            '0bb91f6db9b4f250e485489c4e197559228bb62f1ee7c0c96af40e31ac00011a'
            '053bf716176fab8a4d2689e38fc6aace55056c08b367e7956e081659d9407a6e'
            '7d7c552d01f84074976d82a00a432fe507515d91b044c88f0c4f49562897e60c'
            'ded80e37054c58fab9b478dd41b0101bec5e84692f220d17cc7a06dc5fc85873'
            'e42892276bd9f7c23cce7ade30e3bc3e61ea506da39913b74f1aa0b324803520'
            '14cdaa70a590ce39ad792fe1ae5be587f2d7175b48c53d3ba1e9539f7605c90e'
            'a17d69cb86cbfa0602e2ac41265e0f32c5319b27a3a0b787de6c45379a7a34bb'
            'b2eed273092abf2cf4c72637ac8fdbcf3773ae83c0b756a34732a645b00808c8'
            '88939e504a0ea6e4da9ce38a8bd162022385a4756d4b8617059583501e74fa57'
            '11c5c8a4b7dfb68a878186aaa6c483ca7f18a6fec35d2908a9b4648e004bb760'
            '0e53ae1518f4387d68e5e4b2ed572ab5cc11faf2e08f5fbdf856290b8255c1d4'
            '477f2ede944892abfe9a281566fe978895afbcb1ddb82647744ff24b417d3960'
            '9c548463d07ef588f807cacfbf4c32473b95f8b538c633b3c723bb2ade80f75b'
            'aad2ca769b0c1bfaa2ae8e3682e3fdd3e047592014eaac78078c87e623c98609'
            '23518ac5ffa786640939d554311abf50c025225fa241b436dbcad50c91ff4d01'
            '7a5a6bf5d56398d017df77ddbd516f2771f1f02d3f62c666b3264f2b9053f743'
            '5084a444d1df4e6c5e2c4744a03c4b2675dea6ca940724681975605143f64a30'
            '026f8a76a697c72910bdd259e31136a9e365042784645496ce6df860a20fce67'
            'ed585c69059f23c93a0657f6e516a48cb76119c98dd48a6f86f3f1775940d73c'
            '98179077149f83dfda4cb7cb95e01b946acce9535f6af921dafd8b7966b7d665'
            'c43a61ccd08905a02baba318d8832208b62ac3f4db93644ea0f336cdc26b1f11'
            'da2dd899223e23914c42ba9262a3656aab996254668ec8d7a7d03219ff3a6f69'
            'd36f2792e6caa1615de1c3d134d918ec5ebbfab1149e06954e4cbab2b4df49e9'
            '55934ac4f869b48d7781512572cbf4c3e5dcf24210b4a83561bdc929515af667'
            'eeac4f9a5281c97f647d6d4a35117bebb41f6b86805ac9afcfedb13d36de7420'
            '7c5d27ffa2d20694983556c43ecc4e6e6637bed2b77b8cdc4f816e61007b19b8'
            'af4c40d7c30bd2c163a74fec6ee40c5705798fb6b4a6622059d55d88f654185b'
            '459599bfc4b1ce684d22f56c6786eacc336bf6c5856698c18a1689b6b0e4f5ce'
            '9ac0b67bb00be6bb14b5b140ea92c53030b96338ab1f36814f8d891f0a0d9657'
            '81ecb8ba492d84417d3723e814e8cfd031c4d434e3b1c14389d7464243825029'
            '862fb81d0f90dc4870f47466068602469740268937924c1c7de9523681ce6adb'
            '417cfa1e91b99465cf0d75f428664a5258fb5b0560d249d068038c6c4ec3b20a'
            'd9560dbc9080e3840c144b43803487c69780785d18dfa00cadb08a927716ec73'
            '223dc9cd43280ecdb9752704c99e6e212c639e7e450501985175a881fa4ed1da'
            'c7c4067cff81e24a096209a3cd9eafa8c7b34eb41e4e9b2c61c80485992f9dab'
            '474e273a85aef19503476a6a6292dedcd00639d1b4bfeebd10136df17e60ea53'
            '61b9ccc5818f79f69879064f08f812ed3d2364faa3ef83bda6d4774addff3d1b'
            '217fdcfd6fc02b37a4d2af904444c71b39ff3ce588a3603ac697cd224f5d7e2e'
            'd2860089773757658e01a38d5b8fd003afab02e59e1ba2301b798b7f1af715c9'
            '03860bc30fff51e81af60f584d5ab2c6260574f710e6b2965a7af23c9d0171c5'
            '1ac4480922ca122e88fa18607a396eb348a7adf43e33dfb1fa5241e6c349368a'
            'e94e08850accda885d82e154fec040c6bab36ff3839c047fcac2e785410062eb'
            'f4e9b8aadcaa47d1debbb9509836a93e2233aa3c79b56d13897711a92a98e31e'
            '2c393c0dc2b2b5d62a767cccd80dd89c8d7950edbf25e44dd7d2aee89c894b6d'
            '8588d779158683b1df0b6170029e22939af9d7f412e654e8d601e15eff6810cb'
            '35ee2aef88e6e686e9cc762ca7fb1326fd607a49aef103d6e277ce0aaf261ce6')

# 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
  alt=.git/objects/info/alternates
  if grep '^/' $alt
  then
    echo "Fixing absolute paths"
    xargs -n 1 -r -a $alt -d '\n' realpath --relative-to=.git/objects >$alt.new
    mv -f $alt.new $alt
  fi
  if test true != "$(git config core.symlinks)"
  then
    git config core.symlinks true &&
    /usr/bin/git reset --hard
  fi
  if test false != "$(git config core.autocrlf)"
  then
    git config core.autocrlf false &&
    rm "$(git rev-parse --git-path index)" &&
    /usr/bin/git reset --hard
  fi
  del_file_exists winsup/cygwin/msys2_path_conv.cc \
    winsup/cygwin/msys2_path_conv.h \
    winsup/cygwin/include/cygwin/exit_process.h \
    winsup/utils/mingw/getprocaddr.c

  apply_patch_with_msg \
 \
  0001-Fix-msys-library-name-in-import-libraries.patch \
  0002-Rename-dll-from-cygwin-to-msys.patch \
  0003-Convert-Unix-paths-in-args-env-to-Windows-form-for-n.patch \
  0004-Add-functionality-for-changing-OS-name-via-MSYSTEM-e.patch \
  0005-Move-root-to-usr.-Change-sorting-mount-points.-By-de.patch \
  0006-Instead-of-creating-Cygwin-symlinks-use-deep-copy-by.patch \
  0007-Automatically-rewrite-TERM-msys-to-TERM-cygwin.patch \
  0008-Do-not-convert-environment-for-strace.patch \
  0009-strace.cc-Don-t-set-MSYS-noglob.patch \
  0010-Add-debugging-for-strace-make_command_line.patch \
  0011-strace-quiet-be-really-quiet.patch \
  0012-path_conv-special-case-root-directory-to-have-traili.patch \
  0013-When-converting-to-a-Unix-path-avoid-double-trailing.patch \
  0014-dcrt0.cc-Untangle-allow_glob-from-winshell.patch \
  0015-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch \
  0016-Add-debugging-for-build_argv.patch \
  0017-environ.cc-New-facility-environment-variable-MSYS2_E.patch \
  0018-Introduce-the-enable_pcon-value-for-MSYS.patch \
  0019-popen-call-usr-bin-sh-instead-of-bin-sh.patch \
  0020-Expose-full-command-lines-to-other-Win32-processes-b.patch \
  0021-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch \
  0022-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch \
  0023-kill-kill-Win32-processes-more-gently.patch \
  0024-Cygwin-make-option-for-native-inner-link-handling.patch \
  0025-docs-skip-building-texinfo-and-PDF-files.patch \
  0026-install-libs-depend-on-the-toollibs.patch \
  0027-POSIX-ify-the-SHELL-variable.patch \
  0028-Handle-ORIGINAL_PATH-just-like-PATH.patch \
  0029-uname-allow-setting-the-system-name-to-CYGWIN.patch \
  0030-Pass-environment-variables-with-empty-values.patch \
  0031-Optionally-disallow-empty-environment-values-again.patch \
  0032-build_env-respect-the-MSYS-environment-variable.patch \
  0033-Revert-Cygwin-Enable-dynamicbase-on-the-Cygwin-DLL-b.patch \
  0034-Avoid-sharing-cygheaps-across-Cygwin-versions.patch \
  0035-uname-report-msys2-runtime-commit-hash-too.patch \
  0036-Cygwin-Adjust-CWD-magic-to-accommodate-for-the-lates.patch \
  0037-Cygwin-Fix-segfault-when-XSAVE-area-sizes-are-unalig.patch \
  0038-cygcheck-remove-an-unused-variable-causing-a-build-e.patch \
  0039-fixup-Convert-Unix-paths-in-args-env-to-Windows-form.patch \
  0040-Cygwin-pty-detect-pcon-backed-pty-for-non-Cygwin-spa.patch \
  0041-Cygwin-pty-Discard-pcon-input-buffer-when-discard_in.patch \
  0042-Cygwin-pty-Fixup-pty-state-after-a-cygwin-app-exits.patch \
  0043-Cygwin-pty-grow-GetConsoleProcessList-buffer-in-find.patch \
  0044-Cygwin-pty-bound-the-cursor-sync-round-trip-so-an-ex.patch \
  0045-Cygwin-pty-do-not-leak-nat-handles-when-adopting-the.patch \
  0046-Cygwin-pty-validate-the-cursor-position-reply-before.patch \
  0047-Cygwin-pty-keep-interactive-console-input-for-native.patch \
  0048-Mention-the-extremely-useful-small_printf-function.patch \
  0049-Allow-native-symlinks-to-non-existing-targets-in-nat.patch \
  0050-WIP-Handle-8-bit-characters-under-LOCALE-C.patch \
  0051-Make-paths-WCS-MBS-conversion-explicit.patch \
  0052-msys2-runtime-restore-fast-path-for-current-user-pri.patch \
  0053-Change-the-default-base-address-for-x86_64.patch \
  0054-dumper-allow-compiling-with-binutils-2.47.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

  # We use `-g0` here to suppress debug symbols, to avoid having to call `strip.exe`
  # on the binary files (which would break code-signing those files).
  CFLAGS="$OPTIM -pipe -g0"
  CXXFLAGS="$OPTIM -pipe -g0"

  # 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 \
    --with-msys2-runtime-commit="$(cat "${srcdir}/msys2-runtime.commit")" \
    --prefix=/usr \
    --build=${CHOST} \
    --sysconfdir=/etc \
    "${extra_config[@]}"
  # Work around `LC_ALL=C gawk` bug in Git for Windows
  (cd /usr/bin &&
   powershell -command '"#!/bin/sh`n`nLC_ALL=C.UTF-8 exec gawk.exe `"`$@`"" | Out-File -Encoding ascii -FilePath gawk' &&
   powershell -command '"#!/bin/sh`n`nLC_ALL=C.UTF-8 exec awk.exe `"`$@`"" | Out-File -Encoding ascii -FilePath awk')
  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 --strip-debug --strip-unneeded --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"
  options=('!strip')
  conflicts=('catgets' 'libcatgets' 'msys2-runtime-3.6')
  replaces=('catgets' 'libcatgets' 'msys2-runtime-3.6')

  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.6-devel')
  replaces=('libcatgets-devel' 'msys2-runtime-3.6-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/

  # compatibility with Cygwin toolchains
  cp "${srcdir}"/dest/usr/${CHOST}/lib/libmsys-2.0.a "${pkgdir}"/usr/lib/libcygwin.a
}
