• 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      )
66    message(STATUS "CFLAGS: ${_GSS_CFLAGS}")
67    if(NOT _GSS_CONFIGURE_FAILED) # 0 means success
68      # should also work in an odd case when multiple directories are given
69      string(STRIP "${_GSS_CFLAGS}" _GSS_CFLAGS)
70      string(REGEX REPLACE " +-I" ";" _GSS_CFLAGS "${_GSS_CFLAGS}")
71      string(REGEX REPLACE " +-([^I][^ \\t;]*)" ";-\\1" _GSS_CFLAGS "${_GSS_CFLAGS}")
72
73      foreach(_flag ${_GSS_CFLAGS})
74        if(_flag MATCHES "^-I.*")
75          string(REGEX REPLACE "^-I" "" _val "${_flag}")
76          list(APPEND _GSS_INCLUDE_DIR "${_val}")
77        else()
78          list(APPEND _GSS_COMPILER_FLAGS "${_flag}")
79        endif()
80      endforeach()
81    endif()
82
83    execute_process(
84        COMMAND ${_GSS_CONFIGURE_SCRIPT} "--libs" "gssapi"
85        OUTPUT_VARIABLE _GSS_LIB_FLAGS
86        RESULT_VARIABLE _GSS_CONFIGURE_FAILED
87    )
88    message(STATUS "LDFLAGS: ${_GSS_LIB_FLAGS}")
89
90    if(NOT _GSS_CONFIGURE_FAILED) # 0 means success
91      # this script gives us libraries and link directories. Blah. We have to deal with it.
92      string(STRIP "${_GSS_LIB_FLAGS}" _GSS_LIB_FLAGS)
93      string(REGEX REPLACE " +-(L|l)" ";-\\1" _GSS_LIB_FLAGS "${_GSS_LIB_FLAGS}")
94      string(REGEX REPLACE " +-([^Ll][^ \\t;]*)" ";-\\1" _GSS_LIB_FLAGS "${_GSS_LIB_FLAGS}")
95
96      foreach(_flag ${_GSS_LIB_FLAGS})
97        if(_flag MATCHES "^-l.*")
98          string(REGEX REPLACE "^-l" "" _val "${_flag}")
99          list(APPEND _GSS_LIBRARIES "${_val}")
100        elseif(_flag MATCHES "^-L.*")
101          string(REGEX REPLACE "^-L" "" _val "${_flag}")
102          list(APPEND _GSS_LINK_DIRECTORIES "${_val}")
103        else()
104          list(APPEND _GSS_LINKER_FLAGS "${_flag}")
105        endif()
106      endforeach()
107    endif()
108
109    execute_process(
110        COMMAND ${_GSS_CONFIGURE_SCRIPT} "--version"
111        OUTPUT_VARIABLE _GSS_VERSION
112        RESULT_VARIABLE _GSS_CONFIGURE_FAILED
113    )
114
115    # older versions may not have the "--version" parameter. In this case we just don't care.
116    if(_GSS_CONFIGURE_FAILED)
117      set(_GSS_VERSION 0)
118    endif()
119
120    execute_process(
121        COMMAND ${_GSS_CONFIGURE_SCRIPT} "--vendor"
122        OUTPUT_VARIABLE _GSS_VENDOR
123        RESULT_VARIABLE _GSS_CONFIGURE_FAILED
124    )
125
126    # older versions may not have the "--vendor" parameter. In this case we just don't care.
127    if(_GSS_CONFIGURE_FAILED)
128      set(GSS_FLAVOUR "Heimdal") # most probably, shouldn't really matter
129    else()
130      if(_GSS_VENDOR MATCHES ".*H|heimdal.*")
131        set(GSS_FLAVOUR "Heimdal")
132      else()
133        set(GSS_FLAVOUR "MIT")
134      endif()
135    endif()
136
137  else() # either there is no config script or we are on platform that doesn't provide one (Windows?)
138
139    find_path(_GSS_INCLUDE_DIR
140        NAMES
141            "gssapi/gssapi.h"
142        HINTS
143            ${_GSS_ROOT_HINTS}
144        PATH_SUFFIXES
145            include
146            inc
147    )
148
149    if(_GSS_INCLUDE_DIR) #jay, we've found something
150      set(CMAKE_REQUIRED_INCLUDES "${_GSS_INCLUDE_DIR}")
151      check_include_files( "gssapi/gssapi_generic.h;gssapi/gssapi_krb5.h" _GSS_HAVE_MIT_HEADERS)
152
153      if(_GSS_HAVE_MIT_HEADERS)
154        set(GSS_FLAVOUR "MIT")
155      else()
156        # prevent compiling the header - just check if we can include it
157        set(CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} -D__ROKEN_H__")
158        check_include_file( "roken.h" _GSS_HAVE_ROKEN_H)
159
160        check_include_file( "heimdal/roken.h" _GSS_HAVE_HEIMDAL_ROKEN_H)
161        if(_GSS_HAVE_ROKEN_H OR _GSS_HAVE_HEIMDAL_ROKEN_H)
162          set(GSS_FLAVOUR "Heimdal")
163        endif()
164        set(CMAKE_REQUIRED_DEFINITIONS "")
165      endif()
166    else()
167      # I'm not convienced if this is the right way but this is what autotools do at the moment
168      find_path(_GSS_INCLUDE_DIR
169          NAMES
170              "gssapi.h"
171          HINTS
172              ${_GSS_ROOT_HINTS}
173          PATH_SUFFIXES
174              include
175              inc
176      )
177
178      if(_GSS_INCLUDE_DIR)
179        set(GSS_FLAVOUR "Heimdal")
180      endif()
181    endif()
182
183    # if we have headers, check if we can link libraries
184    if(GSS_FLAVOUR)
185      set(_GSS_LIBDIR_SUFFIXES "")
186      set(_GSS_LIBDIR_HINTS ${_GSS_ROOT_HINTS})
187      get_filename_component(_GSS_CALCULATED_POTENTIAL_ROOT "${_GSS_INCLUDE_DIR}" PATH)
188      list(APPEND _GSS_LIBDIR_HINTS ${_GSS_CALCULATED_POTENTIAL_ROOT})
189
190      if(WIN32)
191        if(CMAKE_SIZEOF_VOID_P EQUAL 8)
192          list(APPEND _GSS_LIBDIR_SUFFIXES "lib/AMD64")
193          if(GSS_FLAVOUR STREQUAL "MIT")
194            set(_GSS_LIBNAME "gssapi64")
195          else()
196            set(_GSS_LIBNAME "libgssapi")
197          endif()
198        else()
199          list(APPEND _GSS_LIBDIR_SUFFIXES "lib/i386")
200          if(GSS_FLAVOUR STREQUAL "MIT")
201            set(_GSS_LIBNAME "gssapi32")
202          else()
203            set(_GSS_LIBNAME "libgssapi")
204          endif()
205        endif()
206      else()
207        list(APPEND _GSS_LIBDIR_SUFFIXES "lib;lib64") # those suffixes are not checked for HINTS
208        if(GSS_FLAVOUR STREQUAL "MIT")
209          set(_GSS_LIBNAME "gssapi_krb5")
210        else()
211          set(_GSS_LIBNAME "gssapi")
212        endif()
213      endif()
214
215      find_library(_GSS_LIBRARIES
216          NAMES
217              ${_GSS_LIBNAME}
218          HINTS
219              ${_GSS_LIBDIR_HINTS}
220          PATH_SUFFIXES
221              ${_GSS_LIBDIR_SUFFIXES}
222      )
223
224    endif()
225  endif()
226else()
227  if(_GSS_PKG_${_MIT_MODNAME}_VERSION)
228    set(GSS_FLAVOUR "MIT")
229    set(_GSS_VERSION _GSS_PKG_${_MIT_MODNAME}_VERSION)
230  else()
231    set(GSS_FLAVOUR "Heimdal")
232    set(_GSS_VERSION _GSS_PKG_${_MIT_HEIMDAL}_VERSION)
233  endif()
234endif()
235
236set(GSS_INCLUDE_DIR ${_GSS_INCLUDE_DIR})
237set(GSS_LIBRARIES ${_GSS_LIBRARIES})
238set(GSS_LINK_DIRECTORIES ${_GSS_LINK_DIRECTORIES})
239set(GSS_LINKER_FLAGS ${_GSS_LINKER_FLAGS})
240set(GSS_COMPILER_FLAGS ${_GSS_COMPILER_FLAGS})
241set(GSS_VERSION ${_GSS_VERSION})
242
243if(GSS_FLAVOUR)
244  if(NOT GSS_VERSION AND GSS_FLAVOUR STREQUAL "Heimdal")
245    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
246      set(HEIMDAL_MANIFEST_FILE "Heimdal.Application.amd64.manifest")
247    else()
248      set(HEIMDAL_MANIFEST_FILE "Heimdal.Application.x86.manifest")
249    endif()
250
251    if(EXISTS "${GSS_INCLUDE_DIR}/${HEIMDAL_MANIFEST_FILE}")
252      file(STRINGS "${GSS_INCLUDE_DIR}/${HEIMDAL_MANIFEST_FILE}" heimdal_version_str
253           REGEX "^.*version=\"[0-9]\\.[^\"]+\".*$")
254
255      string(REGEX MATCH "[0-9]\\.[^\"]+"
256             GSS_VERSION "${heimdal_version_str}")
257    endif()
258
259    if(NOT GSS_VERSION)
260      set(GSS_VERSION "Heimdal Unknown")
261    endif()
262  elseif(NOT GSS_VERSION AND GSS_FLAVOUR STREQUAL "MIT")
263    get_filename_component(_MIT_VERSION "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MIT\\Kerberos\\SDK\\CurrentVersion;VersionString]" NAME CACHE)
264    if(WIN32 AND _MIT_VERSION)
265      set(GSS_VERSION "${_MIT_VERSION}")
266    else()
267      set(GSS_VERSION "MIT Unknown")
268    endif()
269  endif()
270endif()
271
272include(FindPackageHandleStandardArgs)
273
274set(_GSS_REQUIRED_VARS GSS_LIBRARIES GSS_FLAVOUR)
275
276find_package_handle_standard_args(GSS
277    REQUIRED_VARS
278        ${_GSS_REQUIRED_VARS}
279    VERSION_VAR
280        GSS_VERSION
281    FAIL_MESSAGE
282        "Could NOT find GSS, try to set the path to GSS root folder in the system variable GSS_ROOT_DIR"
283)
284
285mark_as_advanced(GSS_INCLUDE_DIR GSS_LIBRARIES)
286