FindGSS.cmake 10 KB

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