FindGSS.cmake 10 KB

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