blob: 13ee364411d11a1ca90b738afea775b647d8a68b [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 Benjamin17d553d2018-12-21 17:58:36 -060042if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
Adam Langley9dfaf252019-01-04 07:53:25 -080043 find_package(PkgConfig QUIET)
44 if (PkgConfig_FOUND)
45 pkg_check_modules(LIBUNWIND libunwind-generic)
46 if(LIBUNWIND_FOUND)
47 add_definitions(-DBORINGSSL_HAVE_LIBUNWIND)
48 else()
49 message("libunwind not found. Disabling unwind tests.")
50 endif()
David Benjamin17d553d2018-12-21 17:58:36 -060051 else()
Adam Langley9dfaf252019-01-04 07:53:25 -080052 message("pkgconfig not found. Disabling unwind tests.")
David Benjamin17d553d2018-12-21 17:58:36 -060053 endif()
54endif()
55
David Benjamine6fd1252018-08-10 10:30:55 -050056if(NOT GO_EXECUTABLE)
David Benjamind27eda02015-03-05 01:19:27 -050057 message(FATAL_ERROR "Could not find Go")
58endif()
59
David Benjamine6fd1252018-08-10 10:30:55 -050060if(USE_CUSTOM_LIBCXX)
David Benjamine9ae99b2018-08-09 15:33:07 -050061 set(BORINGSSL_ALLOW_CXX_RUNTIME 1)
62endif()
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070063
David Benjamine6fd1252018-08-10 10:30:55 -050064if(BORINGSSL_ALLOW_CXX_RUNTIME)
David Benjamin2507d9e2017-07-26 11:39:38 -040065 add_definitions(-DBORINGSSL_ALLOW_CXX_RUNTIME)
66endif()
67
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070068if(BORINGSSL_PREFIX AND BORINGSSL_PREFIX_SYMBOLS)
69 add_definitions(-DBORINGSSL_PREFIX=${BORINGSSL_PREFIX})
David Benjamin8c23d3a2018-11-25 15:58:02 -060070 # CMake automatically connects include_directories to the NASM command-line,
71 # but not add_definitions.
72 set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -DBORINGSSL_PREFIX=${BORINGSSL_PREFIX}")
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070073
74 # Use "symbol_prefix_include" to store generated header files
75 include_directories(${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include)
76 add_custom_command(
77 OUTPUT symbol_prefix_include/boringssl_prefix_symbols.h
78 symbol_prefix_include/boringssl_prefix_symbols_asm.h
79 symbol_prefix_include/boringssl_prefix_symbols_nasm.inc
80 COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include
81 COMMAND ${GO_EXECUTABLE} run ${CMAKE_CURRENT_SOURCE_DIR}/util/make_prefix_headers.go -out ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include ${BORINGSSL_PREFIX_SYMBOLS}
82 DEPENDS util/make_prefix_headers.go
83 ${CMAKE_BINARY_DIR}/${BORINGSSL_PREFIX_SYMBOLS})
84
85 # add_dependencies needs a target, not a file, so we add an intermediate
86 # target.
87 add_custom_target(
88 boringssl_prefix_symbols
89 DEPENDS 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 add_dependencies(global_target boringssl_prefix_symbols)
93elseif(BORINGSSL_PREFIX OR BORINGSSL_PREFIX_SYMBOLS)
94 message(FATAL_ERROR "Must specify both or neither of BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS")
95endif()
96
David Benjamin02afbd32017-10-05 15:04:08 -040097if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
98 set(CLANG 1)
99endif()
Vincent Batts60931e22017-09-20 11:51:54 -0400100
David Benjamin02afbd32017-10-05 15:04:08 -0400101if(CMAKE_COMPILER_IS_GNUCXX OR CLANG)
102 # Note clang-cl is odd and sets both CLANG and MSVC. We base our configuration
David Benjamin8d67f6f2018-01-05 15:43:09 -0500103 # primarily on our normal Clang one.
104 set(C_CXX_FLAGS "-Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings")
David Benjamin02afbd32017-10-05 15:04:08 -0400105 if(MSVC)
David Benjamin8d67f6f2018-01-05 15:43:09 -0500106 # clang-cl sets different default warnings than clang. It also treats -Wall
107 # as -Weverything, to match MSVC. Instead -W3 is the alias for -Wall.
108 # See http://llvm.org/viewvc/llvm-project?view=revision&revision=319116
109 set(C_CXX_FLAGS "${C_CXX_FLAGS} -W3 -Wno-unused-parameter -fmsc-version=1900")
David Benjamin02afbd32017-10-05 15:04:08 -0400110 # googletest suppresses warning C4996 via a pragma, but clang-cl does not
111 # honor it. Suppress it here to compensate. See https://crbug.com/772117.
112 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-deprecated-declarations")
113 else()
David Benjamin8d67f6f2018-01-05 15:43:09 -0500114 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wall -ggdb -fvisibility=hidden -fno-common")
David Benjamin02afbd32017-10-05 15:04:08 -0400115 endif()
116
117 if(CLANG)
David Benjamin9fb6fea2017-07-31 14:03:38 -0400118 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof -fcolor-diagnostics")
Adam Langley5c38c052017-04-28 14:47:06 -0700119 else()
120 # GCC (at least 4.8.4) has a bug where it'll find unreachable free() calls
121 # and declare that the code is trying to free a stack pointer.
122 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-free-nonheap-object")
David Benjamin9b7d8362016-08-29 14:59:31 -0400123 endif()
Vincent Batts60931e22017-09-20 11:51:54 -0400124
David Benjamin02afbd32017-10-05 15:04:08 -0400125 if(CLANG OR NOT "7.0.0" VERSION_GREATER CMAKE_C_COMPILER_VERSION)
Vincent Batts60931e22017-09-20 11:51:54 -0400126 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wimplicit-fallthrough")
127 endif()
128
Adam Langley01867872016-06-30 14:16:59 -0700129 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
David Benjamin02afbd32017-10-05 15:04:08 -0400130 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_CXX_FLAGS} -Wmissing-declarations")
David Benjamin2507d9e2017-07-26 11:39:38 -0400131
David Benjamin02afbd32017-10-05 15:04:08 -0400132 if(NOT MSVC)
133 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Daniel Wagner-Hall3b358b22017-10-16 20:58:08 +0100134 if(APPLE)
135 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
136 endif()
David Benjamin02afbd32017-10-05 15:04:08 -0400137 if(NOT BORINGSSL_ALLOW_CXX_RUNTIME)
138 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
139 endif()
David Benjamin2507d9e2017-07-26 11:39:38 -0400140 endif()
141
David Benjamin4a8d1f32017-07-13 17:53:07 -0400142 # In GCC, -Wmissing-declarations is the C++ spelling of -Wmissing-prototypes
143 # and using the wrong one is an error. In Clang, -Wmissing-prototypes is the
144 # spelling for both and -Wmissing-declarations is some other warning.
145 #
146 # https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Warning-Options.html#Warning-Options
147 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-prototypes
148 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-declarations
David Benjamin02afbd32017-10-05 15:04:08 -0400149 if(CLANG)
Vincent Batts60931e22017-09-20 11:51:54 -0400150 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-prototypes")
David Benjamin4a8d1f32017-07-13 17:53:07 -0400151 endif()
Daniel Wagner-Hall10154322017-10-09 20:09:43 +0100152
153 if(CMAKE_COMPILER_IS_GNUCXX AND "4.8" VERSION_GREATER CMAKE_C_COMPILER_VERSION)
154 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-array-bounds")
155 endif()
156
Adam Langley95c29f32014-06-20 12:00:00 -0700157elseif(MSVC)
Brian Smithefed2212015-01-28 16:20:02 -0800158 set(MSVC_DISABLED_WARNINGS_LIST
David Benjamin96628432017-01-19 19:05:47 -0500159 "C4061" # enumerator 'identifier' in switch of enum 'enumeration' is not
160 # explicitly handled by a case label
161 # Disable this because it flags even when there is a default.
Brian Smithefed2212015-01-28 16:20:02 -0800162 "C4100" # 'exarg' : unreferenced formal parameter
163 "C4127" # conditional expression is constant
164 "C4200" # nonstandard extension used : zero-sized array in
165 # struct/union.
David Benjaminffb11072016-11-13 10:32:10 +0900166 "C4204" # nonstandard extension used: non-constant aggregate initializer
167 "C4221" # nonstandard extension used : 'identifier' : cannot be
168 # initialized using address of automatic variable
Brian Smithefed2212015-01-28 16:20:02 -0800169 "C4242" # 'function' : conversion from 'int' to 'uint8_t',
170 # possible loss of data
171 "C4244" # 'function' : conversion from 'int' to 'uint8_t',
172 # possible loss of data
David Benjamin3673be72015-02-11 15:12:05 -0500173 "C4267" # conversion from 'size_t' to 'int', possible loss of data
David Benjamin3673be72015-02-11 15:12:05 -0500174 "C4371" # layout of class may have changed from a previous version of the
175 # compiler due to better packing of member '...'
176 "C4388" # signed/unsigned mismatch
Brian Smithefed2212015-01-28 16:20:02 -0800177 "C4296" # '>=' : expression is always true
178 "C4350" # behavior change: 'std::_Wrap_alloc...'
179 "C4365" # '=' : conversion from 'size_t' to 'int',
180 # signed/unsigned mismatch
181 "C4389" # '!=' : signed/unsigned mismatch
David Benjamin7acd6bc2016-05-02 12:57:01 -0400182 "C4464" # relative include path contains '..'
Brian Smithefed2212015-01-28 16:20:02 -0800183 "C4510" # 'argument' : default constructor could not be generated
184 "C4512" # 'argument' : assignment operator could not be generated
185 "C4514" # 'function': unreferenced inline function has been removed
186 "C4548" # expression before comma has no effect; expected expression with
187 # side-effect" caused by FD_* macros.
188 "C4610" # struct 'argument' can never be instantiated - user defined
189 # constructor required.
David Benjamin7acd6bc2016-05-02 12:57:01 -0400190 "C4623" # default constructor was implicitly defined as deleted
David Benjamin3673be72015-02-11 15:12:05 -0500191 "C4625" # copy constructor could not be generated because a base class
192 # copy constructor is inaccessible or deleted
193 "C4626" # assignment operator could not be generated because a base class
194 # assignment operator is inaccessible or deleted
David Benjamin96628432017-01-19 19:05:47 -0500195 "C4668" # 'symbol' is not defined as a preprocessor macro, replacing with
196 # '0' for 'directives'
197 # Disable this because GTest uses it everywhere.
Brian Smithefed2212015-01-28 16:20:02 -0800198 "C4706" # assignment within conditional expression
199 "C4710" # 'function': function not inlined
200 "C4711" # function 'function' selected for inline expansion
201 "C4800" # 'int' : forcing value to bool 'true' or 'false'
202 # (performance warning)
203 "C4820" # 'bytes' bytes padding added after construct 'member_name'
David Benjamin96628432017-01-19 19:05:47 -0500204 "C5026" # move constructor was implicitly defined as deleted
David Benjamin7acd6bc2016-05-02 12:57:01 -0400205 "C5027" # move assignment operator was implicitly defined as deleted
Adam Vartanian189270c2018-05-22 13:50:44 +0100206 "C5045" # Compiler will insert Spectre mitigation for memory load if
207 # /Qspectre switch specified
David Benjamin7acd6bc2016-05-02 12:57:01 -0400208 )
209 set(MSVC_LEVEL4_WARNINGS_LIST
210 # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
211 "C4265" # class has virtual functions, but destructor is not virtual
212 )
Brian Smithefed2212015-01-28 16:20:02 -0800213 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
214 ${MSVC_DISABLED_WARNINGS_LIST})
David Benjamin7acd6bc2016-05-02 12:57:01 -0400215 string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
216 ${MSVC_LEVEL4_WARNINGS_LIST})
sphawk3ab1a692018-03-11 19:20:51 +0900217 set(CMAKE_C_FLAGS "-utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
218 set(CMAKE_CXX_FLAGS "-utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
David Benjamine2568c42017-08-16 15:25:27 -0400219endif()
220
221if(WIN32)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800222 add_definitions(-D_HAS_EXCEPTIONS=0)
Brian Smitha87de9b2015-01-28 20:34:47 -0800223 add_definitions(-DWIN32_LEAN_AND_MEAN)
David Benjamin0e434b92015-04-02 13:20:01 -0400224 add_definitions(-DNOMINMAX)
David Benjamin9b6ff442017-06-15 20:44:30 -0400225 # Allow use of fopen.
226 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
Guillaume Egles791f2822018-06-29 10:44:36 -0700227 # VS 2017 and higher supports STL-only warning suppressions. Manually add to
228 # C++ only to work around a CMake quoting bug when using NASM with the Visual
David Benjamine7b2b132018-07-06 14:46:30 -0400229 # Studio generator. This will be fixed in CMake 3.13.0. See
230 # https://gitlab.kitware.com/cmake/cmake/merge_requests/2179
Guillaume Egles791f2822018-06-29 10:44:36 -0700231 add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-D_STL_EXTRA_DISABLED_WARNINGS=4774\ 4987>)
Adam Langley95c29f32014-06-20 12:00:00 -0700232endif()
233
David Benjaminb826c0d2015-02-28 00:00:44 -0500234if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
David Benjamin02afbd32017-10-05 15:04:08 -0400235 CLANG)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800236 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
237 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
Adam Langley07100c62015-01-16 15:20:54 -0800238endif()
239
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400240if(CMAKE_COMPILER_IS_GNUCXX)
David Benjamine6fd1252018-08-10 10:30:55 -0500241 if((CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.99") OR CLANG)
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400242 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
243 else()
244 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
245 endif()
246endif()
247
248# pthread_rwlock_t requires a feature flag.
249if(NOT WIN32)
250 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
Adam Langley6f2e7332015-05-15 12:01:29 -0700251endif()
252
Adam Langley9a4beb82015-11-09 13:57:26 -0800253if(FUZZ)
David Benjamin02afbd32017-10-05 15:04:08 -0400254 if(NOT CLANG)
Alessandro Ghedinib6f69272016-09-28 22:14:01 +0100255 message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
Adam Langley9a4beb82015-11-09 13:57:26 -0800256 endif()
257
Adam Langley9c969bf2018-08-24 10:46:01 -0700258 if(CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0.0")
259 message(FATAL_ERROR "You need Clang ≥ 6.0.0")
260 endif()
261
David Benjaminec978dd2016-11-04 18:59:33 -0400262 add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
263 set(RUNNER_ARGS "-deterministic")
264
265 if(NOT NO_FUZZER_MODE)
266 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
267 set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
268 endif()
David Benjaminbc5b2a22016-03-01 22:57:32 -0500269
Adam Langley9c969bf2018-08-24 10:46:01 -0700270 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
271 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
Adam Langley9a4beb82015-11-09 13:57:26 -0800272endif()
273
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700274add_definitions(-DBORINGSSL_IMPLEMENTATION)
275
David Benjamine6fd1252018-08-10 10:30:55 -0500276if(BUILD_SHARED_LIBS)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800277 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
278 # Enable position-independent code globally. This is needed because
279 # some library targets are OBJECT libraries.
280 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
David Benjamin507c1ee2015-01-28 00:50:21 -0500281endif()
282
David Benjamine6fd1252018-08-10 10:30:55 -0500283if(MSAN)
David Benjamin02afbd32017-10-05 15:04:08 -0400284 if(NOT CLANG)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800285 message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
286 endif()
287
David Benjamine6fd1252018-08-10 10:30:55 -0500288 if(ASAN)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800289 message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
290 endif()
291
292 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
293 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
Adam Langleye77c27d2018-09-07 11:20:23 -0700294 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
Adam Langley1bcd10c2016-12-16 10:48:23 -0800295endif()
296
David Benjamine6fd1252018-08-10 10:30:55 -0500297if(ASAN)
David Benjamin02afbd32017-10-05 15:04:08 -0400298 if(NOT CLANG)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800299 message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
300 endif()
301
David Benjamin0d3c9632017-02-28 14:58:00 -0500302 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
303 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 -0800304endif()
305
David Benjaminc367ee52017-11-21 08:16:42 -0500306if(CFI)
307 if(NOT CLANG)
308 message(FATAL_ERROR "Cannot enable CFI unless using Clang")
309 endif()
310
David Benjamin66508982018-09-22 16:19:15 -0500311 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
312 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
David Benjaminc367ee52017-11-21 08:16:42 -0500313 # We use Chromium's copy of clang, which requires -fuse-ld=lld if building
314 # with -flto. That, in turn, can't handle -ggdb.
315 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
316 string(REPLACE "-ggdb" "-g" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
317 string(REPLACE "-ggdb" "-g" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
318 # -flto causes object files to contain LLVM bitcode. Mixing those with
319 # assembly output in the same static library breaks the linker.
320 set(OPENSSL_NO_ASM "1")
321endif()
322
David Benjamin5852cfc2018-07-19 17:50:45 -0400323if(TSAN)
324 if(NOT CLANG)
325 message(FATAL_ERROR "Cannot enable TSAN unless using Clang")
326 endif()
327
328 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
329 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
330 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
331endif()
332
David Benjamine6fd1252018-08-10 10:30:55 -0500333if(GCOV)
David Benjamind035ab32016-12-27 12:15:56 -0500334 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
335 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
336endif()
337
David Benjaminaff72a32017-04-06 23:26:04 -0400338if(FIPS)
339 add_definitions(-DBORINGSSL_FIPS)
Adam Langleyf64a6ee2017-05-17 13:05:50 -0700340 if(FIPS_BREAK_TEST)
341 add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
342 endif()
343 # Delocate does not work for ASan and MSan builds.
344 if(NOT ASAN AND NOT MSAN)
345 set(FIPS_DELOCATE "1")
346 endif()
David Benjaminaff72a32017-04-06 23:26:04 -0400347endif()
348
David Benjamin6291af42018-03-23 13:49:27 -0400349if(OPENSSL_SMALL)
350 add_definitions(-DOPENSSL_SMALL)
351endif()
352
Adam Langleya6a049a2018-12-06 17:15:58 -0800353if(CONSTANT_TIME_VALIDATION)
354 add_definitions(-DBORINGSSL_CONSTANT_TIME_VALIDATION)
355 # Asserts will often test secret data.
356 add_definitions(-DNDEBUG)
357endif()
358
David Benjamin5baee452018-09-13 16:37:28 -0500359function(go_executable dest package)
360 set(godeps "${CMAKE_SOURCE_DIR}/util/godeps.go")
361 if(${CMAKE_VERSION} VERSION_LESS "3.7" OR
362 NOT ${CMAKE_GENERATOR} STREQUAL "Ninja")
363 # The DEPFILE parameter to add_custom_command is new as of CMake 3.7 and
364 # only works with Ninja. Query the sources at configure time. Additionally,
365 # everything depends on go.mod. That affects what external packages to use.
366 execute_process(COMMAND ${GO_EXECUTABLE} run ${godeps} -format cmake
367 -pkg ${package}
368 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
369 OUTPUT_VARIABLE sources
370 RESULT_VARIABLE godeps_result)
371 add_custom_command(OUTPUT ${dest}
372 COMMAND ${GO_EXECUTABLE} build
373 -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
374 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
375 DEPENDS ${sources} ${CMAKE_SOURCE_DIR}/go.mod)
376 else()
377 # Ninja expects the target in the depfile to match the output. This is a
378 # relative path from the build directory.
379 string(LENGTH "${CMAKE_BINARY_DIR}" root_dir_length)
380 math(EXPR root_dir_length "${root_dir_length} + 1")
381 string(SUBSTRING "${CMAKE_CURRENT_BINARY_DIR}" ${root_dir_length} -1 target)
382 set(target "${target}/${dest}")
383
384 set(depfile "${CMAKE_CURRENT_BINARY_DIR}/${dest}.d")
385 add_custom_command(OUTPUT ${dest}
386 COMMAND ${GO_EXECUTABLE} build
387 -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
388 COMMAND ${GO_EXECUTABLE} run ${godeps} -format depfile
389 -target ${target} -pkg ${package} -out ${depfile}
390 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
391 DEPENDS ${godeps} ${CMAKE_SOURCE_DIR}/go.mod
392 DEPFILE ${depfile})
393 endif()
394endfunction()
395
David Benjaminaff72a32017-04-06 23:26:04 -0400396# CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
397# architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
398# alone, and expects all architecture-specific logic to be conditioned within
399# the source files rather than the build. This does not work for our assembly
400# files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
401# builds.
David Benjamine6fd1252018-08-10 10:30:55 -0500402if(NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
David Benjaminaff72a32017-04-06 23:26:04 -0400403 list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
David Benjamine6fd1252018-08-10 10:30:55 -0500404 if(NOT ${NUM_ARCHES} EQUAL 1)
David Benjaminaff72a32017-04-06 23:26:04 -0400405 message(FATAL_ERROR "Universal binaries not supported.")
406 endif()
407 list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
408endif()
409
David Benjamine6fd1252018-08-10 10:30:55 -0500410if(OPENSSL_NO_ASM)
David Benjamin27b08e92015-05-04 15:16:57 -0400411 add_definitions(-DOPENSSL_NO_ASM)
412 set(ARCH "generic")
David Benjamine6fd1252018-08-10 10:30:55 -0500413elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
David Benjaminaff72a32017-04-06 23:26:04 -0400414 set(ARCH "x86_64")
David Benjamine6fd1252018-08-10 10:30:55 -0500415elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
David Benjaminaff72a32017-04-06 23:26:04 -0400416 set(ARCH "x86_64")
David Benjamine6fd1252018-08-10 10:30:55 -0500417elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
David Benjaminaff72a32017-04-06 23:26:04 -0400418 # cmake reports AMD64 on Windows, but we might be building for 32-bit.
David Benjamine6fd1252018-08-10 10:30:55 -0500419 if(CMAKE_CL_64)
David Benjaminaff72a32017-04-06 23:26:04 -0400420 set(ARCH "x86_64")
421 else()
422 set(ARCH "x86")
423 endif()
David Benjamine6fd1252018-08-10 10:30:55 -0500424elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
David Benjaminaff72a32017-04-06 23:26:04 -0400425 set(ARCH "x86")
David Benjamine6fd1252018-08-10 10:30:55 -0500426elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
David Benjaminaff72a32017-04-06 23:26:04 -0400427 set(ARCH "x86")
David Benjamine6fd1252018-08-10 10:30:55 -0500428elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
David Benjaminaff72a32017-04-06 23:26:04 -0400429 set(ARCH "x86")
David Benjamine6fd1252018-08-10 10:30:55 -0500430elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
David Benjaminaff72a32017-04-06 23:26:04 -0400431 set(ARCH "aarch64")
David Benjamine6fd1252018-08-10 10:30:55 -0500432elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
David Benjaminaff72a32017-04-06 23:26:04 -0400433 set(ARCH "aarch64")
Junghoon Jange8ba1e32018-11-15 10:20:42 +0900434# Apple A12 Bionic chipset which is added in iPhone XS/XS Max/XR uses arm64e architecture.
435elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64e")
436 set(ARCH "aarch64")
David Benjamine6fd1252018-08-10 10:30:55 -0500437elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm*")
David Benjaminaff72a32017-04-06 23:26:04 -0400438 set(ARCH "arm")
David Benjamine6fd1252018-08-10 10:30:55 -0500439elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "mips")
David Benjaminaff72a32017-04-06 23:26:04 -0400440 # Just to avoid the “unknown processor” error.
441 set(ARCH "generic")
David Benjamine6fd1252018-08-10 10:30:55 -0500442elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le")
David Benjaminaff72a32017-04-06 23:26:04 -0400443 set(ARCH "ppc64le")
444else()
445 message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
David Benjamin27b08e92015-05-04 15:16:57 -0400446endif()
447
David Benjamine6fd1252018-08-10 10:30:55 -0500448if(ANDROID AND NOT ANDROID_NDK_REVISION AND ${ARCH} STREQUAL "arm")
David Benjamin9894ee92017-12-13 18:08:47 -0500449 # The third-party Android-NDK CMake files somehow fail to set the -march flag
450 # for assembly files. Without this flag, the compiler believes that it's
David Benjaminaff72a32017-04-06 23:26:04 -0400451 # building for ARMv5.
David Benjamin9894ee92017-12-13 18:08:47 -0500452 set(CMAKE_ASM_FLAGS "-march=${CMAKE_SYSTEM_PROCESSOR} ${CMAKE_ASM_FLAGS}")
David Benjaminaff72a32017-04-06 23:26:04 -0400453endif()
454
David Benjamine6fd1252018-08-10 10:30:55 -0500455if(${ARCH} STREQUAL "x86" AND APPLE AND ${CMAKE_VERSION} VERSION_LESS "3.0")
David Benjaminaff72a32017-04-06 23:26:04 -0400456 # With CMake 2.8.x, ${CMAKE_SYSTEM_PROCESSOR} evalutes to i386 on OS X,
457 # but clang defaults to 64-bit builds on OS X unless otherwise told.
458 # Set ARCH to x86_64 so clang and CMake agree. This is fixed in CMake 3.
459 set(ARCH "x86_64")
Adam Langleyfd499932017-04-04 14:21:43 -0700460endif()
461
David Benjamine6fd1252018-08-10 10:30:55 -0500462if(USE_CUSTOM_LIBCXX)
463 if(NOT CLANG)
David Benjamine9ae99b2018-08-09 15:33:07 -0500464 message(FATAL_ERROR "USE_CUSTOM_LIBCXX only supported with Clang")
465 endif()
466
467 # CMAKE_CXX_FLAGS ends up in the linker flags as well, so use
468 # add_compile_options. There does not appear to be a way to set
469 # language-specific compile-only flags.
470 add_compile_options("-nostdinc++")
471 set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -nostdlib++")
472 include_directories(
473 SYSTEM
474 util/bot/libcxx/include
475 util/bot/libcxxabi/include
476 )
477
478 # This is patterned after buildtools/third_party/libc++/BUILD.gn and
479 # buildtools/third_party/libc++abi/BUILD.gn in Chromium.
480
481 file(GLOB LIBCXX_SOURCES "util/bot/libcxx/src/*.cpp")
482 file(GLOB LIBCXXABI_SOURCES "util/bot/libcxxabi/src/*.cpp")
483
484 # This file is meant for exception-less builds.
485 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_noexception.cpp")
486 # libc++ also defines new and delete.
487 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/stdlib_new_delete.cpp")
David Benjamine6fd1252018-08-10 10:30:55 -0500488 if(TSAN)
David Benjamine9ae99b2018-08-09 15:33:07 -0500489 # ThreadSanitizer tries to intercept these symbols. Skip them to avoid
490 # symbol conflicts.
491 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_guard.cpp")
492 endif()
493
494 add_library(libcxxabi ${LIBCXXABI_SOURCES})
495 target_compile_definitions(
496 libcxxabi PRIVATE
497 -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS
498 )
499 set_target_properties(libcxxabi PROPERTIES COMPILE_FLAGS "-Wno-missing-prototypes -Wno-implicit-fallthrough")
500
501 add_library(libcxx ${LIBCXX_SOURCES})
David Benjamine6fd1252018-08-10 10:30:55 -0500502 if(ASAN OR MSAN OR TSAN)
David Benjamine9ae99b2018-08-09 15:33:07 -0500503 # Sanitizers try to intercept new and delete.
504 target_compile_definitions(
505 libcxx PRIVATE
506 -D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
507 )
508 endif()
509 target_compile_definitions(
510 libcxx PRIVATE
511 -D_LIBCPP_BUILDING_LIBRARY
512 -DLIBCXX_BUILDING_LIBCXXABI
513 )
514 target_link_libraries(libcxx libcxxabi)
515endif()
516
David Benjamin96628432017-01-19 19:05:47 -0500517# Add minimal googletest targets. The provided one has many side-effects, and
518# googletest has a very straightforward build.
Marek Gilbert11850d52018-01-03 23:07:58 -0800519add_library(boringssl_gtest third_party/googletest/src/gtest-all.cc)
520target_include_directories(boringssl_gtest PRIVATE third_party/googletest)
David Benjamin96628432017-01-19 19:05:47 -0500521
522include_directories(third_party/googletest/include)
523
David Benjamin301afaf2015-10-14 21:34:40 -0400524# Declare a dummy target to build all unit tests. Test targets should inject
525# themselves as dependencies next to the target definition.
526add_custom_target(all_tests)
527
David Benjamin3ecd0a52017-05-19 15:26:18 -0400528add_custom_command(
529 OUTPUT crypto_test_data.cc
530 COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go ${CRYPTO_TEST_DATA} >
531 ${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc
532 DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
533 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
534
535add_library(crypto_test_data OBJECT crypto_test_data.cc)
536
Adam Langley95c29f32014-06-20 12:00:00 -0700537add_subdirectory(crypto)
538add_subdirectory(ssl)
539add_subdirectory(ssl/test)
Martin Kreichgauer118355c2017-05-12 15:34:45 -0700540add_subdirectory(fipstools)
Adam Langley95c29f32014-06-20 12:00:00 -0700541add_subdirectory(tool)
Adam Langleyc004dfc2015-02-03 10:45:12 -0800542add_subdirectory(decrepit)
David Benjamin301afaf2015-10-14 21:34:40 -0400543
Adam Langley9a4beb82015-11-09 13:57:26 -0800544if(FUZZ)
545 add_subdirectory(fuzz)
546endif()
547
David Benjamine6fd1252018-08-10 10:30:55 -0500548if(NOT ${CMAKE_VERSION} VERSION_LESS "3.2")
David Benjamin301afaf2015-10-14 21:34:40 -0400549 # USES_TERMINAL is only available in CMake 3.2 or later.
550 set(MAYBE_USES_TERMINAL USES_TERMINAL)
551endif()
552
David Benjamin17dc94e2018-08-10 09:05:20 -0500553if(UNIX AND NOT APPLE AND NOT ANDROID)
554 set(HANDSHAKER_ARGS "-handshaker-path" $<TARGET_FILE:handshaker>)
555endif()
556
David Benjamin301afaf2015-10-14 21:34:40 -0400557add_custom_target(
558 run_tests
559 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
560 ${CMAKE_BINARY_DIR}
Adam Langleyd5c72c82016-09-23 16:43:17 -0700561 COMMAND cd ssl/test/runner &&
562 ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
David Benjamin17dc94e2018-08-10 09:05:20 -0500563 ${HANDSHAKER_ARGS} ${RUNNER_ARGS}
David Benjamin301afaf2015-10-14 21:34:40 -0400564 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
Steven Valdeze5388e02018-08-01 16:54:48 -0400565 DEPENDS all_tests bssl_shim handshaker
David Benjamin301afaf2015-10-14 21:34:40 -0400566 ${MAYBE_USES_TERMINAL})