-
Notifications
You must be signed in to change notification settings - Fork 23
/
CMakeLists.txt
340 lines (296 loc) · 11.6 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
cmake_minimum_required(VERSION 3.11.0)
set(VERSION_MAJOR
0
CACHE STRING "Project major version number")
set(VERSION_MINOR
"1"
CACHE STRING "Project minor version number")
set(VERSION_PATCH
"0"
CACHE STRING "Project patch version number")
set(COSE_VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
mark_as_advanced(VERSION_MAJOR VERSION_MINOR VERSION_PATCH COSE_VERSION)
project("cose-c" VERSION "${COSE_VERSION}")
###############################################################################
# OPTIONS
###############################################################################
option(COSE_C_USE_CONTEXT "Use context pointer for COSE functions" ON)
option(COSE_C_VERBOSE "Produce COSE_C_VERBOSE makefile output" OFF)
option(COSE_C_OPTIMIZE "Optimize for size" OFF)
option(COSE_C_FATAL_WARNINGS "Treat build warnings as error" OFF)
option(COSE_C_COVERALLS "Generate coveralls data" ON)
option(COSE_C_COVERALLS_SEND "Send data to coveralls site" OFF)
option(COSE_C_BUILD_DOCS "Create docs using Doxygen" OFF)
option(COSE_C_RUN_CLANG_TIDY "Use Clang-Tidy for static analysis" OFF)
option(COSE_C_BUILD_TESTS "build tests" ON)
option(COSE_C_BUILD_DUMPER "build dumper" ON)
option(BUILD_SHARED_LIBS "Build Shared Libraries" ON)
option(COSE_C_USE_MBEDTLS "Use MBedTLS for the Crypto Package" OFF)
option(COSE_C_USE_FIND_PACKAGE "Use cmake find_package instead of using cmake project_add" OFF)
option(COSE_C_INCLUDE_ENCRYPT "Include COSE_ENCRYPT" ON)
option(COSE_C_INCLUDE_ENCRYPT0 "Include COSE_ENCRYPT0" ON)
option(COSE_C_INCLUDE_MAC "Include COSE_MAC" ON)
option(COSE_C_INCLUDE_MAC0 "Include COSE_MAC0" ON)
option(COSE_C_INCLUDE_SIGN "Include COSE_SIGN" ON)
option(COSE_C_INCLUDE_SIGN1 "Include COSE_SIGN1" ON)
option(COSE_C_INCLUDE_COUNTERSIGN "Include COSE_COUNTERSIGN" OFF)
option(COSE_C_INCLUDE_COUNTERSIGN1 "Include COSE_COUNTERSIGN1" OFF)
option(COSE_C_VALGRIND_MEMORY_CHECK "use Valgrind to check memory, \
run `ctest -D ExperimentalMemCheck` then after build to run tests with valgrind" OFF)
if(COSE_C_VALGRIND_MEMORY_CHECK)
find_program(MEMORYCHECK_COMMAND valgrind)
if(NOT MEMORYCHECK_COMMAND)
message(FATAL_ERROR "valgrind not found.")
endif()
set(MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full")
set(MEMORYCHECK_SUPPRESSIONS_FILE "${PROJECT_SOURCE_DIR}/valgrind_suppress.txt")
endif()
set(COSE_C_USE_SANITIZER
"none"
CACHE
STRING
" \
Name of the sanitizer which the user whats to use \
This behaviour is the same is turning on one the sanitizer flags below \
This option is a convenience option \
")
set_property(CACHE COSE_C_USE_SANITIZER PROPERTY STRINGS none address memory leak undefined)
set(COSE_C_USE_ADDRESS_SANITIZER OFF)
set(COSE_C_USE_MEMORY_SANITIZER OFF)
set(COSE_C_USE_LEAK_SANITIZER OFF)
set(COSE_C_USE_UNDEFINED_SANITIZER OFF)
if(COSE_C_USE_SANITIZER STREQUAL "address")
set(COSE_C_USE_ADDRESS_SANITIZER ON)
endif()
if(COSE_C_USE_SANITIZER STREQUAL "memory")
set(COSE_C_USE_MEMORY_SANITIZER ON)
endif()
if(COSE_C_USE_SANITIZER STREQUAL "leak")
set(COSE_C_USE_LEAK_SANITIZER ON)
endif()
if(COSE_C_USE_SANITIZER STREQUAL "undefined")
set(COSE_C_USE_UNDEFINED_SANITIZER ON)
endif()
# Set the output of the libraries and executables.
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_BINARY_DIR})
set(dist_dir ${CMAKE_BINARY_DIR}/dist)
set(prefix ${CMAKE_INSTALL_PREFIX})
set(exec_prefix ${CMAKE_INSTALL_PREFIX}/bin)
set(libdir ${CMAKE_INSTALL_PREFIX}/lib)
set(includedir ${CMAKE_INSTALL_PREFIX}/include)
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 11)
endif()
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
endif()
if(NOT CMAKE_BUILD_TYPE)
if(COSE_C_OPTIMIZE)
set(CMAKE_BUILD_TYPE MinSizeRel)
set(COSE_C_COVERALLS OFF)
set(COSE_C_COVERALLS_SEND OFF)
else()
set(CMAKE_BUILD_TYPE Debug)
endif()
endif()
if(NOT COSE_C_INCLUDE_ENCRYPT)
add_definitions(-DINCLUDE_ENCRYPT=0)
endif()
if(NOT COSE_C_INCLUDE_ENCRYPT0)
add_definitions(-DINCLUDE_ENCRYPT0=0)
endif()
if(NOT COSE_C_INCLUDE_MAC)
add_definitions(-DINCLUDE_MAC=0)
endif()
if(NOT COSE_C_INCLUDE_MAC0)
add_definitions(-DINCLUDE_MAC0=0)
endif()
if(NOT COSE_C_INCLUDE_SIGN)
add_definitions(-DINCLUDE_SIGN=0)
endif()
if(NOT COSE_C_INCLUDE_SIGN1)
add_definitions(-DINCLUDE_SIGN1=0)
endif()
if(NOT COSE_C_INCLUDE_COUNTERSIGN)
add_definitions(-DINCLUDE_COUNTERSIGN=0)
endif()
if(NOT COSE_C_INCLUDE_COUNTERSIGN1)
add_definitions(-DINCLUDE_COUNTERSIGN1=0)
endif()
if(COSE_C_USE_CONTEXT)
add_definitions(-DUSE_CBOR_CONTEXT)
endif()
if(MSVC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
set(COSE_C_COVERALLS OFF)
endif()
if(versbose)
set(CMAKE_VERBOSE_MAKEFILE ON)
endif()
# Control the order of packages - get the latest not the first
set(CMAKE_FIND_PACKAGE_SORT_ORDER NATURAL)
set(CMAKE_FIND_PACKAGE_SORT_DIRECTION DEC)
###############################################################################
# DOCS
###############################################################################
if(COSE_C_BUILD_DOCS)
find_package(Doxygen)
if(NOT DOXYGEN_FOUND)
message(FATAL_ERROR "Doxygen is needed to build the documenation")
endif()
set(doxyfile_in ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile.in)
set(doxyfile ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile)
configure_file(${doxyfile_in} ${doxyfile} @ONLY)
add_custom_target(
doc
COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen"
VERBATIM)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html DESTINATION share/doc)
endif()
###############################################################################
# STATIC ANALYSIS
###############################################################################
if(COSE_C_RUN_CLANG_TIDY)
find_program(
CLANG_TIDY_EXE
NAMES "clang-tidy"
DOC "/usr/bin/clang-tidy")
if(NOT CLANG_TIDY_EXE)
message(WARNING "clang-tidy not found.")
endif()
endif(COSE_C_RUN_CLANG_TIDY)
include(sanitizers)
###############################################################################
# DEPENDENCIES
###############################################################################
if(COSE_C_USE_FIND_PACKAGE)
find_package(cn-cbor)
else()
include(FetchContent)
fetchcontent_declare(
project_cn_cbor
GIT_REPOSITORY https://github.com/jimsch/cn-cbor.git
GIT_TAG master)
fetchcontent_getproperties(project_cn_cbor)
if(NOT project_cn_cbor_POPULATED)
fetchcontent_populate(project_cn_cbor)
set(CN_CBOR_FATAL_WARNINGS
OFF
CACHE BOOL "" FORCE)
set(CN_CBOR_COVERALLS
OFF
CACHE BOOL "" FORCE)
set(CN_CBOR_USE_CONTEXT
${COSE_C_USE_CONTEXT}
CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS
${BUILD_SHARED_LIBS}
CACHE BOOL "" FORCE)
set(CN_CBOR_BUILD_DOCS
OFF
CACHE BOOL "" FORCE)
set(CN_CBOR_BUILD_TESTS
OFF
CACHE BOOL "" FORCE)
add_subdirectory(${project_cn_cbor_SOURCE_DIR} ${project_cn_cbor_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
endif()
if(BUILD_SHARED_LIBS)
set(BUILD_STATIC_LIBS OFF)
else()
set(BUILD_STATIC_LIBS ON)
endif()
if(COSE_C_USE_MBEDTLS)
set(COSE_C_USE_OPENSSL OFF)
if(COSE_C_USE_FIND_PACKAGE)
find_package(MbedTLS)
else()
include(FetchContent)
fetchcontent_declare(
project_mbedtls
GIT_REPOSITORY https://github.com/ARMmbed/mbedtls
GIT_TAG master)
fetchcontent_getproperties(project_mbedtls)
if(NOT project_mbedtls_POPULATED)
fetchcontent_populate(project_mbedtls)
set(ENABLED_PROGRAMS
OFF
CACHE BOOL "" FORCE)
set(USE_STATIC_MBEDTLS_LIBRARY
${BUILD_STATIC_LIBS}
CACHE BOOL "" FORCE)
set(USE_SHARED_MBEDTLS_LIBRARY
${BUILD_SHARED_LIBS}
CACHE BOOL "" FORCE)
set(ENABLE_TESTING
OFF
CACHE BOOL "" FORCE)
add_subdirectory(${project_mbedtls_SOURCE_DIR} ${project_mbedtls_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()
# TODO: remove next line after https://github.com/ARMmbed/mbedtls/pull/2090
include_directories("${project_mbedtls_SOURCE_DIR}/include")
endif()
else()
find_package(OpenSSL REQUIRED)
set(COSE_C_USE_OPENSSL ON)
endif()
###############################################################################
# ADD LIBRARY
###############################################################################
add_subdirectory(src)
###############################################################################
# ADD DUMPER
###############################################################################
if(COSE_C_BUILD_DUMPER)
add_subdirectory(dumper)
endif()
###############################################################################
# ADD TESTS
###############################################################################
if(COSE_C_BUILD_TESTS)
enable_testing()
include(CTest)
add_subdirectory(test)
endif()
###############################################################################
# PRINT CONFIG
###############################################################################
message(STATUS "COSE_C_INCLUDE_ENCRYPT:..........${COSE_C_INCLUDE_ENCRYPT}")
message(STATUS "COSE_C_INCLUDE_ENCRYPT0:.........${COSE_C_INCLUDE_ENCRYPT0}")
message(STATUS "COSE_C_INCLUDE_MAC:..............${COSE_C_INCLUDE_MAC}")
message(STATUS "COSE_C_INCLUDE_MAC0:.............${COSE_C_INCLUDE_MAC0}")
message(STATUS "COSE_C_INCLUDE_SIGN:.............${COSE_C_INCLUDE_SIGN}")
message(STATUS "COSE_C_INCLUDE_SIGN1:............${COSE_C_INCLUDE_SIGN1}")
message(STATUS "COSE_C_INCLUDE_COUNTERSIGN:......${COSE_C_INCLUDE_COUNTERSIGN}")
message(STATUS "COSE_C_INCLUDE_COUNTERSIGN1:.....${COSE_C_INCLUDE_COUNTERSIGN1}")
message(STATUS "COSE_C_USE_CONTEXT:..............${COSE_C_USE_CONTEXT}")
message(STATUS "COSE_C_FATAL_WARNINGS:...........${COSE_C_FATAL_WARNINGS}")
message(STATUS "COSE_C_BUILD_TESTS:..............${COSE_C_BUILD_TESTS}")
message(STATUS "COSE_C_BUILD_DOCS:...............${COSE_C_BUILD_DOCS}")
message(STATUS "COSE_C_USE_MBEDTLS:..............${COSE_C_USE_MBEDTLS}")
message(STATUS "COSE_C_USE_FIND_PACKAGE:.........${COSE_C_USE_FIND_PACKAGE}")
message(STATUS "COSE_C_BUILD_DUMPER:.............${COSE_C_BUILD_DUMPER}")
message(STATUS "CMAKE_BUILD_TYPE:................${CMAKE_BUILD_TYPE}")
message(STATUS "BUILD_SHARED_LIBS:...............${BUILD_SHARED_LIBS}")
message(STATUS "COSE_VERSION:....................${COSE_VERSION}")
message(STATUS "CMAKE_VERSION:...................${CMAKE_VERSION}")
message(STATUS "CMAKE_C_COMPILER:................${CMAKE_C_COMPILER}")
message(STATUS "CMAKE_CXX_COMPILER:..............${CMAKE_CXX_COMPILER}")
message(STATUS "CLANG_TIDY_EXE:..................${CLANG_TIDY_EXE}")
message(STATUS "COSE_C_USE_SANITIZER:............${COSE_C_USE_SANITIZER}")
message(STATUS "COSE_C_USE_ADDRESS_SANITIZER:....${COSE_C_USE_ADDRESS_SANITIZER}")
message(STATUS "COSE_C_USE_MEMORY_SANITIZER:.....${COSE_C_USE_MEMORY_SANITIZER}")
message(STATUS "COSE_C_USE_LEAK_SANITIZER:.......${COSE_C_USE_LEAK_SANITIZER}")
message(STATUS "COSE_C_USE_UNDEFINED_SANITIZER:..${COSE_C_USE_UNDEFINED_SANITIZER}")
message(STATUS "COSE_C_VALGRIND_MEMORY_CHECK:....${COSE_C_VALGRIND_MEMORY_CHECK}")
if(COSE_C_USE_OPENSSL)
message(STATUS "OpenSSL .........................${OPENSSL_LIBRARIES}")
endif()
message(STATUS "project_cn_cbor_SOURCE_DIR:......${project_cn_cbor_SOURCE_DIR}")
message(STATUS "project_cn_cbor_BINARY_DIR:......${project_cn_cbor_BINARY_DIR}")
message(STATUS "project_mbedtls_SOURCE_DIR:......${project_mbedtls_SOURCE_DIR}")
message(STATUS "project_mbedtls_BINARY_DIR:......${project_mbedtls_BINARY_DIR}")