1# Copyright (c) 2019, 2021, Oracle and/or its affiliates.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License, version 2.0,
5# as published by the Free Software Foundation.
6#
7# This program is also distributed with certain software (including
8# but not limited to OpenSSL) that is licensed under separate terms,
9# as designated in a particular file or component or in included license
10# documentation.  The authors of MySQL hereby grant you an additional
11# permission to link the program and your derivative works with the
12# separately licensed software that they have included with MySQL.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License, version 2.0, for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
22
23# cmake -DWITH_LDAP=system|</path/to/custom/installation>
24# system is the default
25
26INCLUDE (CheckIncludeFile)
27INCLUDE (CheckIncludeFiles)
28
29SET(WITH_LDAP_DOC "\nsystem (use the OS ldap library)")
30STRING_APPEND(WITH_LDAP_DOC ", \n</path/to/custom/installation>")
31
32STRING(REPLACE "\n" "| " WITH_LDAP_DOC_STRING "${WITH_LDAP_DOC}")
33
34MACRO(FIND_SYSTEM_LDAP)
35  IF(WIN32)
36    SET(LDAP_SYSTEM_LIBRARY Wldap32 CACHE INTERNAL
37      "LDAP library is /c/Windows/system32/Wldap32.dll"
38      )
39  ELSE()
40    FIND_LIBRARY(LDAP_SYSTEM_LIBRARY
41      NAMES ldap_r ldap
42      )
43    FIND_LIBRARY(LBER_SYSTEM_LIBRARY
44      NAMES lber
45      )
46  ENDIF()
47  IF(LDAP_SYSTEM_LIBRARY AND (WIN32 OR LBER_SYSTEM_LIBRARY))
48    SET(LDAP_LIBRARY ${LDAP_SYSTEM_LIBRARY})
49    SET(LBER_LIBRARY ${LBER_SYSTEM_LIBRARY})
50    MESSAGE(STATUS "LBER_LIBRARY ${LBER_LIBRARY}")
51    MESSAGE(STATUS "LDAP_LIBRARY ${LDAP_LIBRARY}")
52  ENDIF()
53
54  IF(WIN32)
55    # LDAP system header is Winldap.h and is in some Windows SDK
56  ELSE()
57    CMAKE_PUSH_CHECK_STATE()
58
59    # For Solaris 11.3 we need to explicitly search here:
60    IF(SOLARIS)
61      INCLUDE_DIRECTORIES(BEFORE SYSTEM /usr/include/openldap)
62      SET(CMAKE_REQUIRED_INCLUDES "/usr/include/openldap")
63    ENDIF()
64
65    CHECK_INCLUDE_FILE(lber.h HAVE_LBER_H)
66    CHECK_INCLUDE_FILE(ldap.h HAVE_LDAP_H)
67    CMAKE_POP_CHECK_STATE()
68  ENDIF()
69
70ENDMACRO()
71
72MACRO(FIND_CUSTOM_LDAP)
73  FIND_PATH(LDAP_INCLUDE_DIR
74    NAMES ldap.h
75    PATHS ${WITH_LDAP_PATH}/include
76    NO_DEFAULT_PATH
77    NO_CMAKE_ENVIRONMENT_PATH
78    NO_SYSTEM_ENVIRONMENT_PATH
79    )
80
81  # On mac this list is <.dylib;.so;.a>
82  # We prefer static libraries, so we reverse it here.
83  LIST(REVERSE CMAKE_FIND_LIBRARY_SUFFIXES)
84
85  FIND_LIBRARY(LDAP_CUSTOM_LIBRARY
86    NAMES ldap_r ldap
87    PATHS ${WITH_LDAP_PATH}/lib
88    NO_DEFAULT_PATH
89    NO_CMAKE_ENVIRONMENT_PATH
90    NO_SYSTEM_ENVIRONMENT_PATH
91    )
92  FIND_LIBRARY(LBER_CUSTOM_LIBRARY
93    NAMES lber
94    PATHS ${WITH_LDAP_PATH}/lib
95    NO_DEFAULT_PATH
96    NO_CMAKE_ENVIRONMENT_PATH
97    NO_SYSTEM_ENVIRONMENT_PATH
98    )
99
100  LIST(REVERSE CMAKE_FIND_LIBRARY_SUFFIXES)
101
102  IF(LDAP_INCLUDE_DIR)
103    INCLUDE_DIRECTORIES(BEFORE SYSTEM ${LDAP_INCLUDE_DIR})
104
105    CMAKE_PUSH_CHECK_STATE()
106    SET(CMAKE_REQUIRED_INCLUDES ${LDAP_INCLUDE_DIR})
107    CHECK_INCLUDE_FILE(lber.h HAVE_LBER_H)
108    CHECK_INCLUDE_FILE(ldap.h HAVE_LDAP_H)
109    CMAKE_POP_CHECK_STATE()
110  ENDIF()
111
112  IF(LDAP_CUSTOM_LIBRARY AND LBER_CUSTOM_LIBRARY)
113    SET(LDAP_LIBRARY ${LDAP_CUSTOM_LIBRARY})
114    SET(LBER_LIBRARY ${LBER_CUSTOM_LIBRARY})
115    GET_FILENAME_COMPONENT(LDAP_LIBRARY_EXT ${LDAP_LIBRARY} EXT)
116    IF(LDAP_LIBRARY_EXT STREQUAL ".a")
117      SET(STATIC_LDAP_LIBRARY 1)
118    ENDIF()
119    MESSAGE(STATUS "LDAP_LIBRARY ${LDAP_LIBRARY}")
120    MESSAGE(STATUS "LBER_LIBRARY ${LBER_LIBRARY}")
121  ENDIF()
122ENDMACRO()
123
124MACRO(MYSQL_CHECK_LDAP)
125  IF(NOT WITH_LDAP)
126    SET(WITH_LDAP "system" CACHE STRING ${WITH_LDAP_DOC_STRING} FORCE)
127  ENDIF()
128
129  # See if WITH_LDAP is of the form </path/to/custom/installation>
130  FILE(GLOB WITH_LDAP_HEADER ${WITH_LDAP}/include/ldap.h)
131  IF (WITH_LDAP_HEADER)
132    FILE(TO_CMAKE_PATH "${WITH_LDAP}" WITH_LDAP)
133    SET(WITH_LDAP_PATH ${WITH_LDAP})
134  ENDIF()
135
136  IF(WITH_LDAP STREQUAL "system")
137    FIND_SYSTEM_LDAP()
138  ELSE()
139    FIND_CUSTOM_LDAP()
140  ENDIF()
141
142  IF(WIN32 AND WITH_LDAP STREQUAL "system")
143    SET(LDAP_FOUND 1)
144  ELSEIF(HAVE_LBER_H AND HAVE_LDAP_H AND LDAP_LIBRARY AND LBER_LIBRARY)
145    SET(LDAP_FOUND 1)
146  ELSE()
147    SET(LDAP_FOUND 0)
148  ENDIF()
149
150ENDMACRO()
151