# 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.6
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'
             '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)
sha256sums=('4acc146baed79e6a4594bebf562125d4d6f66c9d1f53f89a1e47d156799508dc'
            '0690fbab79ccb94e79694b6bad4b2bafe808d208131a3f33b14faa4e937ef9a2'
            'd3c997a88bef74e70c7b394ee5c4f63ea758784325809b665724aa521f0fbd70'
            '8b366b9e4925108369e9bd3ea9daaac1903db6599e5586629663e7db2699d406'
            'eb3e71319aa1f0d02c398c4f3ce392a49d1600e89e64782021d732a1eda857ba'
            'a2c3b08fa04d88e0230490df04676f7c574e71d2c03ad0a822c16e3ef6b16a82'
            '4892a07b8420387d3a779404525baa22f19bfb26cc91edb26dd884b2fc6aab2a'
            '74515b662fdab99c8c43fea43d814fa915395038b813a34044d99b4533d463c8'
            'b259ec400241b4c9f28e0acf5f0cd1c98c9a6c56c7f7ddd13f6e8f481618bc6c'
            '499e102283fa099573e5be9bde3c6592ef825d071ff3d8ab22f1d3cef3b81dad'
            'dc05d606ee863d45fc2e197fd552268778e6c073ad22f664e0cf5d5209c936cb'
            '6ad3f346c0b7e1754faf26771b201ed337eb40b28c7e42e29c0091fe68f057e5'
            '37cbbb087b5c6f3c9a5586e7bcd843ae0aac80a737b0bdbbbaef28f9ce1d6dec'
            '6abefb51ff1de3548f44c8a632d1cfa1e87b41e8ddd554ff04b15236a3359e49'
            'baa423ad63c5b350ca17b1e2bc6fc4e1fa5dd021bf8c1baba56d37b98a632d2c'
            'e2b584dfc791e1f42974dece1513c4cc1324543e1e22651ada915b83d22103c1'
            'f668bbc20424b95cabaf87e5af56a54bdeab647afe1ce58fa248cc79a5f86f89'
            '0ba5561a570c72cddeeb180891d9c9272b335fc42ccf7ced1e8fc72b908d4145'
            'a3ef5350eb78e879a010d61aa3e6f30f13f6e8182f000a85f9fd19b8bcce7cd3'
            'e561c433e931913cf0fdf151ca0fe5b3d21bf19bea0e73329407e48fd3e75ba2'
            '971638e00d50c6f749c940dc5c69bfaae9ebf33d271b54407993a619385c4a75'
            'b1ab44101e4e97a8319aecea3d6cd3647a0d5ea757c6e44a81c69a400157ef4f'
            'b48b07b362aeefde4b652a809ed22aadd1e6b5d3a1c7fc955de84920cda5ce6f'
            '6c036a34d3bda3fad0805c15d67897a059af499804858bc6ba493803e4841ef4'
            '1fc0279a90a8b2dc3962dc512c881039a65ef057f42028299099c1eb1fb1cb5b'
            '11372d6fad0ca0704cc15a028f835f0b58f363340a74fab8fede898f227f574b'
            '665a4716e11f7d83e3c6271a9ce40556576fb5aa4d20fe090cfcb03e06f2a964'
            '97473d1d15db954214cdc980c929a82e54d63e4c97cbca896eba317bdf13fa9e'
            'e3e6a7781622590b17c6ade477bd8341eb735b196339aa8f38ad2c888c1f3f15'
            'a48447154c4aeb68526a729aa928db9e8e49908ff0f8bff8aa898e7590d38ddb'
            '59cd2964764d83a611e312dcb95b5b3fb4a1550fe9f9bc57fca96f1277352380'
            '83edec2403f7de50629a0e37f28f40a87fc6aae5531b768c2e84d37be90c4dd1'
            '23e3189555d64c61966b0db0102b7e6b8805224118391786cd42d56acf975c2a'
            '3d1f46948d53230bebe9b4287e6cfdfd6981697fe57897ecc3ac2b566d4556d0'
            '7e1e4f7a5d6eab8ff7df40e7aa2de6f6a33af51c3452f8cf83b409896eb5a871'
            'c1a16dfa508c21bdb677347e5a33283e45d05079c8a777a7ea09419439fdd3ca'
            '9c43e96e7f25776249bc16c25796a3386892391bf3cdb79624e4af5dc8e38c83'
            '897dbcf17d65e6ec44841fff7e821bb9ed9560fcd8a177fbdfa987ee8cfe158b'
            'f521b0b8f0331e4f884aec46178d1d94cbc3ce83e0b2452603b06c4a355c47d3'
            'fa97dbea1dd01a4e1e15f5d3bacd76fe2cdd70c792e78bb8a48b42dad9ec7229')

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

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
}
