• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# - Try to find the GSS Kerberos library
2# Once done this will define
3#
4#  GSS_ROOT_DIR - Set this variable to the root installation of GSS
5#
6# Read-Only variables:
7#  GSS_FOUND - system has the Heimdal library
8#  GSS_FLAVOUR - "MIT" or "Heimdal" if anything found.
9#  GSS_INCLUDE_DIR - the Heimdal include directory
10#  GSS_LIBRARIES - The libraries needed to use GSS
11#  GSS_LINK_DIRECTORIES - Directories to add to linker search path
12#  GSS_LINKER_FLAGS - Additional linker flags
13#  GSS_COMPILER_FLAGS - Additional compiler flags
14#  GSS_VERSION - This is set to version advertised by pkg-config or read from manifest.
15#                In case the library is found but no version info available it'll be set to "unknown"
16
17set(_MIT_MODNAME mit-krb5-gssapi)
18set(_HEIMDAL_MODNAME heimdal-gssapi)
19
20include(CheckIncludeFile)
21include(CheckIncludeFiles)
22include(CheckTypeSize)
23
24set(_GSS_ROOT_HINTS
25    "${GSS_ROOT_DIR}"
26    "$ENV{GSS_ROOT_DIR}"
27)
28
29# try to find library using system pkg-config if user didn't specify root dir
30if(NOT GSS_ROOT_DIR AND NOT "$ENV{GSS_ROOT_DIR}")
31  if(UNIX)
32    find_package(PkgConfig QUIET)
33    pkg_search_module(_GSS_PKG ${_MIT_MODNAME} ${_HEIMDAL_MODNAME})
34    list(APPEND _GSS_ROOT_HINTS "${_GSS_PKG_PREFIX}")
35  elseif(WIN32)
36    list(APPEND _GSS_ROOT_HINTS "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MIT\\Kerberos;InstallDir]")
37  endif()
38endif()
39
40if(NOT _GSS_FOUND) #not found by pkg-config. Let's take more traditional approach.
41  find_file(_GSS_CONFIGURE_SCRIPT
42      NAMES
43          "krb5-config"
44      HINTS
45          ${_GSS_ROOT_HINTS}
46      PATH_SUFFIXES
47          bin
48      NO_CMAKE_PATH
49      NO_CMAKE_ENVIRONMENT_PATH
50  )
51
52  # if not found in user-supplied directories, maybe system knows better
53  find_file(_GSS_CONFIGURE_SCRIPT
54      NAMES
55          "krb5-config"
56      PATH_SUFFIXES
57          bin
58  )
59
60  if(_GSS_CONFIGURE_SCRIPT)
61    execute_process(
62          COMMAND ${_GSS_CONFIGURE_SCRIPT} "--cflags" "gssapi"
63          OUTPUT_VARIABLE _GSS_CFLAGS
64          RESULT_VARIABLE _GSS_CONFIGURE_FAILED
65          OUTPUT_STRIP_TRAILING_WHITESPACE
66      )
67    message(STATUS "CFLAGS: ${_GSS_CFLAGS}")
68    if(NOT _GSS_CONFIGURE_FAILED) # 0 means success
69      # should also work in an odd case when multiple directories are given
70      string(STRIP "${_GSS_CFLAGS}" _GSS_CFLAGS)
71      string(REGEX REPLACE " +-I" ";" _GSS_CFLAGS "${_GSS_CFLAGS}")
72      string(REGEX REPLACE " +-([^I][^ \\t;]*)" ";-\\1" _GSS_CFLAGS "${_GSS_CFLAGS}")
73
74      foreach(_flag ${_GSS_CFLAGS})
75        if(_flag MATCHES "^-I.*")
76          string(REGEX REPLACE "^-I" "" _val "${_flag}")
77          list(APPEND _GSS_INCLUDE_DIR "${_val}")
78        else()
79          list(APPEND _GSS_COMPILER_FLAGS "${_flag}")
80        endif()
81      endforeach()
82    endif()
83
84    execute_process(
85        COMMAND ${_GSS_CONFIGURE_SCRIPT} "--libs" "gssapi"
86        OUTPUT_VARIABLE _GSS_LIB_FLAGS
87        RESULT_VARIABLE _GSS_CONFIGURE_FAILED
88        OUTPUT_STRIP_TRAILING_WHITESPACE
89    )
90    message(STATUS "LDFLAGS: ${_GSS_LIB_FLAGS}")
91
92    if(NOT _GSS_CONFIGURE_FAILED) # 0 means success
93      # this script gives us libraries and link directories. Blah. We have to deal with it.
94      string(STRIP "${_GSS_LIB_FLAGS}" _GSS_LIB_FLAGS)
95      string(REGEX REPLACE " +-(L|l)" ";-\\1" _GSS_LIB_FLAGS "${_GSS_LIB_FLAGS}")
96      string(REGEX REPLACE " +-([^Ll][^ \\t;]*)" ";-\\1" _GSS_LIB_FLAGS "${_GSS_LIB_FLAGS}")
97
98      foreach(_flag ${_GSS_LIB_FLAGS})
99        if(_flag MATCHES "^-l.*")
100          string(REGEX REPLACE "^-l" "" _val "${_flag}")
101          list(APPEND _GSS_LIBRARIES "${_val}")
102        elseif(_flag MATCHES "^-L.*")
103          string(REGEX REPLACE "^-L" "" _val "${_flag}")
104          list(APPEND _GSS_LINK_DIRECTORIES "${_val}")
105        else()
106          list(APPEND _GSS_LINKER_FLAGS "${_flag}")
107        endif()
108      endforeach()
109    endif()
110
111    execute_process(
112        COMMAND ${_GSS_CONFIGURE_SCRIPT} "--version"
113        OUTPUT_VARIABLE _GSS_VERSION
114        RESULT_VARIABLE _GSS_CONFIGURE_FAILED
115        OUTPUT_STRIP_TRAILING_WHITESPACE
116    )
117
118    # older versions may not have the "--version" parameter. In this case we just don't care.
119    if(_GSS_CONFIGURE_FAILED)
120      set(_GSS_VERSION 0)
121    endif()
122
123    execute_process(
124        COMMAND ${_GSS_CONFIGURE_SCRIPT} "--vendor"
125        OUTPUT_VARIABLE _GSS_VENDOR
126        RESULT_VARIABLE _GSS_CONFIGURE_FAILED
127        OUTPUT_STRIP_TRAILING_WHITESPACE
128    )
129
130    # older versions may not have the "--vendor" parameter. In this case we just don't care.
131    if(_GSS_CONFIGURE_FAILED)
132      set(GSS_FLAVOUR "Heimdal") # most probably, shouldn't really matter
133    else()
134      if(_GSS_VENDOR MATCHES ".*H|heimdal.*")
135        set(GSS_FLAVOUR "Heimdal")
136      else()
137        set(GSS_FLAVOUR "MIT")
138      endif()
139    endif()
140
141  else() # either there is no config script or we are on a platform that doesn't provide one (Windows?)
142
143    find_path(_GSS_INCLUDE_DIR
144        NAMES
145            "gssapi/gssapi.h"
146        HINTS
147            ${_GSS_ROOT_HINTS}
148        PATH_SUFFIXES
149            include
150            inc
151    )
152
153    if(_GSS_INCLUDE_DIR) #jay, we've found something
154      set(CMAKE_REQUIRED_INCLUDES "${_GSS_INCLUDE_DIR}")
155      check_include_files( "gssapi/gssapi_generic.h;gssapi/gssapi_krb5.h" _GSS_HAVE_MIT_HEADERS)
156
157      if(_GSS_HAVE_MIT_HEADERS)
158        set(GSS_FLAVOUR "MIT")
159      else()
160        # prevent compiling the header - just check if we can include it
161        set(CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} -D__ROKEN_H__")
162        check_include_file( "roken.h" _GSS_HAVE_ROKEN_H)
163
164        check_include_file( "heimdal/roken.h" _GSS_HAVE_HEIMDAL_ROKEN_H)
165        if(_GSS_HAVE_ROKEN_H OR _GSS_HAVE_HEIMDAL_ROKEN_H)
166          set(GSS_FLAVOUR "Heimdal")
167        endif()
168        set(CMAKE_REQUIRED_DEFINITIONS "")
169      endif()
170    else()
171      # I'm not convinced if this is the right way but this is what autotools do at the moment
172      find_path(_GSS_INCLUDE_DIR
173          NAMES
174              "gssapi.h"
175          HINTS
176              ${_GSS_ROOT_HINTS}
177          PATH_SUFFIXES
178              include
179              inc
180      )
181
182      if(_GSS_INCLUDE_DIR)
183        set(GSS_FLAVOUR "Heimdal")
184      endif()
185    endif()
186
187    # if we have headers, check if we can link libraries
188    if(GSS_FLAVOUR)
189      set(_GSS_LIBDIR_SUFFIXES "")
190      set(_GSS_LIBDIR_HINTS ${_GSS_ROOT_HINTS})
191      get_filename_component(_GSS_CALCULATED_POTENTIAL_ROOT "${_GSS_INCLUDE_DIR}" PATH)
192      list(APPEND _GSS_LIBDIR_HINTS ${_GSS_CALCULATED_POTENTIAL_ROOT})
193
194      if(WIN32)
195        if(CMAKE_SIZEOF_VOID_P EQUAL 8)
196          list(APPEND _GSS_LIBDIR_SUFFIXES "lib/AMD64")
197          if(GSS_FLAVOUR STREQUAL "MIT")
198            set(_GSS_LIBNAME "gssapi64")
199          else()
200            set(_GSS_LIBNAME "libgssapi")
201          endif()
202        else()
203          list(APPEND _GSS_LIBDIR_SUFFIXES "lib/i386")
204          if(GSS_FLAVOUR STREQUAL "MIT")
205            set(_GSS_LIBNAME "gssapi32")
206          else()
207            set(_GSS_LIBNAME "libgssapi")
208          endif()
209        endif()
210      else()
211        list(APPEND _GSS_LIBDIR_SUFFIXES "lib;lib64") # those suffixes are not checked for HINTS
212        if(GSS_FLAVOUR STREQUAL "MIT")
213          set(_GSS_LIBNAME "gssapi_krb5")
214        else()
215          set(_GSS_LIBNAME "gssapi")
216        endif()
217      endif()
218
219      find_library(_GSS_LIBRARIES
220          NAMES
221              ${_GSS_LIBNAME}
222          HINTS
223              ${_GSS_LIBDIR_HINTS}
224          PATH_SUFFIXES
225              ${_GSS_LIBDIR_SUFFIXES}
226      )
227
228    endif()
229  endif()
230else()
231  if(_GSS_PKG_${_MIT_MODNAME}_VERSION)
232    set(GSS_FLAVOUR "MIT")
233    set(_GSS_VERSION _GSS_PKG_${_MIT_MODNAME}_VERSION)
234  else()
235    set(GSS_FLAVOUR "Heimdal")
236    set(_GSS_VERSION _GSS_PKG_${_MIT_HEIMDAL}_VERSION)
237  endif()
238endif()
239
240set(GSS_INCLUDE_DIR ${_GSS_INCLUDE_DIR})
241set(GSS_LIBRARIES ${_GSS_LIBRARIES})
242set(GSS_LINK_DIRECTORIES ${_GSS_LINK_DIRECTORIES})
243set(GSS_LINKER_FLAGS ${_GSS_LINKER_FLAGS})
244set(GSS_COMPILER_FLAGS ${_GSS_COMPILER_FLAGS})
245set(GSS_VERSION ${_GSS_VERSION})
246
247if(GSS_FLAVOUR)
248  if(NOT GSS_VERSION AND GSS_FLAVOUR STREQUAL "Heimdal")
249    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
250      set(HEIMDAL_MANIFEST_FILE "Heimdal.Application.amd64.manifest")
251    else()
252      set(HEIMDAL_MANIFEST_FILE "Heimdal.Application.x86.manifest")
253    endif()
254
255    if(EXISTS "${GSS_INCLUDE_DIR}/${HEIMDAL_MANIFEST_FILE}")
256      file(STRINGS "${GSS_INCLUDE_DIR}/${HEIMDAL_MANIFEST_FILE}" heimdal_version_str
257           REGEX "^.*version=\"[0-9]\\.[^\"]+\".*$")
258
259      string(REGEX MATCH "[0-9]\\.[^\"]+"
260             GSS_VERSION "${heimdal_version_str}")
261    endif()
262
263    if(NOT GSS_VERSION)
264      set(GSS_VERSION "Heimdal Unknown")
265    endif()
266  elseif(NOT GSS_VERSION AND GSS_FLAVOUR STREQUAL "MIT")
267    get_filename_component(_MIT_VERSION "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MIT\\Kerberos\\SDK\\CurrentVersion;VersionString]" NAME CACHE)
268    if(WIN32 AND _MIT_VERSION)
269      set(GSS_VERSION "${_MIT_VERSION}")
270    else()
271      set(GSS_VERSION "MIT Unknown")
272    endif()
273  endif()
274endif()
275
276include(FindPackageHandleStandardArgs)
277
278set(_GSS_REQUIRED_VARS GSS_LIBRARIES GSS_FLAVOUR)
279
280find_package_handle_standard_args(GSS
281    REQUIRED_VARS
282        ${_GSS_REQUIRED_VARS}
283    VERSION_VAR
284        GSS_VERSION
285    FAIL_MESSAGE
286        "Could NOT find GSS, try to set the path to GSS root folder in the system variable GSS_ROOT_DIR"
287)
288
289mark_as_advanced(GSS_INCLUDE_DIR GSS_LIBRARIES)
290