blob: 95e2590f315f5a204d78b31c68d1ffa6710da392 [file] [log] [blame]
David Benjaminf31229b2017-01-25 14:08:15 -05001cmake_minimum_required (VERSION 2.8.11)
Adam Langleyd9e397b2015-01-22 14:27:53 -08002
Robert Sloancd79cde2017-12-11 09:06:12 -08003# 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 Benjamin4969cc92016-04-22 15:02:23 -04009# Defer enabling C and CXX languages.
10project (BoringSSL NONE)
11
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
Robert Sloan8ff03552017-06-14 12:40:58 -070018include(sources.cmake)
19
David Benjamin4969cc92016-04-22 15:02:23 -040020enable_language(C)
21enable_language(CXX)
Adam Langleyd9e397b2015-01-22 14:27:53 -080022
Adam Langleye9ada862015-05-11 17:20:37 -070023if(ANDROID)
24 # Android-NDK CMake files reconfigure the path and so Go and Perl won't be
25 # found. However, ninja will still find them in $PATH if we just name them.
David Benjamind316cba2016-06-02 16:17:39 -040026 if(NOT PERL_EXECUTABLE)
27 set(PERL_EXECUTABLE "perl")
28 endif()
29 if(NOT GO_EXECUTABLE)
30 set(GO_EXECUTABLE "go")
31 endif()
Adam Langleye9ada862015-05-11 17:20:37 -070032else()
33 find_package(Perl REQUIRED)
34 find_program(GO_EXECUTABLE go)
35endif()
36
37if (NOT GO_EXECUTABLE)
38 message(FATAL_ERROR "Could not find Go")
39endif()
40
Robert Sloanfe7cd212017-08-07 09:03:39 -070041if (BORINGSSL_ALLOW_CXX_RUNTIME)
42 add_definitions(-DBORINGSSL_ALLOW_CXX_RUNTIME)
43endif()
44
Robert Sloan73fa5d62017-10-09 13:53:06 -070045if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
46 set(CLANG 1)
47endif()
Robert Sloan2e9e66a2017-09-25 09:08:29 -070048
Robert Sloan73fa5d62017-10-09 13:53:06 -070049if(CMAKE_COMPILER_IS_GNUCXX OR CLANG)
50 # Note clang-cl is odd and sets both CLANG and MSVC. We base our configuration
51 # primarily on our normal Clang one because the MSVC one is mostly
52 # suppressions for an overaggressive -Wall.
53 set(C_CXX_FLAGS "-Wall -Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings")
54 if(MSVC)
55 # clang-cl sets different default warnings than clang.
56 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-unused-parameter -fmsc-version=1900")
57 # googletest suppresses warning C4996 via a pragma, but clang-cl does not
58 # honor it. Suppress it here to compensate. See https://crbug.com/772117.
59 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-deprecated-declarations")
60 else()
61 set(C_CXX_FLAGS "${C_CXX_FLAGS} -ggdb -fvisibility=hidden -fno-common")
62 endif()
63
64 if(CLANG)
Robert Sloanfe7cd212017-08-07 09:03:39 -070065 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof -fcolor-diagnostics")
Robert Sloan8ff03552017-06-14 12:40:58 -070066 else()
67 # GCC (at least 4.8.4) has a bug where it'll find unreachable free() calls
68 # and declare that the code is trying to free a stack pointer.
69 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-free-nonheap-object")
David Benjaminf0c4a6c2016-08-11 13:26:41 -040070 endif()
Robert Sloan2e9e66a2017-09-25 09:08:29 -070071
Robert Sloancd79cde2017-12-11 09:06:12 -080072 if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang" AND
73 NOT "6.0.0" VERSION_GREATER CMAKE_C_COMPILER_VERSION)
74 # Clang's -Wtautological-constant-compare is far too aggressive and does not
75 # account for, say, wanting the same code to work on both 32-bit and 64-bit
76 # platforms.
77 #
78 # Note "Clang" and "AppleClang" version differently, so we check for an
79 # exact match on the COMPILER_ID. As of writing, the warning is not in any
80 # release of AppleClang yet.
81 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-tautological-constant-compare -Wtautological-constant-out-of-range-compare")
82 endif()
83
Robert Sloan73fa5d62017-10-09 13:53:06 -070084 if(CLANG OR NOT "7.0.0" VERSION_GREATER CMAKE_C_COMPILER_VERSION)
Robert Sloan2e9e66a2017-09-25 09:08:29 -070085 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wimplicit-fallthrough")
86 endif()
87
David Benjaminc895d6b2016-08-11 13:26:41 -040088 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
Robert Sloan73fa5d62017-10-09 13:53:06 -070089 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_CXX_FLAGS} -Wmissing-declarations")
Robert Sloanfe7cd212017-08-07 09:03:39 -070090
Robert Sloan73fa5d62017-10-09 13:53:06 -070091 if(NOT MSVC)
92 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Robert Sloan29c1d2c2017-10-30 14:10:28 -070093 if(APPLE)
94 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
95 endif()
Robert Sloan73fa5d62017-10-09 13:53:06 -070096 if(NOT BORINGSSL_ALLOW_CXX_RUNTIME)
97 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
98 endif()
Robert Sloanfe7cd212017-08-07 09:03:39 -070099 endif()
100
Robert Sloana12bf462017-07-17 07:08:26 -0700101 # In GCC, -Wmissing-declarations is the C++ spelling of -Wmissing-prototypes
102 # and using the wrong one is an error. In Clang, -Wmissing-prototypes is the
103 # spelling for both and -Wmissing-declarations is some other warning.
104 #
105 # https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Warning-Options.html#Warning-Options
106 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-prototypes
107 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-declarations
Robert Sloan73fa5d62017-10-09 13:53:06 -0700108 if(CLANG)
Robert Sloan2e9e66a2017-09-25 09:08:29 -0700109 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-prototypes")
Robert Sloana12bf462017-07-17 07:08:26 -0700110 endif()
Robert Sloan73fa5d62017-10-09 13:53:06 -0700111
112 if(CMAKE_COMPILER_IS_GNUCXX AND "4.8" VERSION_GREATER CMAKE_C_COMPILER_VERSION)
113 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-array-bounds")
114 endif()
115
Adam Langleyd9e397b2015-01-22 14:27:53 -0800116elseif(MSVC)
117 set(MSVC_DISABLED_WARNINGS_LIST
David Benjaminf31229b2017-01-25 14:08:15 -0500118 "C4061" # enumerator 'identifier' in switch of enum 'enumeration' is not
119 # explicitly handled by a case label
120 # Disable this because it flags even when there is a default.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800121 "C4100" # 'exarg' : unreferenced formal parameter
122 "C4127" # conditional expression is constant
123 "C4200" # nonstandard extension used : zero-sized array in
124 # struct/union.
Steven Valdez909b19f2016-11-21 15:35:44 -0500125 "C4204" # nonstandard extension used: non-constant aggregate initializer
126 "C4221" # nonstandard extension used : 'identifier' : cannot be
127 # initialized using address of automatic variable
Adam Langleyd9e397b2015-01-22 14:27:53 -0800128 "C4242" # 'function' : conversion from 'int' to 'uint8_t',
129 # possible loss of data
130 "C4244" # 'function' : conversion from 'int' to 'uint8_t',
131 # possible loss of data
Adam Langleye9ada862015-05-11 17:20:37 -0700132 "C4267" # conversion from 'size_t' to 'int', possible loss of data
133 "C4371" # layout of class may have changed from a previous version of the
134 # compiler due to better packing of member '...'
135 "C4388" # signed/unsigned mismatch
Adam Langleyd9e397b2015-01-22 14:27:53 -0800136 "C4296" # '>=' : expression is always true
137 "C4350" # behavior change: 'std::_Wrap_alloc...'
138 "C4365" # '=' : conversion from 'size_t' to 'int',
139 # signed/unsigned mismatch
140 "C4389" # '!=' : signed/unsigned mismatch
David Benjamin9aaebef2016-04-22 15:02:23 -0400141 "C4464" # relative include path contains '..'
Adam Langleyd9e397b2015-01-22 14:27:53 -0800142 "C4510" # 'argument' : default constructor could not be generated
143 "C4512" # 'argument' : assignment operator could not be generated
144 "C4514" # 'function': unreferenced inline function has been removed
145 "C4548" # expression before comma has no effect; expected expression with
146 # side-effect" caused by FD_* macros.
147 "C4610" # struct 'argument' can never be instantiated - user defined
148 # constructor required.
David Benjamin9aaebef2016-04-22 15:02:23 -0400149 "C4623" # default constructor was implicitly defined as deleted
Adam Langleye9ada862015-05-11 17:20:37 -0700150 "C4625" # copy constructor could not be generated because a base class
151 # copy constructor is inaccessible or deleted
152 "C4626" # assignment operator could not be generated because a base class
153 # assignment operator is inaccessible or deleted
David Benjaminf31229b2017-01-25 14:08:15 -0500154 "C4668" # 'symbol' is not defined as a preprocessor macro, replacing with
155 # '0' for 'directives'
156 # Disable this because GTest uses it everywhere.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800157 "C4706" # assignment within conditional expression
158 "C4710" # 'function': function not inlined
159 "C4711" # function 'function' selected for inline expansion
160 "C4800" # 'int' : forcing value to bool 'true' or 'false'
161 # (performance warning)
162 "C4820" # 'bytes' bytes padding added after construct 'member_name'
David Benjaminf31229b2017-01-25 14:08:15 -0500163 "C5026" # move constructor was implicitly defined as deleted
David Benjamin9aaebef2016-04-22 15:02:23 -0400164 "C5027" # move assignment operator was implicitly defined as deleted
165 )
166 set(MSVC_LEVEL4_WARNINGS_LIST
167 # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
168 "C4265" # class has virtual functions, but destructor is not virtual
169 )
Adam Langleyd9e397b2015-01-22 14:27:53 -0800170 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
171 ${MSVC_DISABLED_WARNINGS_LIST})
David Benjamin9aaebef2016-04-22 15:02:23 -0400172 string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
173 ${MSVC_LEVEL4_WARNINGS_LIST})
David Benjamin4969cc92016-04-22 15:02:23 -0400174 set(CMAKE_C_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
175 set(CMAKE_CXX_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
Robert Sloan8f860b12017-08-28 07:37:06 -0700176endif()
177
178if(WIN32)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800179 add_definitions(-D_HAS_EXCEPTIONS=0)
180 add_definitions(-DWIN32_LEAN_AND_MEAN)
Adam Langleye9ada862015-05-11 17:20:37 -0700181 add_definitions(-DNOMINMAX)
Robert Sloane56da3e2017-06-26 08:26:42 -0700182 # Allow use of fopen.
183 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
184 # VS 2017 and higher supports STL-only warning suppressions.
185 add_definitions("-D_STL_EXTRA_DISABLED_WARNINGS=4774 4987")
Adam Langleyd9e397b2015-01-22 14:27:53 -0800186endif()
187
Adam Langleye9ada862015-05-11 17:20:37 -0700188if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
Robert Sloan73fa5d62017-10-09 13:53:06 -0700189 CLANG)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800190 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
191 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
192endif()
193
David Benjamin6e899c72016-06-09 18:02:18 -0400194if(CMAKE_COMPILER_IS_GNUCXX)
Robert Sloan73fa5d62017-10-09 13:53:06 -0700195 if ((CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.99") OR CLANG)
David Benjamin6e899c72016-06-09 18:02:18 -0400196 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
197 else()
198 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
199 endif()
200endif()
201
202# pthread_rwlock_t requires a feature flag.
203if(NOT WIN32)
204 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
Adam Langleyf4e42722015-06-04 17:45:09 -0700205endif()
206
Adam Langleyfad63272015-11-12 12:15:39 -0800207if(FUZZ)
Robert Sloan73fa5d62017-10-09 13:53:06 -0700208 if(NOT CLANG)
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400209 message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
Adam Langleyfad63272015-11-12 12:15:39 -0800210 endif()
211
Steven Valdez909b19f2016-11-21 15:35:44 -0500212 add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
213 set(RUNNER_ARGS "-deterministic")
214
215 if(NOT NO_FUZZER_MODE)
216 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
217 set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
218 endif()
David Benjamin4969cc92016-04-22 15:02:23 -0400219
Robert Sloan8ff03552017-06-14 12:40:58 -0700220 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,trace-pc-guard")
221 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,trace-pc-guard")
Adam Langleyfad63272015-11-12 12:15:39 -0800222 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
223 link_directories(.)
224endif()
225
Adam Langleyd9e397b2015-01-22 14:27:53 -0800226add_definitions(-DBORINGSSL_IMPLEMENTATION)
227
228if (BUILD_SHARED_LIBS)
229 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
230 # Enable position-independent code globally. This is needed because
231 # some library targets are OBJECT libraries.
232 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
233endif()
234
Robert Sloan69939df2017-01-09 10:53:07 -0800235if (MSAN)
Robert Sloan73fa5d62017-10-09 13:53:06 -0700236 if(NOT CLANG)
Robert Sloan69939df2017-01-09 10:53:07 -0800237 message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
238 endif()
239
240 if (ASAN)
241 message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
242 endif()
243
244 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
245 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
246 set(OPENSSL_NO_ASM "1")
247endif()
248
249if (ASAN)
Robert Sloan73fa5d62017-10-09 13:53:06 -0700250 if(NOT CLANG)
Robert Sloan69939df2017-01-09 10:53:07 -0800251 message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
252 endif()
253
Robert Sloan7d422bc2017-03-06 10:04:29 -0800254 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
255 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
Robert Sloan69939df2017-01-09 10:53:07 -0800256 set(OPENSSL_NO_ASM "1")
257endif()
258
Robert Sloan99319a12017-11-27 10:32:46 -0800259if(CFI)
260 if(NOT CLANG)
261 message(FATAL_ERROR "Cannot enable CFI unless using Clang")
262 endif()
263
264 # TODO(crbug.com/785442): Remove -fsanitize-cfi-icall-generalize-pointers.
265 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -fsanitize-cfi-icall-generalize-pointers -flto")
266 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -fsanitize-cfi-icall-generalize-pointers -flto")
267 # We use Chromium's copy of clang, which requires -fuse-ld=lld if building
268 # with -flto. That, in turn, can't handle -ggdb.
269 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
270 string(REPLACE "-ggdb" "-g" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
271 string(REPLACE "-ggdb" "-g" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
272 # -flto causes object files to contain LLVM bitcode. Mixing those with
273 # assembly output in the same static library breaks the linker.
274 set(OPENSSL_NO_ASM "1")
275endif()
276
Robert Sloan69939df2017-01-09 10:53:07 -0800277if (GCOV)
278 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
279 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
280endif()
281
Robert Sloan572a4e22017-04-17 10:52:19 -0700282if(FIPS)
283 add_definitions(-DBORINGSSL_FIPS)
Robert Sloan8ff03552017-06-14 12:40:58 -0700284 if(FIPS_BREAK_TEST)
285 add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
286 endif()
287 # Delocate does not work for ASan and MSan builds.
288 if(NOT ASAN AND NOT MSAN)
289 set(FIPS_DELOCATE "1")
290 endif()
Robert Sloan572a4e22017-04-17 10:52:19 -0700291endif()
292
293# CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
294# architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
295# alone, and expects all architecture-specific logic to be conditioned within
296# the source files rather than the build. This does not work for our assembly
297# files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
298# builds.
299if (NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
300 list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
301 if (NOT ${NUM_ARCHES} EQUAL 1)
302 message(FATAL_ERROR "Universal binaries not supported.")
303 endif()
304 list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
305endif()
306
Adam Langleye9ada862015-05-11 17:20:37 -0700307if (OPENSSL_NO_ASM)
308 add_definitions(-DOPENSSL_NO_ASM)
309 set(ARCH "generic")
Robert Sloan572a4e22017-04-17 10:52:19 -0700310elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
311 set(ARCH "x86_64")
312elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
313 set(ARCH "x86_64")
314elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
315 # cmake reports AMD64 on Windows, but we might be building for 32-bit.
316 if (CMAKE_CL_64)
317 set(ARCH "x86_64")
318 else()
319 set(ARCH "x86")
320 endif()
321elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
322 set(ARCH "x86")
323elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
324 set(ARCH "x86")
325elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
326 set(ARCH "x86")
327elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
328 set(ARCH "aarch64")
329elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
330 set(ARCH "aarch64")
331elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm*")
332 set(ARCH "arm")
333elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "mips")
334 # Just to avoid the “unknown processor” error.
335 set(ARCH "generic")
336elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le")
337 set(ARCH "ppc64le")
338else()
339 message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
340endif()
341
Robert Sloan55818102017-12-18 11:26:17 -0800342if (ANDROID AND NOT ANDROID_NDK_REVISION AND ${ARCH} STREQUAL "arm")
343 # The third-party Android-NDK CMake files somehow fail to set the -march flag
344 # for assembly files. Without this flag, the compiler believes that it's
Robert Sloan572a4e22017-04-17 10:52:19 -0700345 # building for ARMv5.
Robert Sloan55818102017-12-18 11:26:17 -0800346 set(CMAKE_ASM_FLAGS "-march=${CMAKE_SYSTEM_PROCESSOR} ${CMAKE_ASM_FLAGS}")
Robert Sloan572a4e22017-04-17 10:52:19 -0700347endif()
348
Robert Sloan29c1d2c2017-10-30 14:10:28 -0700349if (${ARCH} STREQUAL "x86" AND APPLE AND ${CMAKE_VERSION} VERSION_LESS "3.0")
Robert Sloan572a4e22017-04-17 10:52:19 -0700350 # With CMake 2.8.x, ${CMAKE_SYSTEM_PROCESSOR} evalutes to i386 on OS X,
351 # but clang defaults to 64-bit builds on OS X unless otherwise told.
352 # Set ARCH to x86_64 so clang and CMake agree. This is fixed in CMake 3.
353 set(ARCH "x86_64")
Adam Langleye9ada862015-05-11 17:20:37 -0700354endif()
355
David Benjaminf31229b2017-01-25 14:08:15 -0500356# Add minimal googletest targets. The provided one has many side-effects, and
357# googletest has a very straightforward build.
358add_library(gtest third_party/googletest/src/gtest-all.cc)
359target_include_directories(gtest PRIVATE third_party/googletest)
David Benjaminf31229b2017-01-25 14:08:15 -0500360
361include_directories(third_party/googletest/include)
362
Kenny Roote99801b2015-11-06 15:31:15 -0800363# Declare a dummy target to build all unit tests. Test targets should inject
364# themselves as dependencies next to the target definition.
365add_custom_target(all_tests)
366
Robert Sloan8ff03552017-06-14 12:40:58 -0700367add_custom_command(
368 OUTPUT crypto_test_data.cc
369 COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go ${CRYPTO_TEST_DATA} >
370 ${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc
371 DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
372 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
373
374add_library(crypto_test_data OBJECT crypto_test_data.cc)
375
Adam Langleyd9e397b2015-01-22 14:27:53 -0800376add_subdirectory(crypto)
Robert Sloanb1b54b82017-11-06 13:50:02 -0800377add_subdirectory(third_party/fiat)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800378add_subdirectory(ssl)
379add_subdirectory(ssl/test)
Robert Sloan8ff03552017-06-14 12:40:58 -0700380add_subdirectory(fipstools)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800381add_subdirectory(tool)
Adam Langleye9ada862015-05-11 17:20:37 -0700382add_subdirectory(decrepit)
Kenny Roote99801b2015-11-06 15:31:15 -0800383
Adam Langleyfad63272015-11-12 12:15:39 -0800384if(FUZZ)
Robert Sloan8ff03552017-06-14 12:40:58 -0700385 if(LIBFUZZER_FROM_DEPS)
386 file(GLOB LIBFUZZER_SOURCES "util/bot/libFuzzer/*.cpp")
387 add_library(Fuzzer STATIC ${LIBFUZZER_SOURCES})
388 # libFuzzer does not pass our aggressive warnings. It also must be built
389 # without -fsanitize-coverage options or clang crashes.
Robert Sloana12bf462017-07-17 07:08:26 -0700390 set_target_properties(Fuzzer PROPERTIES COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -Wno-missing-prototypes -fsanitize-coverage=0")
Robert Sloan8ff03552017-06-14 12:40:58 -0700391 endif()
392
Adam Langleyfad63272015-11-12 12:15:39 -0800393 add_subdirectory(fuzz)
394endif()
395
Kenny Roote99801b2015-11-06 15:31:15 -0800396if (NOT ${CMAKE_VERSION} VERSION_LESS "3.2")
397 # USES_TERMINAL is only available in CMake 3.2 or later.
398 set(MAYBE_USES_TERMINAL USES_TERMINAL)
399endif()
400
401add_custom_target(
402 run_tests
403 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
404 ${CMAKE_BINARY_DIR}
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400405 COMMAND cd ssl/test/runner &&
406 ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
407 ${RUNNER_ARGS}
Kenny Roote99801b2015-11-06 15:31:15 -0800408 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
409 DEPENDS all_tests bssl_shim
410 ${MAYBE_USES_TERMINAL})