blob: 9c4edba4bc61224dc3b42dad738fc418181f640c [file] [log] [blame]
David Benjamine6fd1252018-08-10 10:30:55 -05001cmake_minimum_required(VERSION 2.8.11)
Adam Langley95c29f32014-06-20 12:00:00 -07002
David Benjamin6979c7e2017-12-04 17:19:49 -05003# Report AppleClang separately from Clang. Their version numbers are different.
4# https://cmake.org/cmake/help/v3.0/policy/CMP0025.html
5if(POLICY CMP0025)
6 cmake_policy(SET CMP0025 NEW)
7endif()
8
David Benjamin6b34d542016-02-05 21:58:39 -05009# Defer enabling C and CXX languages.
David Benjamine6fd1252018-08-10 10:30:55 -050010project(BoringSSL NONE)
David Benjamin6b34d542016-02-05 21:58:39 -050011
12if(WIN32)
13 # On Windows, prefer cl over gcc if both are available. By default most of
14 # the CMake generators prefer gcc, even on Windows.
15 set(CMAKE_GENERATOR_CC cl)
16endif()
17
David Benjamin3ecd0a52017-05-19 15:26:18 -040018include(sources.cmake)
19
David Benjamin6b34d542016-02-05 21:58:39 -050020enable_language(C)
21enable_language(CXX)
Adam Langley95c29f32014-06-20 12:00:00 -070022
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070023# This is a dummy target which all other targets depend on (manually - see other
24# CMakeLists.txt files). This gives us a hook to add any targets which need to
25# run before all other targets.
26add_custom_target(global_target)
27
Adam Langley843ab662015-04-28 17:46:58 -070028if(ANDROID)
29 # Android-NDK CMake files reconfigure the path and so Go and Perl won't be
30 # found. However, ninja will still find them in $PATH if we just name them.
Tamas Berghammer5693e422016-05-19 14:28:14 +010031 if(NOT PERL_EXECUTABLE)
32 set(PERL_EXECUTABLE "perl")
33 endif()
34 if(NOT GO_EXECUTABLE)
35 set(GO_EXECUTABLE "go")
36 endif()
Adam Langley843ab662015-04-28 17:46:58 -070037else()
38 find_package(Perl REQUIRED)
39 find_program(GO_EXECUTABLE go)
40endif()
David Benjamin3ce3c362015-02-23 13:06:19 -050041
David Benjamine6fd1252018-08-10 10:30:55 -050042if(NOT GO_EXECUTABLE)
David Benjamind27eda02015-03-05 01:19:27 -050043 message(FATAL_ERROR "Could not find Go")
44endif()
45
David Benjamine6fd1252018-08-10 10:30:55 -050046if(USE_CUSTOM_LIBCXX)
David Benjamine9ae99b2018-08-09 15:33:07 -050047 set(BORINGSSL_ALLOW_CXX_RUNTIME 1)
48endif()
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070049
David Benjamine6fd1252018-08-10 10:30:55 -050050if(BORINGSSL_ALLOW_CXX_RUNTIME)
David Benjamin2507d9e2017-07-26 11:39:38 -040051 add_definitions(-DBORINGSSL_ALLOW_CXX_RUNTIME)
52endif()
53
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070054if(BORINGSSL_PREFIX AND BORINGSSL_PREFIX_SYMBOLS)
55 add_definitions(-DBORINGSSL_PREFIX=${BORINGSSL_PREFIX})
56
57 # Use "symbol_prefix_include" to store generated header files
58 include_directories(${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include)
59 add_custom_command(
60 OUTPUT symbol_prefix_include/boringssl_prefix_symbols.h
61 symbol_prefix_include/boringssl_prefix_symbols_asm.h
62 symbol_prefix_include/boringssl_prefix_symbols_nasm.inc
63 COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include
64 COMMAND ${GO_EXECUTABLE} run ${CMAKE_CURRENT_SOURCE_DIR}/util/make_prefix_headers.go -out ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include ${BORINGSSL_PREFIX_SYMBOLS}
65 DEPENDS util/make_prefix_headers.go
66 ${CMAKE_BINARY_DIR}/${BORINGSSL_PREFIX_SYMBOLS})
67
68 # add_dependencies needs a target, not a file, so we add an intermediate
69 # target.
70 add_custom_target(
71 boringssl_prefix_symbols
72 DEPENDS symbol_prefix_include/boringssl_prefix_symbols.h
73 symbol_prefix_include/boringssl_prefix_symbols_asm.h
74 symbol_prefix_include/boringssl_prefix_symbols_nasm.inc)
75 add_dependencies(global_target boringssl_prefix_symbols)
76elseif(BORINGSSL_PREFIX OR BORINGSSL_PREFIX_SYMBOLS)
77 message(FATAL_ERROR "Must specify both or neither of BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS")
78endif()
79
David Benjamin02afbd32017-10-05 15:04:08 -040080if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
81 set(CLANG 1)
82endif()
Vincent Batts60931e22017-09-20 11:51:54 -040083
David Benjamin02afbd32017-10-05 15:04:08 -040084if(CMAKE_COMPILER_IS_GNUCXX OR CLANG)
85 # Note clang-cl is odd and sets both CLANG and MSVC. We base our configuration
David Benjamin8d67f6f2018-01-05 15:43:09 -050086 # primarily on our normal Clang one.
87 set(C_CXX_FLAGS "-Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings")
David Benjamin02afbd32017-10-05 15:04:08 -040088 if(MSVC)
David Benjamin8d67f6f2018-01-05 15:43:09 -050089 # clang-cl sets different default warnings than clang. It also treats -Wall
90 # as -Weverything, to match MSVC. Instead -W3 is the alias for -Wall.
91 # See http://llvm.org/viewvc/llvm-project?view=revision&revision=319116
92 set(C_CXX_FLAGS "${C_CXX_FLAGS} -W3 -Wno-unused-parameter -fmsc-version=1900")
David Benjamin02afbd32017-10-05 15:04:08 -040093 # googletest suppresses warning C4996 via a pragma, but clang-cl does not
94 # honor it. Suppress it here to compensate. See https://crbug.com/772117.
95 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-deprecated-declarations")
96 else()
David Benjamin8d67f6f2018-01-05 15:43:09 -050097 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wall -ggdb -fvisibility=hidden -fno-common")
David Benjamin02afbd32017-10-05 15:04:08 -040098 endif()
99
100 if(CLANG)
David Benjamin9fb6fea2017-07-31 14:03:38 -0400101 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof -fcolor-diagnostics")
Adam Langley5c38c052017-04-28 14:47:06 -0700102 else()
103 # GCC (at least 4.8.4) has a bug where it'll find unreachable free() calls
104 # and declare that the code is trying to free a stack pointer.
105 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-free-nonheap-object")
David Benjamin9b7d8362016-08-29 14:59:31 -0400106 endif()
Vincent Batts60931e22017-09-20 11:51:54 -0400107
David Benjamin02afbd32017-10-05 15:04:08 -0400108 if(CLANG OR NOT "7.0.0" VERSION_GREATER CMAKE_C_COMPILER_VERSION)
Vincent Batts60931e22017-09-20 11:51:54 -0400109 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wimplicit-fallthrough")
110 endif()
111
Adam Langley01867872016-06-30 14:16:59 -0700112 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
David Benjamin02afbd32017-10-05 15:04:08 -0400113 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_CXX_FLAGS} -Wmissing-declarations")
David Benjamin2507d9e2017-07-26 11:39:38 -0400114
David Benjamin02afbd32017-10-05 15:04:08 -0400115 if(NOT MSVC)
116 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Daniel Wagner-Hall3b358b22017-10-16 20:58:08 +0100117 if(APPLE)
118 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
119 endif()
David Benjamin02afbd32017-10-05 15:04:08 -0400120 if(NOT BORINGSSL_ALLOW_CXX_RUNTIME)
121 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
122 endif()
David Benjamin2507d9e2017-07-26 11:39:38 -0400123 endif()
124
David Benjamin4a8d1f32017-07-13 17:53:07 -0400125 # In GCC, -Wmissing-declarations is the C++ spelling of -Wmissing-prototypes
126 # and using the wrong one is an error. In Clang, -Wmissing-prototypes is the
127 # spelling for both and -Wmissing-declarations is some other warning.
128 #
129 # https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Warning-Options.html#Warning-Options
130 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-prototypes
131 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-declarations
David Benjamin02afbd32017-10-05 15:04:08 -0400132 if(CLANG)
Vincent Batts60931e22017-09-20 11:51:54 -0400133 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-prototypes")
David Benjamin4a8d1f32017-07-13 17:53:07 -0400134 endif()
Daniel Wagner-Hall10154322017-10-09 20:09:43 +0100135
136 if(CMAKE_COMPILER_IS_GNUCXX AND "4.8" VERSION_GREATER CMAKE_C_COMPILER_VERSION)
137 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-array-bounds")
138 endif()
139
Adam Langley95c29f32014-06-20 12:00:00 -0700140elseif(MSVC)
Brian Smithefed2212015-01-28 16:20:02 -0800141 set(MSVC_DISABLED_WARNINGS_LIST
David Benjamin96628432017-01-19 19:05:47 -0500142 "C4061" # enumerator 'identifier' in switch of enum 'enumeration' is not
143 # explicitly handled by a case label
144 # Disable this because it flags even when there is a default.
Brian Smithefed2212015-01-28 16:20:02 -0800145 "C4100" # 'exarg' : unreferenced formal parameter
146 "C4127" # conditional expression is constant
147 "C4200" # nonstandard extension used : zero-sized array in
148 # struct/union.
David Benjaminffb11072016-11-13 10:32:10 +0900149 "C4204" # nonstandard extension used: non-constant aggregate initializer
150 "C4221" # nonstandard extension used : 'identifier' : cannot be
151 # initialized using address of automatic variable
Brian Smithefed2212015-01-28 16:20:02 -0800152 "C4242" # 'function' : conversion from 'int' to 'uint8_t',
153 # possible loss of data
154 "C4244" # 'function' : conversion from 'int' to 'uint8_t',
155 # possible loss of data
David Benjamin3673be72015-02-11 15:12:05 -0500156 "C4267" # conversion from 'size_t' to 'int', possible loss of data
David Benjamin3673be72015-02-11 15:12:05 -0500157 "C4371" # layout of class may have changed from a previous version of the
158 # compiler due to better packing of member '...'
159 "C4388" # signed/unsigned mismatch
Brian Smithefed2212015-01-28 16:20:02 -0800160 "C4296" # '>=' : expression is always true
161 "C4350" # behavior change: 'std::_Wrap_alloc...'
162 "C4365" # '=' : conversion from 'size_t' to 'int',
163 # signed/unsigned mismatch
164 "C4389" # '!=' : signed/unsigned mismatch
David Benjamin7acd6bc2016-05-02 12:57:01 -0400165 "C4464" # relative include path contains '..'
Brian Smithefed2212015-01-28 16:20:02 -0800166 "C4510" # 'argument' : default constructor could not be generated
167 "C4512" # 'argument' : assignment operator could not be generated
168 "C4514" # 'function': unreferenced inline function has been removed
169 "C4548" # expression before comma has no effect; expected expression with
170 # side-effect" caused by FD_* macros.
171 "C4610" # struct 'argument' can never be instantiated - user defined
172 # constructor required.
David Benjamin7acd6bc2016-05-02 12:57:01 -0400173 "C4623" # default constructor was implicitly defined as deleted
David Benjamin3673be72015-02-11 15:12:05 -0500174 "C4625" # copy constructor could not be generated because a base class
175 # copy constructor is inaccessible or deleted
176 "C4626" # assignment operator could not be generated because a base class
177 # assignment operator is inaccessible or deleted
David Benjamin96628432017-01-19 19:05:47 -0500178 "C4668" # 'symbol' is not defined as a preprocessor macro, replacing with
179 # '0' for 'directives'
180 # Disable this because GTest uses it everywhere.
Brian Smithefed2212015-01-28 16:20:02 -0800181 "C4706" # assignment within conditional expression
182 "C4710" # 'function': function not inlined
183 "C4711" # function 'function' selected for inline expansion
184 "C4800" # 'int' : forcing value to bool 'true' or 'false'
185 # (performance warning)
186 "C4820" # 'bytes' bytes padding added after construct 'member_name'
David Benjamin96628432017-01-19 19:05:47 -0500187 "C5026" # move constructor was implicitly defined as deleted
David Benjamin7acd6bc2016-05-02 12:57:01 -0400188 "C5027" # move assignment operator was implicitly defined as deleted
Adam Vartanian189270c2018-05-22 13:50:44 +0100189 "C5045" # Compiler will insert Spectre mitigation for memory load if
190 # /Qspectre switch specified
David Benjamin7acd6bc2016-05-02 12:57:01 -0400191 )
192 set(MSVC_LEVEL4_WARNINGS_LIST
193 # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
194 "C4265" # class has virtual functions, but destructor is not virtual
195 )
Brian Smithefed2212015-01-28 16:20:02 -0800196 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
197 ${MSVC_DISABLED_WARNINGS_LIST})
David Benjamin7acd6bc2016-05-02 12:57:01 -0400198 string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
199 ${MSVC_LEVEL4_WARNINGS_LIST})
sphawk3ab1a692018-03-11 19:20:51 +0900200 set(CMAKE_C_FLAGS "-utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
201 set(CMAKE_CXX_FLAGS "-utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
David Benjamine2568c42017-08-16 15:25:27 -0400202endif()
203
204if(WIN32)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800205 add_definitions(-D_HAS_EXCEPTIONS=0)
Brian Smitha87de9b2015-01-28 20:34:47 -0800206 add_definitions(-DWIN32_LEAN_AND_MEAN)
David Benjamin0e434b92015-04-02 13:20:01 -0400207 add_definitions(-DNOMINMAX)
David Benjamin9b6ff442017-06-15 20:44:30 -0400208 # Allow use of fopen.
209 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
Guillaume Egles791f2822018-06-29 10:44:36 -0700210 # VS 2017 and higher supports STL-only warning suppressions. Manually add to
211 # C++ only to work around a CMake quoting bug when using NASM with the Visual
David Benjamine7b2b132018-07-06 14:46:30 -0400212 # Studio generator. This will be fixed in CMake 3.13.0. See
213 # https://gitlab.kitware.com/cmake/cmake/merge_requests/2179
Guillaume Egles791f2822018-06-29 10:44:36 -0700214 add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-D_STL_EXTRA_DISABLED_WARNINGS=4774\ 4987>)
Adam Langley95c29f32014-06-20 12:00:00 -0700215endif()
216
David Benjaminb826c0d2015-02-28 00:00:44 -0500217if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
David Benjamin02afbd32017-10-05 15:04:08 -0400218 CLANG)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800219 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
220 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
Adam Langley07100c62015-01-16 15:20:54 -0800221endif()
222
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400223if(CMAKE_COMPILER_IS_GNUCXX)
David Benjamine6fd1252018-08-10 10:30:55 -0500224 if((CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.99") OR CLANG)
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400225 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
226 else()
227 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
228 endif()
229endif()
230
231# pthread_rwlock_t requires a feature flag.
232if(NOT WIN32)
233 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
Adam Langley6f2e7332015-05-15 12:01:29 -0700234endif()
235
Adam Langley9a4beb82015-11-09 13:57:26 -0800236if(FUZZ)
David Benjamin02afbd32017-10-05 15:04:08 -0400237 if(NOT CLANG)
Alessandro Ghedinib6f69272016-09-28 22:14:01 +0100238 message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
Adam Langley9a4beb82015-11-09 13:57:26 -0800239 endif()
240
Adam Langley9c969bf2018-08-24 10:46:01 -0700241 if(CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0.0")
242 message(FATAL_ERROR "You need Clang ≥ 6.0.0")
243 endif()
244
David Benjaminec978dd2016-11-04 18:59:33 -0400245 add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
246 set(RUNNER_ARGS "-deterministic")
247
248 if(NOT NO_FUZZER_MODE)
249 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
250 set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
251 endif()
David Benjaminbc5b2a22016-03-01 22:57:32 -0500252
Adam Langley9c969bf2018-08-24 10:46:01 -0700253 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
254 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
Adam Langley9a4beb82015-11-09 13:57:26 -0800255endif()
256
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700257add_definitions(-DBORINGSSL_IMPLEMENTATION)
258
David Benjamine6fd1252018-08-10 10:30:55 -0500259if(BUILD_SHARED_LIBS)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800260 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
261 # Enable position-independent code globally. This is needed because
262 # some library targets are OBJECT libraries.
263 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
David Benjamin507c1ee2015-01-28 00:50:21 -0500264endif()
265
David Benjamine6fd1252018-08-10 10:30:55 -0500266if(MSAN)
David Benjamin02afbd32017-10-05 15:04:08 -0400267 if(NOT CLANG)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800268 message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
269 endif()
270
David Benjamine6fd1252018-08-10 10:30:55 -0500271 if(ASAN)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800272 message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
273 endif()
274
275 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
276 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
Adam Langleye77c27d2018-09-07 11:20:23 -0700277 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
Adam Langley1bcd10c2016-12-16 10:48:23 -0800278endif()
279
David Benjamine6fd1252018-08-10 10:30:55 -0500280if(ASAN)
David Benjamin02afbd32017-10-05 15:04:08 -0400281 if(NOT CLANG)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800282 message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
283 endif()
284
David Benjamin0d3c9632017-02-28 14:58:00 -0500285 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
286 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 -0800287endif()
288
David Benjaminc367ee52017-11-21 08:16:42 -0500289if(CFI)
290 if(NOT CLANG)
291 message(FATAL_ERROR "Cannot enable CFI unless using Clang")
292 endif()
293
David Benjamin66508982018-09-22 16:19:15 -0500294 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
295 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
David Benjaminc367ee52017-11-21 08:16:42 -0500296 # We use Chromium's copy of clang, which requires -fuse-ld=lld if building
297 # with -flto. That, in turn, can't handle -ggdb.
298 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
299 string(REPLACE "-ggdb" "-g" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
300 string(REPLACE "-ggdb" "-g" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
301 # -flto causes object files to contain LLVM bitcode. Mixing those with
302 # assembly output in the same static library breaks the linker.
303 set(OPENSSL_NO_ASM "1")
304endif()
305
David Benjamin5852cfc2018-07-19 17:50:45 -0400306if(TSAN)
307 if(NOT CLANG)
308 message(FATAL_ERROR "Cannot enable TSAN unless using Clang")
309 endif()
310
311 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
312 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
313 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
314endif()
315
David Benjamine6fd1252018-08-10 10:30:55 -0500316if(GCOV)
David Benjamind035ab32016-12-27 12:15:56 -0500317 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
318 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
319endif()
320
David Benjaminaff72a32017-04-06 23:26:04 -0400321if(FIPS)
322 add_definitions(-DBORINGSSL_FIPS)
Adam Langleyf64a6ee2017-05-17 13:05:50 -0700323 if(FIPS_BREAK_TEST)
324 add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
325 endif()
326 # Delocate does not work for ASan and MSan builds.
327 if(NOT ASAN AND NOT MSAN)
328 set(FIPS_DELOCATE "1")
329 endif()
David Benjaminaff72a32017-04-06 23:26:04 -0400330endif()
331
David Benjamin6291af42018-03-23 13:49:27 -0400332if(OPENSSL_SMALL)
333 add_definitions(-DOPENSSL_SMALL)
334endif()
335
David Benjamin5baee452018-09-13 16:37:28 -0500336function(go_executable dest package)
337 set(godeps "${CMAKE_SOURCE_DIR}/util/godeps.go")
338 if(${CMAKE_VERSION} VERSION_LESS "3.7" OR
339 NOT ${CMAKE_GENERATOR} STREQUAL "Ninja")
340 # The DEPFILE parameter to add_custom_command is new as of CMake 3.7 and
341 # only works with Ninja. Query the sources at configure time. Additionally,
342 # everything depends on go.mod. That affects what external packages to use.
343 execute_process(COMMAND ${GO_EXECUTABLE} run ${godeps} -format cmake
344 -pkg ${package}
345 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
346 OUTPUT_VARIABLE sources
347 RESULT_VARIABLE godeps_result)
348 add_custom_command(OUTPUT ${dest}
349 COMMAND ${GO_EXECUTABLE} build
350 -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
351 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
352 DEPENDS ${sources} ${CMAKE_SOURCE_DIR}/go.mod)
353 else()
354 # Ninja expects the target in the depfile to match the output. This is a
355 # relative path from the build directory.
356 string(LENGTH "${CMAKE_BINARY_DIR}" root_dir_length)
357 math(EXPR root_dir_length "${root_dir_length} + 1")
358 string(SUBSTRING "${CMAKE_CURRENT_BINARY_DIR}" ${root_dir_length} -1 target)
359 set(target "${target}/${dest}")
360
361 set(depfile "${CMAKE_CURRENT_BINARY_DIR}/${dest}.d")
362 add_custom_command(OUTPUT ${dest}
363 COMMAND ${GO_EXECUTABLE} build
364 -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
365 COMMAND ${GO_EXECUTABLE} run ${godeps} -format depfile
366 -target ${target} -pkg ${package} -out ${depfile}
367 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
368 DEPENDS ${godeps} ${CMAKE_SOURCE_DIR}/go.mod
369 DEPFILE ${depfile})
370 endif()
371endfunction()
372
David Benjaminaff72a32017-04-06 23:26:04 -0400373# CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
374# architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
375# alone, and expects all architecture-specific logic to be conditioned within
376# the source files rather than the build. This does not work for our assembly
377# files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
378# builds.
David Benjamine6fd1252018-08-10 10:30:55 -0500379if(NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
David Benjaminaff72a32017-04-06 23:26:04 -0400380 list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
David Benjamine6fd1252018-08-10 10:30:55 -0500381 if(NOT ${NUM_ARCHES} EQUAL 1)
David Benjaminaff72a32017-04-06 23:26:04 -0400382 message(FATAL_ERROR "Universal binaries not supported.")
383 endif()
384 list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
385endif()
386
David Benjamine6fd1252018-08-10 10:30:55 -0500387if(OPENSSL_NO_ASM)
David Benjamin27b08e92015-05-04 15:16:57 -0400388 add_definitions(-DOPENSSL_NO_ASM)
389 set(ARCH "generic")
David Benjamine6fd1252018-08-10 10:30:55 -0500390elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
David Benjaminaff72a32017-04-06 23:26:04 -0400391 set(ARCH "x86_64")
David Benjamine6fd1252018-08-10 10:30:55 -0500392elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
David Benjaminaff72a32017-04-06 23:26:04 -0400393 set(ARCH "x86_64")
David Benjamine6fd1252018-08-10 10:30:55 -0500394elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
David Benjaminaff72a32017-04-06 23:26:04 -0400395 # cmake reports AMD64 on Windows, but we might be building for 32-bit.
David Benjamine6fd1252018-08-10 10:30:55 -0500396 if(CMAKE_CL_64)
David Benjaminaff72a32017-04-06 23:26:04 -0400397 set(ARCH "x86_64")
398 else()
399 set(ARCH "x86")
400 endif()
David Benjamine6fd1252018-08-10 10:30:55 -0500401elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
David Benjaminaff72a32017-04-06 23:26:04 -0400402 set(ARCH "x86")
David Benjamine6fd1252018-08-10 10:30:55 -0500403elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
David Benjaminaff72a32017-04-06 23:26:04 -0400404 set(ARCH "x86")
David Benjamine6fd1252018-08-10 10:30:55 -0500405elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
David Benjaminaff72a32017-04-06 23:26:04 -0400406 set(ARCH "x86")
David Benjamine6fd1252018-08-10 10:30:55 -0500407elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
David Benjaminaff72a32017-04-06 23:26:04 -0400408 set(ARCH "aarch64")
David Benjamine6fd1252018-08-10 10:30:55 -0500409elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
David Benjaminaff72a32017-04-06 23:26:04 -0400410 set(ARCH "aarch64")
David Benjamine6fd1252018-08-10 10:30:55 -0500411elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm*")
David Benjaminaff72a32017-04-06 23:26:04 -0400412 set(ARCH "arm")
David Benjamine6fd1252018-08-10 10:30:55 -0500413elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "mips")
David Benjaminaff72a32017-04-06 23:26:04 -0400414 # Just to avoid the “unknown processor” error.
415 set(ARCH "generic")
David Benjamine6fd1252018-08-10 10:30:55 -0500416elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le")
David Benjaminaff72a32017-04-06 23:26:04 -0400417 set(ARCH "ppc64le")
418else()
419 message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
David Benjamin27b08e92015-05-04 15:16:57 -0400420endif()
421
David Benjamine6fd1252018-08-10 10:30:55 -0500422if(ANDROID AND NOT ANDROID_NDK_REVISION AND ${ARCH} STREQUAL "arm")
David Benjamin9894ee92017-12-13 18:08:47 -0500423 # The third-party Android-NDK CMake files somehow fail to set the -march flag
424 # for assembly files. Without this flag, the compiler believes that it's
David Benjaminaff72a32017-04-06 23:26:04 -0400425 # building for ARMv5.
David Benjamin9894ee92017-12-13 18:08:47 -0500426 set(CMAKE_ASM_FLAGS "-march=${CMAKE_SYSTEM_PROCESSOR} ${CMAKE_ASM_FLAGS}")
David Benjaminaff72a32017-04-06 23:26:04 -0400427endif()
428
David Benjamine6fd1252018-08-10 10:30:55 -0500429if(${ARCH} STREQUAL "x86" AND APPLE AND ${CMAKE_VERSION} VERSION_LESS "3.0")
David Benjaminaff72a32017-04-06 23:26:04 -0400430 # With CMake 2.8.x, ${CMAKE_SYSTEM_PROCESSOR} evalutes to i386 on OS X,
431 # but clang defaults to 64-bit builds on OS X unless otherwise told.
432 # Set ARCH to x86_64 so clang and CMake agree. This is fixed in CMake 3.
433 set(ARCH "x86_64")
Adam Langleyfd499932017-04-04 14:21:43 -0700434endif()
435
David Benjamine6fd1252018-08-10 10:30:55 -0500436if(USE_CUSTOM_LIBCXX)
437 if(NOT CLANG)
David Benjamine9ae99b2018-08-09 15:33:07 -0500438 message(FATAL_ERROR "USE_CUSTOM_LIBCXX only supported with Clang")
439 endif()
440
441 # CMAKE_CXX_FLAGS ends up in the linker flags as well, so use
442 # add_compile_options. There does not appear to be a way to set
443 # language-specific compile-only flags.
444 add_compile_options("-nostdinc++")
445 set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -nostdlib++")
446 include_directories(
447 SYSTEM
448 util/bot/libcxx/include
449 util/bot/libcxxabi/include
450 )
451
452 # This is patterned after buildtools/third_party/libc++/BUILD.gn and
453 # buildtools/third_party/libc++abi/BUILD.gn in Chromium.
454
455 file(GLOB LIBCXX_SOURCES "util/bot/libcxx/src/*.cpp")
456 file(GLOB LIBCXXABI_SOURCES "util/bot/libcxxabi/src/*.cpp")
457
458 # This file is meant for exception-less builds.
459 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_noexception.cpp")
460 # libc++ also defines new and delete.
461 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/stdlib_new_delete.cpp")
David Benjamine6fd1252018-08-10 10:30:55 -0500462 if(TSAN)
David Benjamine9ae99b2018-08-09 15:33:07 -0500463 # ThreadSanitizer tries to intercept these symbols. Skip them to avoid
464 # symbol conflicts.
465 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_guard.cpp")
466 endif()
467
468 add_library(libcxxabi ${LIBCXXABI_SOURCES})
469 target_compile_definitions(
470 libcxxabi PRIVATE
471 -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS
472 )
473 set_target_properties(libcxxabi PROPERTIES COMPILE_FLAGS "-Wno-missing-prototypes -Wno-implicit-fallthrough")
474
475 add_library(libcxx ${LIBCXX_SOURCES})
David Benjamine6fd1252018-08-10 10:30:55 -0500476 if(ASAN OR MSAN OR TSAN)
David Benjamine9ae99b2018-08-09 15:33:07 -0500477 # Sanitizers try to intercept new and delete.
478 target_compile_definitions(
479 libcxx PRIVATE
480 -D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
481 )
482 endif()
483 target_compile_definitions(
484 libcxx PRIVATE
485 -D_LIBCPP_BUILDING_LIBRARY
486 -DLIBCXX_BUILDING_LIBCXXABI
487 )
488 target_link_libraries(libcxx libcxxabi)
489endif()
490
David Benjamin96628432017-01-19 19:05:47 -0500491# Add minimal googletest targets. The provided one has many side-effects, and
492# googletest has a very straightforward build.
Marek Gilbert11850d52018-01-03 23:07:58 -0800493add_library(boringssl_gtest third_party/googletest/src/gtest-all.cc)
494target_include_directories(boringssl_gtest PRIVATE third_party/googletest)
David Benjamin96628432017-01-19 19:05:47 -0500495
496include_directories(third_party/googletest/include)
497
David Benjamin301afaf2015-10-14 21:34:40 -0400498# Declare a dummy target to build all unit tests. Test targets should inject
499# themselves as dependencies next to the target definition.
500add_custom_target(all_tests)
501
David Benjamin3ecd0a52017-05-19 15:26:18 -0400502add_custom_command(
503 OUTPUT crypto_test_data.cc
504 COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go ${CRYPTO_TEST_DATA} >
505 ${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc
506 DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
507 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
508
509add_library(crypto_test_data OBJECT crypto_test_data.cc)
510
Adam Langley95c29f32014-06-20 12:00:00 -0700511add_subdirectory(crypto)
512add_subdirectory(ssl)
513add_subdirectory(ssl/test)
Martin Kreichgauer118355c2017-05-12 15:34:45 -0700514add_subdirectory(fipstools)
Adam Langley95c29f32014-06-20 12:00:00 -0700515add_subdirectory(tool)
Adam Langleyc004dfc2015-02-03 10:45:12 -0800516add_subdirectory(decrepit)
David Benjamin301afaf2015-10-14 21:34:40 -0400517
Adam Langley9a4beb82015-11-09 13:57:26 -0800518if(FUZZ)
519 add_subdirectory(fuzz)
520endif()
521
David Benjamine6fd1252018-08-10 10:30:55 -0500522if(NOT ${CMAKE_VERSION} VERSION_LESS "3.2")
David Benjamin301afaf2015-10-14 21:34:40 -0400523 # USES_TERMINAL is only available in CMake 3.2 or later.
524 set(MAYBE_USES_TERMINAL USES_TERMINAL)
525endif()
526
David Benjamin17dc94e2018-08-10 09:05:20 -0500527if(UNIX AND NOT APPLE AND NOT ANDROID)
528 set(HANDSHAKER_ARGS "-handshaker-path" $<TARGET_FILE:handshaker>)
529endif()
530
David Benjamin301afaf2015-10-14 21:34:40 -0400531add_custom_target(
532 run_tests
533 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
534 ${CMAKE_BINARY_DIR}
Adam Langleyd5c72c82016-09-23 16:43:17 -0700535 COMMAND cd ssl/test/runner &&
536 ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
David Benjamin17dc94e2018-08-10 09:05:20 -0500537 ${HANDSHAKER_ARGS} ${RUNNER_ARGS}
David Benjamin301afaf2015-10-14 21:34:40 -0400538 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
Steven Valdeze5388e02018-08-01 16:54:48 -0400539 DEPENDS all_tests bssl_shim handshaker
David Benjamin301afaf2015-10-14 21:34:40 -0400540 ${MAYBE_USES_TERMINAL})