From b51a8293680f622f842c9da4c49fed38979dd682 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Fri, 2 Jul 2021 10:32:31 +0200
Subject: [PATCH] [cmake] Moved PETSc dependency to ExternalProject_Add()
 instead of Conan.

Moved tfel dep to DependenciesExternalProject.cmake
---
 CMakeLists.txt                                |  1 +
 scripts/cmake/BuildExternalProject.cmake      |  8 +--
 scripts/cmake/ConanSetup.cmake                | 18 +-----
 scripts/cmake/Dependencies.cmake              | 14 -----
 .../cmake/DependenciesExternalProject.cmake   | 57 +++++++++++++++++++
 scripts/cmake/Find.cmake                      | 21 -------
 6 files changed, 63 insertions(+), 56 deletions(-)
 create mode 100644 scripts/cmake/DependenciesExternalProject.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index ab1d55500e3..50dadd24481 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -78,6 +78,7 @@ include(JobPools)
 include(Find)
 include(CLCacheSetup)
 include(packaging/PackagingDependencies)
+include(DependenciesExternalProject)
 include(Dependencies)
 include(DocumentationSetup)
 include(test/Test)
diff --git a/scripts/cmake/BuildExternalProject.cmake b/scripts/cmake/BuildExternalProject.cmake
index 45e80d6d85d..dfae85566f8 100644
--- a/scripts/cmake/BuildExternalProject.cmake
+++ b/scripts/cmake/BuildExternalProject.cmake
@@ -1,7 +1,7 @@
 # Modified from
 # https://github.com/Sbte/BuildExternalProject/commit/ce1a70996aa538aac17a6faf07db487c3a238838
 macro(BuildExternalProject_find_package target)
-    set(build_dir ${CMAKE_BINARY_DIR}/external/build_${target})
+    set(build_dir ${PROJECT_BINARY_DIR}/_ext/${target})
 
     # Set CMake prefix path so we can look there for the module
     set(_CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH})
@@ -22,7 +22,7 @@ macro(BuildExternalProject_find_package target)
 endmacro()
 
 function(BuildExternalProject target)
-    set(build_dir ${CMAKE_BINARY_DIR}/external/build_${target})
+    set(build_dir ${PROJECT_BINARY_DIR}/_ext/${target})
 
     message(STATUS "Building ${target}")
 
@@ -64,8 +64,6 @@ function(BuildExternalProject target)
         BuildExternalProject_configure(${build_dir})
     endif()
 
-    BuildExternalProject_build(${build_dir})
-
     message(
         STATUS
             "Finished building ${target}. Logs in ${build_dir}/src/${target}-stamp"
@@ -80,6 +78,8 @@ function(BuildExternalProject_configure build_dir)
 
     if(result)
         message(FATAL_ERROR "CMake step for external project failed: ${result}")
+    else()
+        BuildExternalProject_build(${build_dir})
     endif()
 endfunction()
 
diff --git a/scripts/cmake/ConanSetup.cmake b/scripts/cmake/ConanSetup.cmake
index 0e6e7ed38b3..a2f4c6d73d6 100644
--- a/scripts/cmake/ConanSetup.cmake
+++ b/scripts/cmake/ConanSetup.cmake
@@ -11,12 +11,7 @@ if(OGS_USE_CONAN_lower STREQUAL "auto" AND POETRY)
 else()
     find_program(CONAN_CMD conan)
 endif()
-if(NOT CONAN_CMD
-   AND (OGS_USE_PETSC
-        OR OGS_USE_LIS
-        OR OGS_BUILD_GUI
-       )
-)
+if(NOT CONAN_CMD AND (OGS_USE_LIS OR OGS_BUILD_GUI))
     message(WARNING "conan executable not found. Specify CMake option "
         "OGS_USE_CONAN=auto for automatic installation in the build directory "
         "OR install it system-wide (https://www.opengeosys.org/docs/devguide/"
@@ -37,13 +32,6 @@ set(CONAN_SYSTEM_INCLUDES ON)
 
 include(${PROJECT_SOURCE_DIR}/scripts/cmake/conan/conan.cmake)
 
-if(OGS_USE_PETSC)
-    set(CONAN_REQUIRES ${CONAN_REQUIRES} petsc/${ogs.minimum_version.petsc}@bilke/testing)
-    if(OGS_CONAN_USE_SYSTEM_OPENMPI)
-        set(CONAN_OPTIONS ${CONAN_OPTIONS} petsc:skip_install_openmpi=True)
-    endif()
-endif()
-
 if(OGS_USE_LIS)
     list(APPEND CONAN_OPTIONS lis:with_omp=True)
     set(CONAN_REQUIRES ${CONAN_REQUIRES} lis/1.7.37@bilke/stable)
@@ -164,7 +152,3 @@ if(NOT ${OGS_CONAN_BUILD} MATCHES "never|always|missing|outdated")
     message(STATUS "Warning: Resetting CMake variable OGS_CONAN_BUILD to its default value of 'missing'")
     set(OGS_CONAN_BUILD "missing" CACHE INTERNAL "")
 endif()
-
-if(OGS_USE_PETSC)
-    set(PETSC_DIR ${CONAN_PETSC_ROOT} CACHE INTERNAL "")
-endif()
diff --git a/scripts/cmake/Dependencies.cmake b/scripts/cmake/Dependencies.cmake
index 3fcaecd4cf4..64354ee5e6e 100644
--- a/scripts/cmake/Dependencies.cmake
+++ b/scripts/cmake/Dependencies.cmake
@@ -1,17 +1,3 @@
-include(BuildExternalProject)
-
-if(OGS_USE_MFRONT)
-    find_program(MFRONT mfront)
-    if(NOT MFRONT)
-        BuildExternalProject(
-            TFEL GIT_REPOSITORY https://github.com/thelfer/tfel.git GIT_TAG
-            rliv-${ogs.minimum_version.tfel-rliv}
-        )
-        set(ENV{TFELHOME} ${PROJECT_BINARY_DIR}/external/build_TFEL)
-    endif()
-    list(APPEND CMAKE_INSTALL_RPATH $ENV{TFELHOME}/${CMAKE_INSTALL_LIBDIR})
-endif()
-
 set(CMAKE_FOLDER ThirdParty)
 
 # ccache
diff --git a/scripts/cmake/DependenciesExternalProject.cmake b/scripts/cmake/DependenciesExternalProject.cmake
new file mode 100644
index 00000000000..28e597dcfaa
--- /dev/null
+++ b/scripts/cmake/DependenciesExternalProject.cmake
@@ -0,0 +1,57 @@
+# Build dependencies via ExternalProject_Add() at configure time in
+# ${PROJECT_BINARY_DIR}/_ext
+include(BuildExternalProject)
+
+if(OGS_USE_MFRONT)
+    find_program(MFRONT mfront)
+    if(NOT MFRONT)
+        BuildExternalProject(
+            TFEL GIT_REPOSITORY https://github.com/thelfer/tfel.git
+            GIT_TAG rliv-${ogs.minimum_version.tfel-rliv}
+        )
+        set(ENV{TFELHOME} ${PROJECT_BINARY_DIR}/_ext/TFEL)
+    endif()
+    list(APPEND CMAKE_INSTALL_RPATH $ENV{TFELHOME}/${CMAKE_INSTALL_LIBDIR})
+endif()
+
+if(OGS_USE_PETSC)
+    # Force CMake to accept a given PETSc configuration in case the failure of
+    # MPI tests. This may cause the compilation broken.
+    option(FORCE_PETSC_EXECUTABLE_RUNS
+           "Force CMake to accept a given PETSc configuration" ON
+    )
+    if(FORCE_PETSC_EXECUTABLE_RUNS)
+        set(PETSC_EXECUTABLE_RUNS YES)
+    endif()
+
+    find_package(PETSc ${ogs.minimum_version.petsc})
+    if(NOT PETSC_FOUND)
+        set(_configure_opts "")
+        if(ENV{CC})
+            list(APPEND _configure_opts --with-cc=$ENV{CC})
+        endif()
+        if(ENV{CXX})
+            list(APPEND _configure_opts --with-cxx=$ENV{CXX})
+        endif()
+        BuildExternalProject(
+            PETSc
+            LOG_OUTPUT_ON_FAILURE ON
+            GIT_REPOSITORY https://gitlab.com/petsc/petsc.git
+            GIT_TAG v${ogs.minimum_version.petsc}
+            CONFIGURE_COMMAND
+                ./configure --with-fc=0 --download-f2cblaslapack=1
+                --prefix=<INSTALL_DIR> --with-debugging=$<CONFIG:Debug>
+                ${_configure_opts}
+            BUILD_IN_SOURCE ON
+            BUILD_COMMAND make -j all
+            INSTALL_COMMAND make -j install
+        )
+        set(PETSC_DIR ${PROJECT_BINARY_DIR}/_ext/PETSc)
+        find_package(PETSc ${ogs.minimum_version.petsc} REQUIRED)
+    endif()
+
+    add_library(petsc SHARED IMPORTED)
+    target_include_directories(petsc INTERFACE ${PETSC_INCLUDES})
+    set_target_properties(petsc PROPERTIES IMPORTED_LOCATION ${PETSC_LIBRARIES})
+    target_compile_definitions(petsc INTERFACE USE_PETSC)
+endif()
diff --git a/scripts/cmake/Find.cmake b/scripts/cmake/Find.cmake
index faac5224537..fac0eeacbfd 100644
--- a/scripts/cmake/Find.cmake
+++ b/scripts/cmake/Find.cmake
@@ -118,27 +118,6 @@ if(OGS_USE_MKL)
     find_package(MKL REQUIRED)
 endif()
 
-if(OGS_USE_PETSC)
-    message(STATUS "Configuring for PETSc")
-
-    option(FORCE_PETSC_EXECUTABLE_RUNS
-           "Force CMake to accept a given PETSc configuration" ON
-    )
-
-    # Force CMake to accept a given PETSc configuration in case the failure of
-    # MPI tests. This may cause the compilation broken.
-    if(FORCE_PETSC_EXECUTABLE_RUNS)
-        set(PETSC_EXECUTABLE_RUNS YES)
-    endif()
-
-    find_package(PETSc ${ogs.minimum_version.petsc} REQUIRED)
-
-    add_library(petsc SHARED IMPORTED)
-    target_include_directories(petsc INTERFACE ${PETSC_INCLUDES})
-    set_target_properties(petsc PROPERTIES IMPORTED_LOCATION ${PETSC_LIBRARIES})
-    target_compile_definitions(petsc INTERFACE USE_PETSC)
-endif()
-
 # Check MPI package
 if(OGS_USE_MPI)
     find_package(MPI REQUIRED)
-- 
GitLab