From b9e9ec80030a349e1c768f8aec2879d1a6ed28a9 Mon Sep 17 00:00:00 2001
From: Florent Pruvost <florent.pruvost@inria.fr>
Date: Wed, 5 Apr 2023 16:54:41 +0200
Subject: [PATCH] SCOTCH: add a component feature to be able to choose between
 scotcherr and scotcherrexit libraries

---
 modules/find/FindPTSCOTCH.cmake | 13 +++++++++++--
 modules/find/FindSCOTCH.cmake   | 16 ++++++++++++++--
 2 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/modules/find/FindPTSCOTCH.cmake b/modules/find/FindPTSCOTCH.cmake
index 37e9b80..b13259f 100644
--- a/modules/find/FindPTSCOTCH.cmake
+++ b/modules/find/FindPTSCOTCH.cmake
@@ -32,6 +32,7 @@
 #   - MPI
 #
 #  COMPONENTS can be some of the following:
+#   - ERREXIT: to choose libptscotcherrexit as error library instead of libptscotcherr (default)
 #   - ESMUMPS: to activate detection of PT-Scotch with the esmumps interface
 #
 # This module finds headers and ptscotch library.
@@ -70,10 +71,15 @@ include(FindMorseInit)
 morse_find_package_get_envdir(PTSCOTCH)
 
 # Set the version to find
+set(PTSCOTCH_LOOK_FOR_ERREXIT OFF)
 set(PTSCOTCH_LOOK_FOR_ESMUMPS OFF)
 
 if( PTSCOTCH_FIND_COMPONENTS )
   foreach( component ${PTSCOTCH_FIND_COMPONENTS} )
+    if (${component} STREQUAL "ERREXIT")
+      # means we look for ptscotcherrexit library
+      set(PTSCOTCH_LOOK_FOR_ERREXIT ON)
+    endif()
     if (${component} STREQUAL "ESMUMPS")
       # means we look for esmumps library
       set(PTSCOTCH_LOOK_FOR_ESMUMPS ON)
@@ -107,12 +113,15 @@ morse_find_path(PTSCOTCH
 
 # Looking for lib
 # ---------------
-set(PTSCOTCH_libs_to_find "ptscotch;ptscotcherr")
+if (PTSCOTCH_LOOK_FOR_ERREXIT)
+  list(APPEND PTSCOTCH_libs_to_find "ptscotch;ptscotcherrexit;scotch;scotcherrexit")
+else()
+  list(APPEND PTSCOTCH_libs_to_find "ptscotch;ptscotcherr;scotch;scotcherr")
+endif()
 if (PTSCOTCH_LOOK_FOR_ESMUMPS)
   list(INSERT PTSCOTCH_libs_to_find 0 "ptesmumps")
   list(APPEND PTSCOTCH_libs_to_find   "esmumps"  )
 endif()
-list(APPEND PTSCOTCH_libs_to_find "scotch;scotcherr")
 
 morse_find_library(PTSCOTCH
   LIBRARIES ${PTSCOTCH_libs_to_find}
diff --git a/modules/find/FindSCOTCH.cmake b/modules/find/FindSCOTCH.cmake
index 6dfb1b1..5cb2065 100644
--- a/modules/find/FindSCOTCH.cmake
+++ b/modules/find/FindSCOTCH.cmake
@@ -28,6 +28,7 @@
 #              )
 #
 #  COMPONENTS can be some of the following:
+#   - ERREXIT: to choose libscotcherrexit as error library instead of libscotcherr (default)
 #   - ESMUMPS: to activate detection of Scotch with the esmumps interface
 #
 # This module finds headers and scotch library.
@@ -64,10 +65,15 @@ include(FindMorseInit)
 morse_find_package_get_envdir(SCOTCH)
 
 # Set the version to find
+set(SCOTCH_LOOK_FOR_ERREXIT OFF)
 set(SCOTCH_LOOK_FOR_ESMUMPS OFF)
 
 if( SCOTCH_FIND_COMPONENTS )
   foreach( component ${SCOTCH_FIND_COMPONENTS} )
+    if (${component} STREQUAL "ERREXIT")
+      # means we look for scotcherrexit library
+      set(SCOTCH_LOOK_FOR_ERREXIT ON)
+    endif()
     if (${component} STREQUAL "ESMUMPS")
       # means we look for esmumps library
       set(SCOTCH_LOOK_FOR_ESMUMPS ON)
@@ -94,7 +100,11 @@ morse_find_path(SCOTCH
 
 # Looking for lib
 # ---------------
-set(SCOTCH_libs_to_find "scotch;scotcherrexit")
+if (SCOTCH_LOOK_FOR_ERREXIT)
+  list(APPEND SCOTCH_libs_to_find "scotch;scotcherrexit")
+else()
+  list(APPEND SCOTCH_libs_to_find "scotch;scotcherr")
+endif()
 if (SCOTCH_LOOK_FOR_ESMUMPS)
   list(INSERT SCOTCH_libs_to_find 0 "esmumps")
 endif()
@@ -131,7 +141,9 @@ if(SCOTCH_LIBRARIES)
     list(APPEND REQUIRED_FLAGS "${CMAKE_THREAD_LIBS_INIT}")
     list(APPEND REQUIRED_LDFLAGS "${CMAKE_THREAD_LIBS_INIT}")
   else()
-    list(APPEND REQUIRED_LIBS "${CMAKE_THREAD_LIBS_INIT}")
+    if (CMAKE_THREAD_LIBS_INIT)
+      list(APPEND REQUIRED_LIBS "${CMAKE_THREAD_LIBS_INIT}")
+    endif()
   endif()
   set(Z_LIBRARY "Z_LIBRARY-NOTFOUND")
   find_library(Z_LIBRARY NAMES z)
-- 
GitLab