Skip to content

Commit fc79b45

Browse files
committed
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. git-svn-id: https://svn.apache.org/repos/asf/serf/trunk@1927605 13f79535-47bb-0310-9956-ffa450edef68
1 parent 57e9a9c commit fc79b45

File tree

3 files changed

+164
-119
lines changed

3 files changed

+164
-119
lines changed

CMakeLists.txt

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ if(NOT CMAKE_BUILD_TYPE)
8989
endif()
9090

9191
include(GNUInstallDirs)
92+
include(SerfFindPkgConfig)
9293
include(SerfPlatform)
9394

9495
# On the Mac: Use dependencies from Homebrew or MacPorts
@@ -245,11 +246,30 @@ if(EXPAT)
245246
endif(EXPAT)
246247

247248
# Find required dependencies
248-
find_package(OpenSSL REQUIRED)
249-
find_package(ZLIB REQUIRED)
250249
find_package(APR REQUIRED)
251250
find_package(APRUtil REQUIRED)
252251

252+
253+
SerfFindPkgConfig(OpenSSL_SSL OPENSSL_ROOT_DIR libssl OpenSSL::SSL)
254+
SerfFindPkgConfig(OpenSSL_Crypto OPENSSL_ROOT_DIR libcrypto OpenSSL::Crypto)
255+
if(NOT (OpenSSL_SSL_FOUND AND OpenSSL_Crypto_FOUND))
256+
find_package(OpenSSL REQUIRED)
257+
if(OpenSSL_FOUND)
258+
set(OPENSSL_PC_LIBS ${OPENSSL_LIBRARIES})
259+
endif()
260+
else()
261+
set(OpenSSL_FOUND TRUE)
262+
set(OPENSSL_INCLUDE_DIR ${OPENSSL_SSL_INCLUDE_DIR})
263+
set(OPENSSL_PC_REQUIRES ${OPENSSL_SSL_PC_REQUIRES} ${OPENSSL_CRYPTO_PC_REQUIRES})
264+
set(OPENSSL_LIBRARIES ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY})
265+
endif()
266+
267+
SerfFindPkgConfig(ZLIB ZLIB_ROOT zlib ZLIB::ZLIB)
268+
if(NOT ZLIB_FOUND)
269+
find_package(ZLIB REQUIRED)
270+
set(ZLIB_PC_LIBS ${ZLIB_LIBRARIES})
271+
endif()
272+
253273
# We do not want or need OpenSSL's compatibility macros.
254274
list(APPEND SERF_C_DEFINES "OPENSSL_NO_DEPRECATED")
255275

@@ -258,12 +278,24 @@ list(APPEND SERF_C_DEFINES "OPENSSL_NO_STDIO")
258278

259279
# Find optional dependencies
260280
if(USE_BROTLI)
261-
find_package(Brotli)
281+
SerfFindPkgConfig(Brotli Brotli_ROOT libbrotlidec Brotli::Decode)
282+
if(NOT Brotli_FOUND)
283+
find_package(Brotli)
284+
if(Brotli_FOUND)
285+
set(BROTLI_PC_LIBS ${BROTLI_DECODE_LIBRARY})
286+
endif()
287+
endif()
262288
endif()
263289

264290
if(NOT SERF_WINDOWS)
265291
if(USE_GSSAPI)
266-
find_package(GSSAPI)
292+
SerfFindPkgConfig(GSSAPI GSSAPI_ROOT krb5-gssapi KRB5::GSSAPI)
293+
if(NOT GSSAPI_FOUND)
294+
find_package(GSSAPI)
295+
if(GSSAPI_FOUND)
296+
set(GSSAPI_PC_LIBS ${GSSAPI_LIBRARIES})
297+
endif()
298+
endif()
267299
endif()
268300
else()
269301
# We use SSPI on Windows and there's no Kerberos/GSSAPI port there.
@@ -272,25 +304,18 @@ else()
272304
message(WARNING "option GSSAPI_ROOT is not implemented on this platform")
273305
endif()
274306
endif()
307+
275308
if(USE_UNBOUND)
276-
find_package(Unbound)
309+
SerfFindPkgConfig(Unbound Unbound_ROOT libunbound Unbound::Unbound)
310+
if(NOT Unbound_FOUND)
311+
find_package(Unbound)
312+
if(Unbound_FOUND)
313+
set(UNBOUND_PC_LIBS "${UNBOUND_LIBRARY}")
314+
endif()
315+
endif()
277316
endif()
278317

279318
# Calculate the set of private and public targets
280-
set(SERF_PRIVATE_TARGETS OpenSSL::Crypto OpenSSL::SSL ZLIB::ZLIB)
281-
if(Brotli_FOUND)
282-
list(APPEND SERF_PRIVATE_TARGETS Brotli::Decode)
283-
endif()
284-
if(GSSAPI_FOUND)
285-
list(APPEND SERF_C_DEFINES "SERF_HAVE_GSSAPI")
286-
list(APPEND SERF_PRIVATE_TARGETS KRB5::GSSAPI)
287-
endif()
288-
if(Unbound_FOUND)
289-
list(APPEND SERF_C_DEFINES "SERF_HAVE_ASYNC_RESOLVER=1")
290-
list(APPEND SERF_C_DEFINES "SERF_HAVE_UNBOUND=1")
291-
list(APPEND SERF_PRIVATE_TARGETS Unbound::Unbound)
292-
endif()
293-
294319
if(APR_STATIC)
295320
if(SERF_WINDOWS)
296321
list(APPEND SERF_PUBLIC_TARGETS APR::APR_static)
@@ -307,6 +332,20 @@ else(APR_STATIC)
307332
endif()
308333
endif(APR_STATIC)
309334

335+
list(APPEND SERF_PRIVATE_TARGETS OpenSSL::SSL OpenSSL::Crypto ZLIB::ZLIB)
336+
if(Brotli_FOUND)
337+
list(APPEND SERF_PRIVATE_TARGETS Brotli::Decode)
338+
endif()
339+
if(GSSAPI_FOUND)
340+
list(APPEND SERF_C_DEFINES "SERF_HAVE_GSSAPI")
341+
list(APPEND SERF_PRIVATE_TARGETS KRB5::GSSAPI)
342+
endif()
343+
if(Unbound_FOUND)
344+
list(APPEND SERF_C_DEFINES "SERF_HAVE_ASYNC_RESOLVER=1")
345+
list(APPEND SERF_C_DEFINES "SERF_HAVE_UNBOUND=1")
346+
list(APPEND SERF_PRIVATE_TARGETS Unbound::Unbound)
347+
endif()
348+
310349
# Feature tests
311350
include(SerfChecks)
312351
CheckNotFunction("BIO_set_init" "NULL, 0" "SERF_NO_SSL_BIO_WRAPPERS"
@@ -457,11 +496,6 @@ if(NOT SKIP_SHARED)
457496
install(FILES $<TARGET_PDB_FILE:serf_shared>
458497
CONFIGURATIONS Debug RelWithDebinfo
459498
DESTINATION "${SERF_INSTALL_RUNTIME}")
460-
# string(TOLOWER "${CMAKE_BUILD_TYPE}" config)
461-
# if(NOT "${config}" STREQUAL "release")
462-
# install(FILES $<TARGET_PDB_FILE:serf_shared>
463-
# DESTINATION "${SERF_INSTALL_RUNTIME}")
464-
# endif()
465499
endif()
466500
endif()
467501

@@ -515,8 +549,10 @@ function(make_pkgconfig)
515549
set(VERSION ${SERF_VERSION})
516550
set(MAJOR ${SERF_MAJOR_VERSION})
517551
set(REQUIRES
518-
"libssl"
519-
"libcrypto"
552+
${OPENSSL_PC_REQUIRES}
553+
${ZLIB_PC_REQUIRES}
554+
${BROTLI_PC_REQUIRES}
555+
${GSSAPI_PC_REQUIRES}
520556
${UNBOUND_PC_REQUIRES}
521557
)
522558
set(CFLAGS
@@ -529,10 +565,10 @@ function(make_pkgconfig)
529565
${APRUTIL_LDFLAGS}
530566
${APRUTIL_LIBRARIES}
531567
${APRUTIL_EXTRALIBS}
532-
${ZLIB_LIBRARIES}
533-
${BROTLI_COMMON_LIBRARY}
534-
${BROTLI_DECODE_LIBRARY}
535-
${GSSAPI_LIBRARIES}
568+
${OPENSSL_PC_LIBS}
569+
${ZLIB_PC_LIBS}
570+
${BROTLI_PC_LIBS}
571+
${GSSAPI_PC_LIBS}
536572
${UNBOUND_PC_LIBS}
537573
${SERF_STANDARD_LIBRARIES}
538574
)

build/FindUnbound.cmake

Lines changed: 34 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -43,116 +43,61 @@ cmake_minimum_required(VERSION 3.12)
4343
# UNBOUND_INCLUDE_DIR - Where to find unbound.h.
4444
# UNBOUND_LIBRARY - Linker switches to use with ld to link the library.
4545
#
46-
# ::
47-
#
48-
# UNBOUND_PC_REQUIRES - The name of the pkg-config module for libunbound;
49-
# empty if pkg-config was not used to find the library.
50-
# UNBOUND_PC_LIBS - The link libraries for pkg-config, if
51-
# UNBOUND_PC_REQUUIRES is not set.
52-
#
5346
# Hints
5447
# ^^^^^
5548
#
5649
# A user may set ``Unbound_ROOT`` to tell this module where to look.
5750

5851
include(FindPackageHandleStandardArgs)
5952
include(GNUInstallDirs)
60-
find_package(PkgConfig QUIET)
61-
62-
# Save the PKG_CONFIG_PATH environment variable
63-
if(PKG_CONFIG_FOUND)
64-
set(_pkg_config_path $ENV{PKG_CONFIG_PATH})
65-
endif()
6653

6754
set(Unbound_FOUND FALSE)
6855
if(DEFINED Unbound_ROOT)
6956
# Search in the provided root path
7057
set(_root_search PATHS ${Unbound_ROOT} NO_DEFAULT_PATH)
71-
list(APPEND _UNBOUND_SEARCHES _root_search)
72-
73-
# Set the PKG_CONFIG_PATH environment variable
74-
if(PKG_CONFIG_FOUND)
75-
find_path(_pcfile NAMES "libunbound.pc"
76-
${_root_search}
77-
PATH_SUFFIXES
78-
"lib/pkgconfig"
79-
"${CMAKE_INSTALL_LIBDIR}/pkgconfig"
80-
"share/pkgconfig"
81-
"${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig")
82-
if(_pcfile AND EXISTS "${_pcfile}/libunbound.pc")
83-
set(ENV{PKG_CONFIG_PATH} ${_pcfile})
84-
endif()
85-
endif()
58+
list(APPEND _unbound_searches _root_search)
8659
endif()
8760

8861
# Search in default paths
8962
set(_default_search)
90-
list(APPEND _UNBOUND_SEARCHES _default_search)
63+
list(APPEND _unbound_searches _default_search)
9164

92-
# Try pkg-config first
93-
if(PKG_CONFIG_FOUND)
94-
pkg_search_module(UNBOUND QUIET IMPORTED_TARGET libunbound)
95-
if(UNBOUND_FOUND)
96-
find_package_handle_standard_args(Unbound
97-
REQUIRED_VARS UNBOUND_LINK_LIBRARIES UNBOUND_INCLUDEDIR
98-
VERSION_VAR UNBOUND_VERSION)
99-
if(Unbound_FOUND)
100-
add_library(Unbound::Unbound ALIAS PkgConfig::UNBOUND)
101-
set(UNBOUND_INCLUDE_DIR ${UNBOUND_INCLUDEDIR})
102-
set(UNBOUND_LIBRARY ${UNBOUND_LINK_LIBRARIES})
103-
set(UNBOUND_PC_REQUIRES libunbound)
104-
set(UNBOUND_PC_LIBS)
65+
# Try each search configuration
66+
foreach(search ${_unbound_searches})
67+
find_path(UNBOUND_INCLUDE_DIR NAMES "unbound.h" ${${search}}
68+
PATH_SUFFIXES "include" "${CMAKE_INSTALL_INCLUDEDIR}")
69+
if(UNBOUND_INCLUDE_DIR AND EXISTS "${UNBOUND_INCLUDE_DIR}/unbound.h")
70+
# Use the first successful search to find the libraries
71+
if(NOT DEFINED libsearch)
72+
set(libsearch ${search})
10573
endif()
10674
endif()
107-
endif()
108-
109-
if(NOT Unbound_FOUND)
110-
# Try each search configuration
111-
foreach(search ${_UNBOUND_SEARCHES})
112-
find_path(UNBOUND_INCLUDE_DIR NAMES "unbound.h" ${${search}}
113-
PATH_SUFFIXES "include" "${CMAKE_INSTALL_INCLUDEDIR}")
114-
if(UNBOUND_INCLUDE_DIR AND EXISTS "${UNBOUND_INCLUDE_DIR}/unbound.h")
115-
# Use the first successful search to find the libraries
116-
if(NOT DEFINED libsearch)
117-
set(libsearch ${search})
118-
endif()
119-
endif()
120-
endforeach()
121-
122-
find_library(UNBOUND_LIBRARY NAMES "unbound" NAMES_PER_DIR ${${libsearch}}
123-
PATH_SUFFIXES "lib" "${CMAKE_INSTALL_LIBDIR}")
75+
endforeach()
12476

125-
# Extract the version number from the header
126-
if(UNBOUND_INCLUDE_DIR AND EXISTS "${UNBOUND_INCLUDE_DIR}/unbound.h")
127-
file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _major
128-
REGEX "^ *# *define +UNBOUND_VERSION_MAJOR +[0-9]+.*$")
129-
file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _minor
130-
REGEX "^ *# *define +UNBOUND_VERSION_MINOR +[0-9]+.*$")
131-
file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _micro
132-
REGEX "^ *# *define +UNBOUND_VERSION_MICRO +[0-9]+.*$")
133-
string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _major ${_major})
134-
string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _minor ${_minor})
135-
string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _micro ${_micro})
136-
set(UNBOUND_VERSION "${_major}.${_minor}.${_micro}")
137-
endif()
77+
find_library(UNBOUND_LIBRARY NAMES "unbound" NAMES_PER_DIR ${${libsearch}}
78+
PATH_SUFFIXES "lib" "${CMAKE_INSTALL_LIBDIR}")
13879

139-
find_package_handle_standard_args(Unbound
140-
REQUIRED_VARS UNBOUND_LIBRARY UNBOUND_INCLUDE_DIR
141-
VERSION_VAR UNBOUND_VERSION)
142-
if(Unbound_FOUND)
143-
add_library(Unbound::Unbound UNKNOWN IMPORTED)
144-
set_target_properties(Unbound::Unbound PROPERTIES
145-
INTERFACE_INCLUDE_DIRECTORIES "${UNBOUND_INCLUDE_DIR}"
146-
IMPORTED_LOCATION "${UNBOUND_LIBRARY}")
147-
set(UNBOUND_PC_REQUIRES)
148-
set(UNBOUND_PC_LIBS "${UNBOUND_LIBRARY}")
149-
endif()
80+
# Extract the version number from the header
81+
if(UNBOUND_INCLUDE_DIR AND EXISTS "${UNBOUND_INCLUDE_DIR}/unbound.h")
82+
file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _major
83+
REGEX "^ *# *define +UNBOUND_VERSION_MAJOR +[0-9]+.*$")
84+
file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _minor
85+
REGEX "^ *# *define +UNBOUND_VERSION_MINOR +[0-9]+.*$")
86+
file(STRINGS "${UNBOUND_INCLUDE_DIR}/unbound.h" _micro
87+
REGEX "^ *# *define +UNBOUND_VERSION_MICRO +[0-9]+.*$")
88+
string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _major ${_major})
89+
string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _minor ${_minor})
90+
string(REGEX REPLACE "^[^0-9]+([0-9]+).*$" "\\1" _micro ${_micro})
91+
set(UNBOUND_VERSION "${_major}.${_minor}.${_micro}")
15092
endif()
15193

152-
mark_as_advanced(UNBOUND_INCLUDE_DIR)
153-
mark_as_advanced(UNBOUND_LIBRARY)
154-
155-
# Restore the PKG_CONFIG_PATH environment variable
156-
if(PKG_CONFIG_FOUND)
157-
set(ENV{PKG_CONFIG_PATH} ${_pkg_config_path})
94+
find_package_handle_standard_args(Unbound
95+
REQUIRED_VARS UNBOUND_LIBRARY UNBOUND_INCLUDE_DIR
96+
VERSION_VAR UNBOUND_VERSION)
97+
if(Unbound_FOUND)
98+
add_library(Unbound::Unbound UNKNOWN IMPORTED)
99+
set_target_properties(Unbound::Unbound PROPERTIES
100+
INTERFACE_INCLUDE_DIRECTORIES "${UNBOUND_INCLUDE_DIR}"
101+
IMPORTED_LOCATION "${UNBOUND_LIBRARY}")
102+
set(UNBOUND_PC_LIBS "${UNBOUND_LIBRARY}")
158103
endif()

build/SerfFindPkgConfig.cmake

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# ===================================================================
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
# ===================================================================
19+
20+
include(FindPackageHandleStandardArgs)
21+
include(GNUInstallDirs)
22+
find_package(PkgConfig QUIET)
23+
24+
# Generate list of symbols to export from shared libraries..
25+
function(SerfFindPkgConfig name root pkgname target_alias)
26+
set(${name}_FOUND FALSE PARENT_SCOPE)
27+
if(PKG_CONFIG_FOUND)
28+
# Save the PKG_CONFIG_PATH environment variable
29+
set(pkg_config_path $ENV{PKG_CONFIG_PATH})
30+
31+
if(DEFINED ${root})
32+
# Set the PKG_CONFIG_PATH environment variable for the search
33+
find_path(_${name}_pcdir NAMES "${pkgname}.pc"
34+
PATHS ${${root}} NO_DEFAULT_PATH
35+
PATH_SUFFIXES
36+
"lib/pkgconfig"
37+
"${CMAKE_INSTALL_LIBDIR}/pkgconfig"
38+
"share/pkgconfig"
39+
"${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig")
40+
if(_${name}_pcdir AND EXISTS "${_${name}_pcdir}/${pkgname}.pc")
41+
set(ENV{PKG_CONFIG_PATH} ${_${name}_pcdir})
42+
endif()
43+
endif()
44+
45+
string(TOUPPER "${name}" NAME)
46+
pkg_search_module(${NAME} QUIET IMPORTED_TARGET ${pkgname})
47+
if(${NAME}_FOUND)
48+
find_package_handle_standard_args(${name}
49+
REQUIRED_VARS ${NAME}_LINK_LIBRARIES ${NAME}_INCLUDEDIR
50+
VERSION_VAR ${NAME}_VERSION)
51+
if(${name}_FOUND)
52+
add_library(${target_alias} ALIAS PkgConfig::${NAME})
53+
set(${name}_FOUND ${${name}_FOUND} PARENT_SCOPE)
54+
set(${NAME}_INCLUDE_DIR ${${NAME}_INCLUDEDIR} PARENT_SCOPE)
55+
set(${NAME}_LIBRARY ${${NAME}_LINK_LIBRARIES} PARENT_SCOPE)
56+
set(${NAME}_VERSION ${${NAME}_VERSION} PARENT_SCOPE)
57+
set(${NAME}_PC_REQUIRES ${pkgname} PARENT_SCOPE)
58+
endif()
59+
endif()
60+
61+
# Restore the PKG_CONFIG_PATH environment variable
62+
set(ENV{PKG_CONFIG_PATH} ${pkg_config_path})
63+
endif()
64+
endfunction(SerfFindPkgConfig)

0 commit comments

Comments
 (0)