blob: 0867601e58887fe6da1e85f7142c5b736ffb75d4 [file] [log] [blame]
David Benjamin81a998a2020-05-26 11:35:58 -04001cmake_minimum_required(VERSION 3.3)
David Benjamin6979c7e2017-12-04 17:19:49 -05002
David Benjamin6b34d542016-02-05 21:58:39 -05003# Defer enabling C and CXX languages.
David Benjamine6fd1252018-08-10 10:30:55 -05004project(BoringSSL NONE)
David Benjamin6b34d542016-02-05 21:58:39 -05005
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
David Benjamin3ecd0a52017-05-19 15:26:18 -040012include(sources.cmake)
13
David Benjamin6b34d542016-02-05 21:58:39 -050014enable_language(C)
15enable_language(CXX)
Adam Langley95c29f32014-06-20 12:00:00 -070016
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -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 Langley843ab662015-04-28 17:46:58 -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.
Tamas Berghammer5693e422016-05-19 14:28:14 +010025 if(NOT PERL_EXECUTABLE)
26 set(PERL_EXECUTABLE "perl")
27 endif()
28 if(NOT GO_EXECUTABLE)
29 set(GO_EXECUTABLE "go")
30 endif()
Adam Langley843ab662015-04-28 17:46:58 -070031else()
32 find_package(Perl REQUIRED)
33 find_program(GO_EXECUTABLE go)
34endif()
David Benjamin3ce3c362015-02-23 13:06:19 -050035
David Benjaminf36c3ad2019-01-15 12:45:57 -060036if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT CMAKE_CROSSCOMPILING)
Adam Langley9dfaf252019-01-04 07:53:25 -080037 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()
David Benjamin17d553d2018-12-21 17:58:36 -060045 else()
Adam Langley9dfaf252019-01-04 07:53:25 -080046 message("pkgconfig not found. Disabling unwind tests.")
David Benjamin17d553d2018-12-21 17:58:36 -060047 endif()
48endif()
49
David Benjamine6fd1252018-08-10 10:30:55 -050050if(NOT GO_EXECUTABLE)
David Benjamind27eda02015-03-05 01:19:27 -050051 message(FATAL_ERROR "Could not find Go")
52endif()
53
David Benjamine6fd1252018-08-10 10:30:55 -050054if(USE_CUSTOM_LIBCXX)
David Benjamine9ae99b2018-08-09 15:33:07 -050055 set(BORINGSSL_ALLOW_CXX_RUNTIME 1)
56endif()
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070057
David Benjamine6fd1252018-08-10 10:30:55 -050058if(BORINGSSL_ALLOW_CXX_RUNTIME)
David Benjamin2507d9e2017-07-26 11:39:38 -040059 add_definitions(-DBORINGSSL_ALLOW_CXX_RUNTIME)
60endif()
61
David Benjamin0de64a72019-09-26 15:59:40 -040062string(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()
Adam Langleyc1615712018-11-27 14:07:12 -080071endif()
72
David Benjaminc47f7932019-01-22 19:49:35 -060073# 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
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070080if(BORINGSSL_PREFIX AND BORINGSSL_PREFIX_SYMBOLS)
81 add_definitions(-DBORINGSSL_PREFIX=${BORINGSSL_PREFIX})
David Benjamin8c23d3a2018-11-25 15:58:02 -060082 # 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}")
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -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
David Benjamin02afbd32017-10-05 15:04:08 -0400109if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
110 set(CLANG 1)
111endif()
Vincent Batts60931e22017-09-20 11:51:54 -0400112
Alexei Lozovsky356a9a02019-04-10 14:50:55 +0300113if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
114 set(EMSCRIPTEN 1)
115endif()
116
David Benjamin02afbd32017-10-05 15:04:08 -0400117if(CMAKE_COMPILER_IS_GNUCXX OR CLANG)
118 # Note clang-cl is odd and sets both CLANG and MSVC. We base our configuration
David Benjamin8d67f6f2018-01-05 15:43:09 -0500119 # primarily on our normal Clang one.
Adam Langley6a2609d2019-10-03 14:08:09 -0700120 set(C_CXX_FLAGS "-Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings -Wvla")
David Benjamin02afbd32017-10-05 15:04:08 -0400121 if(MSVC)
David Benjamin8d67f6f2018-01-05 15:43:09 -0500122 # 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")
David Benjamin02afbd32017-10-05 15:04:08 -0400126 # 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()
Alexei Lozovsky356a9a02019-04-10 14:50:55 +0300130 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")
David Benjamin02afbd32017-10-05 15:04:08 -0400138 endif()
139
140 if(CLANG)
David Benjamin9fb6fea2017-07-31 14:03:38 -0400141 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof -fcolor-diagnostics")
Adam Langley5c38c052017-04-28 14:47:06 -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 Benjamin9b7d8362016-08-29 14:59:31 -0400146 endif()
Vincent Batts60931e22017-09-20 11:51:54 -0400147
David Benjamin02afbd32017-10-05 15:04:08 -0400148 if(CLANG OR NOT "7.0.0" VERSION_GREATER CMAKE_C_COMPILER_VERSION)
Vincent Batts60931e22017-09-20 11:51:54 -0400149 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wimplicit-fallthrough")
150 endif()
151
Adam Langley01867872016-06-30 14:16:59 -0700152 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
David Benjamin02afbd32017-10-05 15:04:08 -0400153 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_CXX_FLAGS} -Wmissing-declarations")
David Benjamin2507d9e2017-07-26 11:39:38 -0400154
David Benjamin02afbd32017-10-05 15:04:08 -0400155 if(NOT MSVC)
156 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Daniel Wagner-Hall3b358b22017-10-16 20:58:08 +0100157 if(APPLE)
158 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
159 endif()
David Benjamin02afbd32017-10-05 15:04:08 -0400160 if(NOT BORINGSSL_ALLOW_CXX_RUNTIME)
161 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
162 endif()
David Benjamin2507d9e2017-07-26 11:39:38 -0400163 endif()
164
David Benjamin4a8d1f32017-07-13 17:53:07 -0400165 # 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
David Benjamin02afbd32017-10-05 15:04:08 -0400172 if(CLANG)
Vincent Batts60931e22017-09-20 11:51:54 -0400173 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-prototypes")
David Benjamin4a8d1f32017-07-13 17:53:07 -0400174 endif()
Daniel Wagner-Hall10154322017-10-09 20:09:43 +0100175
176 if(CMAKE_COMPILER_IS_GNUCXX AND "4.8" VERSION_GREATER CMAKE_C_COMPILER_VERSION)
177 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-array-bounds")
178 endif()
179
Adam Langley95c29f32014-06-20 12:00:00 -0700180elseif(MSVC)
Brian Smithefed2212015-01-28 16:20:02 -0800181 set(MSVC_DISABLED_WARNINGS_LIST
David Benjamin96628432017-01-19 19:05:47 -0500182 "C4061" # enumerator 'identifier' in switch of enum 'enumeration' is not
183 # explicitly handled by a case label
184 # Disable this because it flags even when there is a default.
Brian Smithefed2212015-01-28 16:20:02 -0800185 "C4100" # 'exarg' : unreferenced formal parameter
186 "C4127" # conditional expression is constant
187 "C4200" # nonstandard extension used : zero-sized array in
188 # struct/union.
David Benjaminffb11072016-11-13 10:32:10 +0900189 "C4204" # nonstandard extension used: non-constant aggregate initializer
190 "C4221" # nonstandard extension used : 'identifier' : cannot be
191 # initialized using address of automatic variable
Brian Smithefed2212015-01-28 16:20:02 -0800192 "C4242" # 'function' : conversion from 'int' to 'uint8_t',
193 # possible loss of data
194 "C4244" # 'function' : conversion from 'int' to 'uint8_t',
195 # possible loss of data
David Benjamin3673be72015-02-11 15:12:05 -0500196 "C4267" # conversion from 'size_t' to 'int', possible loss of data
David Benjamin3673be72015-02-11 15:12:05 -0500197 "C4371" # layout of class may have changed from a previous version of the
198 # compiler due to better packing of member '...'
199 "C4388" # signed/unsigned mismatch
Brian Smithefed2212015-01-28 16:20:02 -0800200 "C4296" # '>=' : expression is always true
201 "C4350" # behavior change: 'std::_Wrap_alloc...'
202 "C4365" # '=' : conversion from 'size_t' to 'int',
203 # signed/unsigned mismatch
204 "C4389" # '!=' : signed/unsigned mismatch
David Benjamin7acd6bc2016-05-02 12:57:01 -0400205 "C4464" # relative include path contains '..'
Brian Smithefed2212015-01-28 16:20:02 -0800206 "C4510" # 'argument' : default constructor could not be generated
207 "C4512" # 'argument' : assignment operator could not be generated
208 "C4514" # 'function': unreferenced inline function has been removed
209 "C4548" # expression before comma has no effect; expected expression with
210 # side-effect" caused by FD_* macros.
211 "C4610" # struct 'argument' can never be instantiated - user defined
212 # constructor required.
David Benjamin7acd6bc2016-05-02 12:57:01 -0400213 "C4623" # default constructor was implicitly defined as deleted
David Benjamin3673be72015-02-11 15:12:05 -0500214 "C4625" # copy constructor could not be generated because a base class
215 # copy constructor is inaccessible or deleted
216 "C4626" # assignment operator could not be generated because a base class
217 # assignment operator is inaccessible or deleted
David Benjaminbe7006a2019-04-09 18:05:02 -0500218 "C4628" # digraphs not supported with -Ze
David Benjamin96628432017-01-19 19:05:47 -0500219 "C4668" # 'symbol' is not defined as a preprocessor macro, replacing with
220 # '0' for 'directives'
221 # Disable this because GTest uses it everywhere.
Brian Smithefed2212015-01-28 16:20:02 -0800222 "C4706" # assignment within conditional expression
223 "C4710" # 'function': function not inlined
224 "C4711" # function 'function' selected for inline expansion
225 "C4800" # 'int' : forcing value to bool 'true' or 'false'
226 # (performance warning)
227 "C4820" # 'bytes' bytes padding added after construct 'member_name'
David Benjamin96628432017-01-19 19:05:47 -0500228 "C5026" # move constructor was implicitly defined as deleted
David Benjamin7acd6bc2016-05-02 12:57:01 -0400229 "C5027" # move assignment operator was implicitly defined as deleted
Adam Vartanian189270c2018-05-22 13:50:44 +0100230 "C5045" # Compiler will insert Spectre mitigation for memory load if
231 # /Qspectre switch specified
David Benjamin7acd6bc2016-05-02 12:57:01 -0400232 )
233 set(MSVC_LEVEL4_WARNINGS_LIST
234 # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
235 "C4265" # class has virtual functions, but destructor is not virtual
236 )
Brian Smithefed2212015-01-28 16:20:02 -0800237 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
238 ${MSVC_DISABLED_WARNINGS_LIST})
David Benjamin7acd6bc2016-05-02 12:57:01 -0400239 string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
240 ${MSVC_LEVEL4_WARNINGS_LIST})
sphawk3ab1a692018-03-11 19:20:51 +0900241 set(CMAKE_C_FLAGS "-utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
242 set(CMAKE_CXX_FLAGS "-utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
David Benjamine2568c42017-08-16 15:25:27 -0400243endif()
244
245if(WIN32)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800246 add_definitions(-D_HAS_EXCEPTIONS=0)
Brian Smitha87de9b2015-01-28 20:34:47 -0800247 add_definitions(-DWIN32_LEAN_AND_MEAN)
David Benjamin0e434b92015-04-02 13:20:01 -0400248 add_definitions(-DNOMINMAX)
David Benjamin9b6ff442017-06-15 20:44:30 -0400249 # Allow use of fopen.
250 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
Adam Langleyf7b830d2019-09-06 08:49:28 -0700251 # VS 2017 and higher supports STL-only warning suppressions.
252 # A bug in CMake < 3.13.0 may cause the space in this value to
253 # cause issues when building with NASM. In that case, update CMake.
254 add_definitions("-D_STL_EXTRA_DISABLED_WARNINGS=4774 4987")
Adam Langley95c29f32014-06-20 12:00:00 -0700255endif()
256
David Benjaminb826c0d2015-02-28 00:00:44 -0500257if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
David Benjamin02afbd32017-10-05 15:04:08 -0400258 CLANG)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800259 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
260 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
Adam Langley07100c62015-01-16 15:20:54 -0800261endif()
262
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400263if(CMAKE_COMPILER_IS_GNUCXX)
David Benjamine6fd1252018-08-10 10:30:55 -0500264 if((CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.99") OR CLANG)
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400265 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
266 else()
267 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
268 endif()
269endif()
270
271# pthread_rwlock_t requires a feature flag.
272if(NOT WIN32)
273 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
Adam Langley6f2e7332015-05-15 12:01:29 -0700274endif()
275
Adam Langley9a4beb82015-11-09 13:57:26 -0800276if(FUZZ)
David Benjamin02afbd32017-10-05 15:04:08 -0400277 if(NOT CLANG)
Alessandro Ghedinib6f69272016-09-28 22:14:01 +0100278 message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
Adam Langley9a4beb82015-11-09 13:57:26 -0800279 endif()
280
Adam Langley9c969bf2018-08-24 10:46:01 -0700281 if(CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0.0")
282 message(FATAL_ERROR "You need Clang ≥ 6.0.0")
283 endif()
284
David Benjaminec978dd2016-11-04 18:59:33 -0400285 add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
286 set(RUNNER_ARGS "-deterministic")
287
288 if(NOT NO_FUZZER_MODE)
289 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
290 set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
291 endif()
David Benjaminbc5b2a22016-03-01 22:57:32 -0500292
Adam Langley9c969bf2018-08-24 10:46:01 -0700293 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
294 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
Adam Langley9a4beb82015-11-09 13:57:26 -0800295endif()
296
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700297add_definitions(-DBORINGSSL_IMPLEMENTATION)
298
David Benjamine6fd1252018-08-10 10:30:55 -0500299if(BUILD_SHARED_LIBS)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800300 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
301 # Enable position-independent code globally. This is needed because
302 # some library targets are OBJECT libraries.
303 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
David Benjamin507c1ee2015-01-28 00:50:21 -0500304endif()
305
David Benjamine6fd1252018-08-10 10:30:55 -0500306if(MSAN)
David Benjamin02afbd32017-10-05 15:04:08 -0400307 if(NOT CLANG)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800308 message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
309 endif()
310
David Benjamine6fd1252018-08-10 10:30:55 -0500311 if(ASAN)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800312 message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
313 endif()
314
315 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
316 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
Adam Langleye77c27d2018-09-07 11:20:23 -0700317 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
Adam Langley1bcd10c2016-12-16 10:48:23 -0800318endif()
319
David Benjamine6fd1252018-08-10 10:30:55 -0500320if(ASAN)
David Benjamin02afbd32017-10-05 15:04:08 -0400321 if(NOT CLANG)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800322 message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
323 endif()
324
David Benjamin0d3c9632017-02-28 14:58:00 -0500325 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
326 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
Adam Langley1bcd10c2016-12-16 10:48:23 -0800327endif()
328
David Benjaminc367ee52017-11-21 08:16:42 -0500329if(CFI)
330 if(NOT CLANG)
331 message(FATAL_ERROR "Cannot enable CFI unless using Clang")
332 endif()
333
David Benjamin66508982018-09-22 16:19:15 -0500334 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
335 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
David Benjaminc367ee52017-11-21 08:16:42 -0500336 # We use Chromium's copy of clang, which requires -fuse-ld=lld if building
337 # with -flto. That, in turn, can't handle -ggdb.
338 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
339 string(REPLACE "-ggdb" "-g" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
340 string(REPLACE "-ggdb" "-g" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
341 # -flto causes object files to contain LLVM bitcode. Mixing those with
342 # assembly output in the same static library breaks the linker.
343 set(OPENSSL_NO_ASM "1")
344endif()
345
David Benjamin5852cfc2018-07-19 17:50:45 -0400346if(TSAN)
347 if(NOT CLANG)
348 message(FATAL_ERROR "Cannot enable TSAN unless using Clang")
349 endif()
350
351 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
352 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
353 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
354endif()
355
David Benjaminfc27a192019-01-21 07:25:12 +0000356if(UBSAN)
357 if(NOT CLANG)
358 message(FATAL_ERROR "Cannot enable UBSAN unless using Clang")
359 endif()
360
361 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
362 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
363 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
364
365 if(NOT UBSAN_RECOVER)
366 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize-recover=undefined")
367 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize-recover=undefined")
368 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-sanitize-recover=undefined")
369 endif()
370endif()
371
David Benjamine6fd1252018-08-10 10:30:55 -0500372if(GCOV)
David Benjamind035ab32016-12-27 12:15:56 -0500373 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
374 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
375endif()
376
David Benjaminaff72a32017-04-06 23:26:04 -0400377if(FIPS)
378 add_definitions(-DBORINGSSL_FIPS)
Adam Langleyf64a6ee2017-05-17 13:05:50 -0700379 if(FIPS_BREAK_TEST)
380 add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
381 endif()
Adam Langleyd72e47f2019-05-09 16:39:49 -0700382 # The FIPS integrity check does not work for ASan and MSan builds.
Adam Langleyf64a6ee2017-05-17 13:05:50 -0700383 if(NOT ASAN AND NOT MSAN)
Adam Langleyd72e47f2019-05-09 16:39:49 -0700384 if(BUILD_SHARED_LIBS)
385 set(FIPS_SHARED "1")
386 else()
387 set(FIPS_DELOCATE "1")
388 endif()
Adam Langleyf64a6ee2017-05-17 13:05:50 -0700389 endif()
David Benjamine481d942019-10-18 16:43:12 -0400390 if(FIPS_SHARED)
391 # The Android CMake files set -ffunction-sections and -fdata-sections,
392 # which is incompatible with FIPS_SHARED.
393 set(CMAKE_C_FLAGS
394 "${CMAKE_C_FLAGS} -fno-function-sections -fno-data-sections")
395 set(CMAKE_CXX_FLAGS
396 "${CMAKE_CXX_FLAGS} -fno-function-sections -fno-data-sections")
397 endif()
David Benjaminaff72a32017-04-06 23:26:04 -0400398endif()
399
David Benjamin6291af42018-03-23 13:49:27 -0400400if(OPENSSL_SMALL)
401 add_definitions(-DOPENSSL_SMALL)
402endif()
403
Adam Langleya6a049a2018-12-06 17:15:58 -0800404if(CONSTANT_TIME_VALIDATION)
405 add_definitions(-DBORINGSSL_CONSTANT_TIME_VALIDATION)
406 # Asserts will often test secret data.
407 add_definitions(-DNDEBUG)
408endif()
409
David Benjamin5baee452018-09-13 16:37:28 -0500410function(go_executable dest package)
411 set(godeps "${CMAKE_SOURCE_DIR}/util/godeps.go")
412 if(${CMAKE_VERSION} VERSION_LESS "3.7" OR
413 NOT ${CMAKE_GENERATOR} STREQUAL "Ninja")
414 # The DEPFILE parameter to add_custom_command is new as of CMake 3.7 and
415 # only works with Ninja. Query the sources at configure time. Additionally,
416 # everything depends on go.mod. That affects what external packages to use.
417 execute_process(COMMAND ${GO_EXECUTABLE} run ${godeps} -format cmake
418 -pkg ${package}
419 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
420 OUTPUT_VARIABLE sources
421 RESULT_VARIABLE godeps_result)
422 add_custom_command(OUTPUT ${dest}
423 COMMAND ${GO_EXECUTABLE} build
424 -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
425 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
426 DEPENDS ${sources} ${CMAKE_SOURCE_DIR}/go.mod)
427 else()
428 # Ninja expects the target in the depfile to match the output. This is a
429 # relative path from the build directory.
430 string(LENGTH "${CMAKE_BINARY_DIR}" root_dir_length)
431 math(EXPR root_dir_length "${root_dir_length} + 1")
432 string(SUBSTRING "${CMAKE_CURRENT_BINARY_DIR}" ${root_dir_length} -1 target)
433 set(target "${target}/${dest}")
434
435 set(depfile "${CMAKE_CURRENT_BINARY_DIR}/${dest}.d")
436 add_custom_command(OUTPUT ${dest}
437 COMMAND ${GO_EXECUTABLE} build
438 -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
439 COMMAND ${GO_EXECUTABLE} run ${godeps} -format depfile
440 -target ${target} -pkg ${package} -out ${depfile}
441 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
442 DEPENDS ${godeps} ${CMAKE_SOURCE_DIR}/go.mod
443 DEPFILE ${depfile})
444 endif()
445endfunction()
446
David Benjaminaff72a32017-04-06 23:26:04 -0400447# CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
448# architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
449# alone, and expects all architecture-specific logic to be conditioned within
450# the source files rather than the build. This does not work for our assembly
451# files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
452# builds.
David Benjamine6fd1252018-08-10 10:30:55 -0500453if(NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
David Benjaminaff72a32017-04-06 23:26:04 -0400454 list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
David Benjamine6fd1252018-08-10 10:30:55 -0500455 if(NOT ${NUM_ARCHES} EQUAL 1)
David Benjaminaff72a32017-04-06 23:26:04 -0400456 message(FATAL_ERROR "Universal binaries not supported.")
457 endif()
458 list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
459endif()
460
David Benjamin6887d5e2019-12-17 13:56:30 -0500461if(OPENSSL_NO_SSE2_FOR_TESTING)
462 add_definitions(-DOPENSSL_NO_SSE2_FOR_TESTING)
463endif()
464
David Benjamine6fd1252018-08-10 10:30:55 -0500465if(OPENSSL_NO_ASM)
David Benjamin27b08e92015-05-04 15:16:57 -0400466 add_definitions(-DOPENSSL_NO_ASM)
467 set(ARCH "generic")
David Benjamine6fd1252018-08-10 10:30:55 -0500468elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
David Benjaminaff72a32017-04-06 23:26:04 -0400469 set(ARCH "x86_64")
David Benjamine6fd1252018-08-10 10:30:55 -0500470elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
David Benjaminaff72a32017-04-06 23:26:04 -0400471 set(ARCH "x86_64")
David Benjamine6fd1252018-08-10 10:30:55 -0500472elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
David Benjaminaff72a32017-04-06 23:26:04 -0400473 # cmake reports AMD64 on Windows, but we might be building for 32-bit.
David Benjamine6fd1252018-08-10 10:30:55 -0500474 if(CMAKE_CL_64)
David Benjaminaff72a32017-04-06 23:26:04 -0400475 set(ARCH "x86_64")
476 else()
477 set(ARCH "x86")
478 endif()
David Benjamine6fd1252018-08-10 10:30:55 -0500479elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
David Benjaminaff72a32017-04-06 23:26:04 -0400480 set(ARCH "x86")
David Benjamine6fd1252018-08-10 10:30:55 -0500481elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
David Benjaminaff72a32017-04-06 23:26:04 -0400482 set(ARCH "x86")
David Benjamine6fd1252018-08-10 10:30:55 -0500483elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
David Benjaminaff72a32017-04-06 23:26:04 -0400484 set(ARCH "x86")
David Benjamine6fd1252018-08-10 10:30:55 -0500485elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
David Benjaminaff72a32017-04-06 23:26:04 -0400486 set(ARCH "aarch64")
David Benjamine6fd1252018-08-10 10:30:55 -0500487elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
David Benjaminaff72a32017-04-06 23:26:04 -0400488 set(ARCH "aarch64")
Junghoon Jange8ba1e32018-11-15 10:20:42 +0900489# Apple A12 Bionic chipset which is added in iPhone XS/XS Max/XR uses arm64e architecture.
490elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64e")
491 set(ARCH "aarch64")
David Benjamine6fd1252018-08-10 10:30:55 -0500492elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm*")
David Benjaminaff72a32017-04-06 23:26:04 -0400493 set(ARCH "arm")
David Benjamine6fd1252018-08-10 10:30:55 -0500494elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "mips")
David Benjaminaff72a32017-04-06 23:26:04 -0400495 # Just to avoid the “unknown processor” error.
496 set(ARCH "generic")
David Benjamine6fd1252018-08-10 10:30:55 -0500497elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le")
David Benjaminaff72a32017-04-06 23:26:04 -0400498 set(ARCH "ppc64le")
499else()
500 message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
David Benjamin27b08e92015-05-04 15:16:57 -0400501endif()
502
David Benjamine6fd1252018-08-10 10:30:55 -0500503if(ANDROID AND NOT ANDROID_NDK_REVISION AND ${ARCH} STREQUAL "arm")
David Benjamin9894ee92017-12-13 18:08:47 -0500504 # The third-party Android-NDK CMake files somehow fail to set the -march flag
505 # for assembly files. Without this flag, the compiler believes that it's
David Benjaminaff72a32017-04-06 23:26:04 -0400506 # building for ARMv5.
David Benjamin9894ee92017-12-13 18:08:47 -0500507 set(CMAKE_ASM_FLAGS "-march=${CMAKE_SYSTEM_PROCESSOR} ${CMAKE_ASM_FLAGS}")
David Benjaminaff72a32017-04-06 23:26:04 -0400508endif()
509
David Benjamine6fd1252018-08-10 10:30:55 -0500510if(USE_CUSTOM_LIBCXX)
511 if(NOT CLANG)
David Benjamine9ae99b2018-08-09 15:33:07 -0500512 message(FATAL_ERROR "USE_CUSTOM_LIBCXX only supported with Clang")
513 endif()
514
515 # CMAKE_CXX_FLAGS ends up in the linker flags as well, so use
516 # add_compile_options. There does not appear to be a way to set
517 # language-specific compile-only flags.
518 add_compile_options("-nostdinc++")
519 set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -nostdlib++")
520 include_directories(
521 SYSTEM
522 util/bot/libcxx/include
523 util/bot/libcxxabi/include
524 )
525
526 # This is patterned after buildtools/third_party/libc++/BUILD.gn and
527 # buildtools/third_party/libc++abi/BUILD.gn in Chromium.
528
529 file(GLOB LIBCXX_SOURCES "util/bot/libcxx/src/*.cpp")
530 file(GLOB LIBCXXABI_SOURCES "util/bot/libcxxabi/src/*.cpp")
531
532 # This file is meant for exception-less builds.
533 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_noexception.cpp")
534 # libc++ also defines new and delete.
535 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/stdlib_new_delete.cpp")
David Benjamine6fd1252018-08-10 10:30:55 -0500536 if(TSAN)
David Benjamine9ae99b2018-08-09 15:33:07 -0500537 # ThreadSanitizer tries to intercept these symbols. Skip them to avoid
538 # symbol conflicts.
539 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_guard.cpp")
540 endif()
541
542 add_library(libcxxabi ${LIBCXXABI_SOURCES})
543 target_compile_definitions(
544 libcxxabi PRIVATE
545 -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS
546 )
547 set_target_properties(libcxxabi PROPERTIES COMPILE_FLAGS "-Wno-missing-prototypes -Wno-implicit-fallthrough")
548
549 add_library(libcxx ${LIBCXX_SOURCES})
David Benjamine6fd1252018-08-10 10:30:55 -0500550 if(ASAN OR MSAN OR TSAN)
David Benjamine9ae99b2018-08-09 15:33:07 -0500551 # Sanitizers try to intercept new and delete.
552 target_compile_definitions(
553 libcxx PRIVATE
554 -D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
555 )
556 endif()
557 target_compile_definitions(
558 libcxx PRIVATE
559 -D_LIBCPP_BUILDING_LIBRARY
560 -DLIBCXX_BUILDING_LIBCXXABI
561 )
562 target_link_libraries(libcxx libcxxabi)
563endif()
564
David Benjamin96628432017-01-19 19:05:47 -0500565# Add minimal googletest targets. The provided one has many side-effects, and
566# googletest has a very straightforward build.
Marek Gilbert11850d52018-01-03 23:07:58 -0800567add_library(boringssl_gtest third_party/googletest/src/gtest-all.cc)
568target_include_directories(boringssl_gtest PRIVATE third_party/googletest)
David Benjamin96628432017-01-19 19:05:47 -0500569
570include_directories(third_party/googletest/include)
571
David Benjamin301afaf2015-10-14 21:34:40 -0400572# Declare a dummy target to build all unit tests. Test targets should inject
573# themselves as dependencies next to the target definition.
574add_custom_target(all_tests)
575
David Benjamin3ecd0a52017-05-19 15:26:18 -0400576add_custom_command(
577 OUTPUT crypto_test_data.cc
578 COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go ${CRYPTO_TEST_DATA} >
579 ${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc
580 DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
581 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
582
583add_library(crypto_test_data OBJECT crypto_test_data.cc)
584
Adam Langley95c29f32014-06-20 12:00:00 -0700585add_subdirectory(crypto)
586add_subdirectory(ssl)
587add_subdirectory(ssl/test)
588add_subdirectory(tool)
Adam Langley60cc4d42019-07-01 14:27:30 -0700589add_subdirectory(util/fipstools/cavp)
Adam Langleyb7f0c1b2019-07-09 18:02:14 -0700590add_subdirectory(util/fipstools/acvp/modulewrapper)
Adam Langleyc004dfc2015-02-03 10:45:12 -0800591add_subdirectory(decrepit)
David Benjamin301afaf2015-10-14 21:34:40 -0400592
Adam Langley9a4beb82015-11-09 13:57:26 -0800593if(FUZZ)
David Benjamin6c597be2019-01-07 15:13:04 -0600594 if(LIBFUZZER_FROM_DEPS)
595 file(GLOB LIBFUZZER_SOURCES "util/bot/libFuzzer/*.cpp")
596 add_library(Fuzzer STATIC ${LIBFUZZER_SOURCES})
597 # libFuzzer does not pass our aggressive warnings. It also must be built
598 # without -fsanitize-coverage options or clang crashes.
599 set_target_properties(Fuzzer PROPERTIES COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -Wno-missing-prototypes -fsanitize-coverage=0")
600 endif()
601
Adam Langley9a4beb82015-11-09 13:57:26 -0800602 add_subdirectory(fuzz)
603endif()
604
David Benjamin17dc94e2018-08-10 09:05:20 -0500605if(UNIX AND NOT APPLE AND NOT ANDROID)
606 set(HANDSHAKER_ARGS "-handshaker-path" $<TARGET_FILE:handshaker>)
607endif()
608
David Benjamin301afaf2015-10-14 21:34:40 -0400609add_custom_target(
610 run_tests
611 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
612 ${CMAKE_BINARY_DIR}
Adam Langleyd5c72c82016-09-23 16:43:17 -0700613 COMMAND cd ssl/test/runner &&
614 ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
David Benjamin17dc94e2018-08-10 09:05:20 -0500615 ${HANDSHAKER_ARGS} ${RUNNER_ARGS}
David Benjamin301afaf2015-10-14 21:34:40 -0400616 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
Steven Valdeze5388e02018-08-01 16:54:48 -0400617 DEPENDS all_tests bssl_shim handshaker
David Benjamin81a998a2020-05-26 11:35:58 -0400618 USES_TERMINAL)