File 0004-Fix-handling-of-external-dependencies-allow-forcing-.patch of Package failed_pybind11_protobuf

From 2fc54dda601311172670a277ffc56dd35e27d915 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20Br=C3=BCns?= <stefan.bruens@rwth-aachen.de>
Date: Sat, 15 Jun 2024 01:09:03 +0200
Subject: [PATCH 4/6] Fix handling of external dependencies, allow forcing
 system deps

The current implementation reimplements CMake's native handling of
system packages, i.e. "FIND_PACKAGE_ARGS" in `FetchContent_Declare`.

Add options to force usage of system provided abseil-cpp/protobuf/pybind11,
to avoid having different versions here and in dependent packages.

Use the correct version for protobuf, v23.3 corresponds to 4.23.3.

The output for FetchContent is a little bit less verbose now, but in
case debugging is required it is much more useful to use the
"-DFETCHCONTENT_QUIET=False" option.
---
 CMakeLists.txt                    | 62 +++++++++++++++++++++++--------
 cmake/dependencies/CMakeLists.txt | 62 -------------------------------
 2 files changed, 47 insertions(+), 77 deletions(-)
 delete mode 100644 cmake/dependencies/CMakeLists.txt

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 02a773e..8d0fda9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -18,6 +18,9 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
 
 option(BUILD_TESTS "Build tests." OFF)
 option(ENABLE_PYPROTO_API "Enable usage of proto_api." OFF)
+option(USE_SYSTEM_ABSEIL "Force usage of system provided abseil-cpp" OFF)
+option(USE_SYSTEM_PROTOBUF "Force usage of system provided Protobuf" OFF)
+option(USE_SYSTEM_PYBIND "Force usage of system provided pybind11" OFF)
 
 # ============================================================================
 # Find Python
@@ -27,22 +30,51 @@ find_package(Python COMPONENTS Interpreter Development)
 # ============================================================================
 # Build dependencies
 
-set(_absl_repository "https://github.com/abseil/abseil-cpp.git")
-set(_absl_version 20230125)
-set(_absl_tag 20230125.3)
-find_package(absl ${_absl_version} QUIET)
-
-set(_protobuf_repository "https://github.com/protocolbuffers/protobuf.git")
-set(_protobuf_version 3.23.3)
-set(_protobuf_tag v23.3)
-find_package(Protobuf ${_protobuf_version} QUIET)
-
-set(_pybind11_repository "https://github.com/pybind/pybind11.git")
-set(_pybind11_version 2.11.1)
-set(_pybind11_tag v2.11.1)
-find_package(pybind11 ${_pybind11_version} QUIET)
+if(USE_SYSTEM_ABSEIL)
+  # Version omitted, as absl only allows EXACT version matches
+  set(_absl_package_args REQUIRED)
+else()
+  set(_absl_package_args 20230125)
+endif()
+if(USE_SYSTEM_PROTOBUF)
+  set(_protobuf_package_args 4.23.3 REQUIRED)
+else()
+  set(_protobuf_package_args 4.23.3)
+endif()
+if(USE_SYSTEM_PYBIND)
+  set(_pybind11_package_args 2.11.1 REQUIRED)
+else()
+  set(_pybind11_package_args 2.11.1)
+endif()
 
-add_subdirectory(cmake/dependencies dependencies)
+set(ABSL_PROPAGATE_CXX_STD ON)
+set(ABSL_ENABLE_INSTALL ON)
+
+include(FetchContent)
+FetchContent_Declare(
+  absl
+  GIT_REPOSITORY "https://github.com/abseil/abseil-cpp.git"
+  GIT_TAG 20230125.3
+  FIND_PACKAGE_ARGS ${_absl_package_args} NAMES absl)
+
+FetchContent_Declare(
+  Protobuf
+  GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
+  GIT_TAG v23.3
+  GIT_SUBMODULES "" #
+  FIND_PACKAGE_ARGS ${_protobuf_package_args} NAMES protobuf)
+set(protobuf_BUILD_TESTS OFF CACHE INTERNAL "")
+
+FetchContent_Declare(
+  pybind11
+  GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
+  GIT_TAG v2.11.1
+  FIND_PACKAGE_ARGS ${_pybind11_package_args} NAMES pybind11)
+
+message(CHECK_START "Checking for external dependencies")
+list(APPEND CMAKE_MESSAGE_INDENT "  ")
+FetchContent_MakeAvailable(absl Protobuf pybind11)
+list(POP_BACK CMAKE_MESSAGE_INDENT)
 
 # ============================================================================
 # pybind11_proto_utils pybind11 extension module
diff --git a/cmake/dependencies/CMakeLists.txt b/cmake/dependencies/CMakeLists.txt
deleted file mode 100644
index 111b34f..0000000
--- a/cmake/dependencies/CMakeLists.txt
+++ /dev/null
@@ -1,62 +0,0 @@
-include(FetchContent)
-
-# ============================================================================
-# Declare all dependencies first
-
-if(NOT absl_FOUND)
-  set(ABSL_PROPAGATE_CXX_STD ON)
-  set(ABSL_ENABLE_INSTALL ON)
-  FetchContent_Declare(
-    absl
-    GIT_REPOSITORY ${_absl_repository}
-    GIT_TAG ${_absl_tag})
-endif()
-
-# https://stackoverflow.com/questions/63309544/cmake-protobuf-external-to-application-code
-# https://cmake.org/cmake/help/latest/policy/CMP0077.html
-# https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7565/diffs
-if(NOT Protobuf_FOUND)
-  set(protobuf_BUILD_TESTS
-      OFF
-      CACHE INTERNAL "")
-  FetchContent_Declare(
-    Protobuf
-    GIT_REPOSITORY ${_protobuf_repository}
-    GIT_TAG ${_protobuf_tag}
-    GIT_SUBMODULES "")
-endif()
-
-if(NOT pybind11_FOUND)
-  set(PYBIND11_TEST OFF)
-  FetchContent_Declare(
-    pybind11
-    GIT_REPOSITORY ${_pybind11_repository}
-    GIT_TAG ${_pybind11_tag})
-endif()
-
-# ============================================================================
-# Make dependencies avaialble
-
-if(NOT absl_FOUND)
-  message(CHECK_START "Fetching Abseil-cpp")
-  list(APPEND CMAKE_MESSAGE_INDENT "  ")
-  FetchContent_MakeAvailable(absl)
-  list(POP_BACK CMAKE_MESSAGE_INDENT)
-  message(CHECK_PASS "fetched")
-endif()
-
-if(NOT Protobuf_FOUND)
-  message(CHECK_START "Fetching Protobuf")
-  list(APPEND CMAKE_MESSAGE_INDENT "  ")
-  FetchContent_MakeAvailable(Protobuf)
-  list(POP_BACK CMAKE_MESSAGE_INDENT)
-  message(CHECK_PASS "fetched")
-endif()
-
-if(NOT pybind11_FOUND)
-  message(CHECK_START "Fetching pybind11")
-  list(APPEND CMAKE_MESSAGE_INDENT "  ")
-  FetchContent_MakeAvailable(pybind11)
-  list(POP_BACK CMAKE_MESSAGE_INDENT)
-  message(CHECK_PASS "fetched")
-endif()
-- 
2.45.0

openSUSE Build Service is sponsored by