summaryrefslogtreecommitdiff
path: root/CMakeLists.txt
diff options
context:
space:
mode:
authornicoo <nicoo@debian.org>2020-02-12 13:42:22 +0100
committerNicolas Braud-Santoni <nicolas@braud-santoni.eu>2020-02-12 13:42:22 +0100
commitc79050aa44b8836d836c5dd22a383a073c28b74b (patch)
tree7bcca9fabd7718bf87ca600a6594f57b76d8de7d /CMakeLists.txt
Import upstream release 1.3.0
Closes: #951184
Diffstat (limited to 'CMakeLists.txt')
-rw-r--r--CMakeLists.txt383
1 files changed, 383 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..c7c5991
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,383 @@
1# Copyright (c) 2018 Yubico AB. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file.
4
5# detect AppleClang; needs to come before project()
6cmake_policy(SET CMP0025 NEW)
7
8project(libfido2 C)
9cmake_minimum_required(VERSION 3.0)
10
11include(CheckCCompilerFlag)
12include(CheckFunctionExists)
13include(CheckIncludeFiles)
14include(CheckTypeSize)
15include(GNUInstallDirs)
16
17set(CMAKE_COLOR_MAKEFILE off)
18set(CMAKE_VERBOSE_MAKEFILE on)
19set(CMAKE_POSITION_INDEPENDENT_CODE ON)
20
21set(FIDO_MAJOR "1")
22set(FIDO_MINOR "3")
23set(FIDO_PATCH "0")
24set(FIDO_VERSION ${FIDO_MAJOR}.${FIDO_MINOR}.${FIDO_PATCH})
25
26add_definitions(-D_FIDO_MAJOR=${FIDO_MAJOR})
27add_definitions(-D_FIDO_MINOR=${FIDO_MINOR})
28add_definitions(-D_FIDO_PATCH=${FIDO_PATCH})
29
30if(WIN32)
31 add_definitions(-DWIN32_LEAN_AND_MEAN)
32endif()
33
34if(APPLE)
35 set(CMAKE_INSTALL_NAME_DIR
36 "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
37endif()
38
39# /dev/urandom
40if(UNIX)
41 add_definitions(-DHAS_DEV_URANDOM)
42endif()
43
44# Observe OpenBSD's library versioning scheme.
45if(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
46 set(LIB_VERSION ${FIDO_MAJOR}.${FIDO_MINOR})
47 set(LIB_SOVERSION ${LIB_VERSION})
48else()
49 set(LIB_VERSION ${FIDO_VERSION})
50 set(LIB_SOVERSION ${FIDO_MAJOR})
51endif()
52
53if(MSVC)
54 if((NOT CBOR_INCLUDE_DIRS) OR (NOT CBOR_LIBRARY_DIRS) OR
55 (NOT CRYPTO_INCLUDE_DIRS) OR (NOT CRYPTO_LIBRARY_DIRS))
56 message(FATAL_ERROR "please provide definitions for "
57 "{CBOR,CRYPTO}_{INCLUDE,LIBRARY}_DIRS when building "
58 "under msvc")
59 endif()
60 set(CBOR_LIBRARIES cbor)
61 set(CRYPTO_LIBRARIES crypto-45)
62 set(MSVC_DISABLED_WARNINGS_LIST
63 "C4200" # nonstandard extension used: zero-sized array in
64 # struct/union;
65 "C4204" # nonstandard extension used: non-constant aggregate
66 # initializer;
67 "C4706" # assignment within conditional expression;
68 "C4996" # The POSIX name for this item is deprecated. Instead,
69 # use the ISO C and C++ conformant name
70 )
71 # The construction in the following 3 lines was taken from LibreSSL's
72 # CMakeLists.txt.
73 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
74 ${MSVC_DISABLED_WARNINGS_LIST})
75 string(REGEX REPLACE "[/-]W[1234][ ]?" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
76 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -MP -W4 ${MSVC_DISABLED_WARNINGS_STR}")
77 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} /Z7")
78 set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} /Zi")
79else()
80 include(FindPkgConfig)
81 pkg_search_module(CBOR libcbor)
82 pkg_search_module(CRYPTO libcrypto REQUIRED)
83
84 # XXX workaround libcbor's missing .pc file
85 if(NOT CBOR_FOUND)
86 check_include_files(cbor.h HAVE_CBOR_H)
87 if(NOT HAVE_CBOR_H)
88 message(FATAL_ERROR "could not find cbor header files")
89 endif()
90 set(CBOR_LIBRARIES "cbor")
91 endif()
92
93 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
94 pkg_search_module(UDEV libudev REQUIRED)
95 set(UDEV_NAME "udev")
96 # Define be32toh().
97 add_definitions(-D_GNU_SOURCE)
98 elseif(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
99 set(BASE_LIBRARIES usbhid)
100 endif()
101
102 if(MINGW)
103 # MinGW is stuck with a flavour of C89.
104 add_definitions(-DFIDO_NO_DIAGNOSTIC)
105 add_definitions(-DWC_ERR_INVALID_CHARS=0x80)
106 endif()
107
108 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
109 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra")
110 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
111 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
112 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wwrite-strings")
113 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-prototypes")
114 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wbad-function-cast")
115 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pedantic")
116 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pedantic-errors")
117 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector-all")
118 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
119
120 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g2")
121 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -fno-omit-frame-pointer")
122
123 if(FUZZ)
124 if(LIBFUZZER)
125 set(FUZZ_LDFLAGS "-fsanitize=fuzzer")
126 endif()
127 add_definitions(-DFIDO_FUZZ)
128 endif()
129
130 if(ASAN)
131 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,leak")
132 endif()
133
134 if(MSAN)
135 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory")
136 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize-memory-track-origins")
137 endif()
138
139 if(UBSAN)
140 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
141 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize-trap=undefined")
142 endif()
143
144 if(COVERAGE)
145 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
146 endif()
147endif()
148
149# Use -Wshorten-64-to-32 if available.
150check_c_compiler_flag("-Wshorten-64-to-32" HAVE_SHORTEN_64_TO_32)
151if(HAVE_SHORTEN_64_TO_32)
152 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshorten-64-to-32")
153endif()
154
155# Avoid https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425
156if(CMAKE_COMPILER_IS_GNUCC)
157 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-result")
158endif()
159
160# Decide which keyword to use for thread-local storage.
161if(CMAKE_COMPILER_IS_GNUCC OR
162 CMAKE_C_COMPILER_ID STREQUAL "Clang" OR
163 CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
164 set(TLS "__thread")
165elseif(WIN32)
166 set(TLS "__declspec(thread)")
167endif()
168
169add_definitions(-DTLS=${TLS})
170
171# endian.h
172check_include_files(endian.h HAVE_ENDIAN_H)
173if(HAVE_ENDIAN_H)
174 add_definitions(-DHAVE_ENDIAN_H)
175endif()
176
177# err.h
178check_include_files(err.h HAVE_ERR_H)
179if(HAVE_ERR_H)
180 add_definitions(-DHAVE_ERR_H)
181endif()
182
183# unistd.h
184check_include_files(unistd.h HAVE_UNISTD_H)
185if(HAVE_UNISTD_H)
186 add_definitions(-DHAVE_UNISTD_H)
187endif()
188
189# signal.h
190check_include_files(signal.h HAVE_SIGNAL_H)
191if(HAVE_SIGNAL_H)
192 add_definitions(-DHAVE_SIGNAL_H)
193endif()
194
195# strlcpy
196check_function_exists(strlcpy HAVE_STRLCPY)
197if(HAVE_STRLCPY)
198 add_definitions(-DHAVE_STRLCPY)
199endif()
200
201# strlcat
202check_function_exists(strlcpy HAVE_STRLCAT)
203if(HAVE_STRLCAT)
204 add_definitions(-DHAVE_STRLCAT)
205endif()
206
207# recallocarray
208check_function_exists(recallocarray HAVE_RECALLOCARRAY)
209if(HAVE_RECALLOCARRAY)
210 add_definitions(-DHAVE_RECALLOCARRAY)
211endif()
212
213# XXX getpagesize is incorrectly detected when cross-compiling
214# with mingw on Linux. Avoid.
215if(NOT WIN32)
216 check_function_exists(getpagesize HAVE_GETPAGESIZE)
217endif()
218if(HAVE_GETPAGESIZE)
219 add_definitions(-DHAVE_GETPAGESIZE)
220endif()
221
222# sysconf
223check_function_exists(sysconf HAVE_SYSCONF)
224if(HAVE_SYSCONF)
225 add_definitions(-DHAVE_SYSCONF)
226endif()
227
228# memset_s
229if(APPLE)
230 add_definitions(-D__STDC_WANT_LIB_EXT1__=1)
231endif()
232check_function_exists(memset_s HAVE_MEMSET_S)
233if(HAVE_MEMSET_S)
234 add_definitions(-DHAVE_MEMSET_S)
235endif()
236
237# explicit_bzero
238if(NOT LIBFUZZER)
239 check_function_exists(explicit_bzero HAVE_EXPLICIT_BZERO)
240 if(HAVE_EXPLICIT_BZERO)
241 add_definitions(-DHAVE_EXPLICIT_BZERO)
242 endif()
243endif()
244
245# timingsafe_bcmp
246check_function_exists(timingsafe_bcmp HAVE_TIMINGSAFE_BCMP)
247if(HAVE_TIMINGSAFE_BCMP)
248 add_definitions(-DHAVE_TIMINGSAFE_BCMP)
249endif()
250
251# readpassphrase
252check_function_exists(readpassphrase HAVE_READPASSPHRASE)
253if(HAVE_READPASSPHRASE)
254 add_definitions(-DHAVE_READPASSPHRASE)
255endif()
256
257# getline
258check_function_exists(getline HAVE_GETLINE)
259if(HAVE_GETLINE)
260 add_definitions(-DHAVE_GETLINE)
261endif()
262
263# getopt
264check_function_exists(getopt HAVE_GETOPT)
265if(HAVE_GETOPT)
266 add_definitions(-DHAVE_GETOPT)
267 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wcast-qual")
268else()
269 if(CMAKE_COMPILER_IS_GNUCC)
270 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-discarded-qualifiers")
271 endif()
272 if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
273 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-incompatible-pointer-types-discards-qualifiers")
274 endif()
275endif()
276
277# usable sigaction
278set(CMAKE_EXTRA_INCLUDE_FILES signal.h)
279check_function_exists(sigaction HAVE_SIGACTION)
280check_type_size("sig_atomic_t" HAVE_SIG_ATOMIC_T)
281if(HAVE_SIGACTION AND (NOT HAVE_SIG_ATOMIC_T STREQUAL ""))
282 add_definitions(-DSIGNAL_EXAMPLE)
283endif()
284set(CMAKE_EXTRA_INCLUDE_FILES)
285
286# arc4random_buf
287check_function_exists(arc4random_buf HAVE_ARC4RANDOM_BUF)
288if(HAVE_ARC4RANDOM_BUF)
289 add_definitions(-DHAVE_ARC4RANDOM_BUF)
290endif()
291
292# getentropy
293check_function_exists(getentropy HAVE_GETENTROPY)
294if(HAVE_GETENTROPY)
295 add_definitions(-DHAVE_GETENTROPY)
296endif()
297
298# export list
299if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
300 # clang + lld
301 string(CONCAT CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS}
302 " -exported_symbols_list ${CMAKE_CURRENT_SOURCE_DIR}/src/export.llvm")
303elseif(NOT MSVC)
304 # clang/gcc + gnu ld
305 string(CONCAT CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS}
306 " -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/src/export.gnu")
307 if(NOT WIN32)
308 string(CONCAT CMAKE_SHARED_LINKER_FLAGS
309 ${CMAKE_SHARED_LINKER_FLAGS}
310 " -Wl,-z,noexecstack -Wl,-z,relro,-z,now")
311 string(CONCAT CMAKE_EXE_LINKER_FLAGS
312 ${CMAKE_EXE_LINKER_FLAGS}
313 " -Wl,-z,noexecstack -Wl,-z,relro,-z,now")
314 if(FUZZ)
315 file(STRINGS fuzz/wrapped.sym WRAPPED_SYMBOLS)
316 foreach(s ${WRAPPED_SYMBOLS})
317 string(CONCAT CMAKE_SHARED_LINKER_FLAGS
318 ${CMAKE_SHARED_LINKER_FLAGS}
319 " -Wl,--wrap=${s}")
320 endforeach()
321 endif()
322 endif()
323else()
324 string(CONCAT CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS}
325 " /def:${CMAKE_CURRENT_SOURCE_DIR}/src/export.msvc")
326endif()
327
328include_directories(${CMAKE_SOURCE_DIR}/src)
329include_directories(${CBOR_INCLUDE_DIRS})
330include_directories(${CRYPTO_INCLUDE_DIRS})
331
332link_directories(${CBOR_LIBRARY_DIRS})
333link_directories(${CRYPTO_LIBRARY_DIRS})
334
335message(STATUS "CMAKE_C_COMPILER: ${CMAKE_C_COMPILER}")
336message(STATUS "CMAKE_C_COMPILER_ID: ${CMAKE_C_COMPILER_ID}")
337message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
338message(STATUS "CMAKE_INSTALL_LIBDIR: ${CMAKE_INSTALL_LIBDIR}")
339message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
340message(STATUS "CBOR_INCLUDE_DIRS: ${CBOR_INCLUDE_DIRS}")
341message(STATUS "CBOR_LIBRARY_DIRS: ${CBOR_LIBRARY_DIRS}")
342message(STATUS "CBOR_LIBRARIES: ${CBOR_LIBRARIES}")
343message(STATUS "CRYPTO_INCLUDE_DIRS: ${CRYPTO_INCLUDE_DIRS}")
344message(STATUS "CRYPTO_LIBRARY_DIRS: ${CRYPTO_LIBRARY_DIRS}")
345message(STATUS "CRYPTO_LIBRARIES: ${CRYPTO_LIBRARIES}")
346message(STATUS "BASE_LIBRARIES: ${BASE_LIBRARIES}")
347message(STATUS "VERSION: ${FIDO_VERSION}")
348message(STATUS "LIB_VERSION: ${LIB_VERSION}")
349message(STATUS "LIB_SOVERSION: ${LIB_SOVERSION}")
350message(STATUS "FUZZ: ${FUZZ}")
351message(STATUS "AFL: ${AFL}")
352message(STATUS "LIBFUZZER: ${LIBFUZZER}")
353message(STATUS "ASAN: ${ASAN}")
354message(STATUS "MSAN: ${MSAN}")
355message(STATUS "COVERAGE: ${COVERAGE}")
356message(STATUS "TLS: ${TLS}")
357
358if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
359 message(STATUS "UDEV_INCLUDE_DIRS: ${UDEV_INCLUDE_DIRS}")
360 message(STATUS "UDEV_LIBRARY_DIRS: ${UDEV_LIBRARY_DIRS}")
361 message(STATUS "UDEV_LIBRARIES: ${UDEV_LIBRARIES}")
362 message(STATUS "UDEV_RULES_DIR: ${UDEV_RULES_DIR}")
363endif()
364
365subdirs(src)
366subdirs(examples)
367subdirs(tools)
368subdirs(man)
369
370if(NOT WIN32)
371 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
372 if(NOT MSAN AND NOT LIBFUZZER)
373 subdirs(regress)
374 endif()
375 endif()
376 if(FUZZ)
377 subdirs(fuzz)
378 endif()
379
380 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
381 subdirs(udev)
382 endif()
383endif()