forked from BitBoxSwiss/bitbox02-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
361 lines (311 loc) · 15.1 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
352
353
354
355
356
357
358
359
360
361
# Copyright (c) 2015-2016 Lucas Betschart, Douglas J. Bakkum
# Copyright 2019 Shift Cryptosecurity AG
#
# 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.
cmake_minimum_required(VERSION 3.10)
# Set a default build type if none was specified
set(default_build_type "RelWithDebInfo")
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to '${default_build_type}' as none was specified.")
set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE STRING "Choose the type of build." FORCE)
# We don't use the builtin MinSizeRel because Release always optimizes for size.
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "RelWithDebInfo")
endif()
# TODO: Using ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE}} does not work the first time due to this.
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE)
if(
NOT CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
NOT CMAKE_BUILD_TYPE STREQUAL "RELEASE" AND
NOT CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO"
)
message(FATAL_ERROR "Invalid CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}")
endif()
project(bitbox02 C)
# Where to find custom cmake modules
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/tools/nanopb/extra)
#-----------------------------------------------------------------------------
# Options for building
option(COVERAGE "Compile with test coverage flags." OFF)
option(SANITIZE_ADDRESS "Compile with asan." OFF)
option(SANITIZE_UNDEFINED "Compile with ubsan." OFF)
option(BUILD_DOCUMENTATION "Build the Doxygen documentation." OFF)
option(CMAKE_VERBOSE_MAKEFILE "Verbose build." OFF)
# Generate compile_command.json (for tidy and other tools)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
#-----------------------------------------------------------------------------
# Force out-of-source build
if(${CMAKE_CURRENT_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_BINARY_DIR})
message(FATAL_ERROR
"CMake generation is not allowed within the source directory!\n"
"Remove the CMakeCache.txt file and try again from another folder, e.g.:\n\n"
" rm CMakeCache.txt\n"
" mkdir build\n"
" cd build\n"
" cmake ..\n"
)
endif()
# Finds objcopy and other utilities for us
if(CMAKE_CROSSCOMPILING)
include(CMakeFindBinUtils)
find_program(CMAKE_SIZE "${TOOLCHAIN_PREFIX}size" HINTS /usr/local/bin)
message(STATUS size: ${CMAKE_SIZE} ${TOOLCHAIN_PREFIX}size)
endif()
#-----------------------------------------------------------------------------
# Create version header file
#
# Versions MUST contain three parts and start with lowercase 'v'.
# Example 'v1.0.0'. They MUST not contain a pre-release label such as '-beta'.
set(FIRMWARE_VERSION "v3.0.0")
set(BOOTLOADER_VERSION "v1.0.1")
find_package(PythonInterp 3.6 REQUIRED)
find_package(Git)
if(GIT_FOUND AND PYTHONINTERP_FOUND)
# TODO: --verify cannot be used in container. Add our pubkeys to image?
execute_process(
COMMAND ${PYTHON_EXECUTABLE} ./scripts/get_version firmware --check-semver --check-gpg
RESULT_VARIABLE exit_code
OUTPUT_VARIABLE GIT_FIRMWARE_VERSION_STRING
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT exit_code EQUAL "0")
set(GIT_FIRMWARE_VERSION_STRING "v0.0.0")
endif()
execute_process(
COMMAND ${PYTHON_EXECUTABLE} ./scripts/get_version bootloader --check-semver --check-gpg
RESULT_VARIABLE exit_code
OUTPUT_VARIABLE GIT_BOOTLOADER_VERSION_STRING
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT exit_code EQUAL "0")
set(GIT_BOOTLOADER_VERSION_STRING "v0.0.0")
endif()
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse HEAD
RESULT_VARIABLE exit_code
OUTPUT_VARIABLE GIT_COMMIT_HASH
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT exit_code EQUAL "0")
set(GIT_COMMIT_HASH "0000000000000000000000000000000000000000")
endif()
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
RESULT_VARIABLE exit_code
OUTPUT_VARIABLE GIT_COMMIT_HASH_SHORT
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT exit_code EQUAL "0")
set(GIT_COMMIT_HASH_SHORT "000000")
endif()
else()
set(GIT_FIRMWARE_VERSION_STRING "v0.0.0")
set(GIT_BOOTLOADER_VERSION_STRING "v0.0.0")
set(GIT_COMMIT_HASH "0000000000000000000000000000000000000000")
set(GIT_COMMIT_HASH_SHORT "000000")
endif()
# If the current HEAD is not on the matching tag append '-pre' for "pre-release"
set(FIRMWARE_VERSION_FULL ${FIRMWARE_VERSION})
if(NOT FIRMWARE_VERSION STREQUAL GIT_FIRMWARE_VERSION_STRING)
string(APPEND FIRMWARE_VERSION_FULL "-pre+${GIT_COMMIT_HASH_SHORT}")
endif()
set(BOOTLOADER_VERSION_FULL ${BOOTLOADER_VERSION})
if(NOT BOOTLOADER_VERSION STREQUAL GIT_BOOTLOADER_VERSION_STRING)
string(APPEND BOOTLOADER_VERSION_FULL "-pre+${GIT_COMMIT_HASH_SHORT}")
add_definitions("-DBOOTLOADER_VERSION_HAS_METADATA")
endif()
string(REPLACE "." ";" VERSION_LIST ${FIRMWARE_VERSION})
list(GET VERSION_LIST 0 vMAJOR)
list(GET VERSION_LIST 1 MINOR)
list(GET VERSION_LIST 2 PATCH)
string(REPLACE "v" "" MAJOR ${vMAJOR})
string(LENGTH ${BOOTLOADER_VERSION_FULL} BOOTLOADER_VERSION_FULL_LEN)
string(REGEX REPLACE "(.)" "'\\1', 0, " BOOTLOADER_VERSION_FULL_W16 ${BOOTLOADER_VERSION_FULL})
string(LENGTH ${FIRMWARE_VERSION_FULL} FIRMWARE_VERSION_FULL_LEN)
string(REGEX REPLACE "(.)" "'\\1', 0, " FIRMWARE_VERSION_FULL_W16 ${FIRMWARE_VERSION_FULL})
set(FIRMWARE_VERSION_MAJOR ${MAJOR})
set(FIRMWARE_VERSION_MINOR ${MINOR})
set(FIRMWARE_VERSION_PATCH ${PATCH})
configure_file(src/version.h.in src/version.h)
configure_file(src/bootloader/bootloader_version.h.in src/bootloader/bootloader_version.h)
#-----------------------------------------------------------------------------
# Set the default compiler options (Only set global options that truly are necessary for all files)
string(APPEND CMAKE_C_FLAGS " -std=c99 -pipe")
if(CMAKE_CROSSCOMPILING)
set(CMAKE_C_FLAGS "\
${CMAKE_C_FLAGS} -mcpu=cortex-m4 -mthumb -mlong-calls \
-mfloat-abi=softfp -mfpu=fpv4-sp-d16 -fomit-frame-pointer -D__SAMD51J20A__ \
"
)
# Allow gc-sections at linking stage. (TODO: move to targets that use that linking option)
string(APPEND CMAKE_C_FLAGS " -ffunction-sections -fdata-sections")
set(CMAKE_C_LINK_FLAGS "\
${CMAKE_C_LINK_FLAGS} -mcpu=cortex-m4 -mthumb \
-Wl,--start-group -u _printf_float -lm -Wl,--end-group -Wl,--gc-sections \
"
)
endif()
# Optimize for size by default
set(CMAKE_C_FLAGS_RELEASE "-Os -DNDEBUG")
# Allow gdb extensions if available
set(CMAKE_C_FLAGS_DEBUG "-Og -ggdb")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-Os -ggdb -DNDEBUG")
#-----------------------------------------------------------------------------
# Print system information and build options
execute_process(
COMMAND ${CMAKE_C_COMPILER} -dumpversion
OUTPUT_VARIABLE C_COMPILER_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
)
message(STATUS "\n\n=============================================")
message(STATUS " - General -")
message(STATUS "Firmware version: ${FIRMWARE_VERSION_FULL}")
message(STATUS "Bootloader version: ${BOOTLOADER_VERSION_FULL}")
message(STATUS "Firmware v. (git): ${GIT_FIRMWARE_VERSION_STRING}")
message(STATUS "Bootloader v. (git): ${GIT_BOOTLOADER_VERSION_STRING}")
message(STATUS "CMake version: ${CMAKE_VERSION}")
message(STATUS "System: ${CMAKE_SYSTEM}")
message(STATUS "Processor: ${CMAKE_SYSTEM_PROCESSOR}")
message(STATUS " - Build -")
message(STATUS "Compiler version: ${CMAKE_C_COMPILER_ID} ${C_COMPILER_VERSION}")
message(STATUS "Compiler: ${CMAKE_C_COMPILER}")
message(STATUS "Linker: ${CMAKE_LINKER}")
message(STATUS "Archiver: ${CMAKE_AR}")
message(STATUS "Default CFLAGS: ${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE}}")
message(STATUS "Default EXE LDFLAGS: ${CMAKE_EXE_LINKER_FLAGS}")
message(STATUS " - Options -")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "Hardware version: ${HARDWARE}")
message(STATUS "Verbose: ${CMAKE_VERBOSE_MAKEFILE}")
message(STATUS "Documentation: ${BUILD_DOCUMENTATION} (make doc)")
message(STATUS "Coverage flags: ${COVERAGE}")
message(STATUS "\n=============================================\n\n")
#-----------------------------------------------------------------------------
# Collect all binaries into bin/lib subdirectory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
#-----------------------------------------------------------------------------
# Compiler
# more specific CFLAGS might be added in the subdirectories src/ and test/.
# Compile all code with additional warnings by default and treat warnings as errors
# Excerpt from GCC manual to avoid manually enabling warnings
# -Wall:
# -Waddress -Warray-bounds=1 (only with -O2) -Wbool-compare -Wbool-operation -Wc++11-compat -Wc++14-compat
# -Wcatch-value (C++ and Objective-C++ only) -Wchar-subscripts -Wcomment -Wduplicate-decl-specifier (C and
# Objective-C only) -Wenum-compare (in C/ObjC; this is on by default in C++) -Wformat -Wint-in-bool-context
# -Wimplicit (C and Objective-C only) -Wimplicit-int (C and Objective-C only) -Wimplicit-function-declaration (C
# and Objective-C only) -Winit-self (only for C++) -Wlogical-not-parentheses -Wmain (only for C/ObjC and unless
# -ffreestanding) -Wmaybe-uninitialized -Wmemset-elt-size -Wmemset-transposed-args -Wmisleading-indentation (only
# for C/C++) -Wmissing-attributes -Wmissing-braces (only for C/ObjC) -Wmultistatement-macros -Wnarrowing (only for
# C++) -Wnonnull -Wnonnull-compare -Wopenmp-simd -Wparentheses -Wpointer-sign -Wreorder -Wrestrict -Wreturn-type
# -Wsequence-point -Wsign-compare (only in C++) -Wsizeof-pointer-div -Wsizeof-pointer-memaccess -Wstrict-aliasing
# -Wstrict-overflow=1 -Wstringop-truncation -Wswitch -Wtautological-compare -Wtrigraphs -Wuninitialized
# -Wunknown-pragmas -Wunused-function -Wunused-label -Wunused-value -Wunused-variable -Wvolatile-register-var
#
# -Wextra:
# -Wclobbered -Wcast-function-type -Wempty-body -Wignored-qualifiers -Wimplicit-fallthrough=3
# -Wmissing-field-initializers -Wmissing-parameter-type (C only) -Wold-style-declaration (C only) -Woverride-init
# -Wsign-compare (C only) -Wtype-limits -Wuninitialized -Wshift-negative-value (in C++03 and in C99 and newer)
# -Wunused-parameter (only with -Wunused or -Wall) -Wunused-but-set-parameter (only with -Wunused or -Wall)
#
# -Wformat=2:
# Enable -Wformat plus additional format checks. Currently equivalent to -Wformat -Wformat-nonliteral
# -Wformat-security -Wformat-y2k.
string(APPEND CMAKE_C_FLAGS " -Wall -Wextra -Wpedantic")
string(APPEND CMAKE_C_FLAGS " -Wmissing-prototypes -Werror-implicit-function-declaration ")
string(APPEND CMAKE_C_FLAGS " -Wpointer-arith -Wunused -Wfloat-equal -Wshadow -Wbad-function-cast")
string(APPEND CMAKE_C_FLAGS " -Wformat=2 -Wformat-signedness -Wwrite-strings ")
string(APPEND CMAKE_C_FLAGS " -Wmissing-declarations -Wmissing-format-attribute -Wpacked ")
string(APPEND CMAKE_C_FLAGS " -Wredundant-decls -Wnested-externs -Wmultichar -Winit-self ")
string(APPEND CMAKE_C_FLAGS " -Wold-style-definition -Wswitch-default -Wattributes ")
string(APPEND CMAKE_C_FLAGS " -Wdeprecated-declarations -Wcast-qual -Wstrict-prototypes")
string(APPEND CMAKE_C_FLAGS " -Wundef -Wmissing-include-dirs")
# Disable builtin warning
string(APPEND CMAKE_C_FLAGS " -Wno-cast-function-type")
# Hardening
string(APPEND CMAKE_C_FLAGS " -fstack-protector-all")
# For `struct timespec` and `strdup`
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=600")
#-----------------------------------------------------------------------------
# Build Documentation
if(BUILD_DOCUMENTATION)
set(DOC_GRAPHS "YES" CACHE STRING "Create dependency graphs (needs graphviz)")
set(DOC_FULLGRAPHS "NO" CACHE STRING "Create call/callee graphs (large)")
find_program(DOT_PATH dot)
if(DOT_PATH STREQUAL "DOT_PATH-NOTFOUND")
message("Doxygen: graphviz not found - graphs disabled")
set(DOC_GRAPHS "NO")
endif()
find_package(Doxygen)
if(DOXYGEN_FOUND)
configure_file("doc/Doxyfile.in" "Doxyfile" @ONLY)
add_custom_target(doc
${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/Doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Generating API documentation with Doxygen.." VERBATIM)
endif()
endif()
#-----------------------------------------------------------------------------
# Build
add_subdirectory(external)
add_subdirectory(src)
add_subdirectory(scripts)
if(NOT CMAKE_CROSSCOMPILING)
include(CTest)
add_subdirectory(test/unit-test)
if(COVERAGE)
find_program(GCOVR gcovr)
if(NOT GCOVR STREQUAL "GCOVR-NOTFOUND")
# __dummy is used to always force regeneration of coverage file (There is no reasonable way to track generation of gcda files...)
add_custom_command(
OUTPUT gcovr/coverage.html __dummy
COMMAND ${CMAKE_COMMAND} --build . --target test
COMMAND ${CMAKE_COMMAND} -E make_directory gcovr
COMMAND ${GCOVR} --gcov-executable gcov-8 --html-details -o gcovr/coverage.html -r ${CMAKE_SOURCE_DIR} -f ${CMAKE_SOURCE_DIR}/src
)
add_custom_target(
coverage
COMMAND ${CMAKE_COMMAND} -E echo Coverage at file:///${CMAKE_BINARY_DIR}/gcovr/coverage.html
DEPENDS gcovr/coverage.html
)
endif()
find_program(LCOV lcov)
find_program(GENHTML genhtml)
if(NOT LCOV STREQUAL "LOCV-NOTFOUND" AND NOT GENHTML STREQUAL "GENHTML-NOTFOUND")
add_custom_command(
OUTPUT lcovr/raw_coverage.info lcovr/coverage.info lcovr/index.html __dummy
COMMAND ${CMAKE_COMMAND} --build . --target test
COMMAND ${CMAKE_COMMAND} -E make_directory lcovr
COMMAND ${LCOV} --capture --directory ${CMAKE_BINARY_DIR} --output-file lcovr/raw_coverage.info
COMMAND ${LCOV} --remove lcovr/raw_coverage.info --output-file lcovr/coverage.info '*/test/*' '*/external/*' '*/src/drivers/*' '/usr/include/*' '*/tools/*'
COMMAND ${GENHTML} lcovr/coverage.info --output-directory lcovr
)
add_custom_target(
coverage-lcovr
COMMAND ${CMAKE_COMMAND} -E echo Coverage at file:///${CMAKE_BINARY_DIR}/lcovr/index.html
DEPENDS lcovr/index.html
)
endif()
endif()
else()
# TODO: Fix device tests
#add_subdirectory(test/device-test)
endif()
#-----------------------------------------------------------------------------
# Clean
set(removefiles "bin/*.* lib/*.*")
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${removefiles}")