From 15f4de232ab4dce43232fab760866890f0e64503 Mon Sep 17 00:00:00 2001
From: Boris Basic <boris.basic@edf.fr>
Date: Fri, 24 Feb 2023 08:58:01 +0100
Subject: [PATCH] Downgrade METIS to 5.1.0 and libaed2 to 1.2.0

Versions 5.1.1 of METIS and 1.3.0 of libaed2 lead to crashes.
Also:
- Fix HDF5 and MED versions in pysource batches
- Upgrade Mingw version to the latest stable (12.2)
---
 README.md                     |   2 +-
 build-telemac-deps.bat        |  18 +++---
 config/pysource.win.bat       |   8 +--
 config/pysource.win.debug.bat |   8 +--
 patches/gklib.patch           | 103 ----------------------------------
 patches/libaed2-1.2.0.patch   |  11 ++++
 patches/libaed2-1.3.0.patch   |  12 ----
 patches/metis-5.1.1.patch     |  79 --------------------------
 8 files changed, 27 insertions(+), 214 deletions(-)
 delete mode 100644 patches/gklib.patch
 create mode 100644 patches/libaed2-1.2.0.patch
 delete mode 100644 patches/libaed2-1.3.0.patch
 delete mode 100644 patches/metis-5.1.1.patch

diff --git a/README.md b/README.md
index 7fb6102..9851591 100644
--- a/README.md
+++ b/README.md
@@ -7,7 +7,7 @@ This provides a set of scripts and a configuration file allowing to build TELEMA
 - **Git**: download it from [gitforwindows.org](https://gitforwindows.org/) and install it with the LFS extension.
 - **CMake**: install the latest 64 bits version available from [cmake.org](https://cmake.org), and ensure that CMake `bin` directory is added to your `PATH` environment variable.<br>
 Note: to install CMake without administrator rights, you need to download and extract the zip file and set your `PATH` manually.
-- **Mingw-w64**: download the 8.5 version from [winlibs](https://github.com/brechtsanders/winlibs_mingw/releases/download/8.5.0-9.0.0-r1/winlibs-x86_64-posix-seh-gcc-8.5.0-mingw-w64-9.0.0-r1.zip) and extract it to `C:\`.<br>
+- **Mingw-w64**: download the 12.2 version from [winlibs](https://github.com/brechtsanders/winlibs_mingw/releases/download/12.2.0-15.0.7-10.0.0-msvcrt-r4/winlibs-x86_64-posix-seh-gcc-12.2.0-mingw-w64msvcrt-10.0.0-r4.zip) and extract it to `C:\`.<br>
 Note: if you don't install Mingw-w64 in `C:\`, you will need to add its `bin` directory to your `PATH`.
 
 ## Proxy configuration
diff --git a/build-telemac-deps.bat b/build-telemac-deps.bat
index 95f9f3c..b403181 100644
--- a/build-telemac-deps.bat
+++ b/build-telemac-deps.bat
@@ -56,12 +56,12 @@ rem * Set dependency versions *
 rem ***************************
 set hdf5=hdf5-1.10.9
 set med=med-4.1.1
-set metis=metis-5.1.1
+set metis=metis-5.1.0
 set msmpi=msmpi-10.1.2
 set openblas=openblas-0.3.21
 set scalapack=scalapack-2.1.0
 set mumps=mumps-5.4.1
-set aed2=libaed2-1.3.0
+set aed2=libaed2-1.2.0
 set gotm=gotm-2019-06-14-opentelemac
 set python=python-3.7.9
 rem GDAL, Fiona and Rasterio Python packages cannot be installed using PyPI on Windows
@@ -150,12 +150,8 @@ exit /b 0
 rem *********
 rem * METIS *
 rem *********
-call :download %metis% "https://github.com/KarypisLab/METIS/archive/refs/tags/v5.1.1-DistDGL-v0.5.zip" || exit /b 1
-pushd %metis%
-rmdir GKLib 2>nul
-call :download GKLib "https://github.com/KarypisLab/GKlib/archive/refs/tags/METIS-v5.1.1-DistDGL-0.5.zip" || popd && exit /b 1
-popd
-set cmake_config_options=-DBUILD_SHARED_LIBS:BOOL=ON
+call :download %metis% "https://github.com/scivision/METIS/archive/refs/tags/v5.1.0.3.tar.gz" || exit /b 1
+set cmake_config_options=-DBUILD_SHARED_LIBS:BOOL=OFF
 call :cmake_build %metis% || exit /b 1
 exit /b 0
 
@@ -243,7 +239,7 @@ exit /b 0
 rem ********
 rem * AED2 *
 rem ********
-call :download %aed2% "https://github.com/AquaticEcoDynamics/libaed2/archive/refs/tags/v1.3.0.zip" || exit /b 1
+call :download %aed2% "http://www.opentelemac.org/index.php/component/jdownloads/finish/39-manual-installation-sources/2126-aed2m/0?Itemid=55" || exit /b 1
 echo Building libaed2 with %compiler_name% compiler...
 set install_dir=%install_path:/=\%\%aed2%
 rd /s/q %install_dir% 2>nul
@@ -251,8 +247,8 @@ pushd %aed2%
 mingw32-make || goto build_aed2_error
 mkdir %install_dir%
 xcopy /e/y include %install_dir%\include\
-xcopy /e/y mod %install_dir%\include\
-xcopy /e/y lib %install_dir%\lib\
+xcopy /y .\*.mod %install_dir%\include\
+xcopy /y .\*.a %install_dir%\lib\
 popd
 exit /b 0
 
diff --git a/config/pysource.win.bat b/config/pysource.win.bat
index 10d8840..0b3a055 100644
--- a/config/pysource.win.bat
+++ b/config/pysource.win.bat
@@ -38,13 +38,13 @@ set PYTHONPATH=%HOMETEL%\builds\%USETELCFG%\wrap_api\lib;%PYTHONPATH%
 
 rem External libraries versions
 rem HDF
-set HDF5HOME=%TELEMACDEPS%\hdf5-1.10.8
+set HDF5HOME=%TELEMACDEPS%\hdf5-1.10.9
 set PATH=%HDF5HOME%\bin;%PATH%
 rem MED
-set MEDHOME=%TELEMACDEPS%\med-4.1.0
+set MEDHOME=%TELEMACDEPS%\med-4.1.1
 set PATH=%MEDHOME%\bin;%PATH%
 rem METIS
-set METISHOME=%TELEMACDEPS%\metis-5.1.1
+set METISHOME=%TELEMACDEPS%\metis-5.1.0
 set PATH=%METISHOME%\bin;%PATH%
 rem MSPMPI
 set MPIHOME=%TELEMACDEPS%\msmpi-10.1.2
@@ -54,7 +54,7 @@ set OPENBLASHOME=%TELEMACDEPS%\openblas-0.3.21
 set SCALAPACKHOME=%TELEMACDEPS%\scalapack-2.1.0
 set MUMPSHOME=%TELEMACDEPS%\mumps-5.4.1
 rem AED
-set AEDHOME=%TELEMACDEPS%\libaed2-1.3.0
+set AEDHOME=%TELEMACDEPS%\libaed2-1.2.0
 rem GOTM
 set GOTMHOME=%TELEMACDEPS%\gotm-2019-06-14-opentelemac
 set PATH=%GOTMHOME%\bin;%PATH%
diff --git a/config/pysource.win.debug.bat b/config/pysource.win.debug.bat
index 23bd846..28988e3 100644
--- a/config/pysource.win.debug.bat
+++ b/config/pysource.win.debug.bat
@@ -38,13 +38,13 @@ set PYTHONPATH=%HOMETEL%\builds\%USETELCFG%\wrap_api\lib;%PYTHONPATH%
 
 rem External libraries versions
 rem HDF
-set HDF5HOME=%TELEMACDEPS%\hdf5-1.10.8
+set HDF5HOME=%TELEMACDEPS%\hdf5-1.10.9
 set PATH=%HDF5HOME%\bin;%PATH%
 rem MED
-set MEDHOME=%TELEMACDEPS%\med-4.1.0
+set MEDHOME=%TELEMACDEPS%\med-4.1.1
 set PATH=%MEDHOME%\bin;%PATH%
 rem METIS
-set METISHOME=%TELEMACDEPS%\metis-5.1.1
+set METISHOME=%TELEMACDEPS%\metis-5.1.0
 set PATH=%METISHOME%\bin;%PATH%
 rem MSPMPI
 set MPIHOME=%TELEMACDEPS%\msmpi-10.1.2
@@ -54,7 +54,7 @@ set OPENBLASHOME=%TELEMACDEPS%\openblas-0.3.21
 set SCALAPACKHOME=%TELEMACDEPS%\scalapack-2.1.0
 set MUMPSHOME=%TELEMACDEPS%\mumps-5.4.1
 rem AED
-set AEDHOME=%TELEMACDEPS%\libaed2-1.3.0
+set AEDHOME=%TELEMACDEPS%\libaed2-1.2.0
 rem GOTM
 set GOTMHOME=%TELEMACDEPS%\gotm-2019-06-14-opentelemac
 set PATH=%GOTMHOME%\bin;%PATH%
diff --git a/patches/gklib.patch b/patches/gklib.patch
deleted file mode 100644
index 5b6e20f..0000000
--- a/patches/gklib.patch
+++ /dev/null
@@ -1,103 +0,0 @@
-diff -ur GKlib/GKlibSystem.cmake GKlib-patched/GKlibSystem.cmake
---- GKlib/GKlibSystem.cmake	2020-09-13 21:54:26.000000000 +0200
-+++ GKlib-patched/GKlibSystem.cmake	2022-01-07 15:26:55.463399500 +0100
-@@ -19,7 +19,7 @@
-   set(GKlib_COPTS "/Ox")
-   set(GKlib_COPTIONS "-DWIN32 -DMSC -D_CRT_SECURE_NO_DEPRECATE -DUSE_GKREGEX")
- elseif(MINGW)
--  set(GKlib_COPTS "-DUSE_GKREGEX")
-+  set(GKlib_COPTIONS "-DUSE_GKREGEX")
- else()
-   set(GKlib_COPTIONS "-DLINUX -D_FILE_OFFSET_BITS=64")
- endif(MSVC)
-@@ -34,7 +34,7 @@
-       set(GKlib_COPTIONS "${GKlib_COPTIONS} -fPIC")
-   endif(NOT MINGW)
- # GCC warnings.
--  set(GKlib_COPTIONS "${GKlib_COPTIONS} -Werror -Wall -pedantic -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-variable -Wno-unknown-pragmas -Wno-unused-label")
-+  set(GKlib_COPTIONS "${GKlib_COPTIONS} -Werror -Wall -pedantic -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-variable -Wno-unknown-pragmas -Wno-unused-label -Wno-maybe-uninitialized")
- elseif(${CMAKE_C_COMPILER_ID} MATCHES "Sun")
- # Sun insists on -xc99.
-   set(GKlib_COPTIONS "${GKlib_COPTIONS} -xc99")
-diff -ur GKlib/gk_arch.h GKlib-patched/gk_arch.h
---- GKlib/gk_arch.h	2020-09-13 21:54:26.000000000 +0200
-+++ GKlib-patched/gk_arch.h	2022-01-07 14:40:00.573084500 +0100
-@@ -42,7 +42,9 @@
- #endif
-   #include <inttypes.h>
-   #include <sys/types.h>
--  #include <sys/resource.h>
-+  #ifndef __MINGW32__
-+    #include <sys/resource.h>
-+  #endif
-   #include <sys/time.h>
-   #include <unistd.h>
- #endif
-diff -ur GKlib/gk_getopt.h GKlib-patched/gk_getopt.h
---- GKlib/gk_getopt.h	2020-09-13 21:54:26.000000000 +0200
-+++ GKlib-patched/gk_getopt.h	2022-01-07 14:44:18.222358300 +0100
-@@ -52,11 +52,11 @@
- 
- 
- /* Function prototypes */
--extern int gk_getopt(int __argc, char **__argv, char *__shortopts);
--extern int gk_getopt_long(int __argc, char **__argv, char *__shortopts,
--              struct gk_option *__longopts, int *__longind);
--extern int gk_getopt_long_only (int __argc, char **__argv,
--              char *__shortopts, struct gk_option *__longopts, int *__longind);
-+extern int gk_getopt(int argc, char **argv, char *shortopts);
-+extern int gk_getopt_long(int argc, char **argv, char *shortopts,
-+              struct gk_option *longopts, int *longind);
-+extern int gk_getopt_long_only (int argc, char **argv,
-+              char *shortopts, struct gk_option *longopts, int *longind);
- 
- 
- 
-diff -ur GKlib/gkregex.c GKlib-patched/gkregex.c
---- GKlib/gkregex.c	2020-09-13 21:54:26.000000000 +0200
-+++ GKlib-patched/gkregex.c	2022-01-08 23:34:27.270118500 +0100
-@@ -484,15 +484,17 @@
- #define re_string_skip_bytes(pstr,idx) ((pstr)->cur_idx += (idx))
- #define re_string_set_index(pstr,idx) ((pstr)->cur_idx = (idx))
- 
--#ifdef __GNUC__
--# define alloca(size)   __builtin_alloca (size)
--# define HAVE_ALLOCA 1
--#elif defined(_MSC_VER)
--# include <malloc.h>
--# define alloca _alloca
--# define HAVE_ALLOCA 1
--#else
--# error No alloca()
-+#ifndef alloca
-+# ifdef __GNUC__
-+#   define alloca(size)   __builtin_alloca (size)
-+#   define HAVE_ALLOCA 1
-+# elif defined(_MSC_VER)
-+#   include <malloc.h>
-+#   define alloca _alloca
-+#   define HAVE_ALLOCA 1
-+# else
-+#   error No alloca()
-+# endif
- #endif
- 
- #ifndef _LIBC
-@@ -5086,7 +5088,7 @@
-     old_tree = NULL;
- 
-   if (elem->token.type == SUBEXP)
--    postorder (elem, mark_opt_subexp, (void *) (long) elem->token.opr.idx);
-+    postorder (elem, mark_opt_subexp, (void *) (size_t) elem->token.opr.idx);
- 
-   tree = create_tree (dfa, elem, NULL, (end == -1 ? OP_DUP_ASTERISK : OP_ALT));
-   if (BE (tree == NULL, 0))
-@@ -6298,7 +6300,7 @@
- static reg_errcode_t
- mark_opt_subexp (void *extra, bin_tree_t *node)
- {
--  int idx = (int) (long) extra;
-+  int idx = (int) (size_t) extra;
-   if (node->token.type == SUBEXP && node->token.opr.idx == idx)
-     node->token.opt_subexp = 1;
- 
diff --git a/patches/libaed2-1.2.0.patch b/patches/libaed2-1.2.0.patch
new file mode 100644
index 0000000..bbbfaf4
--- /dev/null
+++ b/patches/libaed2-1.2.0.patch
@@ -0,0 +1,11 @@
+diff -ur libaed2-1.2.0/Makefile libaed2-1.2.0-patched/Makefile
+--- libaed2-1.2.0/Makefile	2018-05-17 15:36:37.000000000 +0200
++++ libaed2-1.2.0-patched/Makefile	2023-02-24 08:44:36.261956600 +0100
+@@ -47,7 +47,6 @@
+   LIBS+=-L/opt/open64/lib -Wl,-rpath=/opt/open64/lib
+ else
+   F90=gfortran
+-  INCLUDES+=-I/usr/include
+   DEBUG_FFLAGS=-g -fbacktrace
+   OPT_FFLAGS=-O3
+   FFLAGS=-fPIC -Wall -ffree-line-length-none -std=f2003 $(DEFINES) $(INCLUDES) -fall-intrinsics -Wno-unused-dummy-argument -fno-range-check
diff --git a/patches/libaed2-1.3.0.patch b/patches/libaed2-1.3.0.patch
deleted file mode 100644
index cdcddbe..0000000
--- a/patches/libaed2-1.3.0.patch
+++ /dev/null
@@ -1,12 +0,0 @@
-diff -ur libaed2-1.3.0/Makefile libaed2-1.3.0-patched/Makefile
---- libaed2-1.3.0/Makefile	2018-12-12 02:03:03.000000000 +0100
-+++ libaed2-1.3.0-patched/Makefile	2022-01-14 19:45:29.687008000 +0100
-@@ -68,7 +68,7 @@
-   endif
-   FFLAGS+=-r8
- else
--  INCLUDES+=-I/usr/include
-+  F90=gfortran
-   DEBUG_FFLAGS=-g -fbacktrace
-   OPT_FFLAGS=-O3
-   FFLAGS=-fPIC -Wall -J ${moddir} -ffree-line-length-none -std=f2008 $(DEFINES) $(INCLUDES) -fall-intrinsics -Wno-unused -Wno-unused-dummy-argument -fno-range-check -Wno-integer-division
diff --git a/patches/metis-5.1.1.patch b/patches/metis-5.1.1.patch
deleted file mode 100644
index 1e1903c..0000000
--- a/patches/metis-5.1.1.patch
+++ /dev/null
@@ -1,79 +0,0 @@
-diff -ur metis-5.1.1/CMakeLists.txt metis-5.1.1-patched/CMakeLists.txt
---- metis-5.1.1/CMakeLists.txt	2020-09-13 22:46:57.000000000 +0200
-+++ metis-5.1.1-patched/CMakeLists.txt	2022-01-07 14:48:37.770277300 +0100
-@@ -4,11 +4,7 @@
- set(GKLIB_PATH "${CMAKE_SOURCE_DIR}/GKlib" CACHE PATH "path to GKlib")
- set(SHARED FALSE CACHE BOOL "build a shared library")
- 
--if(MSVC)
--  set(METIS_INSTALL FALSE)
--else()
--  set(METIS_INSTALL TRUE)
--endif()
-+option(METIS_INSTALL "Install METIS libraries" ON)
- 
- # Configure libmetis library.
- if(SHARED)
-@@ -20,26 +16,26 @@
- include(${GKLIB_PATH}/GKlibSystem.cmake)
- 
- # METIS' custom options
--#option(IDX64 "enable 64 bit ints" OFF)
--#option(REAL64 "enable 64 bit floats (i.e., double)" OFF)
--#if(IDX64)
--#  set(METIS_COPTIONS "${METIS_COPTIONS} -DIDXTYPEWIDTH=64")
--#else()
--#  set(METIS_COPTIONS "${METIS_COPTIONS} -DIDXTYPEWIDTH=32")
--#endif(IDX64)
--#if(REAL64)
--#  set(METIS_COPTIONS "${METIS_COPTIONS} -DREALTYPEWIDTH=64")
--#else()
--#  set(METIS_COPTIONS "${METIS_COPTIONS} -DREALTYPEWIDTH=32")
--#endif(REAL64)
--#
--#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${METIS_COPTIONS}")
-+option(IDX64 "enable 64 bit ints" OFF)
-+option(REAL64 "enable 64 bit floats (i.e., double)" OFF)
-+if(IDX64)
-+ set(METIS_COPTIONS "${METIS_COPTIONS} -DIDXTYPEWIDTH=64")
-+else()
-+ set(METIS_COPTIONS "${METIS_COPTIONS} -DIDXTYPEWIDTH=32")
-+endif(IDX64)
-+if(REAL64)
-+ set(METIS_COPTIONS "${METIS_COPTIONS} -DREALTYPEWIDTH=64")
-+else()
-+ set(METIS_COPTIONS "${METIS_COPTIONS} -DREALTYPEWIDTH=32")
-+endif(REAL64)
-+
-+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${METIS_COPTIONS}")
- 
- 
- # Add include directories.
- include_directories(${GKLIB_PATH})
--include_directories(build/xinclude)
-+include_directories(include)
- # Recursively look for CMakeLists.txt in subdirs.
--add_subdirectory("build/xinclude")
-+add_subdirectory("include")
- add_subdirectory("libmetis")
- add_subdirectory("programs")
-diff -ur metis-5.1.1/libmetis/CMakeLists.txt metis-5.1.1-patched/libmetis/CMakeLists.txt
---- metis-5.1.1/libmetis/CMakeLists.txt	2020-09-13 22:46:57.000000000 +0200
-+++ metis-5.1.1-patched/libmetis/CMakeLists.txt	2022-01-07 14:45:33.939128900 +0100
-@@ -8,9 +8,15 @@
-   target_link_libraries(metis m)
- endif()
- 
-+if(WIN32)
-+  set(RT_DEST bin)
-+else()
-+  set(RT_DEST lib)
-+endif()
-+
- if(METIS_INSTALL)
-   install(TARGETS metis
-     LIBRARY DESTINATION lib
--    RUNTIME DESTINATION lib
-+    RUNTIME DESTINATION ${RT_DEST}
-     ARCHIVE DESTINATION lib)
- endif()
-- 
GitLab