-
Notifications
You must be signed in to change notification settings - Fork 24
/
CMakeLists.txt
351 lines (295 loc) · 12.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
341
342
343
344
345
346
347
348
349
350
351
# Copyright 2019 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
project (sxg LANGUAGES C CXX)
set (CMAKE_PROJECT_VERSION "0")
set (CMAKE_PROJECT_VERSION_MAJOR "0")
set (CMAKE_PROJECT_VERSION_MINOR "2")
set (CMAKE_PROJECT_VERSION_PATCH "0")
set (LIBSXG_CORE_VERSION
"${CMAKE_PROJECT_VERSION_MAJOR}.${CMAKE_PROJECT_VERSION_MINOR}")
set (LIBSXG_SOVERSION "${CMAKE_PROJECT_VERSION_MAJOR}")
option (SXG_BUILD_SHARED "Build shared library" ON)
option (SXG_BUILD_STATIC "Build static library" OFF)
option (SXG_WITH_CERT_CHAIN
"Build cert chain functions (depends on OpenSSL)" ON)
option (SXG_BUILD_MAN "Build SXG man target" ON)
option (SXG_BUILD_FUZZER "Build SXG fuzzer" ON)
if (SXG_BUILD_SHARED AND SXG_WITH_CERT_CHAIN)
option (SXG_BUILD_EXECUTABLES "Build gensxg/gencertchain executables" ON)
endif()
if (NOT SXG_WITH_CERT_CHAIN AND SXG_BUILD_EXECUTABLES)
message(
FATAL_ERROR
"Cert chain library required for gensxg/gencertchain executables" )
endif ()
if (NOT SXG_BUILD_SHARED AND SXG_BUILD_EXECUTABLES)
message(
FATAL_ERROR
"Shared library required for gensxg/gencertchain executables" )
endif ()
if (NOT SXG_BUILD_SHARED AND NOT SXG_BUILD_STATIC)
message(
FATAL_ERROR
"One of SXG_BUILD_SHARED or SXG_BUILD_STATIC is required" )
endif ()
option (
RUN_TEST
"If false, build libsxg without building tests.\
Building tests of libsxg requires more recent version of cmake (>= 3.13).\
Tests include fuzzer will be disabled automatically if it is not supported
by the compiler."
TRUE)
if (NOT CMAKE_BUILD_TYPE)
set (CMAKE_BUILD_TYPE "Debug")
endif ()
# When RUN_TEST is enabled, `target_link_options` used in fuzzing test requires 3.13.
# When RUN_TEST is disabled, I'm not sure what minimum version is required, but
# it worked when tested at 3.1.
if (RUN_TEST)
cmake_minimum_required (VERSION 3.13)
else ()
cmake_minimum_required (VERSION 3.1)
endif ()
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -std=c++11 -fPIC")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -std=c99 -fPIC -D_POSIX_C_SOURCE=200112L")
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb")
set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb")
include (ExternalProject)
set_property (DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY EP_UPDATE_DISCONNECTED 1)
find_package (OpenSSL REQUIRED)
set (SXG_SOURCES
src/sxg_buffer.c
src/sxg_buffer_debug.c
src/sxg_cbor.c
src/sxg_codec.c
src/sxg_encoded_response.c
src/sxg_generate.c
src/sxg_header.c
src/sxg_raw_response.c
src/sxg_sig.c
src/sxg_signer_list.c)
if (SXG_WITH_CERT_CHAIN)
list (APPEND SXG_SOURCES src/sxg_cert_chain.c)
endif ()
configure_file(
"include/libsxg.h.in"
"include/libsxg.h"
)
if (SXG_BUILD_SHARED)
add_library (sxg SHARED ${SXG_SOURCES})
target_include_directories (sxg PRIVATE ${PROJECT_BINARY_DIR}/include
${PROJECT_SOURCE_DIR}/include
${OPENSSL_INCLUDE_DIR})
target_link_libraries (sxg PRIVATE ${OPENSSL_LIBRARIES})
endif()
if (SXG_BUILD_STATIC)
add_library (sxg_static STATIC ${SXG_SOURCES})
set_target_properties(sxg_static PROPERTIES OUTPUT_NAME sxg)
target_include_directories (sxg_static PRIVATE ${PROJECT_BINARY_DIR}/include
${PROJECT_SOURCE_DIR}/include
${OPENSSL_INCLUDE_DIR})
target_link_libraries (sxg_static PRIVATE ${OPENSSL_LIBRARIES})
endif ()
if (SXG_BUILD_EXECUTABLES)
add_executable (gensxg src/gensxg.c)
target_include_directories (gensxg PRIVATE ${PROJECT_SOURCE_DIR}/include
${PROJECT_BINARY_DIR}/include
${OPENSSL_INCLUDE_DIR})
target_link_libraries (gensxg PRIVATE ${OPENSSL_LIBRARIES} sxg)
add_executable (gencertchain src/gencertchain.c)
target_include_directories (gencertchain PRIVATE ${PROJECT_SOURCE_DIR}/include
${PROJECT_BINARY_DIR}/include
${OPENSSL_INCLUDE_DIR})
target_link_libraries (gencertchain PRIVATE ${OPENSSL_LIBRARIES} sxg)
add_executable (verifysignature src/verifysignature.c)
target_include_directories (verifysignature PRIVATE ${PROJECT_SOURCE_DIR}/include
${PROJECT_BINARY_DIR}/include
${OPENSSL_INCLUDE_DIR})
target_link_libraries (verifysignature PRIVATE ${OPENSSL_LIBRARIES} sxg)
endif ()
# ##############################################################################
# Installing
# ##############################################################################
if (SXG_BUILD_SHARED)
set_target_properties (
sxg PROPERTIES VERSION ${LIBSXG_CORE_VERSION} SOVERSION ${LIBSXG_SOVERSION})
set_target_properties (sxg PROPERTIES PUBLIC_HEADER "${HEADERS}")
install (
TARGETS sxg
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT library)
endif ()
if (SXG_BUILD_STATIC)
set_target_properties (
sxg_static PROPERTIES VERSION ${LIBSXG_CORE_VERSION} SOVERSION ${LIBSXG_SOVERSION})
set_target_properties (sxg_static PROPERTIES PUBLIC_HEADER "${HEADERS}")
install (
TARGETS sxg_static
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT library)
endif ()
install (
DIRECTORY ${CMAKE_SOURCE_DIR}/include/ PUBLIC_HEADER
DIRECTORY ${CMAKE_BINARY_DIR}/include/ PUBLIC_HEADER
DESTINATION ${CMAKE_INSTALL_PREFIX}/include
FILES_MATCHING
PATTERN "*.h")
if (SXG_BUILD_EXECUTABLES)
install (TARGETS gensxg RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install (TARGETS gencertchain RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install (TARGETS verifysignature RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif ()
# ##############################################################################
# Generate man files
# ##############################################################################
# This target requires ronn executable in your path.
if (SXG_BUILD_MAN)
add_custom_target (
man
COMMAND ronn -r ${CMAKE_SOURCE_DIR}/docs/gencertchain.md --pipe > ${CMAKE_SOURCE_DIR}/docs/gencertchain.1 &&
ronn -r ${CMAKE_SOURCE_DIR}/docs/gensxg.md --pipe > ${CMAKE_SOURCE_DIR}/docs/gensxg.1 &&
ronn -r ${CMAKE_SOURCE_DIR}/docs/libsxg.md --pipe > ${CMAKE_SOURCE_DIR}/docs/libsxg.3
COMMENT "Generating roff files with ronn ...")
endif ()
# ##############################################################################
# Linter
# ##############################################################################
# This target requires cmake-format executable in your path.
add_custom_target (
cmake_format
COMMAND cmake-format -i ${CMAKE_SOURCE_DIR}/CMakeLists.txt
COMMENT "Formating with cmake-format ...")
# This target requires clang-format executable in your path.
add_custom_target (
clang_format
COMMAND
cd ${CMAKE_SOURCE_DIR} && git ls-files *.{c,h,hpp,cpp,cc} | xargs
clang-format -style=file -i
COMMENT "Formating with clang-format ...")
# This target requires [markdownlint](https://github.com/igorshubovych/markdownlint-cli) executable in your path.
add_custom_target (
man_format
COMMAND
find ${CMAKE_SOURCE_DIR}/docs/ -name '*.md' | xargs markdownlint -c ${CMAKE_SOURCE_DIR}/.markdownlint
COMMENT "Formating with markdownlint ...")
# ##############################################################################
# Testing
# ##############################################################################
if (RUN_TEST)
enable_testing ()
file (COPY tests/testdata DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
ExternalProject_Add (
gtest
GIT_REPOSITORY https://github.com/google/googletest
GIT_TAG main
SOURCE_DIR ${PROJECT_BINARY_DIR}/third_party/gtest
BINARY_DIR ${PROJECT_BINARY_DIR}/gtest
INSTALL_COMMAND "")
add_library (libgtest IMPORTED STATIC GLOBAL)
add_dependencies (libgtest gtest)
set_target_properties (
libgtest
PROPERTIES "IMPORTED_LOCATION" "${PROJECT_BINARY_DIR}/gtest/lib/libgtest.a"
"IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}")
add_library (libgtest_main IMPORTED STATIC GLOBAL)
add_dependencies (libgtest_main gtest)
set_target_properties (
libgtest_main
PROPERTIES "IMPORTED_LOCATION"
"${PROJECT_BINARY_DIR}/gtest/lib/libgtest_main.a"
"IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}")
set (GTEST_INCLUDE
"${PROJECT_BINARY_DIR}/third_party/gtest/googletest/include")
if (SXG_BUILD_SHARED)
add_library (test_util SHARED tests/test_util.cc)
else ()
add_library (test_util STATIC tests/test_util.cc)
endif ()
target_include_directories (test_util PUBLIC ${PROJECT_BINARY_DIR}/include
${PROJECT_SOURCE_DIR}/include
${OPENSSL_INCLUDE_DIR}
${GTEST_INCLUDE})
if (SXG_BUILD_SHARED)
add_dependencies (test_util gtest libgtest libgtest_main sxg)
target_link_libraries (test_util INTERFACE libgtest libgtest_main pthread sxg
${OPENSSL_LIBRARIES})
else ()
add_dependencies (test_util gtest libgtest libgtest_main sxg_static)
target_link_libraries (test_util INTERFACE libgtest libgtest_main pthread sxg_static
${OPENSSL_LIBRARIES})
endif ()
endif ()
set (SANITIZER_OPTIONS "-fsanitize=address,leak,undefined")
macro (add_test_macro target_name test_name with_sanitizer)
add_executable (${target_name} tests/${test_name}.cc)
add_test (NAME ${target_name} COMMAND ${target_name})
add_dependencies (${target_name} test_util)
target_link_libraries (${target_name} PRIVATE test_util)
if (with_sanitizer)
target_compile_options (${target_name} PRIVATE ${SANITIZER_OPTIONS})
target_link_options (${target_name} PRIVATE ${SANITIZER_OPTIONS})
endif ()
endmacro ()
function (configure_test test_name)
message ("-- Configuring test: ${test_name}")
add_test_macro (${test_name} ${test_name} FALSE)
message ("-- Configuring test: ${test_name}_sanitizer")
add_test_macro ("${test_name}_sanitizer" ${test_name} TRUE)
endfunction ()
if (RUN_TEST)
#configure_test (nfail_malloc_test)
configure_test (sxg_buffer_test)
configure_test (sxg_cbor_test)
if (SXG_WITH_CERT_CHAIN)
configure_test (sxg_cert_chain_test)
endif ()
configure_test (sxg_codec_test)
configure_test (sxg_encoded_response_test)
configure_test (sxg_generate_test)
configure_test (sxg_header_test)
configure_test (sxg_sig_test)
configure_test (sxg_signer_list_test)
configure_test (toplevel_test)
endif ()
# ##############################################################################
# Fuzzing
# ##############################################################################
if (SXG_BUILD_FUZZER)
set (fuzzer_option "-fsanitize=address,fuzzer")
function (configure_fuzzer fuzzer_name)
message ("-- Configuring fuzzer: ${fuzzer_name}")
add_executable (${fuzzer_name} fuzzers/${fuzzer_name}.cc)
target_compile_options (${fuzzer_name} PRIVATE ${fuzzer_option})
target_link_options (${fuzzer_name} PRIVATE ${fuzzer_option})
add_dependencies (${fuzzer_name} sxg)
target_link_libraries (${fuzzer_name} PRIVATE sxg ${OPENSSL_LIBRARIES})
target_include_directories (${fuzzer_name}
PRIVATE ${PROJECT_SOURCE_DIR}/include)
endfunction ()
# check_cxx_compiler_flag command may be good.
try_compile (
fuzzer_available ${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_SOURCE_DIR}/fuzzers/signer_fuzzer.cc
CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:STRING=${PROJECT_SOURCE_DIR}/include"
LINK_LIBRARIES "sxg ${OPENSSL_LIBRARIES}" LINK_OPTIONS ${fuzzer_option})
if (RUN_TEST)
if (fuzzer_available)
configure_fuzzer (signer_fuzzer)
else ()
message (
"fuzzer disabled because the compiler ${CMAKE_CXX_COMPILER} does not support [${fuzzer_option}] option"
)
endif ()
endif ()
endif ()