From fa1492dc3f0516aa4e2f6c271ca769d0823527b9 Mon Sep 17 00:00:00 2001
From: Melih Mutlu <m.melihmutlu@gmail.com>
Date: Fri, 2 Sep 2022 23:10:02 +0300
Subject: [PATCH] Added Windows with MinGW environment in Cirrus CI

CI task on Windows Server with MinGW has been added as optional.

Note that trigger_type line for MinGW task has to be uncommented before
merge
---
 .cirrus.yml                     | 98 ++++++++++++++++++++++++++-------
 src/tools/ci/cores_backtrace.sh | 17 +++++-
 2 files changed, 94 insertions(+), 21 deletions(-)

diff --git a/.cirrus.yml b/.cirrus.yml
index 9f2282471a..07b4c5e268 100644
--- a/.cirrus.yml
+++ b/.cirrus.yml
@@ -393,24 +393,37 @@ task:
     cores_script: src/tools/ci/cores_backtrace.sh macos "${HOME}/cores"
 
 
+WINDOWS_ENVIRONMENT_BASE: &WINDOWS_ENVIRONMENT_BASE
+    env:
+      # Half the allowed per-user CPU cores
+      CPUS: 4
+      TEST_JOBS: 4 # wild guess, data based value welcome
+
+      # The default working dir is in a directory msbuild complains about
+      CIRRUS_WORKING_DIR: "c:/cirrus"
+
+      PG_REGRESS_SOCK_DIR: "c:/cirrus/"
+
+      # Avoids port conflicts between concurrent tap test runs
+      PG_TEST_USE_UNIX_SOCKETS: 1
+
+      # git's tar doesn't deal with drive letters, see
+      # https://postgr.es/m/b6782dc3-a7b0-ed56-175f-f8f54cb08d67%40dunslane.net
+      TAR: "c:/windows/system32/tar.exe"
+
+    sysinfo_script: |
+      chcp
+      systeminfo
+      powershell -Command get-psdrive -psprovider filesystem
+      set
+
 task:
   name: Windows - Server 2019, VS 2019 - Meson & ninja
+  << : *WINDOWS_ENVIRONMENT_BASE
 
   env:
-    # Half the allowed per-user CPU cores
-    CPUS: 4
-    TEST_JOBS: 8 # wild guess, data based value welcome
-
-    # The default cirrus working dir is in a directory msbuild complains about
-    CIRRUS_WORKING_DIR: "c:/cirrus"
     # Avoid re-installing over and over
     NO_TEMP_INSTALL: 1
-    # git's tar doesn't deal with drive letters, see
-    # https://postgr.es/m/b6782dc3-a7b0-ed56-175f-f8f54cb08d67%40dunslane.net
-    TAR: "c:/windows/system32/tar.exe"
-    # Avoids port conflicts between concurrent tap test runs
-    PG_TEST_USE_UNIX_SOCKETS: 1
-    PG_REGRESS_SOCK_DIR: "c:/cirrus/"
 
     # startcreate_script starts a postgres instance that we don't want to get
     # killed at the end of that script (it's stopped in stop_script). Can't
@@ -435,19 +448,13 @@ task:
     cpu: $CPUS
     memory: 4G
 
-  sysinfo_script: |
-    chcp
-    systeminfo
-    powershell -Command get-psdrive -psprovider filesystem
-    set
-
   setup_additional_packages_script: |
     REM choco install -y --no-progress ...
 
   # Use /DEBUG:FASTLINK to avoid high memory usage during linking
   configure_script: |
     vcvarsall x64
-    meson setup --backend ninja --buildtype debug -Dc_link_args=/DEBUG:FASTLINK -Dcassert=true -Db_pch=true -Dssl=openssl -Dextra_lib_dirs=c:\openssl\1.1\lib -Dextra_include_dirs=c:\openssl\1.1\include -DTAR=c:/windows/system32/tar.exe -DPG_TEST_EXTRA="%PG_TEST_EXTRA%" build
+    meson setup --backend ninja --buildtype debug -Dc_link_args=/DEBUG:FASTLINK -Dcassert=true -Db_pch=true -Dssl=openssl -Dextra_lib_dirs=c:\openssl\1.1\lib -Dextra_include_dirs=c:\openssl\1.1\include -DTAR=%TAR% -DPG_TEST_EXTRA="%PG_TEST_EXTRA%" build
 
   build_script: |
     vcvarsall x64
@@ -463,6 +470,59 @@ task:
       path: "crashlog-*.txt"
       type: text/plain
 
+task:
+  << : *WINDOWS_ENVIRONMENT_BASE
+  name: Windows - Server 2019, MinGW64 - Meson
+
+  # This task needs to be triggered manually on Cirrus CI.
+  # Otherwise it will not run and be shown as paused
+  # XXX: Below line needs to be uncommented before merge. It's commented for now so that cfbot can run it
+  #trigger_type: manual
+
+  windows_container:
+    image: $CONTAINER_REPO/windows_ci_mingw64:latest
+    cpu: $CPUS
+    memory: 4G
+
+  env:
+    CCACHE_DIR: C:/msys64/ccache
+    BUILD_DIR: "%CIRRUS_WORKING_DIR%/build"
+    PYTHONHOME: C:/msys64/ucrt64
+    MSYS: winjitdebug
+    CCACHE_SLOPPINESS: pch_defines,time_macros
+    CCACHE_DEPEND: 1
+    # Start bash in current working directory
+    CHERE_INVOKING: 1
+    BASH_EXE: C:\msys64\usr\bin\bash.exe
+
+  ccache_cache:
+    folder: ${CCACHE_DIR}
+
+  setup_additional_packages_script: |
+    REM C:\msys64\usr\bin\pacman.exe -S --noconfirm ...
+
+  mingw_info_script: |
+    %BASH_EXE% -lc "where gcc"
+    %BASH_EXE% -lc "gcc --version"
+    %BASH_EXE% -lc "where perl"
+    %BASH_EXE% -lc "perl --version"
+
+  configure_script: |
+    %BASH_EXE% -lc "meson setup --buildtype debug -Dcassert=true -Db_pch=true -DTAR=%TAR% build"
+
+  build_script: |
+    %BASH_EXE% -lc "cd %CIRRUS_WORKING_DIR% && ninja -C build"
+
+  upload_caches: ccache
+
+  test_world_script: |
+    %BASH_EXE% -lc "meson test --print-errorlogs --num-processes %TEST_JOBS% -C build"
+
+  on_failure:
+    <<: *on_failure_meson
+    cores_script: |
+      %BASH_EXE% -lc "cd build src/tools/ci/cores_backtrace.sh msys build/tmp_install"
+
 
 task:
   name: CompilerWarnings
diff --git a/src/tools/ci/cores_backtrace.sh b/src/tools/ci/cores_backtrace.sh
index 28d3cecfc6..93e90f284e 100755
--- a/src/tools/ci/cores_backtrace.sh
+++ b/src/tools/ci/cores_backtrace.sh
@@ -10,11 +10,24 @@ directory=$2
 
 case $os in
     freebsd|linux|macos)
-    ;;
+        ;;
+
+    msys)
+        # XXX Evidently I don't know how to write two arguments here without pathname expansion later, other than eval.
+        #findargs='-name "*.stackdump"'
+        for corefile in $(find "$directory" -type f -name "*.stackdump") ; do
+            binary=`basename "$corefile" .stackdump`
+            echo;echo;
+            echo "dumping ${corefile} for ${binary}"
+            awk '/^0/{print $2}' $corefile |addr2line -f -i -e ./src/backend/postgres.exe
+        done
+        exit 0
+        ;;
+
     *)
         echo "unsupported operating system ${os}"
         exit 1
-    ;;
+        ;;
 esac
 
 first=1
-- 
2.25.1

