# 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=('spdx:GPL-3.0-or-later')
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')
# 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}
        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)
sha256sums=('6d03f5393cb76a8a28db3eab5ea855fab39ffc7d928d6fc7596af13b70867dc1'
            '5a3af19fdbd2b69f1f34468e7f1b1f00aeb51642588700aaed9560d14d3ed2b8'
            '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')

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

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"

  # 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[@]}"
  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.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
}
