1# FindLDAP.cmake
2#
3# Searches for OpenLDAP/SunLDAP library
4#
5# Adds these options:
6#    -DWITH_OPENLDAP=ON/OFF/PATH - enable/disable OpenLDAP, eventually set prefix to find it
7#    -DWITH_SUNLDAP=OFF/ON/PATH - enable/disable SunLDAP, eventually set prefix to find it
8#    -DWITH_STATIC_LDAP=OFF/ON - enable/disable static LDAP linking
9#
10# The OpenLDAP has precedence over SunLDAP, if both are specified. The default is to use OpenLDAP.
11#
12# The output is:
13#    HAVE_LDAP - set to ON, if LDAP support is enabled and libraries found
14#    SUNLDAP - set to ON, when using SunLDAP implementation
15#    LDAP_CFLAGS - CFLAGS to use with target_compile_options() and similar commands
16#    LDAP_INCLUDE_DIRS - include directories to use with target_include_directories() and similar commands
17#    LDAP_LIBS - libraries to use with target_link_libraries() and similar commands
18
19include(CheckCSourceCompiles)
20include(CheckLibraryExists)
21include(PrintableOptions)
22
23add_printable_variable_path(WITH_OPENLDAP "Enable LDAP support using OpenLDAP, default ON" "ON")
24add_printable_variable_path(WITH_SUNLDAP "Enable LDAP support using SunLDAP, default OFF" "OFF")
25add_printable_option(WITH_STATIC_LDAP "Link LDAP statically, default OFF" OFF)
26
27if((NOT WITH_OPENLDAP) AND (NOT WITH_SUNLDAP))
28	return()
29endif((NOT WITH_OPENLDAP) AND (NOT WITH_SUNLDAP))
30
31string(LENGTH "${CMAKE_BINARY_DIR}" bindirlen)
32string(LENGTH "${WITH_OPENLDAP}" maxlen)
33if(maxlen LESS bindirlen)
34	set(substr "***")
35else(maxlen LESS bindirlen)
36	string(SUBSTRING "${WITH_OPENLDAP}" 0 ${bindirlen} substr)
37endif(maxlen LESS bindirlen)
38string(TOUPPER "${WITH_OPENLDAP}" optupper)
39
40if(("${optupper}" STREQUAL "ON") OR ("${substr}" STREQUAL "${CMAKE_BINARY_DIR}"))
41	set(WITH_OPENLDAP "/usr")
42endif(("${optupper}" STREQUAL "ON") OR ("${substr}" STREQUAL "${CMAKE_BINARY_DIR}"))
43
44string(LENGTH "${WITH_SUNLDAP}" maxlen)
45if(maxlen LESS bindirlen)
46	set(substr "***")
47else(maxlen LESS bindirlen)
48	string(SUBSTRING "${WITH_SUNLDAP}" 0 ${bindirlen} substr)
49endif(maxlen LESS bindirlen)
50string(TOUPPER "${WITH_SUNLDAP}" optupper)
51
52if(("${optupper}" STREQUAL "ON") OR ("${substr}" STREQUAL "${CMAKE_BINARY_DIR}"))
53	set(WITH_SUNLDAP "/usr")
54endif(("${optupper}" STREQUAL "ON") OR ("${substr}" STREQUAL "${CMAKE_BINARY_DIR}"))
55
56unset(bindirlen)
57unset(maxlen)
58unset(substr)
59unset(optupper)
60
61set(HAVE_LDAP ON)
62set(SUNLDAP OFF)
63
64macro(add_ldap_lib_if_provides _lib _symbol)
65	CHECK_LIBRARY_EXISTS(${_lib} ${_symbol} "" lib${_lib}_provides_${_symbol})
66	if(lib${_lib}_provides_${_symbol})
67		set(LDAP_LIBS "${LDAP_LIBS} -l${_lib}")
68	endif(lib${_lib}_provides_${_symbol})
69endmacro(add_ldap_lib_if_provides)
70
71set(LDAP_PREFIX "")
72if(WITH_OPENLDAP)
73	set(LDAP_PREFIX "${WITH_OPENLDAP}")
74else(WITH_OPENLDAP)
75	set(LDAP_PREFIX "${WITH_SUNLDAP}")
76	set(SUNLDAP ON)
77endif(WITH_OPENLDAP)
78
79set(LDAP_CFLAGS "")
80set(LDAP_INCLUDE_DIRS "${LDAP_PREFIX}/include")
81set(LDAP_LIBS "-L${LDAP_PREFIX}/lib${LIB_SUFFIX}")
82
83set(CMAKE_REQUIRED_INCLUDES "${LDAP_INCLUDE_DIRS}")
84set(CMAKE_REQUIRED_LIBRARIES "${LDAP_LIBS}")
85
86if(WITH_OPENLDAP)
87	CHECK_C_SOURCE_COMPILES("#include \"ldap.h\"
88				int main(void) {
89					/* LDAP_VENDOR_VERSION is 0 if OpenLDAP is built from git/master */
90					#if !defined(LDAP_VENDOR_VERSION) || (LDAP_VENDOR_VERSION != 0 && LDAP_VENDOR_VERSION < 20000)
91					#error OpenLDAP version not at least 2.0
92					#endif
93					return 0; }" openldap_2_x)
94	if(NOT openldap_2_x)
95		message(FATAL_ERROR "At least 2.0 OpenLDAP version required; either use -DWITH_OPENLDAP=OFF argument to cmake command to disable LDAP support, or install OpenLDAP")
96	endif(NOT openldap_2_x)
97else(WITH_OPENLDAP)
98	CHECK_C_SOURCE_COMPILES("#include \"ldap.h\"
99				int main(void) {
100					#if !defined(LDAP_VENDOR_VERSION) || LDAP_VENDOR_VERSION < 500
101					#error SunLDAP version not at least 2.0
102					#endif
103					return 0; }" sunldap_2_x)
104	if(NOT sunldap_2_x)
105		message(FATAL_ERROR "At least 2.0 SunLDAP version required; either use -DWITH_SUNLDAP=OFF argument to cmake command to disable LDAP support, or install SunLDAP")
106	endif(NOT sunldap_2_x)
107endif(WITH_OPENLDAP)
108
109add_ldap_lib_if_provides(resolv res_query)
110add_ldap_lib_if_provides(resolv __res_query)
111add_ldap_lib_if_provides(socket bind)
112CHECK_LIBRARY_EXISTS(lber ber_get_tag "" liblber_provides_ber_get_tag)
113if(liblber_provides_ber_get_tag)
114	if(WITH_STATIC_LDAP)
115		set(LDAP_LIBS "${LDAP_LIBS} ${LDAP_PREFIX}/lib${LIB_SUFFIX}/liblber.a")
116#		# libldap might depend on OpenSSL... We need to pull
117#		# in the dependency libs explicitly here since we're
118#		# not using libtool for the configure test.
119#		if test -f ${LDAP_PREFIX}/lib${LIB_SUFFIX}/libldap.la; then
120#			LDAP_LIBS="`. ${LDAP_PREFIX}/libPLIB_SUFFIX}/libldap.la; echo $dependency_libs` $LDAP_LIBS"
121#		fi
122	else(WITH_STATIC_LDAP)
123		set(LDAP_LIBS "${LDAP_LIBS} -llber")
124	endif(WITH_STATIC_LDAP)
125endif(liblber_provides_ber_get_tag)
126
127CHECK_LIBRARY_EXISTS(ldap ldap_open "" libldap_provides_ldap_open)
128if(libldap_provides_ldap_open)
129	if(WITH_STATIC_LDAP)
130		set(LDAP_LIBS "${LDAP_LIBS} ${LDAP_PREFIX}/lib${LIB_SUFFIX}/libldap.a")
131	else(WITH_STATIC_LDAP)
132		set(LDAP_LIBS "${LDAP_LIBS} -lldap")
133	endif(WITH_STATIC_LDAP)
134else(libldap_provides_ldap_open)
135	if(WITH_OPENLDAP)
136		message(FATAL_ERROR "Could not find OpenLDAP libraries; either use -DWITH_OPENLDAP=OFF argument to cmake command to disable LDAP support, or install OpenLDAP")
137	else(WITH_OPENLDAP)
138		message(FATAL_ERROR "Could not find SunLDAP libraries; either use -DWITH_SUNLDAP=OFF argument to cmake command to disable LDAP support, or install SunLDAP")
139	endif(WITH_OPENLDAP)
140endif(libldap_provides_ldap_open)
141
142unset(CMAKE_REQUIRED_INCLUDES)
143unset(CMAKE_REQUIRED_LIBRARIES)
144
145add_definitions(-DLDAP_DEPRECATED)
146