From 085d70df5a9b4069bd4b5fb94ef0f24f2a00ef38 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= <klinkovsky@mmg.fjfi.cvut.cz>
Date: Thu, 14 Oct 2021 15:29:26 +0200
Subject: [PATCH] Skip downloading and installing pybind11 when it is available
 on the system

---
 src/Python/CMakeLists.txt    | 36 ++++++++++++++++++++----------------
 src/Python/pybind11.cmake.in |  4 +++-
 2 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/src/Python/CMakeLists.txt b/src/Python/CMakeLists.txt
index 71997fd1e6..505e5f1942 100644
--- a/src/Python/CMakeLists.txt
+++ b/src/Python/CMakeLists.txt
@@ -11,23 +11,27 @@ if( PYTHONINTERP_FOUND )
 endif()
 
 if( PYTHONLIBS_FOUND )
-   # download and build pybind11 at configure time
-   configure_file(pybind11.cmake.in ${CMAKE_BINARY_DIR}/pybind11-download/CMakeLists.txt)
-   execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
-      RESULT_VARIABLE result
-      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/pybind11-download )
-   if(result)
-      message(FATAL_ERROR "CMake step for pybind11 failed: ${result}")
-   endif()
-   execute_process(COMMAND ${CMAKE_COMMAND} --build .
-      RESULT_VARIABLE result
-      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/pybind11-download )
-   if(result)
-      message(FATAL_ERROR "Build step for pybind11 failed: ${result}")
-   endif()
+   # check if pybind11 exists on the system
+   find_package(pybind11 QUIET)
+   if( NOT pybind11_FOUND )
+      # download and build pybind11 at configure time
+      configure_file(pybind11.cmake.in ${CMAKE_BINARY_DIR}/pybind11-download/CMakeLists.txt)
+      execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
+         RESULT_VARIABLE result
+         WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/pybind11-download )
+      if(result)
+         message(FATAL_ERROR "CMake step for pybind11 failed: ${result}")
+      endif()
+      execute_process(COMMAND ${CMAKE_COMMAND} --build .
+         RESULT_VARIABLE result
+         WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/pybind11-download )
+      if(result)
+         message(FATAL_ERROR "Build step for pybind11 failed: ${result}")
+      endif()
 
-   # add the pybind11 subdirectory to provide the pybind11_add_module macro
-   add_subdirectory(${CMAKE_BINARY_DIR}/pybind11-src ${CMAKE_BINARY_DIR}/pybind11-build)
+      # add the pybind11 subdirectory to provide the pybind11_add_module macro
+      add_subdirectory(${CMAKE_BINARY_DIR}/pybind11-src ${CMAKE_BINARY_DIR}/pybind11-build)
+   endif()
 
    # add the subdirectory with our bindings
    add_subdirectory(pytnl)
diff --git a/src/Python/pybind11.cmake.in b/src/Python/pybind11.cmake.in
index 885e62e5e6..1f28673a1d 100644
--- a/src/Python/pybind11.cmake.in
+++ b/src/Python/pybind11.cmake.in
@@ -8,7 +8,9 @@ ExternalProject_Add(pybind11
   GIT_TAG           master
   SOURCE_DIR        "${CMAKE_BINARY_DIR}/pybind11-src"
   BINARY_DIR        "${CMAKE_BINARY_DIR}/pybind11-build"
-  CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} -DPYBIND11_TEST=FALSE
+  CMAKE_ARGS -DPYBIND11_TEST=FALSE
+  # Do not install pybind11 in the system or user's home directory
+  INSTALL_COMMAND   ""
   # Disable update of the external project in an offline build
   # reference: https://stackoverflow.com/a/40423683
   UPDATE_DISCONNECTED ${OFFLINE_BUILD}
-- 
GitLab