diff options
Diffstat (limited to 'CMakeLists.txt')
-rw-r--r-- | CMakeLists.txt | 383 |
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() | ||
6 | cmake_policy(SET CMP0025 NEW) | ||
7 | |||
8 | project(libfido2 C) | ||
9 | cmake_minimum_required(VERSION 3.0) | ||
10 | |||
11 | include(CheckCCompilerFlag) | ||
12 | include(CheckFunctionExists) | ||
13 | include(CheckIncludeFiles) | ||
14 | include(CheckTypeSize) | ||
15 | include(GNUInstallDirs) | ||
16 | |||
17 | set(CMAKE_COLOR_MAKEFILE off) | ||
18 | set(CMAKE_VERBOSE_MAKEFILE on) | ||
19 | set(CMAKE_POSITION_INDEPENDENT_CODE ON) | ||
20 | |||
21 | set(FIDO_MAJOR "1") | ||
22 | set(FIDO_MINOR "3") | ||
23 | set(FIDO_PATCH "0") | ||
24 | set(FIDO_VERSION ${FIDO_MAJOR}.${FIDO_MINOR}.${FIDO_PATCH}) | ||
25 | |||
26 | add_definitions(-D_FIDO_MAJOR=${FIDO_MAJOR}) | ||
27 | add_definitions(-D_FIDO_MINOR=${FIDO_MINOR}) | ||
28 | add_definitions(-D_FIDO_PATCH=${FIDO_PATCH}) | ||
29 | |||
30 | if(WIN32) | ||
31 | add_definitions(-DWIN32_LEAN_AND_MEAN) | ||
32 | endif() | ||
33 | |||
34 | if(APPLE) | ||
35 | set(CMAKE_INSTALL_NAME_DIR | ||
36 | "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") | ||
37 | endif() | ||
38 | |||
39 | # /dev/urandom | ||
40 | if(UNIX) | ||
41 | add_definitions(-DHAS_DEV_URANDOM) | ||
42 | endif() | ||
43 | |||
44 | # Observe OpenBSD's library versioning scheme. | ||
45 | if(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD") | ||
46 | set(LIB_VERSION ${FIDO_MAJOR}.${FIDO_MINOR}) | ||
47 | set(LIB_SOVERSION ${LIB_VERSION}) | ||
48 | else() | ||
49 | set(LIB_VERSION ${FIDO_VERSION}) | ||
50 | set(LIB_SOVERSION ${FIDO_MAJOR}) | ||
51 | endif() | ||
52 | |||
53 | if(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") | ||
79 | else() | ||
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() | ||
147 | endif() | ||
148 | |||
149 | # Use -Wshorten-64-to-32 if available. | ||
150 | check_c_compiler_flag("-Wshorten-64-to-32" HAVE_SHORTEN_64_TO_32) | ||
151 | if(HAVE_SHORTEN_64_TO_32) | ||
152 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshorten-64-to-32") | ||
153 | endif() | ||
154 | |||
155 | # Avoid https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 | ||
156 | if(CMAKE_COMPILER_IS_GNUCC) | ||
157 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-result") | ||
158 | endif() | ||
159 | |||
160 | # Decide which keyword to use for thread-local storage. | ||
161 | if(CMAKE_COMPILER_IS_GNUCC OR | ||
162 | CMAKE_C_COMPILER_ID STREQUAL "Clang" OR | ||
163 | CMAKE_C_COMPILER_ID STREQUAL "AppleClang") | ||
164 | set(TLS "__thread") | ||
165 | elseif(WIN32) | ||
166 | set(TLS "__declspec(thread)") | ||
167 | endif() | ||
168 | |||
169 | add_definitions(-DTLS=${TLS}) | ||
170 | |||
171 | # endian.h | ||
172 | check_include_files(endian.h HAVE_ENDIAN_H) | ||
173 | if(HAVE_ENDIAN_H) | ||
174 | add_definitions(-DHAVE_ENDIAN_H) | ||
175 | endif() | ||
176 | |||
177 | # err.h | ||
178 | check_include_files(err.h HAVE_ERR_H) | ||
179 | if(HAVE_ERR_H) | ||
180 | add_definitions(-DHAVE_ERR_H) | ||
181 | endif() | ||
182 | |||
183 | # unistd.h | ||
184 | check_include_files(unistd.h HAVE_UNISTD_H) | ||
185 | if(HAVE_UNISTD_H) | ||
186 | add_definitions(-DHAVE_UNISTD_H) | ||
187 | endif() | ||
188 | |||
189 | # signal.h | ||
190 | check_include_files(signal.h HAVE_SIGNAL_H) | ||
191 | if(HAVE_SIGNAL_H) | ||
192 | add_definitions(-DHAVE_SIGNAL_H) | ||
193 | endif() | ||
194 | |||
195 | # strlcpy | ||
196 | check_function_exists(strlcpy HAVE_STRLCPY) | ||
197 | if(HAVE_STRLCPY) | ||
198 | add_definitions(-DHAVE_STRLCPY) | ||
199 | endif() | ||
200 | |||
201 | # strlcat | ||
202 | check_function_exists(strlcpy HAVE_STRLCAT) | ||
203 | if(HAVE_STRLCAT) | ||
204 | add_definitions(-DHAVE_STRLCAT) | ||
205 | endif() | ||
206 | |||
207 | # recallocarray | ||
208 | check_function_exists(recallocarray HAVE_RECALLOCARRAY) | ||
209 | if(HAVE_RECALLOCARRAY) | ||
210 | add_definitions(-DHAVE_RECALLOCARRAY) | ||
211 | endif() | ||
212 | |||
213 | # XXX getpagesize is incorrectly detected when cross-compiling | ||
214 | # with mingw on Linux. Avoid. | ||
215 | if(NOT WIN32) | ||
216 | check_function_exists(getpagesize HAVE_GETPAGESIZE) | ||
217 | endif() | ||
218 | if(HAVE_GETPAGESIZE) | ||
219 | add_definitions(-DHAVE_GETPAGESIZE) | ||
220 | endif() | ||
221 | |||
222 | # sysconf | ||
223 | check_function_exists(sysconf HAVE_SYSCONF) | ||
224 | if(HAVE_SYSCONF) | ||
225 | add_definitions(-DHAVE_SYSCONF) | ||
226 | endif() | ||
227 | |||
228 | # memset_s | ||
229 | if(APPLE) | ||
230 | add_definitions(-D__STDC_WANT_LIB_EXT1__=1) | ||
231 | endif() | ||
232 | check_function_exists(memset_s HAVE_MEMSET_S) | ||
233 | if(HAVE_MEMSET_S) | ||
234 | add_definitions(-DHAVE_MEMSET_S) | ||
235 | endif() | ||
236 | |||
237 | # explicit_bzero | ||
238 | if(NOT LIBFUZZER) | ||
239 | check_function_exists(explicit_bzero HAVE_EXPLICIT_BZERO) | ||
240 | if(HAVE_EXPLICIT_BZERO) | ||
241 | add_definitions(-DHAVE_EXPLICIT_BZERO) | ||
242 | endif() | ||
243 | endif() | ||
244 | |||
245 | # timingsafe_bcmp | ||
246 | check_function_exists(timingsafe_bcmp HAVE_TIMINGSAFE_BCMP) | ||
247 | if(HAVE_TIMINGSAFE_BCMP) | ||
248 | add_definitions(-DHAVE_TIMINGSAFE_BCMP) | ||
249 | endif() | ||
250 | |||
251 | # readpassphrase | ||
252 | check_function_exists(readpassphrase HAVE_READPASSPHRASE) | ||
253 | if(HAVE_READPASSPHRASE) | ||
254 | add_definitions(-DHAVE_READPASSPHRASE) | ||
255 | endif() | ||
256 | |||
257 | # getline | ||
258 | check_function_exists(getline HAVE_GETLINE) | ||
259 | if(HAVE_GETLINE) | ||
260 | add_definitions(-DHAVE_GETLINE) | ||
261 | endif() | ||
262 | |||
263 | # getopt | ||
264 | check_function_exists(getopt HAVE_GETOPT) | ||
265 | if(HAVE_GETOPT) | ||
266 | add_definitions(-DHAVE_GETOPT) | ||
267 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wcast-qual") | ||
268 | else() | ||
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() | ||
275 | endif() | ||
276 | |||
277 | # usable sigaction | ||
278 | set(CMAKE_EXTRA_INCLUDE_FILES signal.h) | ||
279 | check_function_exists(sigaction HAVE_SIGACTION) | ||
280 | check_type_size("sig_atomic_t" HAVE_SIG_ATOMIC_T) | ||
281 | if(HAVE_SIGACTION AND (NOT HAVE_SIG_ATOMIC_T STREQUAL "")) | ||
282 | add_definitions(-DSIGNAL_EXAMPLE) | ||
283 | endif() | ||
284 | set(CMAKE_EXTRA_INCLUDE_FILES) | ||
285 | |||
286 | # arc4random_buf | ||
287 | check_function_exists(arc4random_buf HAVE_ARC4RANDOM_BUF) | ||
288 | if(HAVE_ARC4RANDOM_BUF) | ||
289 | add_definitions(-DHAVE_ARC4RANDOM_BUF) | ||
290 | endif() | ||
291 | |||
292 | # getentropy | ||
293 | check_function_exists(getentropy HAVE_GETENTROPY) | ||
294 | if(HAVE_GETENTROPY) | ||
295 | add_definitions(-DHAVE_GETENTROPY) | ||
296 | endif() | ||
297 | |||
298 | # export list | ||
299 | if(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") | ||
303 | elseif(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() | ||
323 | else() | ||
324 | string(CONCAT CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS} | ||
325 | " /def:${CMAKE_CURRENT_SOURCE_DIR}/src/export.msvc") | ||
326 | endif() | ||
327 | |||
328 | include_directories(${CMAKE_SOURCE_DIR}/src) | ||
329 | include_directories(${CBOR_INCLUDE_DIRS}) | ||
330 | include_directories(${CRYPTO_INCLUDE_DIRS}) | ||
331 | |||
332 | link_directories(${CBOR_LIBRARY_DIRS}) | ||
333 | link_directories(${CRYPTO_LIBRARY_DIRS}) | ||
334 | |||
335 | message(STATUS "CMAKE_C_COMPILER: ${CMAKE_C_COMPILER}") | ||
336 | message(STATUS "CMAKE_C_COMPILER_ID: ${CMAKE_C_COMPILER_ID}") | ||
337 | message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}") | ||
338 | message(STATUS "CMAKE_INSTALL_LIBDIR: ${CMAKE_INSTALL_LIBDIR}") | ||
339 | message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}") | ||
340 | message(STATUS "CBOR_INCLUDE_DIRS: ${CBOR_INCLUDE_DIRS}") | ||
341 | message(STATUS "CBOR_LIBRARY_DIRS: ${CBOR_LIBRARY_DIRS}") | ||
342 | message(STATUS "CBOR_LIBRARIES: ${CBOR_LIBRARIES}") | ||
343 | message(STATUS "CRYPTO_INCLUDE_DIRS: ${CRYPTO_INCLUDE_DIRS}") | ||
344 | message(STATUS "CRYPTO_LIBRARY_DIRS: ${CRYPTO_LIBRARY_DIRS}") | ||
345 | message(STATUS "CRYPTO_LIBRARIES: ${CRYPTO_LIBRARIES}") | ||
346 | message(STATUS "BASE_LIBRARIES: ${BASE_LIBRARIES}") | ||
347 | message(STATUS "VERSION: ${FIDO_VERSION}") | ||
348 | message(STATUS "LIB_VERSION: ${LIB_VERSION}") | ||
349 | message(STATUS "LIB_SOVERSION: ${LIB_SOVERSION}") | ||
350 | message(STATUS "FUZZ: ${FUZZ}") | ||
351 | message(STATUS "AFL: ${AFL}") | ||
352 | message(STATUS "LIBFUZZER: ${LIBFUZZER}") | ||
353 | message(STATUS "ASAN: ${ASAN}") | ||
354 | message(STATUS "MSAN: ${MSAN}") | ||
355 | message(STATUS "COVERAGE: ${COVERAGE}") | ||
356 | message(STATUS "TLS: ${TLS}") | ||
357 | |||
358 | if(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}") | ||
363 | endif() | ||
364 | |||
365 | subdirs(src) | ||
366 | subdirs(examples) | ||
367 | subdirs(tools) | ||
368 | subdirs(man) | ||
369 | |||
370 | if(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() | ||
383 | endif() | ||