cmake: Modernize cmake support for LDAP
authorManuel Nickschas <sputnick@quassel-irc.org>
Wed, 8 Aug 2018 20:22:39 +0000 (22:22 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Sun, 18 Nov 2018 10:06:43 +0000 (11:06 +0100)
Use a more modern, property-based find script for the LDAP libraries
and get rid of the deprecated use of LDAP_LIBRARIES and similar
variables in favor of a namespaced Ldap::Ldap target.
Set package properties for better diagnostic output at configure time.

CMakeLists.txt
cmake/FindLdap.cmake
src/core/CMakeLists.txt
src/core/core.cpp

index f6b7f65..e4f5d61 100644 (file)
@@ -312,13 +312,11 @@ if (BUILD_CORE)
 
     if (WITH_LDAP)
         find_package(Ldap QUIET)
-        if (LDAP_FOUND)
-            message(STATUS "Enabling LDAP authentication support")
-        else()
-            message(STATUS "Disabling LDAP authentication support")
-        endif()
-    else()
-        message(STATUS "Not enabling LDAP authentication support")
+        set_package_properties(Ldap PROPERTIES TYPE OPTIONAL
+            URL "http://www.openldap.org/"
+            DESCRIPTION "LDAP (Lightweight Directory Access Protocol) libraries"
+            PURPOSE "Enables core user authentication via LDAP"
+        )
     endif()
 endif()
 
index 2c96fae..1d1d164 100644 (file)
-# Copied from https://raw.github.com/facebook/hiphop-php/master/CMake/FindLdap.cmake
-
-# - Try to find the LDAP client libraries
-# Once done this will define
+#.rst:
+# FindLdap
+# --------
+#
+# Try to find the LDAP client libraries.
+#
+# This will define the following variables:
+#
+# ``Ldap_FOUND``
+#     True if libldap is available.
+#
+# ``Ldap_VERSION``
+#     The version of libldap
+#
+# ``Ldap_INCLUDE_DIRS``
+#     This should be passed to target_include_directories() if
+#     the target is not used for linking
+#
+# ``Ldap_LIBRARIES``
+#     The LDAP libraries (libldap + liblber if available)
+#     This can be passed to target_link_libraries() instead of
+#     the ``Ldap::Ldap`` target
+#
+# If ``Ldap_FOUND`` is TRUE, the following imported target
+# will be available:
 #
-#  LDAP_FOUND - system has libldap
-#  LDAP_INCLUDE_DIR - the ldap include directory
-#  LDAP_LIBRARIES - libldap + liblber (if found) library
-#  LBER_LIBRARIES - liblber library
+# ``Ldap::Ldap``
+#     The LDAP library
+#
+#=============================================================================
+# Copyright 2006 Szombathelyi György <gyurco@freemail.hu>
+# Copyright 2007-2016 Laurent Montel <montel@kde.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#
+# 1. Redistributions of source code must retain the copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+# 3. The name of the author may not be used to endorse or promote products
+#    derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#=============================================================================
+
+if (NOT APPLE)
+    find_path(Ldap_INCLUDE_DIRS NAMES ldap.h)
+    find_library(Ldap_LIBRARY NAMES ldap)
+    find_library(Lber_LIBRARY NAMES lber)
+else()
+    # OSX ships with an LDAP.framework which seems to be completely broken.
+    # Force using the working version in /usr instead.
+    find_path(Ldap_INCLUDE_DIRS NAMES ldap.h PATHS /usr/include NO_CMAKE_SYSTEM_PATH)
+    find_library(Ldap_LIBRARY NAMES ldap PATHS /usr/lib NO_CMAKE_SYSTEM_PATH)
+    find_library(Lber_LIBRARY NAMES lber PATHS /usr/lib NO_CMAKE_SYSTEM_PATH)
+endif()
+
+set(Ldap_LIBRARIES ${Ldap_LIBRARY})
+if (Ldap_LIBRARY AND Lber_LIBRARY)
+    list(APPEND Ldap_LIBRARIES ${Lber_LIBRARY})
+endif()
 
-if(LDAP_INCLUDE_DIR AND LDAP_LIBRARIES)
-    # Already in cache, be silent
-    set(Ldap_FIND_QUIETLY TRUE)
-endif(LDAP_INCLUDE_DIR AND LDAP_LIBRARIES)
+if(EXISTS ${Ldap_INCLUDE_DIRS}/ldap_features.h)
+    file(READ ${Ldap_INCLUDE_DIRS}/ldap_features.h LDAP_FEATURES_H_CONTENT)
+    string(REGEX MATCH "#define LDAP_VENDOR_VERSION_MAJOR[ ]+[0-9]+" _LDAP_VERSION_MAJOR_MATCH ${LDAP_FEATURES_H_CONTENT})
+    string(REGEX MATCH "#define LDAP_VENDOR_VERSION_MINOR[ ]+[0-9]+" _LDAP_VERSION_MINOR_MATCH ${LDAP_FEATURES_H_CONTENT})
+    string(REGEX MATCH "#define LDAP_VENDOR_VERSION_PATCH[ ]+[0-9]+" _LDAP_VERSION_PATCH_MATCH ${LDAP_FEATURES_H_CONTENT})
 
-# Attempt to link against ldap.h regardless of platform!
-FIND_PATH(LDAP_INCLUDE_DIR ldap.h)
+    string(REGEX REPLACE ".*_MAJOR[ ]+(.*)" "\\1" LDAP_VERSION_MAJOR ${_LDAP_VERSION_MAJOR_MATCH})
+    string(REGEX REPLACE ".*_MINOR[ ]+(.*)" "\\1" LDAP_VERSION_MINOR ${_LDAP_VERSION_MINOR_MATCH})
+    string(REGEX REPLACE ".*_PATCH[ ]+(.*)" "\\1" LDAP_VERSION_PATCH ${_LDAP_VERSION_PATCH_MATCH})
 
-# If we detect path to invalid ldap.h on osx, try /usr/include/
-# This might also be achievable with additional parameters to FIND_PATH.
-string(TOLOWER ${LDAP_INCLUDE_DIR} ldapincludelower)
-if("${ldapincludelower}" MATCHES "\\/system\\/library\\/frameworks\\/ldap\\.framework\\/headers")
-  set(LDAP_INCLUDE_DIR "/usr/include/")
+    set(Ldap_VERSION "${LDAP_VERSION_MAJOR}.${LDAP_VERSION_MINOR}.${LDAP_VERSION_PATCH}")
 endif()
 
-FIND_LIBRARY(LDAP_LIBRARIES NAMES ldap)
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(Ldap
+    FOUND_VAR Ldap_FOUND
+    REQUIRED_VARS Ldap_LIBRARIES Ldap_INCLUDE_DIRS
+    VERSION_VAR Ldap_VERSION
+)
 
-# On osx remove invalid ldap.h
-string(TOLOWER ${LDAP_LIBRARIES} ldaplower)
-if("${ldaplower}" MATCHES "\\/system\\/library\\/frameworks\\/ldap\\.framework")
-  set(LDAP_LIBRARIES FALSE)
+if (Ldap_FOUND AND NOT TARGET Ldap::Ldap)
+    add_library(Ldap::Ldap UNKNOWN IMPORTED)
+    set_target_properties(Ldap::Ldap PROPERTIES
+        IMPORTED_LOCATION "${Ldap_LIBRARY}"
+        INTERFACE_LINK_LIBRARIES "${Lber_LIBRARY}"
+        INTERFACE_INCLUDE_DIRECTORIES "${Ldap_INCLUDE_DIRS}"
+    )
 endif()
 
-FIND_LIBRARY(LBER_LIBRARIES NAMES lber)
-
-# It'd be nice to link against winldap on Windows, unfortunately
-# the interfaces are different. In theory a compatibility shim
-# could be written; if someone ever gets around to doing that these
-# lines should be uncommented and used on Windows.
-#   FIND_PATH(LDAP_INCLUDE_DIR winldap.h)
-#   FIND_LIBRARY(LDAP_LIBRARIES NAMES wldap32)
-
-if(LDAP_INCLUDE_DIR AND LDAP_LIBRARIES)
-   set(LDAP_FOUND TRUE)
-   if(LBER_LIBRARIES)
-     set(LDAP_LIBRARIES ${LDAP_LIBRARIES} ${LBER_LIBRARIES})
-   endif(LBER_LIBRARIES)
-endif(LDAP_INCLUDE_DIR AND LDAP_LIBRARIES)
-
-if(LDAP_FOUND)
-   if(NOT Ldap_FIND_QUIETLY)
-      message(STATUS "Found ldap: ${LDAP_LIBRARIES}")
-   endif(NOT Ldap_FIND_QUIETLY)
-else(LDAP_FOUND)
-   if (Ldap_FIND_REQUIRED)
-        message(FATAL_ERROR "Could NOT find ldap")
-   endif (Ldap_FIND_REQUIRED)
-endif(LDAP_FOUND)
-
-MARK_AS_ADVANCED(LDAP_INCLUDE_DIR LDAP_LIBRARIES LBER_LIBRARIES LDAP_DIR)
+mark_as_advanced(Ldap_INCLUDE_DIRS Ldap_LIBRARY Lber_LIBRARY Ldap_LIBRARIES Ldap_VERSION)
index b7858f9..e571708 100644 (file)
@@ -55,11 +55,10 @@ if (HAVE_UMASK)
     set_source_files_properties(oidentdconfiggenerator.cpp PROPERTIES COMPILE_DEFINITIONS HAVE_UMASK)
 endif()
 
-if (LDAP_FOUND)
+if (Ldap_FOUND)
     target_sources(${TARGET} PRIVATE ldapauthenticator.cpp)
-    target_link_libraries(${TARGET} PRIVATE ${LDAP_LIBRARIES})
-    target_include_directories(${TARGET} PRIVATE ${LDAP_INCLUDE_DIR})
-    target_compile_definitions(${TARGET} PRIVATE -DHAVE_LDAP)
+    target_link_libraries(${TARGET} PRIVATE Ldap::Ldap)
+    set_source_files_properties(core.cpp PROPERTIES COMPILE_DEFINITIONS HAVE_LDAP)
 endif()
 
 if (Qca-qt5_FOUND)
index db30b30..e1ea083 100644 (file)
@@ -36,9 +36,8 @@
 #include "types.h"
 #include "util.h"
 
-// Currently building with LDAP bindings is optional.
 #ifdef HAVE_LDAP
-#include "ldapauthenticator.h"
+#  include "ldapauthenticator.h"
 #endif
 
 // migration related