blob: d5f9ed99c6ac0c9cd65d56665ffcd7f8a687f119 [file] [log] [blame]
David Benjamin96628432017-01-19 19:05:47 -05001cmake_minimum_required (VERSION 2.8.11)
Adam Langley95c29f32014-06-20 12:00:00 -07002
David Benjamin6b34d542016-02-05 21:58:39 -05003# Defer enabling C and CXX languages.
4project (BoringSSL NONE)
5
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
Adam Langley843ab662015-04-28 17:46:58 -070017if(ANDROID)
18 # Android-NDK CMake files reconfigure the path and so Go and Perl won't be
19 # found. However, ninja will still find them in $PATH if we just name them.
Tamas Berghammer5693e422016-05-19 14:28:14 +010020 if(NOT PERL_EXECUTABLE)
21 set(PERL_EXECUTABLE "perl")
22 endif()
23 if(NOT GO_EXECUTABLE)
24 set(GO_EXECUTABLE "go")
25 endif()
Adam Langley843ab662015-04-28 17:46:58 -070026else()
27 find_package(Perl REQUIRED)
28 find_program(GO_EXECUTABLE go)
29endif()
David Benjamin3ce3c362015-02-23 13:06:19 -050030
David Benjamind27eda02015-03-05 01:19:27 -050031if (NOT GO_EXECUTABLE)
32 message(FATAL_ERROR "Could not find Go")
33endif()
34
Brian Smith1d75c8b2015-01-23 17:25:44 -080035if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Adam Langley01867872016-06-30 14:16:59 -070036 set(C_CXX_FLAGS "-Wall -Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings -ggdb -fvisibility=hidden -fno-common")
David Benjamin9b7d8362016-08-29 14:59:31 -040037 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
38 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof")
Adam Langley5c38c052017-04-28 14:47:06 -070039 else()
40 # GCC (at least 4.8.4) has a bug where it'll find unreachable free() calls
41 # and declare that the code is trying to free a stack pointer.
42 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-free-nonheap-object")
David Benjamin9b7d8362016-08-29 14:59:31 -040043 endif()
Adam Langley01867872016-06-30 14:16:59 -070044 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
David Benjamin37e01b32016-06-13 13:42:04 -040045 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${C_CXX_FLAGS} -Wmissing-declarations")
Adam Langley95c29f32014-06-20 12:00:00 -070046elseif(MSVC)
Brian Smithefed2212015-01-28 16:20:02 -080047 set(MSVC_DISABLED_WARNINGS_LIST
David Benjamin96628432017-01-19 19:05:47 -050048 "C4061" # enumerator 'identifier' in switch of enum 'enumeration' is not
49 # explicitly handled by a case label
50 # Disable this because it flags even when there is a default.
Brian Smithefed2212015-01-28 16:20:02 -080051 "C4100" # 'exarg' : unreferenced formal parameter
52 "C4127" # conditional expression is constant
53 "C4200" # nonstandard extension used : zero-sized array in
54 # struct/union.
David Benjaminffb11072016-11-13 10:32:10 +090055 "C4204" # nonstandard extension used: non-constant aggregate initializer
56 "C4221" # nonstandard extension used : 'identifier' : cannot be
57 # initialized using address of automatic variable
Brian Smithefed2212015-01-28 16:20:02 -080058 "C4242" # 'function' : conversion from 'int' to 'uint8_t',
59 # possible loss of data
60 "C4244" # 'function' : conversion from 'int' to 'uint8_t',
61 # possible loss of data
62 "C4245" # 'initializing' : conversion from 'long' to
63 # 'unsigned long', signed/unsigned mismatch
David Benjamin3673be72015-02-11 15:12:05 -050064 "C4267" # conversion from 'size_t' to 'int', possible loss of data
David Benjamin3673be72015-02-11 15:12:05 -050065 "C4371" # layout of class may have changed from a previous version of the
66 # compiler due to better packing of member '...'
67 "C4388" # signed/unsigned mismatch
Brian Smithefed2212015-01-28 16:20:02 -080068 "C4296" # '>=' : expression is always true
69 "C4350" # behavior change: 'std::_Wrap_alloc...'
70 "C4365" # '=' : conversion from 'size_t' to 'int',
71 # signed/unsigned mismatch
72 "C4389" # '!=' : signed/unsigned mismatch
David Benjamin7acd6bc2016-05-02 12:57:01 -040073 "C4464" # relative include path contains '..'
Brian Smithefed2212015-01-28 16:20:02 -080074 "C4510" # 'argument' : default constructor could not be generated
75 "C4512" # 'argument' : assignment operator could not be generated
76 "C4514" # 'function': unreferenced inline function has been removed
77 "C4548" # expression before comma has no effect; expected expression with
78 # side-effect" caused by FD_* macros.
79 "C4610" # struct 'argument' can never be instantiated - user defined
80 # constructor required.
David Benjamin7acd6bc2016-05-02 12:57:01 -040081 "C4623" # default constructor was implicitly defined as deleted
David Benjamin3673be72015-02-11 15:12:05 -050082 "C4625" # copy constructor could not be generated because a base class
83 # copy constructor is inaccessible or deleted
84 "C4626" # assignment operator could not be generated because a base class
85 # assignment operator is inaccessible or deleted
David Benjamin96628432017-01-19 19:05:47 -050086 "C4668" # 'symbol' is not defined as a preprocessor macro, replacing with
87 # '0' for 'directives'
88 # Disable this because GTest uses it everywhere.
Brian Smithefed2212015-01-28 16:20:02 -080089 "C4706" # assignment within conditional expression
90 "C4710" # 'function': function not inlined
91 "C4711" # function 'function' selected for inline expansion
92 "C4800" # 'int' : forcing value to bool 'true' or 'false'
93 # (performance warning)
94 "C4820" # 'bytes' bytes padding added after construct 'member_name'
David Benjamin96628432017-01-19 19:05:47 -050095 "C5026" # move constructor was implicitly defined as deleted
David Benjamin7acd6bc2016-05-02 12:57:01 -040096 "C5027" # move assignment operator was implicitly defined as deleted
97 )
98 set(MSVC_LEVEL4_WARNINGS_LIST
99 # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
100 "C4265" # class has virtual functions, but destructor is not virtual
101 )
Brian Smithefed2212015-01-28 16:20:02 -0800102 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
103 ${MSVC_DISABLED_WARNINGS_LIST})
David Benjamin7acd6bc2016-05-02 12:57:01 -0400104 string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
105 ${MSVC_LEVEL4_WARNINGS_LIST})
Brian Smithdc6c1b82016-01-17 22:21:42 -1000106 set(CMAKE_C_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
107 set(CMAKE_CXX_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
David Benjamin72088222016-08-04 19:09:45 -0400108 set(CMAKE_ASM_NASM_FLAGS "-g cv8")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800109 add_definitions(-D_HAS_EXCEPTIONS=0)
Brian Smitha87de9b2015-01-28 20:34:47 -0800110 add_definitions(-DWIN32_LEAN_AND_MEAN)
David Benjamin0e434b92015-04-02 13:20:01 -0400111 add_definitions(-DNOMINMAX)
nmittlerf0322b22016-05-19 08:49:59 -0700112 add_definitions(-D_CRT_SECURE_NO_WARNINGS) # Allow use of fopen
Adam Langley95c29f32014-06-20 12:00:00 -0700113endif()
114
David Benjaminb826c0d2015-02-28 00:00:44 -0500115if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
Brian Smith1d75c8b2015-01-23 17:25:44 -0800116 CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800117 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
118 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
Adam Langley07100c62015-01-16 15:20:54 -0800119endif()
120
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400121if(CMAKE_COMPILER_IS_GNUCXX)
122 if ((CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.99") OR
123 CMAKE_CXX_COMPILER_ID MATCHES "Clang")
124 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
125 else()
126 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
127 endif()
128endif()
129
130# pthread_rwlock_t requires a feature flag.
131if(NOT WIN32)
132 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
Adam Langley6f2e7332015-05-15 12:01:29 -0700133endif()
134
Adam Langley9a4beb82015-11-09 13:57:26 -0800135if(FUZZ)
Alessandro Ghedinib6f69272016-09-28 22:14:01 +0100136 if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
137 message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
Adam Langley9a4beb82015-11-09 13:57:26 -0800138 endif()
139
David Benjaminec978dd2016-11-04 18:59:33 -0400140 add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
141 set(RUNNER_ARGS "-deterministic")
142
143 if(NOT NO_FUZZER_MODE)
144 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
145 set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
146 endif()
David Benjaminbc5b2a22016-03-01 22:57:32 -0500147
David Benjamin08ab59b2017-05-10 12:02:58 -0400148 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,trace-pc-guard")
149 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,trace-pc-guard")
Adam Langley9a4beb82015-11-09 13:57:26 -0800150 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
Adam Langley7104cc92015-11-10 15:00:51 -0800151 link_directories(.)
Adam Langley9a4beb82015-11-09 13:57:26 -0800152endif()
153
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700154add_definitions(-DBORINGSSL_IMPLEMENTATION)
155
David Benjamin507c1ee2015-01-28 00:50:21 -0500156if (BUILD_SHARED_LIBS)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800157 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
158 # Enable position-independent code globally. This is needed because
159 # some library targets are OBJECT libraries.
160 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
David Benjamin507c1ee2015-01-28 00:50:21 -0500161endif()
162
Adam Langley1bcd10c2016-12-16 10:48:23 -0800163if (MSAN)
164 if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
165 message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
166 endif()
167
168 if (ASAN)
169 message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
170 endif()
171
172 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
173 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
174 set(OPENSSL_NO_ASM "1")
175endif()
176
177if (ASAN)
178 if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
179 message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
180 endif()
181
David Benjamin0d3c9632017-02-28 14:58:00 -0500182 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
183 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 -0800184 set(OPENSSL_NO_ASM "1")
185endif()
186
David Benjamind035ab32016-12-27 12:15:56 -0500187if (GCOV)
188 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
189 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
190endif()
191
David Benjaminaff72a32017-04-06 23:26:04 -0400192if(FIPS)
193 add_definitions(-DBORINGSSL_FIPS)
194endif()
195
196# CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
197# architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
198# alone, and expects all architecture-specific logic to be conditioned within
199# the source files rather than the build. This does not work for our assembly
200# files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
201# builds.
202if (NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
203 list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
204 if (NOT ${NUM_ARCHES} EQUAL 1)
205 message(FATAL_ERROR "Universal binaries not supported.")
206 endif()
207 list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
208endif()
209
David Benjamin27b08e92015-05-04 15:16:57 -0400210if (OPENSSL_NO_ASM)
211 add_definitions(-DOPENSSL_NO_ASM)
212 set(ARCH "generic")
David Benjaminaff72a32017-04-06 23:26:04 -0400213elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
214 set(ARCH "x86_64")
215elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
216 set(ARCH "x86_64")
217elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
218 # cmake reports AMD64 on Windows, but we might be building for 32-bit.
219 if (CMAKE_CL_64)
220 set(ARCH "x86_64")
221 else()
222 set(ARCH "x86")
223 endif()
224elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
225 set(ARCH "x86")
226elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
227 set(ARCH "x86")
228elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
229 set(ARCH "x86")
230elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
231 set(ARCH "aarch64")
232elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
233 set(ARCH "aarch64")
234elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm*")
235 set(ARCH "arm")
236elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "mips")
237 # Just to avoid the “unknown processor” error.
238 set(ARCH "generic")
239elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le")
240 set(ARCH "ppc64le")
241else()
242 message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
David Benjamin27b08e92015-05-04 15:16:57 -0400243endif()
244
David Benjaminaff72a32017-04-06 23:26:04 -0400245if (ANDROID AND ${ARCH} STREQUAL "arm")
246 # The Android-NDK CMake files somehow fail to set the -march flag for
247 # assembly files. Without this flag, the compiler believes that it's
248 # building for ARMv5.
249 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -march=${CMAKE_SYSTEM_PROCESSOR}")
250endif()
251
252if (${ARCH} STREQUAL "x86" AND APPLE)
253 # With CMake 2.8.x, ${CMAKE_SYSTEM_PROCESSOR} evalutes to i386 on OS X,
254 # but clang defaults to 64-bit builds on OS X unless otherwise told.
255 # Set ARCH to x86_64 so clang and CMake agree. This is fixed in CMake 3.
256 set(ARCH "x86_64")
Adam Langleyfd499932017-04-04 14:21:43 -0700257endif()
258
David Benjamin96628432017-01-19 19:05:47 -0500259# Add minimal googletest targets. The provided one has many side-effects, and
260# googletest has a very straightforward build.
261add_library(gtest third_party/googletest/src/gtest-all.cc)
262target_include_directories(gtest PRIVATE third_party/googletest)
David Benjamin96628432017-01-19 19:05:47 -0500263
264include_directories(third_party/googletest/include)
265
David Benjamin301afaf2015-10-14 21:34:40 -0400266# Declare a dummy target to build all unit tests. Test targets should inject
267# themselves as dependencies next to the target definition.
268add_custom_target(all_tests)
269
David Benjamin3ecd0a52017-05-19 15:26:18 -0400270add_custom_command(
271 OUTPUT crypto_test_data.cc
272 COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go ${CRYPTO_TEST_DATA} >
273 ${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc
274 DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
275 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
276
277add_library(crypto_test_data OBJECT crypto_test_data.cc)
278
Adam Langley95c29f32014-06-20 12:00:00 -0700279add_subdirectory(crypto)
280add_subdirectory(ssl)
281add_subdirectory(ssl/test)
Martin Kreichgauer118355c2017-05-12 15:34:45 -0700282add_subdirectory(fipstools)
Adam Langley95c29f32014-06-20 12:00:00 -0700283add_subdirectory(tool)
Adam Langleyc004dfc2015-02-03 10:45:12 -0800284add_subdirectory(decrepit)
David Benjamin301afaf2015-10-14 21:34:40 -0400285
Adam Langley9a4beb82015-11-09 13:57:26 -0800286if(FUZZ)
David Benjamin1e5cb822017-05-10 16:07:56 -0400287 if(LIBFUZZER_FROM_DEPS)
288 file(GLOB LIBFUZZER_SOURCES "util/bot/libFuzzer/*.cpp")
289 add_library(Fuzzer STATIC ${LIBFUZZER_SOURCES})
290 # libFuzzer does not pass our aggressive warnings. It also must be built
291 # without -fsanitize-coverage options or clang crashes.
292 set_target_properties(Fuzzer PROPERTIES COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -fsanitize-coverage=0")
293 endif()
294
Adam Langley9a4beb82015-11-09 13:57:26 -0800295 add_subdirectory(fuzz)
296endif()
297
David Benjamin301afaf2015-10-14 21:34:40 -0400298if (NOT ${CMAKE_VERSION} VERSION_LESS "3.2")
299 # USES_TERMINAL is only available in CMake 3.2 or later.
300 set(MAYBE_USES_TERMINAL USES_TERMINAL)
301endif()
302
303add_custom_target(
304 run_tests
305 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
306 ${CMAKE_BINARY_DIR}
Adam Langleyd5c72c82016-09-23 16:43:17 -0700307 COMMAND cd ssl/test/runner &&
308 ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
309 ${RUNNER_ARGS}
David Benjamin301afaf2015-10-14 21:34:40 -0400310 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
311 DEPENDS all_tests bssl_shim
312 ${MAYBE_USES_TERMINAL})