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

pkgbase=msys2-runtime-3.4
pkgname=('msys2-runtime-3.4' 'msys2-runtime-3.4-devel')
pkgver=3.4.10
pkgrel=5
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}
        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.patch
        0009-Do-not-convert-environment-for-strace.patch
        0010-path_conv-special-case-root-directory-to-have-traili.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-strace-quiet-be-really-quiet.patch
        0019-Introduce-the-enable_pcon-value-for-MSYS.patch
        0020-popen-call-usr-bin-sh-instead-of-bin-sh.patch
        0021-Expose-full-command-lines-to-other-Win32-processes-b.patch
        0022-Do-not-show-Error-dialogs-by-default.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-When-converting-to-a-Unix-path-avoid-double-trailing.patch
        0036-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch
        0037-Revert-Cygwin-Enable-dynamicbase-on-the-Cygwin-DLL-b.patch
        0038-dumper-avoid-linker-problem-when-libbfd-depends-on-l.patch
        0039-path-conversion-Introduce-ability-to-switch-off-conv.patch
        0040-fixup-Add-functionality-for-converting-UNIX-paths-in.patch
        0041-fixup-Add-functionality-for-converting-UNIX-paths-in.patch
        0042-fixup-Add-functionality-for-converting-UNIX-paths-in.patch
        0043-fixup-Add-functionality-for-converting-UNIX-paths-in.patch
        0044-fixup-Add-functionality-for-converting-UNIX-paths-in.patch
        0045-fixup-Add-functionality-for-converting-UNIX-paths-in.patch
        0046-fixup-Add-functionality-for-converting-UNIX-paths-in.patch
        0047-fixup-Add-functionality-for-converting-UNIX-paths-in.patch
        0048-Handle-8-bit-characters-under-LOCALE-C.patch
        0049-fixup-path-conversion-Introduce-ability-to-switch-of.patch
        0050-pathconv-don-t-skip-arguments-with-double-quote.patch
        0051-Work-around-fragile-include-in-binutils.patch
        0052-Cygwin-try-to-avoid-recalling-offline-files.patch
        0053-Cygwin-get-set-security-descriptors-using-FILE_OPEN_.patch
        0054-Cygwin-FILE_OPEN_NO_RECALL-is-incompatible-with-FILE.patch
        0055-Cygwin-console-Fix-exit-code-for-non-cygwin-process.patch
        0056-Cygwin-console-Avoid-slipping-past-disable_master_th.patch
        0057-Cygwin-pty-Fix-handle-leak-in-master-process.patch
        0058-Cygwin-pty-Fix-potential-handle-leak-regarding-CallN.patch
        0059-Cygwin-console-Make-VMIN-and-VTIME-work.patch
        0060-Cygwin-pipe-Give-up-to-use-query_hdl-for-non-cygwin-.patch
        0061-Avoid-sharing-cygheaps-across-Cygwin-versions.patch
        0062-uname-report-msys2-runtime-commit-hash-too.patch
        0063-Cygwin-suppress-a-warning-generated-with-w32api-12.0.patch
        0064-Cygwin-Fix-compatibility-with-w32api-headers-v13.patch)
sha256sums=('a40476d03803bfa6875cde92221db4326b3479147e873fbeca2e4236d48fe875'
            '351bb1efdbdafe80c981e92d6b425c6ab71c85ce4e990db184e2118158eb2ab6'
            'd3d3a01feeae9f7d5e6cb32f4662df74fc9476ff11a1aac3dad2df3e43fd88e4'
            '2e50ecd65f2fd413baaf39e5058a6b252245abc7d34f4ebf17dd4f7ffed60ced'
            '231757d797cf9be6a92b6f6976ee85216e9fa8eaba4aca241ace4b76e41cd862'
            '23f97fac32ae2ba7681985e0d4910ec2d6d265f970bb0db6cec48540b6ebf055'
            'd47c570e2ac7a9b1c15f1a9e4e51b530eef0a2dc0f261ddc088ad449840deb02'
            '1bc7e3275eb9ffb3b70a0b9a5049bc6180d893d28ac6fc1d0e2ca29f8d890a33'
            '8113de5e8ae37da6b96a3c7eb379806ccb1a90f42ae0d9061941d5e224f754a0'
            '09ec71a8292035b38e5fb6db7fdd4f2fb6c5071ec9a7c180c72bb22216db88df'
            '02cc70007deccabac988ae02a82b201a71772b0dc6570b0275793f37f71ef09d'
            'b70c4cc2f37632d51e9ddcae56f12c9ec92357e11347cb806ed23af9251a601a'
            '51571b603ca6a75f22125daeb1973a9f8ecdf82b1d4637814287fa311f9480c3'
            'd4dbcdbfa4f2dbac9a06eac0cd42154e30df03466554778340dadf67ab861c8b'
            '5c018ce376a7f2b0b8e3dc3a4eedc5eebf618ef2337889188e6f914d0ac09795'
            '2350b6193166cab2596162d8a203a4edbddb413a615e84f7ecc120ffa98f609f'
            '7d25457a43facd4231aad09bc5acf4717468a937c087427bd8cc5e2a1046db84'
            '233b59d7ca0a339e09c15f7c64989ba347f1316f353d2d366dede33325b4091f'
            'b4eeb001292c5a34df2ebe65d8deef987eb70baf60f821d86c3cbccb49fea40a'
            '3afbea60cc0edfc7931ecaafeb1d6b7dfda1aac10be590afd41abff8a27815ef'
            '691328ad116d972eeb0d04558c68ad605e306faec26a3e43de90024e31d797d6'
            'b36301669c8e2802cc82c8ac7b99b10135f0aee12e3b01b258272e7089a9c4ed'
            '231d6372dcbdb72f4cd10dad60bbdf9f4d55236193e5cbfc2377dd1af1194360'
            'c6cfb73dab426e9b7649514408d4a0577ebac43aef9c0585e720e5624c2a904e'
            'aa3c148a0655d47d9acd822547ef4e8ef35be309e71e7d85c4af8bb3a485f7db'
            '4318b3e6e99f724c211b0c8b6ff93d644336a08fd29219381de9035b8f7b5415'
            '4cf58088663b156ea6ca2278eb24226ef3f57f9bd486dea9b1ac7b04fa62eb0c'
            '44d207e6e7cda10d427b6705735622fd7847a58d4f133862e9f82f11fd57cf0c'
            '161e4bcbf55f5d4b81bdbed73e0c8fdc339c75b70cd9e292fde41b53988bfb5b'
            '5528c8e0715404fc03068efcd8c42010604fe5940a7af962211066f9670276b5'
            'd121cbfed47c72787c92eb817bba2071930f86fdf2bff424d1082ceccd59da49'
            '832aaa93defa111e3d9f540532288398ad27352122b1ff84a8a2f53232e601b9'
            'de44e80e60e61dbf91b6b99a3f5740f653f83b96c64f862f4aa99fd42e7db0d7'
            '7cebdd623fe080bda89b0505d4e8dc8600969d3b7d71e6bdf0a7e6ac1c62708f'
            '008adf33a2b3774d85aabe1b2b2a3fa32615c0cf7bfbc7cf4e4ce3c0302957d6'
            'd5981d518ba4756dd6e976e9cc20ff8f52001aac588ee083a33045d260e02d6b'
            '599905c7454e2f3039c717de8186d4e1cb7943f9b47a7745ce36e4acfa100e31'
            'd37c835bc188786a4e8050f0a73d4f0b3070ded9030943ba2a6c47b03d9d96e8'
            'a799e7edde58a0ec519bb7df6d0bcf4177e1c6236a72a8240936c574be1bdd61'
            'aef2222ac4444efbd77de36bb92628f32f1467acea522e344fd28153ea235721'
            '5f61639a3c449ae90358cb621c67bade1b65ebef73fda64a7bc369f6fed4b266'
            '9b31f6d42920df0103f16ef51d6467b5f0db91d462545430047bf207fcc59371'
            'cda68bcfe03c76fc57435a25540c6f29aedf003bdb3dc434ecce86599f8293cc'
            '3fc79c86927d0983f892e99a978ab448f808f3fcdad6e5b101f35fc213af7f63'
            '22bb5e0738f16d0e8d2732307efe336226a58165d54616297aa5caf82e5bea4d'
            '58d8d131f62e7424e284c012504eb704f1a94c30a8572f04f8c4d77d1501eebd'
            'f5a6e7d85741b44daf62f7f80ac1e64f0336d3be084ebe9b7ce2e796f493b952'
            '73e74ea6f4f05d212fbdfab2b8dec2ad1faded660a833b24b55a058719ad7c01'
            '6c4cc6db864addb7ac78c5e2cbb0be41c7f853726c08b92fa33d573c8dcf0c16'
            'f9533f44a33f192716bdb7b613c66a675f37d3c613590d505737a76c80a3e75e'
            '1665c7654a3f5fbdf42fdc7c3b0bfeaa2f26a9534962ffece26da97122e3018e'
            '164527ad2e289050ed336a6f1aa8e1af40fb864e7e4aed545bde2703d41203bd'
            '9879a0fe09147b1e0d41f42211bb309dd08ea088186f413d4dec6e0300ef9fbc'
            '677c013c456ca1ec239a694340ae36eae99a5700d0dda09e3679a29997234512'
            '266db90ae4f67f2818d015272cdda3d751a62724149d035d1a04a4407a7dbcef'
            '368c5a3c9df12d4fd0716979ec449890cb39690801b13121c06575a136727b5f'
            '27b0596bea70532f0f1a017bda81a87e273ddcb76b638887adb3dd3171bdccf4'
            '54afb4ad79fccdb1230181729edca861119b7668aa35715265461c0b0317a6a2'
            '71d11ca9d0a39bb643ccff2c74e90fd2008453ef81361ddf00f3cc4429b2e687'
            '43383dd8358ae49865f1856493f482eec1fa56710e990029028296888f50ac2c'
            '45cf4e509845d27bf0d12bc6b878e8c2e33754e232a8d1de43ed7e19e451bfac'
            '80119c6ee0e8c44260ead25f2e0a9d0aa9f6ae244521541db3a53c424f9f72fd'
            'f8210e329bb0cac0028c25a415755fe8a0b4339eaeb6f4117d9a7c6d0559579e'
            '2ba10495562b73a44d50526c923b523dd3b38071b49a19d02ab5223a3684ff2c'
            '418c643b9195e25d9aba284de396afff8b4e168a0969a58816cfff6ae3a7519b')

# 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.patch \
  0009-Do-not-convert-environment-for-strace.patch \
  0010-path_conv-special-case-root-directory-to-have-traili.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-strace-quiet-be-really-quiet.patch \
  0019-Introduce-the-enable_pcon-value-for-MSYS.patch \
  0020-popen-call-usr-bin-sh-instead-of-bin-sh.patch \
  0021-Expose-full-command-lines-to-other-Win32-processes-b.patch \
  0022-Do-not-show-Error-dialogs-by-default.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-When-converting-to-a-Unix-path-avoid-double-trailing.patch \
  0036-msys2_path_conv-pass-PC_NOFULL-to-path_conv.patch \
  0037-Revert-Cygwin-Enable-dynamicbase-on-the-Cygwin-DLL-b.patch \
  0038-dumper-avoid-linker-problem-when-libbfd-depends-on-l.patch \
  0039-path-conversion-Introduce-ability-to-switch-off-conv.patch \
  0040-fixup-Add-functionality-for-converting-UNIX-paths-in.patch \
  0041-fixup-Add-functionality-for-converting-UNIX-paths-in.patch \
  0042-fixup-Add-functionality-for-converting-UNIX-paths-in.patch \
  0043-fixup-Add-functionality-for-converting-UNIX-paths-in.patch \
  0044-fixup-Add-functionality-for-converting-UNIX-paths-in.patch \
  0045-fixup-Add-functionality-for-converting-UNIX-paths-in.patch \
  0046-fixup-Add-functionality-for-converting-UNIX-paths-in.patch \
  0047-fixup-Add-functionality-for-converting-UNIX-paths-in.patch \
  0048-Handle-8-bit-characters-under-LOCALE-C.patch \
  0049-fixup-path-conversion-Introduce-ability-to-switch-of.patch \
  0050-pathconv-don-t-skip-arguments-with-double-quote.patch \
  0051-Work-around-fragile-include-in-binutils.patch \
  0052-Cygwin-try-to-avoid-recalling-offline-files.patch \
  0053-Cygwin-get-set-security-descriptors-using-FILE_OPEN_.patch \
  0054-Cygwin-FILE_OPEN_NO_RECALL-is-incompatible-with-FILE.patch \
  0055-Cygwin-console-Fix-exit-code-for-non-cygwin-process.patch \
  0056-Cygwin-console-Avoid-slipping-past-disable_master_th.patch \
  0057-Cygwin-pty-Fix-handle-leak-in-master-process.patch \
  0058-Cygwin-pty-Fix-potential-handle-leak-regarding-CallN.patch \
  0059-Cygwin-console-Make-VMIN-and-VTIME-work.patch \
  0060-Cygwin-pipe-Give-up-to-use-query_hdl-for-non-cygwin-.patch \
  0061-Avoid-sharing-cygheaps-across-Cygwin-versions.patch \
  0062-uname-report-msys2-runtime-commit-hash-too.patch \
  0063-Cygwin-suppress-a-warning-generated-with-w32api-12.0.patch \
  0064-Cygwin-Fix-compatibility-with-w32api-headers-v13.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 -Wno-error=stringop-truncation -Wno-error=array-bounds -Wno-error=overloaded-virtual -Wno-narrowing -Wno-use-after-free -Wno-error=maybe-uninitialized"

  # 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 \
    --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-3.4() {
  pkgdesc="Posix emulation engine for Windows"
  provides=("msys2-runtime=${pkgver}")
  conflicts=('catgets' 'libcatgets' 'msys2-runtime')
  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.4-devel() {
  pkgdesc="MSYS2 headers and libraries"
  depends=("msys2-runtime-3.4=${pkgver}")
  provides=("msys2-runtime-devel=${pkgver}")
  conflicts=('libcatgets-devel' 'msys2-runtime-devel')
  replaces=('libcatgets-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/
}
