FindGSS.cmake 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. # Find the GSS Kerberos library
  25. #
  26. # Input variables:
  27. #
  28. # GSS_ROOT_DIR Set this variable to the root installation of GSS
  29. #
  30. # Result variables:
  31. #
  32. # GSS_FOUND System has the Heimdal library
  33. # GSS_FLAVOUR "MIT" or "Heimdal" if anything found
  34. # GSS_INCLUDE_DIRS The GSS include directories
  35. # GSS_LIBRARIES The GSS library names
  36. # GSS_LIBRARY_DIRS The GSS library directories
  37. # GSS_LDFLAGS Required linker flags
  38. # GSS_CFLAGS Required compiler flags
  39. # GSS_VERSION This is set to version advertised by pkg-config or read from manifest.
  40. # In case the library is found but no version info available it is set to "unknown"
  41. set(_mit_modname "mit-krb5-gssapi")
  42. set(_heimdal_modname "heimdal-gssapi")
  43. include(CheckIncludeFile)
  44. include(CheckIncludeFiles)
  45. include(CheckTypeSize)
  46. set(_gss_root_hints
  47. "${GSS_ROOT_DIR}"
  48. "$ENV{GSS_ROOT_DIR}"
  49. )
  50. # Try to find library using system pkg-config if user did not specify root dir
  51. if(NOT GSS_ROOT_DIR AND NOT "$ENV{GSS_ROOT_DIR}")
  52. if(CURL_USE_PKGCONFIG)
  53. find_package(PkgConfig QUIET)
  54. pkg_search_module(_GSS ${_mit_modname} ${_heimdal_modname})
  55. list(APPEND _gss_root_hints "${_GSS_PREFIX}")
  56. endif()
  57. if(WIN32)
  58. list(APPEND _gss_root_hints "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MIT\\Kerberos;InstallDir]")
  59. endif()
  60. endif()
  61. if(NOT _GSS_FOUND) # Not found by pkg-config. Let us take more traditional approach.
  62. find_file(_gss_configure_script
  63. NAMES
  64. "krb5-config"
  65. HINTS
  66. ${_gss_root_hints}
  67. PATH_SUFFIXES
  68. "bin"
  69. NO_CMAKE_PATH
  70. NO_CMAKE_ENVIRONMENT_PATH
  71. )
  72. # If not found in user-supplied directories, maybe system knows better
  73. find_file(_gss_configure_script
  74. NAMES
  75. "krb5-config"
  76. PATH_SUFFIXES
  77. "bin"
  78. )
  79. if(_gss_configure_script)
  80. execute_process(
  81. COMMAND ${_gss_configure_script} "--cflags" "gssapi"
  82. OUTPUT_VARIABLE _GSS_CFLAGS
  83. RESULT_VARIABLE _gss_configure_failed
  84. OUTPUT_STRIP_TRAILING_WHITESPACE
  85. )
  86. message(STATUS "FindGSS CFLAGS: ${_GSS_CFLAGS}")
  87. if(NOT _gss_configure_failed) # 0 means success
  88. # Should also work in an odd case when multiple directories are given
  89. string(STRIP "${_GSS_CFLAGS}" _GSS_CFLAGS)
  90. string(REGEX REPLACE " +-I" ";" _GSS_CFLAGS "${_GSS_CFLAGS}")
  91. string(REGEX REPLACE " +-([^I][^ \\t;]*)" ";-\\1" _GSS_CFLAGS "${_GSS_CFLAGS}")
  92. foreach(_flag IN LISTS _GSS_CFLAGS)
  93. if(_flag MATCHES "^-I.*")
  94. string(REGEX REPLACE "^-I" "" _val "${_flag}")
  95. list(APPEND _GSS_INCLUDE_DIRS "${_val}")
  96. else()
  97. list(APPEND _GSS_CFLAGS "${_flag}")
  98. endif()
  99. endforeach()
  100. endif()
  101. execute_process(
  102. COMMAND ${_gss_configure_script} "--libs" "gssapi"
  103. OUTPUT_VARIABLE _gss_lib_flags
  104. RESULT_VARIABLE _gss_configure_failed
  105. OUTPUT_STRIP_TRAILING_WHITESPACE
  106. )
  107. message(STATUS "FindGSS LDFLAGS: ${_gss_lib_flags}")
  108. if(NOT _gss_configure_failed) # 0 means success
  109. # This script gives us libraries and link directories. Blah. We have to deal with it.
  110. string(STRIP "${_gss_lib_flags}" _gss_lib_flags)
  111. string(REGEX REPLACE " +-(L|l)" ";-\\1" _gss_lib_flags "${_gss_lib_flags}")
  112. string(REGEX REPLACE " +-([^Ll][^ \\t;]*)" ";-\\1" _gss_lib_flags "${_gss_lib_flags}")
  113. foreach(_flag IN LISTS _gss_lib_flags)
  114. if(_flag MATCHES "^-l.*")
  115. string(REGEX REPLACE "^-l" "" _val "${_flag}")
  116. list(APPEND _GSS_LIBRARIES "${_val}")
  117. elseif(_flag MATCHES "^-L.*")
  118. string(REGEX REPLACE "^-L" "" _val "${_flag}")
  119. list(APPEND _GSS_LIBRARY_DIRS "${_val}")
  120. else()
  121. list(APPEND _GSS_LDFLAGS "${_flag}")
  122. endif()
  123. endforeach()
  124. endif()
  125. execute_process(
  126. COMMAND ${_gss_configure_script} "--version"
  127. OUTPUT_VARIABLE _GSS_VERSION
  128. RESULT_VARIABLE _gss_configure_failed
  129. OUTPUT_STRIP_TRAILING_WHITESPACE
  130. )
  131. # Older versions may not have the "--version" parameter. In this case we just do not care.
  132. if(_gss_configure_failed)
  133. set(_GSS_VERSION 0)
  134. endif()
  135. execute_process(
  136. COMMAND ${_gss_configure_script} "--vendor"
  137. OUTPUT_VARIABLE _gss_vendor
  138. RESULT_VARIABLE _gss_configure_failed
  139. OUTPUT_STRIP_TRAILING_WHITESPACE
  140. )
  141. # Older versions may not have the "--vendor" parameter. In this case we just do not care.
  142. if(_gss_configure_failed)
  143. set(GSS_FLAVOUR "Heimdal") # most probably, should not really matter
  144. else()
  145. if(_gss_vendor MATCHES ".*H|heimdal.*")
  146. set(GSS_FLAVOUR "Heimdal")
  147. else()
  148. set(GSS_FLAVOUR "MIT")
  149. endif()
  150. endif()
  151. else() # Either there is no config script or we are on a platform that does not provide one (Windows?)
  152. find_path(_GSS_INCLUDE_DIRS NAMES "gssapi/gssapi.h"
  153. HINTS
  154. ${_gss_root_hints}
  155. PATH_SUFFIXES
  156. "include"
  157. "inc"
  158. )
  159. if(_GSS_INCLUDE_DIRS) # jay, we have found something
  160. set(CMAKE_REQUIRED_INCLUDES "${_GSS_INCLUDE_DIRS}")
  161. check_include_files("gssapi/gssapi_generic.h;gssapi/gssapi_krb5.h" _gss_have_mit_headers)
  162. if(_gss_have_mit_headers)
  163. set(GSS_FLAVOUR "MIT")
  164. else()
  165. # Prevent compiling the header - just check if we can include it
  166. list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D__ROKEN_H__")
  167. check_include_file("roken.h" _gss_have_roken_h)
  168. check_include_file("heimdal/roken.h" _gss_have_heimdal_roken_h)
  169. if(_gss_have_roken_h OR _gss_have_heimdal_roken_h)
  170. set(GSS_FLAVOUR "Heimdal")
  171. endif()
  172. list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS "-D__ROKEN_H__")
  173. endif()
  174. else()
  175. # I am not convinced if this is the right way but this is what autotools do at the moment
  176. find_path(_GSS_INCLUDE_DIRS NAMES "gssapi.h"
  177. HINTS
  178. ${_gss_root_hints}
  179. PATH_SUFFIXES
  180. "include"
  181. "inc"
  182. )
  183. if(_GSS_INCLUDE_DIRS)
  184. set(GSS_FLAVOUR "Heimdal")
  185. endif()
  186. endif()
  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_DIRS}" DIRECTORY)
  192. list(APPEND _gss_libdir_hints ${_gss_calculated_potential_root})
  193. if(WIN32)
  194. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  195. list(APPEND _gss_libdir_suffixes "lib/AMD64")
  196. if(GSS_FLAVOUR STREQUAL "MIT")
  197. set(_gss_libname "gssapi64")
  198. else()
  199. set(_gss_libname "libgssapi")
  200. endif()
  201. else()
  202. list(APPEND _gss_libdir_suffixes "lib/i386")
  203. if(GSS_FLAVOUR STREQUAL "MIT")
  204. set(_gss_libname "gssapi32")
  205. else()
  206. set(_gss_libname "libgssapi")
  207. endif()
  208. endif()
  209. else()
  210. list(APPEND _gss_libdir_suffixes "lib;lib64") # those suffixes are not checked for HINTS
  211. if(GSS_FLAVOUR STREQUAL "MIT")
  212. set(_gss_libname "gssapi_krb5")
  213. else()
  214. set(_gss_libname "gssapi")
  215. endif()
  216. endif()
  217. find_library(_GSS_LIBRARIES NAMES ${_gss_libname}
  218. HINTS
  219. ${_gss_libdir_hints}
  220. PATH_SUFFIXES
  221. ${_gss_libdir_suffixes}
  222. )
  223. endif()
  224. endif()
  225. else()
  226. if(_GSS_MODULE_NAME STREQUAL _mit_modname OR _GSS_${_mit_modname}_VERSION) # _GSS_MODULE_NAME set since CMake 3.16
  227. set(GSS_FLAVOUR "MIT")
  228. if(NOT _GSS_VERSION) # for old CMake versions?
  229. set(_GSS_VERSION ${_GSS_${_mit_modname}_VERSION})
  230. endif()
  231. else()
  232. set(GSS_FLAVOUR "Heimdal")
  233. if(NOT _GSS_VERSION) # for old CMake versions?
  234. set(_GSS_VERSION ${_GSS_${_heimdal_modname}_VERSION})
  235. endif()
  236. endif()
  237. message(STATUS "Found GSS/${GSS_FLAVOUR} (via pkg-config): ${_GSS_INCLUDE_DIRS} (found version \"${_GSS_VERSION}\")")
  238. endif()
  239. set(GSS_INCLUDE_DIRS ${_GSS_INCLUDE_DIRS})
  240. set(GSS_LIBRARIES ${_GSS_LIBRARIES})
  241. set(GSS_LIBRARY_DIRS ${_GSS_LIBRARY_DIRS})
  242. set(GSS_LDFLAGS ${_GSS_LDFLAGS})
  243. set(GSS_CFLAGS ${_GSS_CFLAGS})
  244. set(GSS_VERSION ${_GSS_VERSION})
  245. if(GSS_FLAVOUR)
  246. if(NOT GSS_VERSION AND GSS_FLAVOUR STREQUAL "Heimdal")
  247. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  248. set(_heimdal_manifest_file "Heimdal.Application.amd64.manifest")
  249. else()
  250. set(_heimdal_manifest_file "Heimdal.Application.x86.manifest")
  251. endif()
  252. if(EXISTS "${GSS_INCLUDE_DIRS}/${_heimdal_manifest_file}")
  253. file(STRINGS "${GSS_INCLUDE_DIRS}/${_heimdal_manifest_file}" _heimdal_version_str
  254. REGEX "^.*version=\"[0-9]\\.[^\"]+\".*$")
  255. string(REGEX MATCH "[0-9]\\.[^\"]+" GSS_VERSION "${_heimdal_version_str}")
  256. endif()
  257. if(NOT GSS_VERSION)
  258. set(GSS_VERSION "Heimdal Unknown")
  259. endif()
  260. elseif(NOT GSS_VERSION AND GSS_FLAVOUR STREQUAL "MIT")
  261. get_filename_component(_mit_version "[HKEY_LOCAL_MACHINE\\SOFTWARE\\MIT\\Kerberos\\SDK\\CurrentVersion;VersionString]" NAME
  262. CACHE)
  263. if(WIN32 AND _mit_version)
  264. set(GSS_VERSION "${_mit_version}")
  265. else()
  266. set(GSS_VERSION "MIT Unknown")
  267. endif()
  268. endif()
  269. endif()
  270. include(FindPackageHandleStandardArgs)
  271. find_package_handle_standard_args(GSS
  272. REQUIRED_VARS
  273. GSS_FLAVOUR
  274. GSS_LIBRARIES
  275. VERSION_VAR
  276. GSS_VERSION
  277. FAIL_MESSAGE
  278. "Could NOT find GSS, try to set the path to GSS root folder in the system variable GSS_ROOT_DIR"
  279. )
  280. mark_as_advanced(
  281. _GSS_CFLAGS
  282. _GSS_FOUND
  283. _GSS_INCLUDE_DIRS
  284. _GSS_LDFLAGS
  285. _GSS_LIBRARIES
  286. _GSS_LIBRARY_DIRS
  287. _GSS_MODULE_NAME
  288. _GSS_PREFIX
  289. _GSS_VERSION
  290. )