From 64a90980601a1985e9a610764a36c22900a7a2bc Mon Sep 17 00:00:00 2001
From: Sergey Kosukhin <sergey.kosukhin@mpimet.mpg.de>
Date: Mon, 27 Jun 2022 11:16:55 +0200
Subject: [PATCH] Move CI script for GCC to Levante.

---
 .../test.all.gcc-11.2.0}                      |  16 +-
 .ci/bb/levante-dkrz/utils.sh                  | 140 ++++++++++++++++++
 2 files changed, 154 insertions(+), 2 deletions(-)
 rename .ci/bb/{mistral-dkrz/test.all.gcc-6.4.0 => levante-dkrz/test.all.gcc-11.2.0} (57%)
 create mode 100644 .ci/bb/levante-dkrz/utils.sh

diff --git a/.ci/bb/mistral-dkrz/test.all.gcc-6.4.0 b/.ci/bb/levante-dkrz/test.all.gcc-11.2.0
similarity index 57%
rename from .ci/bb/mistral-dkrz/test.all.gcc-6.4.0
rename to .ci/bb/levante-dkrz/test.all.gcc-11.2.0
index 2f98abbee..7b7b8a524 100755
--- a/.ci/bb/mistral-dkrz/test.all.gcc-6.4.0
+++ b/.ci/bb/levante-dkrz/test.all.gcc-11.2.0
@@ -7,10 +7,20 @@ script_dir=$(cd "$(dirname "$0")"; pwd)
 top_srcdir=$(cd "${script_dir}/../../.."; pwd)
 
 . "${script_dir}/utils.sh"
-init_gcc640
+init_gcc1120
 
 test -f "${top_srcdir}/configure" || "${top_srcdir}/autogen.sh"
 
+# The configure script does not check the 'lib64' subdirectory, therefore we
+# run it with '--with-eccodes' and the following flags. Note that fixing this
+# in the configure script might be tricky: the 'lib64' subdirectory should be
+# checked first but if it does not exist or does not contain the library, we
+# might end up linking to a library from the linker's default search path
+# (e.g. /usr/lib) instead of the one from "$with_eccodes/lib" because
+# "-L${with_eccodes}/lib64" would be ignored in that case:
+CPPFLAGS="-I${ECCODES_ROOT}/include"
+LDFLAGS="-L${ECCODES_ROOT}/lib64"
+
 # We expect --enable-cf-interface and --enable-ppm-dist-array to be set to
 # 'yes' automatically because of --enable-mpi. Function
 # 'check_all_tests_passed' that we run at the end makes sure that the options
@@ -20,10 +30,12 @@ test -f "${top_srcdir}/configure" || "${top_srcdir}/autogen.sh"
 --enable-iso-c-interface \
 --enable-mpi \
 --enable-option-checking=fatal \
---with-grib-api="${GRIBAPI_ROOT}" \
+--with-eccodes \
 --with-netcdf="${NETCDF_ROOT}" \
 CC="${MPICC}" \
+CPPFLAGS="${CPPFLAGS}" \
 FC="${MPIFC}" \
+LDFLAGS="${LDFLAGS}" \
 MPI_LAUNCH="${MPI_LAUNCH}" \
 PKG_CONFIG_PATH="${YAXT_ROOT}/lib/pkgconfig:${PPM_ROOT}/lib/pkgconfig"
 
diff --git a/.ci/bb/levante-dkrz/utils.sh b/.ci/bb/levante-dkrz/utils.sh
new file mode 100644
index 000000000..0113ac6f8
--- /dev/null
+++ b/.ci/bb/levante-dkrz/utils.sh
@@ -0,0 +1,140 @@
+#
+# Accepts a list of environment modules and loads them witch conflict
+# resolution.
+#
+switch_for_module ()
+{
+  for sfm_module in "$@"; do
+    sfm_module_full=
+    sfm_module_short=
+    case $sfm_module in
+      */*)
+        # The module is provided with the version part:
+        sfm_module_full=$sfm_module
+        sfm_module_short=`echo $sfm_module | sed 's%/[^/]*$%%'` ;;
+      *)
+        # Only the name of the module is provided, get the default version:
+        sfm_module_full=`module show $sfm_module 2>&1 | sed -n "s%^[^ \t]*/\\($sfm_module.*\\):%\\1%p"`
+        sfm_module_short=$sfm_module ;;
+    esac
+
+    # A space-separated list of modules that are already loaded:
+    sfm_loaded_full=`module -t list 2>&1 | tr '\n' ' ' | sed 's/^.*Modulefiles://' | sed 's/(default)//g'`
+
+    # Check whether the requested module if already loaded:
+    if test -n "$sfm_module_full"; then
+      case " $sfm_loaded_full " in
+        *" $sfm_module_full "*)
+          echo "module $sfm_module is already loaded"
+          continue ;;
+      esac
+    fi
+
+    # A list of modules in conflict:
+    sfm_conflicts=`module show $sfm_module 2>&1 | sed -n 's/^conflict\(.*\)/\1/p' | tr '\n\t' '  '`
+
+    # A list of loaded modules without version parts:
+    sfm_loaded_short=`echo "$sfm_loaded_full" | sed 's%\([^ ][^ ]*\)/[^ ]*%\1%g'`
+
+    # Add the short name of the module to the list of conflicts:
+    sfm_conflicts="$sfm_conflicts $sfm_module_short"
+
+    # A list of loaded modules that are in conflict with the requested module:
+    sfm_loaded_conflicts=
+    for sfm_conflict in $sfm_conflicts""; do
+      sfm_loaded_list=      
+      case $sfm_conflict in
+        */*)
+          # The conflict is specified with the version part:
+          sfm_loaded_list=$sfm_loaded_full ;;
+        *)
+          # The conflict is specified without the version part:
+          sfm_loaded_list=$sfm_loaded_short ;;
+      esac
+
+      # Check that the conflicted module is loaded:
+      case " $sfm_loaded_list " in
+        *" $sfm_conflict "*)
+          # The conflict is loaded, check whether it is already added to the
+          # list:
+          case " $sfm_loaded_conflicts " in
+            *" $sfm_conflict "*) ;;
+            *)
+              # The conflict is not in the list, append:
+              sfm_loaded_conflicts="$sfm_loaded_conflicts $sfm_conflict" ;;
+          esac
+        ;;
+      esac
+    done
+
+    # Calculate the number of modules that must be unloaded to before loading
+    # the requested module:
+    sfm_loaded_conflicts_count=`echo $sfm_loaded_conflicts | wc -w`
+
+    case $sfm_loaded_conflicts_count in
+      0)
+        # None of the conflicting modules is loaded:
+        sfm_cmd="module load $sfm_module" ;;
+      1)
+        # There is only one module that must be unloaded, use switch command:
+        sfm_cmd="module switch $sfm_loaded_conflicts $sfm_module" ;;
+      *)
+        # There is more than one module that must be unloaded, unload all of
+        # them and then load the requested one:
+        sfm_cmd="module unload $sfm_loaded_conflicts && module load $sfm_module" ;;
+    esac
+
+    echo "$sfm_cmd"
+    eval "$sfm_cmd"
+  done
+}
+
+#
+# Initializes the environment.
+#
+init_env ()
+{
+  :
+}
+
+#
+# Sets variables for tests with GCC 11.2.0.
+#
+init_gcc1120 ()
+{
+  init_env
+  switch_for_module gcc/11.2.0-gcc-11.2.0 openmpi/4.1.2-gcc-11.2.0
+
+  CC=gcc
+  CXX=g++
+  FC=gfortran
+  MPICC=mpicc
+  MPIFC=mpif90
+  MPI_LAUNCH="$(which mpirun)"
+
+  ECCODES_ROOT='/sw/spack-levante/eccodes-2.21.0-4ywkk4'
+  NETCDF_ROOT='/sw/spack-levante/netcdf-c-4.8.1-6qheqr'
+  PPM_ROOT='/work/mh0287/m300488/libcdi-ci-sw/install/ppm-1.0.8-gcc-11.2.0'
+  YAXT_ROOT='/work/mh0287/m300488/libcdi-ci-sw/install/yaxt-0.9.3-gcc-11.2.0'
+
+  # Here we fix a never-ending story with Libtool overlinkning, absence of
+  # '*.la' files when they could help, and '-Wl,--disable/enable-new-dtags':
+  export LD_LIBRARY_PATH="${ECCODES_ROOT}/lib64:${NETCDF_ROOT}/lib:${LD_LIBRARY_PATH-}"
+}
+
+#
+# Accepts a path to a file containing the testsuite summary (either the
+# 'test-suite.log' or the standard output of the 'make check' command) and
+# checks whether all tests were run and passed.
+#
+check_all_tests_passed ()
+{
+  awk '/^# TOTAL: /{ total=$3 };
+       /^# PASS: /{ pass=$3 };
+       END {
+         if ( total == "" || total != pass ) {
+           print "ERROR: the total number of tests is not equal to the number of passed tests";
+           exit 1;
+         }
+       }' $1 >&2
+}
-- 
GitLab