blob: c421e88959885b568541639e2d28e0a85c63bb19 [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
12enable_language(C)
13enable_language(CXX)
Adam Langley95c29f32014-06-20 12:00:00 -070014
Adam Langley843ab662015-04-28 17:46:58 -070015if(ANDROID)
16 # Android-NDK CMake files reconfigure the path and so Go and Perl won't be
17 # found. However, ninja will still find them in $PATH if we just name them.
Tamas Berghammer5693e422016-05-19 14:28:14 +010018 if(NOT PERL_EXECUTABLE)
19 set(PERL_EXECUTABLE "perl")
20 endif()
21 if(NOT GO_EXECUTABLE)
22 set(GO_EXECUTABLE "go")
23 endif()
Adam Langley843ab662015-04-28 17:46:58 -070024else()
25 find_package(Perl REQUIRED)
26 find_program(GO_EXECUTABLE go)
27endif()
David Benjamin3ce3c362015-02-23 13:06:19 -050028
David Benjamind27eda02015-03-05 01:19:27 -050029if (NOT GO_EXECUTABLE)
30 message(FATAL_ERROR "Could not find Go")
31endif()
32
Brian Smith1d75c8b2015-01-23 17:25:44 -080033if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Adam Langley01867872016-06-30 14:16:59 -070034 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 -040035 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
36 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof")
Adam Langley5c38c052017-04-28 14:47:06 -070037 else()
38 # GCC (at least 4.8.4) has a bug where it'll find unreachable free() calls
39 # and declare that the code is trying to free a stack pointer.
40 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-free-nonheap-object")
David Benjamin9b7d8362016-08-29 14:59:31 -040041 endif()
Adam Langley01867872016-06-30 14:16:59 -070042 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
David Benjamin37e01b32016-06-13 13:42:04 -040043 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${C_CXX_FLAGS} -Wmissing-declarations")
David Benjamin41b8ff22016-09-16 15:56:53 -040044 # Clang's integerated assembler does not support debug symbols.
45 if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
46 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,-g")
47 endif()
Adam Langley95c29f32014-06-20 12:00:00 -070048elseif(MSVC)
Brian Smithefed2212015-01-28 16:20:02 -080049 set(MSVC_DISABLED_WARNINGS_LIST
David Benjamin96628432017-01-19 19:05:47 -050050 "C4061" # enumerator 'identifier' in switch of enum 'enumeration' is not
51 # explicitly handled by a case label
52 # Disable this because it flags even when there is a default.
Brian Smithefed2212015-01-28 16:20:02 -080053 "C4100" # 'exarg' : unreferenced formal parameter
54 "C4127" # conditional expression is constant
55 "C4200" # nonstandard extension used : zero-sized array in
56 # struct/union.
David Benjaminffb11072016-11-13 10:32:10 +090057 "C4204" # nonstandard extension used: non-constant aggregate initializer
58 "C4221" # nonstandard extension used : 'identifier' : cannot be
59 # initialized using address of automatic variable
Brian Smithefed2212015-01-28 16:20:02 -080060 "C4242" # 'function' : conversion from 'int' to 'uint8_t',
61 # possible loss of data
62 "C4244" # 'function' : conversion from 'int' to 'uint8_t',
63 # possible loss of data
64 "C4245" # 'initializing' : conversion from 'long' to
65 # 'unsigned long', signed/unsigned mismatch
David Benjamin3673be72015-02-11 15:12:05 -050066 "C4267" # conversion from 'size_t' to 'int', possible loss of data
David Benjamin3673be72015-02-11 15:12:05 -050067 "C4371" # layout of class may have changed from a previous version of the
68 # compiler due to better packing of member '...'
69 "C4388" # signed/unsigned mismatch
Brian Smithefed2212015-01-28 16:20:02 -080070 "C4296" # '>=' : expression is always true
71 "C4350" # behavior change: 'std::_Wrap_alloc...'
72 "C4365" # '=' : conversion from 'size_t' to 'int',
73 # signed/unsigned mismatch
74 "C4389" # '!=' : signed/unsigned mismatch
David Benjamin7acd6bc2016-05-02 12:57:01 -040075 "C4464" # relative include path contains '..'
Brian Smithefed2212015-01-28 16:20:02 -080076 "C4510" # 'argument' : default constructor could not be generated
77 "C4512" # 'argument' : assignment operator could not be generated
78 "C4514" # 'function': unreferenced inline function has been removed
79 "C4548" # expression before comma has no effect; expected expression with
80 # side-effect" caused by FD_* macros.
81 "C4610" # struct 'argument' can never be instantiated - user defined
82 # constructor required.
David Benjamin7acd6bc2016-05-02 12:57:01 -040083 "C4623" # default constructor was implicitly defined as deleted
David Benjamin3673be72015-02-11 15:12:05 -050084 "C4625" # copy constructor could not be generated because a base class
85 # copy constructor is inaccessible or deleted
86 "C4626" # assignment operator could not be generated because a base class
87 # assignment operator is inaccessible or deleted
David Benjamin96628432017-01-19 19:05:47 -050088 "C4668" # 'symbol' is not defined as a preprocessor macro, replacing with
89 # '0' for 'directives'
90 # Disable this because GTest uses it everywhere.
Brian Smithefed2212015-01-28 16:20:02 -080091 "C4706" # assignment within conditional expression
92 "C4710" # 'function': function not inlined
93 "C4711" # function 'function' selected for inline expansion
94 "C4800" # 'int' : forcing value to bool 'true' or 'false'
95 # (performance warning)
96 "C4820" # 'bytes' bytes padding added after construct 'member_name'
David Benjamin96628432017-01-19 19:05:47 -050097 "C5026" # move constructor was implicitly defined as deleted
David Benjamin7acd6bc2016-05-02 12:57:01 -040098 "C5027" # move assignment operator was implicitly defined as deleted
99 )
100 set(MSVC_LEVEL4_WARNINGS_LIST
101 # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
102 "C4265" # class has virtual functions, but destructor is not virtual
103 )
Brian Smithefed2212015-01-28 16:20:02 -0800104 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
105 ${MSVC_DISABLED_WARNINGS_LIST})
David Benjamin7acd6bc2016-05-02 12:57:01 -0400106 string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
107 ${MSVC_LEVEL4_WARNINGS_LIST})
Brian Smithdc6c1b82016-01-17 22:21:42 -1000108 set(CMAKE_C_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
109 set(CMAKE_CXX_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
David Benjamin72088222016-08-04 19:09:45 -0400110 set(CMAKE_ASM_NASM_FLAGS "-g cv8")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800111 add_definitions(-D_HAS_EXCEPTIONS=0)
Brian Smitha87de9b2015-01-28 20:34:47 -0800112 add_definitions(-DWIN32_LEAN_AND_MEAN)
David Benjamin0e434b92015-04-02 13:20:01 -0400113 add_definitions(-DNOMINMAX)
nmittlerf0322b22016-05-19 08:49:59 -0700114 add_definitions(-D_CRT_SECURE_NO_WARNINGS) # Allow use of fopen
Adam Langley95c29f32014-06-20 12:00:00 -0700115endif()
116
David Benjaminb826c0d2015-02-28 00:00:44 -0500117if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
Brian Smith1d75c8b2015-01-23 17:25:44 -0800118 CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800119 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
120 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
Adam Langley07100c62015-01-16 15:20:54 -0800121endif()
122
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400123if(CMAKE_COMPILER_IS_GNUCXX)
124 if ((CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.99") OR
125 CMAKE_CXX_COMPILER_ID MATCHES "Clang")
126 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
127 else()
128 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
129 endif()
130endif()
131
132# pthread_rwlock_t requires a feature flag.
133if(NOT WIN32)
134 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
Adam Langley6f2e7332015-05-15 12:01:29 -0700135endif()
136
Adam Langley9a4beb82015-11-09 13:57:26 -0800137if(FUZZ)
Alessandro Ghedinib6f69272016-09-28 22:14:01 +0100138 if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
139 message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
Adam Langley9a4beb82015-11-09 13:57:26 -0800140 endif()
141
David Benjaminec978dd2016-11-04 18:59:33 -0400142 add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
143 set(RUNNER_ARGS "-deterministic")
144
145 if(NOT NO_FUZZER_MODE)
146 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
147 set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
148 endif()
David Benjaminbc5b2a22016-03-01 22:57:32 -0500149
David Benjamin08ab59b2017-05-10 12:02:58 -0400150 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,trace-pc-guard")
151 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,trace-pc-guard")
Adam Langley9a4beb82015-11-09 13:57:26 -0800152 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
Adam Langley7104cc92015-11-10 15:00:51 -0800153 link_directories(.)
Adam Langley9a4beb82015-11-09 13:57:26 -0800154endif()
155
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700156add_definitions(-DBORINGSSL_IMPLEMENTATION)
157
David Benjamin507c1ee2015-01-28 00:50:21 -0500158if (BUILD_SHARED_LIBS)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800159 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
160 # Enable position-independent code globally. This is needed because
161 # some library targets are OBJECT libraries.
162 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
David Benjamin507c1ee2015-01-28 00:50:21 -0500163endif()
164
Adam Langley1bcd10c2016-12-16 10:48:23 -0800165if (MSAN)
166 if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
167 message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
168 endif()
169
170 if (ASAN)
171 message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
172 endif()
173
174 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
175 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
176 set(OPENSSL_NO_ASM "1")
177endif()
178
179if (ASAN)
180 if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
181 message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
182 endif()
183
David Benjamin0d3c9632017-02-28 14:58:00 -0500184 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
185 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 -0800186 set(OPENSSL_NO_ASM "1")
187endif()
188
David Benjamind035ab32016-12-27 12:15:56 -0500189if (GCOV)
190 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
191 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
192endif()
193
David Benjaminaff72a32017-04-06 23:26:04 -0400194if(FIPS)
195 add_definitions(-DBORINGSSL_FIPS)
196endif()
197
198# CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
199# architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
200# alone, and expects all architecture-specific logic to be conditioned within
201# the source files rather than the build. This does not work for our assembly
202# files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
203# builds.
204if (NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
205 list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
206 if (NOT ${NUM_ARCHES} EQUAL 1)
207 message(FATAL_ERROR "Universal binaries not supported.")
208 endif()
209 list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
210endif()
211
David Benjamin27b08e92015-05-04 15:16:57 -0400212if (OPENSSL_NO_ASM)
213 add_definitions(-DOPENSSL_NO_ASM)
214 set(ARCH "generic")
David Benjaminaff72a32017-04-06 23:26:04 -0400215elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
216 set(ARCH "x86_64")
217elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
218 set(ARCH "x86_64")
219elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
220 # cmake reports AMD64 on Windows, but we might be building for 32-bit.
221 if (CMAKE_CL_64)
222 set(ARCH "x86_64")
223 else()
224 set(ARCH "x86")
225 endif()
226elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
227 set(ARCH "x86")
228elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
229 set(ARCH "x86")
230elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
231 set(ARCH "x86")
232elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
233 set(ARCH "aarch64")
234elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
235 set(ARCH "aarch64")
236elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm*")
237 set(ARCH "arm")
238elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "mips")
239 # Just to avoid the “unknown processor” error.
240 set(ARCH "generic")
241elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le")
242 set(ARCH "ppc64le")
243else()
244 message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
David Benjamin27b08e92015-05-04 15:16:57 -0400245endif()
246
David Benjaminaff72a32017-04-06 23:26:04 -0400247if (ANDROID AND ${ARCH} STREQUAL "arm")
248 # The Android-NDK CMake files somehow fail to set the -march flag for
249 # assembly files. Without this flag, the compiler believes that it's
250 # building for ARMv5.
251 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -march=${CMAKE_SYSTEM_PROCESSOR}")
252endif()
253
254if (${ARCH} STREQUAL "x86" AND APPLE)
255 # With CMake 2.8.x, ${CMAKE_SYSTEM_PROCESSOR} evalutes to i386 on OS X,
256 # but clang defaults to 64-bit builds on OS X unless otherwise told.
257 # Set ARCH to x86_64 so clang and CMake agree. This is fixed in CMake 3.
258 set(ARCH "x86_64")
Adam Langleyfd499932017-04-04 14:21:43 -0700259endif()
260
David Benjamin96628432017-01-19 19:05:47 -0500261# Add minimal googletest targets. The provided one has many side-effects, and
262# googletest has a very straightforward build.
263add_library(gtest third_party/googletest/src/gtest-all.cc)
264target_include_directories(gtest PRIVATE third_party/googletest)
David Benjamin96628432017-01-19 19:05:47 -0500265
266include_directories(third_party/googletest/include)
267
David Benjamin301afaf2015-10-14 21:34:40 -0400268# Declare a dummy target to build all unit tests. Test targets should inject
269# themselves as dependencies next to the target definition.
270add_custom_target(all_tests)
271
Adam Langley95c29f32014-06-20 12:00:00 -0700272add_subdirectory(crypto)
273add_subdirectory(ssl)
274add_subdirectory(ssl/test)
Adam Langley58e44992017-04-28 16:45:49 -0700275add_subdirectory(fipsoracle)
Adam Langley95c29f32014-06-20 12:00:00 -0700276add_subdirectory(tool)
Adam Langleyc004dfc2015-02-03 10:45:12 -0800277add_subdirectory(decrepit)
David Benjamin301afaf2015-10-14 21:34:40 -0400278
Adam Langley9a4beb82015-11-09 13:57:26 -0800279if(FUZZ)
280 add_subdirectory(fuzz)
281endif()
282
David Benjamin301afaf2015-10-14 21:34:40 -0400283if (NOT ${CMAKE_VERSION} VERSION_LESS "3.2")
284 # USES_TERMINAL is only available in CMake 3.2 or later.
285 set(MAYBE_USES_TERMINAL USES_TERMINAL)
286endif()
287
288add_custom_target(
289 run_tests
290 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
291 ${CMAKE_BINARY_DIR}
Adam Langleyd5c72c82016-09-23 16:43:17 -0700292 COMMAND cd ssl/test/runner &&
293 ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
294 ${RUNNER_ARGS}
David Benjamin301afaf2015-10-14 21:34:40 -0400295 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
296 DEPENDS all_tests bssl_shim
297 ${MAYBE_USES_TERMINAL})