Author: brane
Date: Mon Aug  4 09:15:23 2025
New Revision: 1927605

Log:
In the CMake build: Search for most dependencies with pkg-config first,
and find_package if that fails.

* CMakeLists.txt:
   - Use pkg-config to look for OpenSSL, zlib, Brotli, GSSAPI and Unbound.
   - Write the pkg-config file with dependencies as appropriate.
* build/FindUnbound.cmake: Remove pkg-config related parts.
* build/SerfFindPkgConfig.cmake: New.

Added:
   serf/trunk/build/SerfFindPkgConfig.cmake
Modified:
   serf/trunk/CMakeLists.txt
   serf/trunk/build/FindUnbound.cmake

Modified: serf/trunk/CMakeLists.txt
==============================================================================
--- serf/trunk/CMakeLists.txt   Mon Aug  4 08:30:18 2025        (r1927604)
+++ serf/trunk/CMakeLists.txt   Mon Aug  4 09:15:23 2025        (r1927605)
@@ -89,6 +89,7 @@ if(NOT CMAKE_BUILD_TYPE)
 endif()
 
 include(GNUInstallDirs)
+include(SerfFindPkgConfig)
 include(SerfPlatform)
 
 # On the Mac: Use dependencies from Homebrew or MacPorts
@@ -245,11 +246,30 @@ if(EXPAT)
 endif(EXPAT)
 
 # Find required dependencies
-find_package(OpenSSL REQUIRED)
-find_package(ZLIB REQUIRED)
 find_package(APR REQUIRED)
 find_package(APRUtil REQUIRED)
 
+
+SerfFindPkgConfig(OpenSSL_SSL OPENSSL_ROOT_DIR libssl OpenSSL::SSL)
+SerfFindPkgConfig(OpenSSL_Crypto OPENSSL_ROOT_DIR libcrypto OpenSSL::Crypto)
+if(NOT (OpenSSL_SSL_FOUND AND OpenSSL_Crypto_FOUND))
+  find_package(OpenSSL REQUIRED)
+  if(OpenSSL_FOUND)
+    set(OPENSSL_PC_LIBS ${OPENSSL_LIBRARIES})
+  endif()
+else()
+  set(OpenSSL_FOUND TRUE)
+  set(OPENSSL_INCLUDE_DIR ${OPENSSL_SSL_INCLUDE_DIR})
+  set(OPENSSL_PC_REQUIRES ${OPENSSL_SSL_PC_REQUIRES} 
${OPENSSL_CRYPTO_PC_REQUIRES})
+  set(OPENSSL_LIBRARIES ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY})
+endif()
+
+SerfFindPkgConfig(ZLIB ZLIB_ROOT zlib ZLIB::ZLIB)
+if(NOT ZLIB_FOUND)
+  find_package(ZLIB REQUIRED)
+  set(ZLIB_PC_LIBS ${ZLIB_LIBRARIES})
+endif()
+
 # We do not want or need OpenSSL's compatibility macros.
 list(APPEND SERF_C_DEFINES "OPENSSL_NO_DEPRECATED")
 
@@ -258,12 +278,24 @@ list(APPEND SERF_C_DEFINES "OPENSSL_NO_S
 
 # Find optional dependencies
 if(USE_BROTLI)
-  find_package(Brotli)
+  SerfFindPkgConfig(Brotli Brotli_ROOT libbrotlidec Brotli::Decode)
+  if(NOT Brotli_FOUND)
+    find_package(Brotli)
+    if(Brotli_FOUND)
+      set(BROTLI_PC_LIBS ${BROTLI_DECODE_LIBRARY})
+    endif()
+  endif()
 endif()
 
 if(NOT SERF_WINDOWS)
   if(USE_GSSAPI)
-    find_package(GSSAPI)
+    SerfFindPkgConfig(GSSAPI GSSAPI_ROOT krb5-gssapi KRB5::GSSAPI)
+    if(NOT GSSAPI_FOUND)
+      find_package(GSSAPI)
+      if(GSSAPI_FOUND)
+        set(GSSAPI_PC_LIBS ${GSSAPI_LIBRARIES})
+      endif()
+    endif()
   endif()
 else()
   # We use SSPI on Windows and there's no Kerberos/GSSAPI port there.
@@ -272,25 +304,18 @@ else()
     message(WARNING "option GSSAPI_ROOT is not implemented on this platform")
   endif()
 endif()
+
 if(USE_UNBOUND)
-  find_package(Unbound)
+  SerfFindPkgConfig(Unbound Unbound_ROOT libunbound Unbound::Unbound)
+  if(NOT Unbound_FOUND)
+    find_package(Unbound)
+    if(Unbound_FOUND)
+      set(UNBOUND_PC_LIBS "${UNBOUND_LIBRARY}")
+    endif()
+  endif()
 endif()
 
 # Calculate the set of private and public targets
-set(SERF_PRIVATE_TARGETS OpenSSL::Crypto OpenSSL::SSL ZLIB::ZLIB)
-if(Brotli_FOUND)
-  list(APPEND SERF_PRIVATE_TARGETS Brotli::Decode)
-endif()
-if(GSSAPI_FOUND)
-  list(APPEND SERF_C_DEFINES "SERF_HAVE_GSSAPI")
-  list(APPEND SERF_PRIVATE_TARGETS KRB5::GSSAPI)
-endif()
-if(Unbound_FOUND)
-  list(APPEND SERF_C_DEFINES "SERF_HAVE_ASYNC_RESOLVER=1")
-  list(APPEND SERF_C_DEFINES "SERF_HAVE_UNBOUND=1")
-  list(APPEND SERF_PRIVATE_TARGETS Unbound::Unbound)
-endif()
-
 if(APR_STATIC)
   if(SERF_WINDOWS)
     list(APPEND SERF_PUBLIC_TARGETS APR::APR_static)
@@ -307,6 +332,20 @@ else(APR_STATIC)
   endif()
 endif(APR_STATIC)
 
+list(APPEND SERF_PRIVATE_TARGETS OpenSSL::SSL OpenSSL::Crypto ZLIB::ZLIB)
+if(Brotli_FOUND)
+  list(APPEND SERF_PRIVATE_TARGETS Brotli::Decode)
+endif()
+if(GSSAPI_FOUND)
+  list(APPEND SERF_C_DEFINES "SERF_HAVE_GSSAPI")
+  list(APPEND SERF_PRIVATE_TARGETS KRB5::GSSAPI)
+endif()
+if(Unbound_FOUND)
+  list(APPEND SERF_C_DEFINES "SERF_HAVE_ASYNC_RESOLVER=1")
+  list(APPEND SERF_C_DEFINES "SERF_HAVE_UNBOUND=1")
+  list(APPEND SERF_PRIVATE_TARGETS Unbound::Unbound)
+endif()
+
 # Feature tests
 include(SerfChecks)
 CheckNotFunction("BIO_set_init" "NULL, 0" "SERF_NO_SSL_BIO_WRAPPERS"
@@ -457,11 +496,6 @@ if(NOT SKIP_SHARED)
     install(FILES $<TARGET_PDB_FILE:serf_shared>
             CONFIGURATIONS Debug RelWithDebinfo
             DESTINATION "${SERF_INSTALL_RUNTIME}")
-    # string(TOLOWER "${CMAKE_BUILD_TYPE}" config)
-    # if(NOT "${config}" STREQUAL "release")
-    #   install(FILES $<TARGET_PDB_FILE:serf_shared>
-    #           DESTINATION "${SERF_INSTALL_RUNTIME}")
-    # endif()
   endif()
 endif()
 
@@ -515,8 +549,10 @@ function(make_pkgconfig)
   set(VERSION ${SERF_VERSION})
   set(MAJOR ${SERF_MAJOR_VERSION})
   set(REQUIRES
-    "libssl"
-    "libcrypto"
+    ${OPENSSL_PC_REQUIRES}
+    ${ZLIB_PC_REQUIRES}
+    ${BROTLI_PC_REQUIRES}
+    ${GSSAPI_PC_REQUIRES}
     ${UNBOUND_PC_REQUIRES}
   )
   set(CFLAGS
@@ -529,10 +565,10 @@ function(make_pkgconfig)
     ${APRUTIL_LDFLAGS}
     ${APRUTIL_LIBRARIES}
     ${APRUTIL_EXTRALIBS}
-    ${ZLIB_LIBRARIES}
-    ${BROTLI_COMMON_LIBRARY}
-    ${BROTLI_DECODE_LIBRARY}
-    ${GSSAPI_LIBRARIES}
+    ${OPENSSL_PC_LIBS}
+    ${ZLIB_PC_LIBS}
+    ${BROTLI_PC_LIBS}
+    ${GSSAPI_PC_LIBS}
     ${UNBOUND_PC_LIBS}
     ${SERF_STANDARD_LIBRARIES}
   )

Modified: serf/trunk/build/FindUnbound.cmake
==============================================================================
--- serf/trunk/build/FindUnbound.cmake  Mon Aug  4 08:30:18 2025        
(r1927604)
+++ serf/trunk/build/FindUnbound.cmake  Mon Aug  4 09:15:23 2025        
(r1927605)
@@ -43,13 +43,6 @@ cmake_minimum_required(VERSION 3.12)
 #   UNBOUND_INCLUDE_DIR  - Where to find unbound.h.
 #   UNBOUND_LIBRARY      - Linker switches to use with ld to link the library.
 #
-# ::
-#
-#   UNBOUND_PC_REQUIRES  - The name of the pkg-config module for libunbound;
-#                          empty if pkg-config was not used to find the 
library.
-#   UNBOUND_PC_LIBS      - The link libraries for pkg-config, if
-#                          UNBOUND_PC_REQUUIRES is not set.
-#
 # Hints
 # ^^^^^
 #
@@ -57,102 +50,54 @@ cmake_minimum_required(VERSION 3.12)
 
 include(FindPackageHandleStandardArgs)
 include(GNUInstallDirs)
-find_package(PkgConfig QUIET)
-
-# Save the PKG_CONFIG_PATH environment variable
-if(PKG_CONFIG_FOUND)
-  set(_pkg_config_path $ENV{PKG_CONFIG_PATH})
-endif()
 
 set(Unbound_FOUND FALSE)
 if(DEFINED Unbound_ROOT)
   # Search in the provided root path
   set(_root_search PATHS ${Unbound_ROOT} NO_DEFAULT_PATH)
-  list(APPEND _UNBOUND_SEARCHES _root_search)
-
-  # Set the PKG_CONFIG_PATH environment variable
-  if(PKG_CONFIG_FOUND)
-    find_path(_pcfile NAMES "libunbound.pc"
-              ${_root_search}
-              PATH_SUFFIXES
-              "lib/pkgconfig"
-              "${CMAKE_INSTALL_LIBDIR}/pkgconfig"
-              "share/pkgconfig"
-              "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig")
-    if(_pcfile AND EXISTS "${_pcfile}/libunbound.pc")
-      set(ENV{PKG_CONFIG_PATH} ${_pcfile})
-    endif()
-  endif()
+  list(APPEND _unbound_searches _root_search)
 endif()
 
 # Search in default paths
 set(_default_search)
-list(APPEND _UNBOUND_SEARCHES _default_search)
+list(APPEND _unbound_searches _default_search)
 
-# Try pkg-config first
-if(PKG_CONFIG_FOUND)
-  pkg_search_module(UNBOUND QUIET IMPORTED_TARGET libunbound)
-  if(UNBOUND_FOUND)
-    find_package_handle_standard_args(Unbound
-      REQUIRED_VARS UNBOUND_LINK_LIBRARIES UNBOUND_INCLUDEDIR
-      VERSION_VAR UNBOUND_VERSION)
-    if(Unbound_FOUND)
-      add_library(Unbound::Unbound ALIAS PkgConfig::UNBOUND)
-      set(UNBOUND_INCLUDE_DIR ${UNBOUND_INCLUDEDIR})
-      set(UNBOUND_LIBRARY ${UNBOUND_LINK_LIBRARIES})
-      set(UNBOUND_PC_REQUIRES libunbound)
-      set(UNBOUND_PC_LIBS)
+# Try each search configuration
+foreach(search ${_unbound_searches})
+  find_path(UNBOUND_INCLUDE_DIR NAMES "unbound.h" ${${search}}
+            PATH_SUFFIXES "include" "${CMAKE_INSTALL_INCLUDEDIR}")
+  if(UNBOUND_INCLUDE_DIR AND EXISTS "${UNBOUND_INCLUDE_DIR}/unbound.h")
+    # Use the first successful search to find the libraries
+    if(NOT DEFINED libsearch)
+      set(libsearch ${search})
     endif()
   endif()
-endif()
-
-if(NOT Unbound_FOUND)
-  # Try each search configuration
-  foreach(search ${_UNBOUND_SEARCHES})
-    find_path(UNBOUND_INCLUDE_DIR NAMES "unbound.h" ${${search}}
-              PATH_SUFFIXES "include" "${CMAKE_INSTALL_INCLUDEDIR}")
-    if(UNBOUND_INCLUDE_DIR AND EXISTS "${UNBOUND_INCLUDE_DIR}/unbound.h")
-      # Use the first successful search to find the libraries
-      if(NOT DEFINED libsearch)
-        set(libsearch ${search})
-      endif()
-    endif()
-  endforeach()
-
-  find_library(UNBOUND_LIBRARY NAMES "unbound" NAMES_PER_DIR ${${libsearch}}
-               PATH_SUFFIXES "lib" "${CMAKE_INSTALL_LIBDIR}")
+endforeach()
 
-  # Extract the version number from the header
-  if(UNBOUND_INCLUDE_DIR AND EXISTS "${UNBOUND_INCLUDE_DIR}/unbound.h")
-    file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _major
-      REGEX "^ *# *define +UNBOUND_VERSION_MAJOR +[0-9]+.*$")
-    file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _minor
-      REGEX "^ *# *define +UNBOUND_VERSION_MINOR +[0-9]+.*$")
-    file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _micro
-      REGEX "^ *# *define +UNBOUND_VERSION_MICRO +[0-9]+.*$")
-    string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _major ${_major})
-    string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _minor ${_minor})
-    string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _micro ${_micro})
-    set(UNBOUND_VERSION "${_major}.${_minor}.${_micro}")
-  endif()
+find_library(UNBOUND_LIBRARY NAMES "unbound" NAMES_PER_DIR ${${libsearch}}
+             PATH_SUFFIXES "lib" "${CMAKE_INSTALL_LIBDIR}")
 
-  find_package_handle_standard_args(Unbound
-    REQUIRED_VARS UNBOUND_LIBRARY UNBOUND_INCLUDE_DIR
-    VERSION_VAR UNBOUND_VERSION)
-  if(Unbound_FOUND)
-    add_library(Unbound::Unbound UNKNOWN IMPORTED)
-    set_target_properties(Unbound::Unbound PROPERTIES
-      INTERFACE_INCLUDE_DIRECTORIES "${UNBOUND_INCLUDE_DIR}"
-      IMPORTED_LOCATION "${UNBOUND_LIBRARY}")
-    set(UNBOUND_PC_REQUIRES)
-    set(UNBOUND_PC_LIBS "${UNBOUND_LIBRARY}")
-  endif()
+# Extract the version number from the header
+if(UNBOUND_INCLUDE_DIR AND EXISTS "${UNBOUND_INCLUDE_DIR}/unbound.h")
+  file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _major
+    REGEX "^ *# *define +UNBOUND_VERSION_MAJOR +[0-9]+.*$")
+  file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _minor
+    REGEX "^ *# *define +UNBOUND_VERSION_MINOR +[0-9]+.*$")
+  file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _micro
+    REGEX "^ *# *define +UNBOUND_VERSION_MICRO +[0-9]+.*$")
+  string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _major ${_major})
+  string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _minor ${_minor})
+  string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _micro ${_micro})
+  set(UNBOUND_VERSION "${_major}.${_minor}.${_micro}")
 endif()
 
-mark_as_advanced(UNBOUND_INCLUDE_DIR)
-mark_as_advanced(UNBOUND_LIBRARY)
-
-# Restore the PKG_CONFIG_PATH environment variable
-if(PKG_CONFIG_FOUND)
-  set(ENV{PKG_CONFIG_PATH} ${_pkg_config_path})
+find_package_handle_standard_args(Unbound
+  REQUIRED_VARS UNBOUND_LIBRARY UNBOUND_INCLUDE_DIR
+  VERSION_VAR UNBOUND_VERSION)
+if(Unbound_FOUND)
+  add_library(Unbound::Unbound UNKNOWN IMPORTED)
+  set_target_properties(Unbound::Unbound PROPERTIES
+    INTERFACE_INCLUDE_DIRECTORIES "${UNBOUND_INCLUDE_DIR}"
+    IMPORTED_LOCATION "${UNBOUND_LIBRARY}")
+  set(UNBOUND_PC_LIBS "${UNBOUND_LIBRARY}")
 endif()

Added: serf/trunk/build/SerfFindPkgConfig.cmake
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ serf/trunk/build/SerfFindPkgConfig.cmake    Mon Aug  4 09:15:23 2025        
(r1927605)
@@ -0,0 +1,64 @@
+# ===================================================================
+#   Licensed to the Apache Software Foundation (ASF) under one
+#   or more contributor license agreements.  See the NOTICE file
+#   distributed with this work for additional information
+#   regarding copyright ownership.  The ASF licenses this file
+#   to you under the Apache License, Version 2.0 (the
+#   "License"); you may not use this file except in compliance
+#   with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing,
+#   software distributed under the License is distributed on an
+#   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#   KIND, either express or implied.  See the License for the
+#   specific language governing permissions and limitations
+#   under the License.
+# ===================================================================
+
+include(FindPackageHandleStandardArgs)
+include(GNUInstallDirs)
+find_package(PkgConfig QUIET)
+
+# Generate list of symbols to export from shared libraries..
+function(SerfFindPkgConfig name root pkgname target_alias)
+  set(${name}_FOUND FALSE PARENT_SCOPE)
+  if(PKG_CONFIG_FOUND)
+    # Save the PKG_CONFIG_PATH environment variable
+    set(pkg_config_path $ENV{PKG_CONFIG_PATH})
+
+    if(DEFINED ${root})
+      # Set the PKG_CONFIG_PATH environment variable for the search
+      find_path(_${name}_pcdir NAMES "${pkgname}.pc"
+                PATHS ${${root}} NO_DEFAULT_PATH
+                PATH_SUFFIXES
+                "lib/pkgconfig"
+                "${CMAKE_INSTALL_LIBDIR}/pkgconfig"
+                "share/pkgconfig"
+                "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig")
+      if(_${name}_pcdir AND EXISTS "${_${name}_pcdir}/${pkgname}.pc")
+        set(ENV{PKG_CONFIG_PATH} ${_${name}_pcdir})
+      endif()
+    endif()
+
+    string(TOUPPER "${name}" NAME)
+    pkg_search_module(${NAME} QUIET IMPORTED_TARGET ${pkgname})
+    if(${NAME}_FOUND)
+      find_package_handle_standard_args(${name}
+        REQUIRED_VARS ${NAME}_LINK_LIBRARIES ${NAME}_INCLUDEDIR
+        VERSION_VAR ${NAME}_VERSION)
+      if(${name}_FOUND)
+        add_library(${target_alias} ALIAS PkgConfig::${NAME})
+        set(${name}_FOUND ${${name}_FOUND} PARENT_SCOPE)
+        set(${NAME}_INCLUDE_DIR ${${NAME}_INCLUDEDIR} PARENT_SCOPE)
+        set(${NAME}_LIBRARY ${${NAME}_LINK_LIBRARIES} PARENT_SCOPE)
+        set(${NAME}_VERSION ${${NAME}_VERSION} PARENT_SCOPE)
+        set(${NAME}_PC_REQUIRES ${pkgname} PARENT_SCOPE)
+      endif()
+    endif()
+
+    # Restore the PKG_CONFIG_PATH environment variable
+    set(ENV{PKG_CONFIG_PATH} ${pkg_config_path})
+  endif()
+endfunction(SerfFindPkgConfig)

Reply via email to