FindGSS.cmake 10 KB

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