• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#***************************************************************************
2#                                  _   _ ____  _
3#  Project                     ___| | | |  _ \| |
4#                             / __| | | | |_) | |
5#                            | (__| |_| |  _ <| |___
6#                             \___|\___/|_| \_\_____|
7#
8# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9#
10# This software is licensed as described in the file COPYING, which
11# you should have received as part of this distribution. The terms
12# are also available at https://curl.se/docs/copyright.html.
13#
14# You may opt to use, copy, modify, merge, publish, distribute and/or sell
15# copies of the Software, and permit persons to whom the Software is
16# furnished to do so, under the terms of the COPYING file.
17#
18# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19# KIND, either express or implied.
20#
21# SPDX-License-Identifier: curl
22#
23###########################################################################
24# Find the ldap library
25#
26# Input variables:
27#
28# - `LDAP_INCLUDE_DIR`:   The ldap include directory.
29# - `LDAP_LIBRARY`:       Path to `ldap` library.
30# - `LDAP_LBER_LIBRARY`:  Path to `lber` library.
31#
32# Result variables:
33#
34# - `LDAP_FOUND`:         System has ldap.
35# - `LDAP_INCLUDE_DIRS`:  The ldap include directories.
36# - `LDAP_LIBRARIES`:     The ldap library names.
37# - `LDAP_LIBRARY_DIRS`:  The ldap library directories.
38# - `LDAP_PC_REQUIRES`:   The ldap pkg-config packages.
39# - `LDAP_CFLAGS`:        Required compiler flags.
40# - `LDAP_VERSION`:       Version of ldap.
41
42set(LDAP_PC_REQUIRES "ldap")
43
44if(CURL_USE_PKGCONFIG AND
45   NOT DEFINED LDAP_INCLUDE_DIR AND
46   NOT DEFINED LDAP_LIBRARY AND
47   NOT DEFINED LDAP_LBER_LIBRARY)
48  find_package(PkgConfig QUIET)
49  pkg_check_modules(LDAP ${LDAP_PC_REQUIRES})
50  pkg_check_modules(LDAP_LBER "lber")
51endif()
52
53if(LDAP_FOUND AND LDAP_LBER_FOUND)
54  list(APPEND LDAP_LIBRARIES ${LDAP_LBER_LIBRARIES})
55  list(REVERSE LDAP_LIBRARIES)
56  list(REMOVE_DUPLICATES LDAP_LIBRARIES)
57  list(REVERSE LDAP_LIBRARIES)
58  string(REPLACE ";" " " LDAP_CFLAGS "${LDAP_CFLAGS}")
59  message(STATUS "Found LDAP (via pkg-config): ${LDAP_INCLUDE_DIRS} (found version \"${LDAP_VERSION}\")")
60else()
61  set(LDAP_PC_REQUIRES "")  # Depend on pkg-config only when found via pkg-config
62
63  # On Apple the SDK LDAP gets picked up from
64  # 'MacOSX.sdk/System/Library/Frameworks/LDAP.framework/Headers', which contains
65  # ldap.h and lber.h both being stubs to include <ldap.h> and <lber.h>.
66  # This causes an infinite inclusion loop in compile. Also do this for libraries
67  # to avoid picking up the 'ldap.framework' with a full path.
68  set(_save_cmake_system_framework_path ${CMAKE_SYSTEM_FRAMEWORK_PATH})
69  set(CMAKE_SYSTEM_FRAMEWORK_PATH "")
70  find_path(LDAP_INCLUDE_DIR NAMES "ldap.h")
71  find_library(LDAP_LIBRARY NAMES "ldap")
72  find_library(LDAP_LBER_LIBRARY NAMES "lber")
73  set(CMAKE_SYSTEM_FRAMEWORK_PATH ${_save_cmake_system_framework_path})
74
75  unset(LDAP_VERSION CACHE)
76  if(LDAP_INCLUDE_DIR AND EXISTS "${LDAP_INCLUDE_DIR}/ldap_features.h")
77    set(_version_regex1 "#[\t ]*define[\t ]+LDAP_VENDOR_VERSION_MAJOR[\t ]+([0-9]+).*")
78    set(_version_regex2 "#[\t ]*define[\t ]+LDAP_VENDOR_VERSION_MINOR[\t ]+([0-9]+).*")
79    set(_version_regex3 "#[\t ]*define[\t ]+LDAP_VENDOR_VERSION_PATCH[\t ]+([0-9]+).*")
80    file(STRINGS "${LDAP_INCLUDE_DIR}/ldap_features.h" _version_str1 REGEX "${_version_regex1}")
81    file(STRINGS "${LDAP_INCLUDE_DIR}/ldap_features.h" _version_str2 REGEX "${_version_regex2}")
82    file(STRINGS "${LDAP_INCLUDE_DIR}/ldap_features.h" _version_str3 REGEX "${_version_regex3}")
83    string(REGEX REPLACE "${_version_regex1}" "\\1" _version_str1 "${_version_str1}")
84    string(REGEX REPLACE "${_version_regex2}" "\\1" _version_str2 "${_version_str2}")
85    string(REGEX REPLACE "${_version_regex3}" "\\1" _version_str3 "${_version_str3}")
86    set(LDAP_VERSION "${_version_str1}.${_version_str2}.${_version_str3}")
87    unset(_version_regex1)
88    unset(_version_regex2)
89    unset(_version_regex3)
90    unset(_version_str1)
91    unset(_version_str2)
92    unset(_version_str3)
93  endif()
94
95  include(FindPackageHandleStandardArgs)
96  find_package_handle_standard_args(LDAP
97    REQUIRED_VARS
98      LDAP_INCLUDE_DIR
99      LDAP_LIBRARY
100      LDAP_LBER_LIBRARY
101    VERSION_VAR
102      LDAP_VERSION
103  )
104
105  if(LDAP_FOUND)
106    set(LDAP_INCLUDE_DIRS ${LDAP_INCLUDE_DIR})
107    set(LDAP_LIBRARIES    ${LDAP_LIBRARY} ${LDAP_LBER_LIBRARY})
108  endif()
109
110  mark_as_advanced(LDAP_INCLUDE_DIR LDAP_LIBRARY LDAP_LBER_LIBRARY)
111endif()
112