blob: 6a5a6aaafc9ec121ec97bb6f616385762812ec35 [file] [log] [blame]
Pete Bentley00a7c402021-07-23 17:57:12 +01001cmake_minimum_required(VERSION 3.5)
Robert Sloancd79cde2017-12-11 09:06:12 -08002
David Benjamin4969cc92016-04-22 15:02:23 -04003# Defer enabling C and CXX languages.
Robert Sloand9e572d2018-08-27 12:27:00 -07004project(BoringSSL NONE)
David Benjamin4969cc92016-04-22 15:02:23 -04005
6if(WIN32)
7 # On Windows, prefer cl over gcc if both are available. By default most of
8 # the CMake generators prefer gcc, even on Windows.
9 set(CMAKE_GENERATOR_CC cl)
10endif()
11
Robert Sloan8ff03552017-06-14 12:40:58 -070012include(sources.cmake)
13
David Benjamin4969cc92016-04-22 15:02:23 -040014enable_language(C)
15enable_language(CXX)
Adam Langleyd9e397b2015-01-22 14:27:53 -080016
Robert Sloan726e9d12018-09-11 11:45:04 -070017# This is a dummy target which all other targets depend on (manually - see other
18# CMakeLists.txt files). This gives us a hook to add any targets which need to
19# run before all other targets.
20add_custom_target(global_target)
21
Adam Langleye9ada862015-05-11 17:20:37 -070022if(ANDROID)
23 # Android-NDK CMake files reconfigure the path and so Go and Perl won't be
24 # found. However, ninja will still find them in $PATH if we just name them.
David Benjamind316cba2016-06-02 16:17:39 -040025 if(NOT PERL_EXECUTABLE)
26 set(PERL_EXECUTABLE "perl")
27 endif()
28 if(NOT GO_EXECUTABLE)
29 set(GO_EXECUTABLE "go")
30 endif()
Adam Langleye9ada862015-05-11 17:20:37 -070031else()
32 find_package(Perl REQUIRED)
33 find_program(GO_EXECUTABLE go)
34endif()
35
Robert Sloan4c22c5f2019-03-01 15:53:37 -080036if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT CMAKE_CROSSCOMPILING)
37 find_package(PkgConfig QUIET)
38 if (PkgConfig_FOUND)
39 pkg_check_modules(LIBUNWIND libunwind-generic)
40 if(LIBUNWIND_FOUND)
41 add_definitions(-DBORINGSSL_HAVE_LIBUNWIND)
42 else()
43 message("libunwind not found. Disabling unwind tests.")
44 endif()
45 else()
46 message("pkgconfig not found. Disabling unwind tests.")
47 endif()
48endif()
49
Robert Sloand9e572d2018-08-27 12:27:00 -070050if(NOT GO_EXECUTABLE)
Adam Langleye9ada862015-05-11 17:20:37 -070051 message(FATAL_ERROR "Could not find Go")
52endif()
53
Robert Sloand9e572d2018-08-27 12:27:00 -070054if(USE_CUSTOM_LIBCXX)
Adam Vartanianbfcf3a72018-08-10 14:55:24 +010055 set(BORINGSSL_ALLOW_CXX_RUNTIME 1)
56endif()
Robert Sloan726e9d12018-09-11 11:45:04 -070057
Robert Sloand9e572d2018-08-27 12:27:00 -070058if(BORINGSSL_ALLOW_CXX_RUNTIME)
Robert Sloanfe7cd212017-08-07 09:03:39 -070059 add_definitions(-DBORINGSSL_ALLOW_CXX_RUNTIME)
60endif()
61
Pete Bentley470a9302019-10-02 14:44:32 +010062string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
63if(NOT FIPS)
64 if(CMAKE_BUILD_TYPE_LOWER STREQUAL "relwithassert" OR
65 NOT CMAKE_BUILD_TYPE_LOWER MATCHES "rel")
66 add_definitions(-DBORINGSSL_DISPATCH_TEST)
67 # CMake automatically connects include_directories to the NASM
68 # command-line, but not add_definitions.
69 set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -DBORINGSSL_DISPATCH_TEST")
70 endif()
Robert Sloan4c22c5f2019-03-01 15:53:37 -080071endif()
72
73# Add a RelWithAsserts build configuration. It is the same as Release, except it
74# does not define NDEBUG, so asserts run.
75foreach(VAR CMAKE_C_FLAGS CMAKE_CXX_FLAGS CMAKE_ASM_FLAGS)
76 string(REGEX REPLACE "(^| )[/-]DNDEBUG( |$)" " " "${VAR}_RELWITHASSERTS"
77 "${${VAR}_RELEASE}")
78endforeach()
79
Robert Sloan726e9d12018-09-11 11:45:04 -070080if(BORINGSSL_PREFIX AND BORINGSSL_PREFIX_SYMBOLS)
81 add_definitions(-DBORINGSSL_PREFIX=${BORINGSSL_PREFIX})
Robert Sloan6e8c9592018-12-03 11:20:49 -080082 # CMake automatically connects include_directories to the NASM command-line,
83 # but not add_definitions.
84 set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -DBORINGSSL_PREFIX=${BORINGSSL_PREFIX}")
Robert Sloan726e9d12018-09-11 11:45:04 -070085
86 # Use "symbol_prefix_include" to store generated header files
87 include_directories(${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include)
88 add_custom_command(
89 OUTPUT symbol_prefix_include/boringssl_prefix_symbols.h
90 symbol_prefix_include/boringssl_prefix_symbols_asm.h
91 symbol_prefix_include/boringssl_prefix_symbols_nasm.inc
92 COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include
93 COMMAND ${GO_EXECUTABLE} run ${CMAKE_CURRENT_SOURCE_DIR}/util/make_prefix_headers.go -out ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include ${BORINGSSL_PREFIX_SYMBOLS}
94 DEPENDS util/make_prefix_headers.go
95 ${CMAKE_BINARY_DIR}/${BORINGSSL_PREFIX_SYMBOLS})
96
97 # add_dependencies needs a target, not a file, so we add an intermediate
98 # target.
99 add_custom_target(
100 boringssl_prefix_symbols
101 DEPENDS symbol_prefix_include/boringssl_prefix_symbols.h
102 symbol_prefix_include/boringssl_prefix_symbols_asm.h
103 symbol_prefix_include/boringssl_prefix_symbols_nasm.inc)
104 add_dependencies(global_target boringssl_prefix_symbols)
105elseif(BORINGSSL_PREFIX OR BORINGSSL_PREFIX_SYMBOLS)
106 message(FATAL_ERROR "Must specify both or neither of BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS")
107endif()
108
Robert Sloan73fa5d62017-10-09 13:53:06 -0700109if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
110 set(CLANG 1)
111endif()
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700112
Tobias Thierer580fcaf2019-09-12 20:23:01 +0100113if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
114 set(EMSCRIPTEN 1)
115endif()
116
Robert Sloan73fa5d62017-10-09 13:53:06 -0700117if(CMAKE_COMPILER_IS_GNUCXX OR CLANG)
118 # Note clang-cl is odd and sets both CLANG and MSVC. We base our configuration
Robert Sloan978112c2018-01-22 12:53:01 -0800119 # primarily on our normal Clang one.
Pete Bentley2f26c212021-10-01 11:32:03 +0000120 set(C_CXX_FLAGS "-Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings -Wvla -Wshadow")
Robert Sloan73fa5d62017-10-09 13:53:06 -0700121 if(MSVC)
Robert Sloan978112c2018-01-22 12:53:01 -0800122 # clang-cl sets different default warnings than clang. It also treats -Wall
123 # as -Weverything, to match MSVC. Instead -W3 is the alias for -Wall.
124 # See http://llvm.org/viewvc/llvm-project?view=revision&revision=319116
125 set(C_CXX_FLAGS "${C_CXX_FLAGS} -W3 -Wno-unused-parameter -fmsc-version=1900")
Robert Sloan73fa5d62017-10-09 13:53:06 -0700126 # googletest suppresses warning C4996 via a pragma, but clang-cl does not
127 # honor it. Suppress it here to compensate. See https://crbug.com/772117.
128 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-deprecated-declarations")
129 else()
Tobias Thierer580fcaf2019-09-12 20:23:01 +0100130 if(EMSCRIPTEN)
131 # emscripten's emcc/clang does not accept the "-ggdb" flag.
132 set(C_CXX_FLAGS "${C_CXX_FLAGS} -g")
133 else()
134 set(C_CXX_FLAGS "${C_CXX_FLAGS} -ggdb")
135 endif()
136
137 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wall -fvisibility=hidden -fno-common")
Robert Sloan73fa5d62017-10-09 13:53:06 -0700138 endif()
139
140 if(CLANG)
Robert Sloanfe7cd212017-08-07 09:03:39 -0700141 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof -fcolor-diagnostics")
Robert Sloan8ff03552017-06-14 12:40:58 -0700142 else()
143 # GCC (at least 4.8.4) has a bug where it'll find unreachable free() calls
144 # and declare that the code is trying to free a stack pointer.
145 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-free-nonheap-object")
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400146 endif()
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700147
Robert Sloan73fa5d62017-10-09 13:53:06 -0700148 if(CLANG OR NOT "7.0.0" VERSION_GREATER CMAKE_C_COMPILER_VERSION)
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700149 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wimplicit-fallthrough")
150 endif()
151
David Benjaminc895d6b2016-08-11 13:26:41 -0400152 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
Robert Sloan73fa5d62017-10-09 13:53:06 -0700153 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_CXX_FLAGS} -Wmissing-declarations")
Robert Sloanfe7cd212017-08-07 09:03:39 -0700154
Robert Sloan73fa5d62017-10-09 13:53:06 -0700155 if(NOT MSVC)
156 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700157 if(APPLE)
158 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
159 endif()
Robert Sloan73fa5d62017-10-09 13:53:06 -0700160 if(NOT BORINGSSL_ALLOW_CXX_RUNTIME)
161 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
162 endif()
Robert Sloanfe7cd212017-08-07 09:03:39 -0700163 endif()
164
Robert Sloana12bf462017-07-17 07:08:26 -0700165 # In GCC, -Wmissing-declarations is the C++ spelling of -Wmissing-prototypes
166 # and using the wrong one is an error. In Clang, -Wmissing-prototypes is the
167 # spelling for both and -Wmissing-declarations is some other warning.
168 #
169 # https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Warning-Options.html#Warning-Options
170 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-prototypes
171 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-declarations
Robert Sloan73fa5d62017-10-09 13:53:06 -0700172 if(CLANG)
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700173 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-prototypes")
Robert Sloana12bf462017-07-17 07:08:26 -0700174 endif()
Adam Langleyd9e397b2015-01-22 14:27:53 -0800175elseif(MSVC)
176 set(MSVC_DISABLED_WARNINGS_LIST
David Benjaminf31229b2017-01-25 14:08:15 -0500177 "C4061" # enumerator 'identifier' in switch of enum 'enumeration' is not
178 # explicitly handled by a case label
179 # Disable this because it flags even when there is a default.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800180 "C4100" # 'exarg' : unreferenced formal parameter
181 "C4127" # conditional expression is constant
182 "C4200" # nonstandard extension used : zero-sized array in
183 # struct/union.
Steven Valdez909b19f2016-11-21 15:35:44 -0500184 "C4204" # nonstandard extension used: non-constant aggregate initializer
185 "C4221" # nonstandard extension used : 'identifier' : cannot be
186 # initialized using address of automatic variable
Adam Langleyd9e397b2015-01-22 14:27:53 -0800187 "C4242" # 'function' : conversion from 'int' to 'uint8_t',
188 # possible loss of data
189 "C4244" # 'function' : conversion from 'int' to 'uint8_t',
190 # possible loss of data
Adam Langleye9ada862015-05-11 17:20:37 -0700191 "C4267" # conversion from 'size_t' to 'int', possible loss of data
192 "C4371" # layout of class may have changed from a previous version of the
193 # compiler due to better packing of member '...'
194 "C4388" # signed/unsigned mismatch
Adam Langleyd9e397b2015-01-22 14:27:53 -0800195 "C4296" # '>=' : expression is always true
196 "C4350" # behavior change: 'std::_Wrap_alloc...'
197 "C4365" # '=' : conversion from 'size_t' to 'int',
198 # signed/unsigned mismatch
199 "C4389" # '!=' : signed/unsigned mismatch
David Benjamin9aaebef2016-04-22 15:02:23 -0400200 "C4464" # relative include path contains '..'
Adam Langleyd9e397b2015-01-22 14:27:53 -0800201 "C4510" # 'argument' : default constructor could not be generated
202 "C4512" # 'argument' : assignment operator could not be generated
203 "C4514" # 'function': unreferenced inline function has been removed
204 "C4548" # expression before comma has no effect; expected expression with
205 # side-effect" caused by FD_* macros.
206 "C4610" # struct 'argument' can never be instantiated - user defined
207 # constructor required.
David Benjamin9aaebef2016-04-22 15:02:23 -0400208 "C4623" # default constructor was implicitly defined as deleted
Adam Langleye9ada862015-05-11 17:20:37 -0700209 "C4625" # copy constructor could not be generated because a base class
210 # copy constructor is inaccessible or deleted
211 "C4626" # assignment operator could not be generated because a base class
212 # assignment operator is inaccessible or deleted
Robert Sloanf63bd1f2019-04-16 09:26:20 -0700213 "C4628" # digraphs not supported with -Ze
David Benjaminf31229b2017-01-25 14:08:15 -0500214 "C4668" # 'symbol' is not defined as a preprocessor macro, replacing with
215 # '0' for 'directives'
216 # Disable this because GTest uses it everywhere.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800217 "C4706" # assignment within conditional expression
218 "C4710" # 'function': function not inlined
219 "C4711" # function 'function' selected for inline expansion
220 "C4800" # 'int' : forcing value to bool 'true' or 'false'
221 # (performance warning)
222 "C4820" # 'bytes' bytes padding added after construct 'member_name'
David Benjaminf31229b2017-01-25 14:08:15 -0500223 "C5026" # move constructor was implicitly defined as deleted
David Benjamin9aaebef2016-04-22 15:02:23 -0400224 "C5027" # move assignment operator was implicitly defined as deleted
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100225 "C5045" # Compiler will insert Spectre mitigation for memory load if
226 # /Qspectre switch specified
David Benjamin9aaebef2016-04-22 15:02:23 -0400227 )
228 set(MSVC_LEVEL4_WARNINGS_LIST
229 # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
230 "C4265" # class has virtual functions, but destructor is not virtual
231 )
Adam Langleyd9e397b2015-01-22 14:27:53 -0800232 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
233 ${MSVC_DISABLED_WARNINGS_LIST})
David Benjamin9aaebef2016-04-22 15:02:23 -0400234 string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
235 ${MSVC_LEVEL4_WARNINGS_LIST})
Robert Sloanab8b8882018-03-26 11:39:51 -0700236 set(CMAKE_C_FLAGS "-utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
237 set(CMAKE_CXX_FLAGS "-utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
Robert Sloan8f860b12017-08-28 07:37:06 -0700238endif()
239
240if(WIN32)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800241 add_definitions(-D_HAS_EXCEPTIONS=0)
242 add_definitions(-DWIN32_LEAN_AND_MEAN)
Adam Langleye9ada862015-05-11 17:20:37 -0700243 add_definitions(-DNOMINMAX)
Robert Sloane56da3e2017-06-26 08:26:42 -0700244 # Allow use of fopen.
245 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
Tobias Thierer580fcaf2019-09-12 20:23:01 +0100246 # VS 2017 and higher supports STL-only warning suppressions.
247 # A bug in CMake < 3.13.0 may cause the space in this value to
248 # cause issues when building with NASM. In that case, update CMake.
249 add_definitions("-D_STL_EXTRA_DISABLED_WARNINGS=4774 4987")
Adam Langleyd9e397b2015-01-22 14:27:53 -0800250endif()
251
David Benjamin6e899c72016-06-09 18:02:18 -0400252if(CMAKE_COMPILER_IS_GNUCXX)
Pete Bentley00a7c402021-07-23 17:57:12 +0100253 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
David Benjamin6e899c72016-06-09 18:02:18 -0400254endif()
255
256# pthread_rwlock_t requires a feature flag.
257if(NOT WIN32)
258 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
Adam Langleyf4e42722015-06-04 17:45:09 -0700259endif()
260
Adam Langleyfad63272015-11-12 12:15:39 -0800261if(FUZZ)
Robert Sloan73fa5d62017-10-09 13:53:06 -0700262 if(NOT CLANG)
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400263 message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
Adam Langleyfad63272015-11-12 12:15:39 -0800264 endif()
265
Robert Sloand9e572d2018-08-27 12:27:00 -0700266 if(CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0.0")
267 message(FATAL_ERROR "You need Clang ≥ 6.0.0")
268 endif()
269
Steven Valdez909b19f2016-11-21 15:35:44 -0500270 add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
271 set(RUNNER_ARGS "-deterministic")
272
273 if(NOT NO_FUZZER_MODE)
274 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
275 set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
276 endif()
David Benjamin4969cc92016-04-22 15:02:23 -0400277
Robert Sloand9e572d2018-08-27 12:27:00 -0700278 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
279 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
Adam Langleyfad63272015-11-12 12:15:39 -0800280endif()
281
Adam Langleyd9e397b2015-01-22 14:27:53 -0800282add_definitions(-DBORINGSSL_IMPLEMENTATION)
283
Robert Sloand9e572d2018-08-27 12:27:00 -0700284if(BUILD_SHARED_LIBS)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800285 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
286 # Enable position-independent code globally. This is needed because
287 # some library targets are OBJECT libraries.
288 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
289endif()
290
Robert Sloand9e572d2018-08-27 12:27:00 -0700291if(MSAN)
Robert Sloan73fa5d62017-10-09 13:53:06 -0700292 if(NOT CLANG)
Robert Sloan69939df2017-01-09 10:53:07 -0800293 message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
294 endif()
295
Robert Sloand9e572d2018-08-27 12:27:00 -0700296 if(ASAN)
Robert Sloan69939df2017-01-09 10:53:07 -0800297 message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
298 endif()
299
300 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
301 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
Robert Sloan726e9d12018-09-11 11:45:04 -0700302 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
Robert Sloan69939df2017-01-09 10:53:07 -0800303endif()
304
Robert Sloand9e572d2018-08-27 12:27:00 -0700305if(ASAN)
Robert Sloan73fa5d62017-10-09 13:53:06 -0700306 if(NOT CLANG)
Robert Sloan69939df2017-01-09 10:53:07 -0800307 message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
308 endif()
309
Robert Sloan7d422bc2017-03-06 10:04:29 -0800310 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
311 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
Robert Sloan69939df2017-01-09 10:53:07 -0800312endif()
313
Robert Sloan99319a12017-11-27 10:32:46 -0800314if(CFI)
315 if(NOT CLANG)
316 message(FATAL_ERROR "Cannot enable CFI unless using Clang")
317 endif()
318
Robert Sloan5bdaadb2018-10-30 16:00:26 -0700319 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
320 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
Robert Sloan99319a12017-11-27 10:32:46 -0800321 # We use Chromium's copy of clang, which requires -fuse-ld=lld if building
322 # with -flto. That, in turn, can't handle -ggdb.
323 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
324 string(REPLACE "-ggdb" "-g" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
325 string(REPLACE "-ggdb" "-g" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
326 # -flto causes object files to contain LLVM bitcode. Mixing those with
327 # assembly output in the same static library breaks the linker.
328 set(OPENSSL_NO_ASM "1")
329endif()
330
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100331if(TSAN)
332 if(NOT CLANG)
333 message(FATAL_ERROR "Cannot enable TSAN unless using Clang")
334 endif()
335
336 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
337 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
338 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
339endif()
340
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800341if(UBSAN)
342 if(NOT CLANG)
343 message(FATAL_ERROR "Cannot enable UBSAN unless using Clang")
344 endif()
345
346 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
347 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
348 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
349
350 if(NOT UBSAN_RECOVER)
351 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize-recover=undefined")
352 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize-recover=undefined")
353 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-sanitize-recover=undefined")
354 endif()
355endif()
356
Robert Sloand9e572d2018-08-27 12:27:00 -0700357if(GCOV)
Robert Sloan69939df2017-01-09 10:53:07 -0800358 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
359 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
360endif()
361
Robert Sloan572a4e22017-04-17 10:52:19 -0700362if(FIPS)
363 add_definitions(-DBORINGSSL_FIPS)
Robert Sloan8ff03552017-06-14 12:40:58 -0700364 if(FIPS_BREAK_TEST)
365 add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
366 endif()
Pete Bentley0c61efe2019-08-13 09:32:23 +0100367 # The FIPS integrity check does not work for ASan and MSan builds.
Robert Sloan8ff03552017-06-14 12:40:58 -0700368 if(NOT ASAN AND NOT MSAN)
Pete Bentley0c61efe2019-08-13 09:32:23 +0100369 if(BUILD_SHARED_LIBS)
370 set(FIPS_SHARED "1")
371 else()
372 set(FIPS_DELOCATE "1")
373 endif()
Robert Sloan8ff03552017-06-14 12:40:58 -0700374 endif()
Tobias Thierer65d184c2019-10-19 00:33:07 +0100375 if(FIPS_SHARED)
376 # The Android CMake files set -ffunction-sections and -fdata-sections,
377 # which is incompatible with FIPS_SHARED.
378 set(CMAKE_C_FLAGS
379 "${CMAKE_C_FLAGS} -fno-function-sections -fno-data-sections")
380 set(CMAKE_CXX_FLAGS
381 "${CMAKE_CXX_FLAGS} -fno-function-sections -fno-data-sections")
382 endif()
Robert Sloan572a4e22017-04-17 10:52:19 -0700383endif()
384
Robert Sloanab8b8882018-03-26 11:39:51 -0700385if(OPENSSL_SMALL)
386 add_definitions(-DOPENSSL_SMALL)
387endif()
388
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800389if(CONSTANT_TIME_VALIDATION)
390 add_definitions(-DBORINGSSL_CONSTANT_TIME_VALIDATION)
391 # Asserts will often test secret data.
392 add_definitions(-DNDEBUG)
393endif()
394
Robert Sloanf573be72018-09-17 15:29:11 -0700395function(go_executable dest package)
396 set(godeps "${CMAKE_SOURCE_DIR}/util/godeps.go")
Pete Bentley2f26c212021-10-01 11:32:03 +0000397 if(CMAKE_VERSION VERSION_LESS "3.7" OR NOT CMAKE_GENERATOR STREQUAL "Ninja")
Robert Sloanf573be72018-09-17 15:29:11 -0700398 # The DEPFILE parameter to add_custom_command is new as of CMake 3.7 and
399 # only works with Ninja. Query the sources at configure time. Additionally,
400 # everything depends on go.mod. That affects what external packages to use.
401 execute_process(COMMAND ${GO_EXECUTABLE} run ${godeps} -format cmake
402 -pkg ${package}
403 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
404 OUTPUT_VARIABLE sources
405 RESULT_VARIABLE godeps_result)
406 add_custom_command(OUTPUT ${dest}
407 COMMAND ${GO_EXECUTABLE} build
408 -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
409 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
410 DEPENDS ${sources} ${CMAKE_SOURCE_DIR}/go.mod)
411 else()
412 # Ninja expects the target in the depfile to match the output. This is a
413 # relative path from the build directory.
414 string(LENGTH "${CMAKE_BINARY_DIR}" root_dir_length)
415 math(EXPR root_dir_length "${root_dir_length} + 1")
416 string(SUBSTRING "${CMAKE_CURRENT_BINARY_DIR}" ${root_dir_length} -1 target)
417 set(target "${target}/${dest}")
418
419 set(depfile "${CMAKE_CURRENT_BINARY_DIR}/${dest}.d")
420 add_custom_command(OUTPUT ${dest}
421 COMMAND ${GO_EXECUTABLE} build
422 -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
423 COMMAND ${GO_EXECUTABLE} run ${godeps} -format depfile
424 -target ${target} -pkg ${package} -out ${depfile}
425 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
426 DEPENDS ${godeps} ${CMAKE_SOURCE_DIR}/go.mod
427 DEPFILE ${depfile})
428 endif()
429endfunction()
430
Robert Sloan572a4e22017-04-17 10:52:19 -0700431# CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
432# architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
433# alone, and expects all architecture-specific logic to be conditioned within
434# the source files rather than the build. This does not work for our assembly
435# files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
436# builds.
Robert Sloand9e572d2018-08-27 12:27:00 -0700437if(NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
Robert Sloan572a4e22017-04-17 10:52:19 -0700438 list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
Pete Bentley2f26c212021-10-01 11:32:03 +0000439 if(NOT NUM_ARCHES EQUAL 1)
Robert Sloan572a4e22017-04-17 10:52:19 -0700440 message(FATAL_ERROR "Universal binaries not supported.")
441 endif()
442 list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
443endif()
444
Tobias Thierer43be7d22020-03-02 19:23:34 +0000445if(OPENSSL_NO_SSE2_FOR_TESTING)
446 add_definitions(-DOPENSSL_NO_SSE2_FOR_TESTING)
447endif()
448
Robert Sloand9e572d2018-08-27 12:27:00 -0700449if(OPENSSL_NO_ASM)
Adam Langleye9ada862015-05-11 17:20:37 -0700450 add_definitions(-DOPENSSL_NO_ASM)
451 set(ARCH "generic")
Pete Bentley2f26c212021-10-01 11:32:03 +0000452elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
Robert Sloan572a4e22017-04-17 10:52:19 -0700453 set(ARCH "x86_64")
Pete Bentley2f26c212021-10-01 11:32:03 +0000454elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64")
Robert Sloan572a4e22017-04-17 10:52:19 -0700455 set(ARCH "x86_64")
Pete Bentley2f26c212021-10-01 11:32:03 +0000456elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64")
Robert Sloan572a4e22017-04-17 10:52:19 -0700457 # cmake reports AMD64 on Windows, but we might be building for 32-bit.
Pete Bentleyf23caaf2020-09-22 18:02:11 +0100458 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
Robert Sloan572a4e22017-04-17 10:52:19 -0700459 set(ARCH "x86_64")
460 else()
461 set(ARCH "x86")
462 endif()
Pete Bentley2f26c212021-10-01 11:32:03 +0000463elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
Robert Sloan572a4e22017-04-17 10:52:19 -0700464 set(ARCH "x86")
Pete Bentley2f26c212021-10-01 11:32:03 +0000465elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "i386")
Robert Sloan572a4e22017-04-17 10:52:19 -0700466 set(ARCH "x86")
Pete Bentley2f26c212021-10-01 11:32:03 +0000467elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "i686")
Robert Sloan572a4e22017-04-17 10:52:19 -0700468 set(ARCH "x86")
Pete Bentley2f26c212021-10-01 11:32:03 +0000469elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
Robert Sloan572a4e22017-04-17 10:52:19 -0700470 set(ARCH "aarch64")
Pete Bentley2f26c212021-10-01 11:32:03 +0000471elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64")
Pete Bentley17486112021-01-20 11:51:47 +0000472 set(ARCH "aarch64")
Pete Bentley2f26c212021-10-01 11:32:03 +0000473elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
Robert Sloan572a4e22017-04-17 10:52:19 -0700474 set(ARCH "aarch64")
Robert Sloanc9abfe42018-11-26 12:19:07 -0800475# Apple A12 Bionic chipset which is added in iPhone XS/XS Max/XR uses arm64e architecture.
Pete Bentley2f26c212021-10-01 11:32:03 +0000476elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64e")
Robert Sloanc9abfe42018-11-26 12:19:07 -0800477 set(ARCH "aarch64")
Pete Bentley2f26c212021-10-01 11:32:03 +0000478elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm*")
Robert Sloan572a4e22017-04-17 10:52:19 -0700479 set(ARCH "arm")
Pete Bentley2f26c212021-10-01 11:32:03 +0000480elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "mips")
Robert Sloan572a4e22017-04-17 10:52:19 -0700481 # Just to avoid the “unknown processor” error.
482 set(ARCH "generic")
Pete Bentley2f26c212021-10-01 11:32:03 +0000483elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "ppc64le")
Robert Sloan572a4e22017-04-17 10:52:19 -0700484 set(ARCH "ppc64le")
485else()
486 message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
487endif()
488
Pete Bentley2f26c212021-10-01 11:32:03 +0000489if(ANDROID AND NOT ANDROID_NDK_REVISION AND ARCH STREQUAL "arm")
Robert Sloan55818102017-12-18 11:26:17 -0800490 # The third-party Android-NDK CMake files somehow fail to set the -march flag
491 # for assembly files. Without this flag, the compiler believes that it's
Robert Sloan572a4e22017-04-17 10:52:19 -0700492 # building for ARMv5.
Robert Sloan55818102017-12-18 11:26:17 -0800493 set(CMAKE_ASM_FLAGS "-march=${CMAKE_SYSTEM_PROCESSOR} ${CMAKE_ASM_FLAGS}")
Robert Sloan572a4e22017-04-17 10:52:19 -0700494endif()
495
Robert Sloand9e572d2018-08-27 12:27:00 -0700496if(USE_CUSTOM_LIBCXX)
497 if(NOT CLANG)
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100498 message(FATAL_ERROR "USE_CUSTOM_LIBCXX only supported with Clang")
499 endif()
500
501 # CMAKE_CXX_FLAGS ends up in the linker flags as well, so use
502 # add_compile_options. There does not appear to be a way to set
503 # language-specific compile-only flags.
504 add_compile_options("-nostdinc++")
505 set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -nostdlib++")
506 include_directories(
507 SYSTEM
508 util/bot/libcxx/include
509 util/bot/libcxxabi/include
510 )
511
512 # This is patterned after buildtools/third_party/libc++/BUILD.gn and
513 # buildtools/third_party/libc++abi/BUILD.gn in Chromium.
514
515 file(GLOB LIBCXX_SOURCES "util/bot/libcxx/src/*.cpp")
516 file(GLOB LIBCXXABI_SOURCES "util/bot/libcxxabi/src/*.cpp")
517
518 # This file is meant for exception-less builds.
519 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_noexception.cpp")
520 # libc++ also defines new and delete.
521 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/stdlib_new_delete.cpp")
Robert Sloand9e572d2018-08-27 12:27:00 -0700522 if(TSAN)
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100523 # ThreadSanitizer tries to intercept these symbols. Skip them to avoid
524 # symbol conflicts.
525 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_guard.cpp")
526 endif()
527
528 add_library(libcxxabi ${LIBCXXABI_SOURCES})
529 target_compile_definitions(
530 libcxxabi PRIVATE
531 -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS
532 )
533 set_target_properties(libcxxabi PROPERTIES COMPILE_FLAGS "-Wno-missing-prototypes -Wno-implicit-fallthrough")
534
535 add_library(libcxx ${LIBCXX_SOURCES})
Robert Sloand9e572d2018-08-27 12:27:00 -0700536 if(ASAN OR MSAN OR TSAN)
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100537 # Sanitizers try to intercept new and delete.
538 target_compile_definitions(
539 libcxx PRIVATE
540 -D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
541 )
542 endif()
543 target_compile_definitions(
544 libcxx PRIVATE
545 -D_LIBCPP_BUILDING_LIBRARY
546 -DLIBCXX_BUILDING_LIBCXXABI
547 )
548 target_link_libraries(libcxx libcxxabi)
549endif()
550
David Benjaminf31229b2017-01-25 14:08:15 -0500551# Add minimal googletest targets. The provided one has many side-effects, and
552# googletest has a very straightforward build.
Robert Sloana450c922018-01-08 10:35:32 -0800553add_library(boringssl_gtest third_party/googletest/src/gtest-all.cc)
554target_include_directories(boringssl_gtest PRIVATE third_party/googletest)
David Benjaminf31229b2017-01-25 14:08:15 -0500555
556include_directories(third_party/googletest/include)
557
Kenny Roote99801b2015-11-06 15:31:15 -0800558# Declare a dummy target to build all unit tests. Test targets should inject
559# themselves as dependencies next to the target definition.
560add_custom_target(all_tests)
561
Pete Bentleyf23caaf2020-09-22 18:02:11 +0100562# On Windows, CRYPTO_TEST_DATA is too long to fit in command-line limits.
563# TODO(davidben): CMake 3.12 has a list(JOIN) command. Use that when we've
564# updated the minimum version.
565set(EMBED_TEST_DATA_ARGS "")
566foreach(arg ${CRYPTO_TEST_DATA})
567 set(EMBED_TEST_DATA_ARGS "${EMBED_TEST_DATA_ARGS}${arg}\n")
568endforeach()
569file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/embed_test_data_args.txt"
570 "${EMBED_TEST_DATA_ARGS}")
571
Robert Sloan8ff03552017-06-14 12:40:58 -0700572add_custom_command(
573 OUTPUT crypto_test_data.cc
Pete Bentleyf23caaf2020-09-22 18:02:11 +0100574 COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go -file-list
575 "${CMAKE_CURRENT_BINARY_DIR}/embed_test_data_args.txt" >
576 "${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc"
Robert Sloan8ff03552017-06-14 12:40:58 -0700577 DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
578 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
579
580add_library(crypto_test_data OBJECT crypto_test_data.cc)
581
Adam Langleyd9e397b2015-01-22 14:27:53 -0800582add_subdirectory(crypto)
583add_subdirectory(ssl)
584add_subdirectory(ssl/test)
585add_subdirectory(tool)
Pete Bentley0c61efe2019-08-13 09:32:23 +0100586add_subdirectory(util/fipstools/cavp)
587add_subdirectory(util/fipstools/acvp/modulewrapper)
Adam Langleye9ada862015-05-11 17:20:37 -0700588add_subdirectory(decrepit)
Kenny Roote99801b2015-11-06 15:31:15 -0800589
Adam Langleyfad63272015-11-12 12:15:39 -0800590if(FUZZ)
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800591 if(LIBFUZZER_FROM_DEPS)
592 file(GLOB LIBFUZZER_SOURCES "util/bot/libFuzzer/*.cpp")
593 add_library(Fuzzer STATIC ${LIBFUZZER_SOURCES})
594 # libFuzzer does not pass our aggressive warnings. It also must be built
595 # without -fsanitize-coverage options or clang crashes.
596 set_target_properties(Fuzzer PROPERTIES COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -Wno-missing-prototypes -fsanitize-coverage=0")
597 endif()
598
Adam Langleyfad63272015-11-12 12:15:39 -0800599 add_subdirectory(fuzz)
600endif()
601
Robert Sloand9e572d2018-08-27 12:27:00 -0700602if(UNIX AND NOT APPLE AND NOT ANDROID)
603 set(HANDSHAKER_ARGS "-handshaker-path" $<TARGET_FILE:handshaker>)
604endif()
605
Pete Bentley17486112021-01-20 11:51:47 +0000606if(FIPS)
607 add_custom_target(
608 acvp_tests
609 COMMAND ${GO_EXECUTABLE} build -o ${CMAKE_BINARY_DIR}/acvptool
610 boringssl.googlesource.com/boringssl/util/fipstools/acvp/acvptool
611 COMMAND ${GO_EXECUTABLE} build -o ${CMAKE_BINARY_DIR}/testmodulewrapper
612 boringssl.googlesource.com/boringssl/util/fipstools/acvp/acvptool/testmodulewrapper
613 COMMAND cd util/fipstools/acvp/acvptool/test &&
614 ${GO_EXECUTABLE} run check_expected.go
615 -tool ${CMAKE_BINARY_DIR}/acvptool
616 -module-wrappers modulewrapper:$<TARGET_FILE:modulewrapper>,testmodulewrapper:${CMAKE_BINARY_DIR}/testmodulewrapper
617 -tests tests.json
618 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
619 DEPENDS modulewrapper
620 USES_TERMINAL)
621
622 add_custom_target(
623 fips_specific_tests_if_any
624 DEPENDS acvp_tests
625 )
626else()
627 add_custom_target(fips_specific_tests_if_any)
628endif()
629
Kenny Roote99801b2015-11-06 15:31:15 -0800630add_custom_target(
631 run_tests
632 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
633 ${CMAKE_BINARY_DIR}
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400634 COMMAND cd ssl/test/runner &&
635 ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
Robert Sloand9e572d2018-08-27 12:27:00 -0700636 ${HANDSHAKER_ARGS} ${RUNNER_ARGS}
Kenny Roote99801b2015-11-06 15:31:15 -0800637 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
Pete Bentley17486112021-01-20 11:51:47 +0000638 DEPENDS all_tests bssl_shim handshaker fips_specific_tests_if_any
Pete Bentleyf23caaf2020-09-22 18:02:11 +0100639 USES_TERMINAL)