-
Notifications
You must be signed in to change notification settings - Fork 29
/
CMakeLists.txt
118 lines (99 loc) · 4.08 KB
/
CMakeLists.txt
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
112
113
114
115
116
117
118
cmake_minimum_required(VERSION 3.16)
project(cotp VERSION "3.1.0" LANGUAGES "C")
set(CMAKE_C_STANDARD 11)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(GNUInstallDirs)
find_package(PkgConfig)
option(BUILD_SHARED_LIBS "Build libcotp as a shared library" ON)
option(BUILD_TESTS "Build base32 and cotp tests" OFF)
set(HMAC_WRAPPER "gcrypt" CACHE STRING "Choose between gcrypt (default), openssl or mbedtls for HMAC computation")
set_property(CACHE HMAC_WRAPPER PROPERTY STRINGS "gcrypt" "openssl" "mbedtls")
if("${HMAC_WRAPPER}" STREQUAL "gcrypt")
set(HMAC_SOURCE_FILES
src/utils/whmac_gcrypt.c
)
find_package(Gcrypt 1.8.0 REQUIRED)
set(HMAC_INCLUDE_DIR ${GCRYPT_INCLUDE_DIR})
set(HMAC_LIBRARIES ${GCRYPT_LIBRARIES})
message("libcotp will use gcrypt for hmac")
elseif("${HMAC_WRAPPER}" STREQUAL "openssl")
find_package(OpenSSL 3.0.0 REQUIRED)
set(HMAC_SOURCE_FILES
src/utils/whmac_openssl.c
)
set(HMAC_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR})
set(HMAC_LIBRARIES ${OPENSSL_LIBRARIES})
message("libcotp will use openssl for hmac")
elseif("${HMAC_WRAPPER}" STREQUAL "mbedtls")
find_package(MbedTLS REQUIRED)
set(HMAC_SOURCE_FILES
src/utils/whmac_mbedtls.c
)
set(HMAC_INCLUDE_DIR ${MBEDTLS_INCLUDE_DIRS})
set(HMAC_LIBRARIES ${MBEDTLS_LIBRARIES})
message("libcotp will use mbedtls for hmac")
else()
message("libcotp can't use ${HMAC_WRAPPER} for hmac")
endif()
include_directories(${HMAC_INCLUDE_DIR})
if (BUILD_TESTS)
add_subdirectory(tests)
endif ()
set(COTP_HEADERS
src/cotp.h
)
set(SOURCE_FILES
src/otp.c
${HMAC_SOURCE_FILES}
src/utils/base32.c
)
# Set compiler flags for all targets
if(NOT MSVC)
add_compile_options(-Wall -Wextra -O3 -Wformat=2 -Wmissing-format-attribute -fstack-protector-strong -Wundef -Wmissing-format-attribute
-fdiagnostics-color=always -Wstrict-prototypes -Wunreachable-code -Wchar-subscripts -Wwrite-strings -Wpointer-arith -Wbad-function-cast
-Wcast-align -Werror=format-security -Werror=implicit-function-declaration -Wno-sign-compare -Wno-format-nonliteral -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3)
endif()
add_library(cotp ${SOURCE_FILES})
target_link_libraries(cotp ${HMAC_LIBRARIES})
target_include_directories(cotp
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/src
PRIVATE
${HMAC_INCLUDE_DIR})
if(NOT MSVC)
target_compile_options(cotp PRIVATE
-Wall -Wextra -O3 -Wformat=2 -Wmissing-format-attribute -fstack-protector-strong -Wundef -Wmissing-format-attribute
-fdiagnostics-color=always -Wstrict-prototypes -Wunreachable-code -Wchar-subscripts -Wwrite-strings -Wpointer-arith -Wbad-function-cast
-Wcast-align -Werror=format-security -Werror=implicit-function-declaration -Wno-sign-compare -Wno-format-nonliteral -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3)
endif()
target_link_directories(cotp PRIVATE
${HMACLIBRARY_DIRS})
set_target_properties(cotp PROPERTIES VERSION ${CMAKE_PROJECT_VERSION} SOVERSION ${CMAKE_PROJECT_VERSION_MAJOR})
set(COTP_LIB_DIR "${CMAKE_INSTALL_LIBDIR}")
set(COTP_INC_DIR "${CMAKE_INSTALL_INCLUDEDIR}")
install(
TARGETS cotp
ARCHIVE DESTINATION ${COTP_LIB_DIR}
LIBRARY DESTINATION ${COTP_LIB_DIR}
COMPONENT library
)
install(
FILES ${COTP_HEADERS}
DESTINATION ${COTP_INC_DIR}
)
if (PkgConfig_FOUND)
# Allow adding prefix if CMAKE_INSTALL_INCLUDEDIR not absolute.
if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}")
set(PKGCONFIG_TARGET_INCLUDES "${CMAKE_INSTALL_INCLUDEDIR}")
else()
set(PKGCONFIG_TARGET_INCLUDES "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
endif()
# Allow adding prefix if CMAKE_INSTALL_LIBDIR not absolute.
if(IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
set(PKGCONFIG_TARGET_LIBS "${CMAKE_INSTALL_LIBDIR}")
else()
set(PKGCONFIG_TARGET_LIBS "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
endif()
configure_file("cotp.pc.in" "cotp.pc" @ONLY)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/cotp.pc" DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig/)
endif()