summaryrefslogtreecommitdiff
path: root/cmake/modules/FindKRB5.cmake
blob: 2447df55eb903ed69ab04a852a0d3b42021fd842 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# FindKRB5.cmake
#
# Searches for KRB5 library
#
# The output is:
#    HAVE_KRB5 - set to ON, if Kerberos 5 support is enabled and libraries found
#    HAVE_MIT_KRB5 - set to ON, when found MIT implementation
#    HAVE_HEIMDAL_KRB5 - set to ON, when found Heimdal implementation
#    KRB5_CFLAGS - CFLAGS to use with target_compile_options() and similar commands
#    KRB5_LDFLAGS - LDFLAGS to use with target_link_libraries() and similar commands

include(CheckCSourceCompiles)
include(PkgConfigEx)
include(PrintableOptions)

add_printable_variable_path(WITH_KRB5 "Location of Kerberos 5 install dir, defaults to ON to search for it" "ON")
add_printable_variable_path(WITH_KRB5_INCLUDES "Location of Kerberos 5 headers" "")
add_printable_variable_path(WITH_KRB5_LIBS "Location of Kerberos 5 libraries" "")

if(NOT WITH_KRB5)
	return()
endif(NOT WITH_KRB5)

pkg_check_modules(KRB5 krb5 krb5-gssapi)

if(KRB5_FOUND)
	pkg_check_variable(KRB5_VENDOR krb5 vendor)

	if(KRB5_VENDOR STREQUAL "MIT")
		message(STATUS "Using MIT Kerberos 5 (found by pkg-config)")
		set(WITH_KRB5 ON)
		set(HAVE_MIT_KRB5 ON)
		return()
	endif(KRB5_VENDOR STREQUAL "MIT")

	message(STATUS "Found Kerberos 5 with pkg-config, but unknown vendor '${KRB5_VENDOR}', continue with autodetection")
endif()

string(LENGTH "${CMAKE_BINARY_DIR}" bindirlen)
string(SUBSTRING "${WITH_KRB5}" 0 ${bindirlen} substr)
string(TOUPPER "${WITH_KRB5}" optupper)

if(("${optupper}" STREQUAL "ON") OR ("${substr}" STREQUAL "${CMAKE_BINARY_DIR}"))
	set(WITH_KRB5 "/usr")
endif(("${optupper}" STREQUAL "ON") OR ("${substr}" STREQUAL "${CMAKE_BINARY_DIR}"))

unset(bindirlen)
unset(substr)
unset(optupper)

set(mit_includes "${WITH_KRB5}/include")
set(mit_libs "-lkrb5 -lk5crypto -lcom_err -lgssapi_krb5")
set(heimdal_includes "${WITH_KRB5}/include/heimdal")
set(heimdal_libs "-lkrb5 -lcrypto -lasn1 -lcom_err -lroken -lgssapi")
set(sun_includes "${WITH_KRB5}/include/kerberosv5")
set(sun_libs "-lkrb5 -lgss")

set(krb_libs "${WITH_KRB5}/lib${LIB_SUFFIX}")

if(NOT (WITH_KRB5_INCLUDES STREQUAL ""))
	set(mit_includes "${WITH_KRB5_INCLUDES}")
	set(heimdal_includes "${WITH_KRB5_INCLUDES}")
	set(sun_includes "${WITH_KRB5_INCLUDES}")
endif(NOT (WITH_KRB5_INCLUDES STREQUAL ""))

if(NOT (WITH_KRB5_LIBS STREQUAL ""))
	set(krb_libs "${WITH_KRB5_LIBS}")
endif(NOT (WITH_KRB5_LIBS STREQUAL ""))

set(CMAKE_REQUIRED_INCLUDES "-I${mit_includes}")
set(CMAKE_REQUIRED_LIBRARIES "-L${krb_libs} ${mit_libs}")
CHECK_C_SOURCE_COMPILES("#include <krb5/krb5.h>
			int main(void) { krb5_init_context (NULL); return 0; }" HAVE_KRB5)

if(HAVE_KRB5)
	set(HAVE_MIT_KRB5 ON)
	message(STATUS "Found MIT Kerberos 5")
else(HAVE_KRB5)
	unset(HAVE_KRB5 CACHE)
	set(CMAKE_REQUIRED_INCLUDES "-I${heimdal_includes}")
	set(CMAKE_REQUIRED_LIBRARIES "-L${krb_libs} ${heimdal_libs}")
	CHECK_C_SOURCE_COMPILES("#include <krb5.h>
				int main(void) { krb5_init_context (NULL); return 0; }" HAVE_KRB5)

	if(HAVE_KRB5)
		set(HAVE_HEIMDAL_KRB5 ON)
		message(STATUS "Found Heimdal Kerberos 5")
	endif(HAVE_KRB5)
endif(HAVE_KRB5)

if(NOT HAVE_KRB5)
	unset(HAVE_KRB5 CACHE)
	set(CMAKE_REQUIRED_INCLUDES "-I${sun_includes}")
	set(CMAKE_REQUIRED_LIBRARIES "-L${krb_libs} ${sun_libs}")
	CHECK_C_SOURCE_COMPILES("#include <krb5/krb5.h>
				int main(void) { krb5_init_context (NULL); return 0; }" HAVE_KRB5)
	if(HAVE_KRB5)
		set(HAVE_SUN_KRB5 ON)
		message(STATUS "Found Sun Kerberos 5")
	endif(HAVE_KRB5)
endif(NOT HAVE_KRB5)

if(HAVE_KRB5)
	set(KRB5_CFLAGS ${CMAKE_REQUIRED_INCLUDES})
	set(KRB5_LDFLAGS ${CMAKE_REQUIRED_LIBRARIES})
else(HAVE_KRB5)
	message(FATAL_ERROR "Failed to find Kerberos 5 libraries. Use -DWITH_KRB5=OFF to disable Kerberos 5 support")
endif(HAVE_KRB5)

unset(CMAKE_REQUIRED_LIBRARIES)
unset(CMAKE_REQUIRED_INCLUDES)