# 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.7
pkgrel=2
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}
        msys2-runtime.commit
        0001-Fix-msys-library-name-in-import-libraries.patch
        0002-Rename-dll-from-cygwin-to-msys.patch
        0003-Add-functionality-for-converting-UNIX-paths-in-argum.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-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch
        0015-path-conversion-Introduce-ability-to-switch-off-conv.patch
        0016-dcrt0.cc-Untangle-allow_glob-from-winshell.patch
        0017-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch
        0018-Add-debugging-for-build_argv.patch
        0019-environ.cc-New-facility-environment-variable-MSYS2_E.patch
        0020-Introduce-the-enable_pcon-value-for-MSYS.patch
        0021-popen-call-usr-bin-sh-instead-of-bin-sh.patch
        0022-Expose-full-command-lines-to-other-Win32-processes-b.patch
        0023-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch
        0024-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch
        0025-kill-kill-Win32-processes-more-gently.patch
        0026-Cygwin-make-option-for-native-inner-link-handling.patch
        0027-docs-skip-building-texinfo-and-PDF-files.patch
        0028-install-libs-depend-on-the-toollibs.patch
        0029-POSIX-ify-the-SHELL-variable.patch
        0030-Handle-ORIGINAL_PATH-just-like-PATH.patch
        0031-uname-allow-setting-the-system-name-to-CYGWIN.patch
        0032-Pass-environment-variables-with-empty-values.patch
        0033-Optionally-disallow-empty-environment-values-again.patch
        0034-build_env-respect-the-MSYS-environment-variable.patch
        0035-Revert-Cygwin-Enable-dynamicbase-on-the-Cygwin-DLL-b.patch
        0036-Avoid-sharing-cygheaps-across-Cygwin-versions.patch
        0037-uname-report-msys2-runtime-commit-hash-too.patch
        0038-Cygwin-Adjust-CWD-magic-to-accommodate-for-the-lates.patch
        0039-Cygwin-Fix-segfault-when-XSAVE-area-sizes-are-unalig.patch)
sha256sums=('ce144fea2ed9896fc950e811a52ea384d55bad7804b703c1a483b53ae8def0c6'
            'f62650edff96d34beac4a1d10a38807b86d679560aee58ac758a15b2d86059a6'
            'fd5fca07f5febedd4be73af01613847568aec1e6dd1f0aa39658f9c946ae2812'
            'c5d502a01a7206ef26572674cc081e71c11e4796b1ffae7ee06c8afd546abf53'
            '432af98a8df514b3dbf2c6d6a82f19b561b40b109466f8eecc2c74ad5cc03b86'
            '72cfb4385f25b7396c82c490d128306684ba6378994d943935a1d4fac202c696'
            '09b42b2bdab0dbb8cdbc9816f9ef40283f0d089b87aee17335ca74f4949b2443'
            '32bdf069a0880a8ce7762fdafebc6c859d4546fa6f019970a459b4303fbb97be'
            'd7472488226f951d193772894228d54096b935fff7de2effeca9da1c68b09051'
            'd458a01c7453a1dd089dac1c379c8f045ab4ca2c22cfe0facbed9c349fa1b048'
            'f56f878f77bfff04a393e7a80b032a4632f07394a2a5896cca7586ad9ce446ca'
            '843046638a1aa66ca198d3ac8e9d46c09d8b3d45659596044a669c82250c7e7e'
            '7fd9911c61d60da00f14a91c80bfaf5d31189abd9c6b2303db699cc8f79e86a4'
            '304a1db5f1e505c06c299653b9d560781aeb634ddfe0f155048f2ce8d18978c9'
            '22e0b7068d815d1845aac5340bd4af1acc59f0aac828d3c4425f278c041ca674'
            'c3bd4f1da6b9cb3b4e34b8cb9443b294bf231caf7d7d3c006d6d966baa5eb8f1'
            '0f71da0368a7c03c022e7e826092cf2e8163e5a558629c8be5db9d19779e308c'
            'eb4e1390b3a166c59f4afb3a081885c892afe5dfd31b0750f7e7bdc72df68a7a'
            '08ad456dfbdfc33b716661d41c8aebc1ac8316f15901f2b0ee28980a9e534f55'
            'defe6068832c51707b31f6653f49240560c49a008aa83d7dcf681d38e3c01a30'
            '289a36d261ad0e25499038f369ce5330b6f6460e9b9faf408f4719c37e50e968'
            '3c8eba3a74d76bc0b388a73be3973673e3c5ca5d451f2ea4ae8099b0dcbf6821'
            'c6fc86baa4321a8fa200317e453e32eb55db9e1fa2b5da14c5e91fec9ed50dc8'
            '667de1ea4bb5b4531eb9efe0e0ac672bd5fecd669ddca15a4a00536aa100a503'
            'f3541ff3ca6a068dda6a20f9d8eaa12be1d5c790b1e4991bd9dd288f2cb69d8d'
            '4ba3ab99fdb342320b1d266140b3ca87c849490fb88f8ce68c7cced6e88966b4'
            '31113eed3c26d2ae9d22f44c85719652d983d3481fb65ef5c3b20e37bd848d5f'
            'f38112b7fd64fba1c67a4272bcc62e58fe60a060215355c24a9c64d53823257a'
            'f94643f002e21e928480537848c3351202e722782de505553d8b7b4cf6486686'
            '6452c6448fe1628c6b24ef873167197e27c963fcf90474a63fc297a9543cb614'
            '2d6da0dbb9628f92141c10a32ec312e682b2ea2fee212db67b7c315edb9a3bfe'
            'c89977277fff41c54730f7e746923bdb87c0de773c57c1e0a35650c3d01ee604'
            '0cf17a60deeb802ea4f5db31f9502aa3a57fa692e3116ec67033236a59647564'
            'cca947da31c6e78045b9de0e7d0a5bbd3202e7e4da281db578e6d07ba65b97d9'
            '00a37153211dfa6ca123c7f8735b503581720aba15dd8be83383b15209081c17'
            'f013ae55eca6869386c29769fd6e7ddb8b96152f1c0743b97beb5f5f56054398'
            '8e6724cd002b5591e997526d0ef7d5060a6729d1952a46c99c344a4aedca09af'
            '84397a12626727131753c924638b6992740723e346b9499263aeec8cec76aca8'
            '524f0c9922d8ac8398ef6ee8050afee157c8c93b24d4056826db36e406861ab2'
            '15541ccdf894510421b22a6bc901d1fb8da278a4101435b4a7b1ec2a211f84e2'
            '8244f4bb7890db0e87ec9aa21ff58989ca7cf6162f27ea782d72e3d0039545d9')

# 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-Add-functionality-for-converting-UNIX-paths-in-argum.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-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch \
  0015-path-conversion-Introduce-ability-to-switch-off-conv.patch \
  0016-dcrt0.cc-Untangle-allow_glob-from-winshell.patch \
  0017-dcrt0.cc-globify-Don-t-quote-literal-strings-differe.patch \
  0018-Add-debugging-for-build_argv.patch \
  0019-environ.cc-New-facility-environment-variable-MSYS2_E.patch \
  0020-Introduce-the-enable_pcon-value-for-MSYS.patch \
  0021-popen-call-usr-bin-sh-instead-of-bin-sh.patch \
  0022-Expose-full-command-lines-to-other-Win32-processes-b.patch \
  0023-Add-a-helper-to-obtain-a-function-s-address-in-kerne.patch \
  0024-Emulate-GenerateConsoleCtrlEvent-upon-Ctrl-C.patch \
  0025-kill-kill-Win32-processes-more-gently.patch \
  0026-Cygwin-make-option-for-native-inner-link-handling.patch \
  0027-docs-skip-building-texinfo-and-PDF-files.patch \
  0028-install-libs-depend-on-the-toollibs.patch \
  0029-POSIX-ify-the-SHELL-variable.patch \
  0030-Handle-ORIGINAL_PATH-just-like-PATH.patch \
  0031-uname-allow-setting-the-system-name-to-CYGWIN.patch \
  0032-Pass-environment-variables-with-empty-values.patch \
  0033-Optionally-disallow-empty-environment-values-again.patch \
  0034-build_env-respect-the-MSYS-environment-variable.patch \
  0035-Revert-Cygwin-Enable-dynamicbase-on-the-Cygwin-DLL-b.patch \
  0036-Avoid-sharing-cygheaps-across-Cygwin-versions.patch \
  0037-uname-report-msys2-runtime-commit-hash-too.patch \
  0038-Cygwin-Adjust-CWD-magic-to-accommodate-for-the-lates.patch \
  0039-Cygwin-Fix-segfault-when-XSAVE-area-sizes-are-unalig.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
}
