blob: 7b7f9343c5d8abd73a9b08f27cdbac017b7ae5eb [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")
David Benjamin4a8d1f32017-07-13 17:53:07 -040046 # In GCC, -Wmissing-declarations is the C++ spelling of -Wmissing-prototypes
47 # and using the wrong one is an error. In Clang, -Wmissing-prototypes is the
48 # spelling for both and -Wmissing-declarations is some other warning.
49 #
50 # https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Warning-Options.html#Warning-Options
51 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-prototypes
52 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-declarations
53 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
54 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-prototypes")
55 endif()
Adam Langley95c29f32014-06-20 12:00:00 -070056elseif(MSVC)
Brian Smithefed2212015-01-28 16:20:02 -080057 set(MSVC_DISABLED_WARNINGS_LIST
David Benjamin96628432017-01-19 19:05:47 -050058 "C4061" # enumerator 'identifier' in switch of enum 'enumeration' is not
59 # explicitly handled by a case label
60 # Disable this because it flags even when there is a default.
Brian Smithefed2212015-01-28 16:20:02 -080061 "C4100" # 'exarg' : unreferenced formal parameter
62 "C4127" # conditional expression is constant
63 "C4200" # nonstandard extension used : zero-sized array in
64 # struct/union.
David Benjaminffb11072016-11-13 10:32:10 +090065 "C4204" # nonstandard extension used: non-constant aggregate initializer
66 "C4221" # nonstandard extension used : 'identifier' : cannot be
67 # initialized using address of automatic variable
Brian Smithefed2212015-01-28 16:20:02 -080068 "C4242" # 'function' : conversion from 'int' to 'uint8_t',
69 # possible loss of data
70 "C4244" # 'function' : conversion from 'int' to 'uint8_t',
71 # possible loss of data
72 "C4245" # 'initializing' : conversion from 'long' to
73 # 'unsigned long', signed/unsigned mismatch
David Benjamin3673be72015-02-11 15:12:05 -050074 "C4267" # conversion from 'size_t' to 'int', possible loss of data
David Benjamin3673be72015-02-11 15:12:05 -050075 "C4371" # layout of class may have changed from a previous version of the
76 # compiler due to better packing of member '...'
77 "C4388" # signed/unsigned mismatch
Brian Smithefed2212015-01-28 16:20:02 -080078 "C4296" # '>=' : expression is always true
79 "C4350" # behavior change: 'std::_Wrap_alloc...'
80 "C4365" # '=' : conversion from 'size_t' to 'int',
81 # signed/unsigned mismatch
82 "C4389" # '!=' : signed/unsigned mismatch
David Benjamin7acd6bc2016-05-02 12:57:01 -040083 "C4464" # relative include path contains '..'
Brian Smithefed2212015-01-28 16:20:02 -080084 "C4510" # 'argument' : default constructor could not be generated
85 "C4512" # 'argument' : assignment operator could not be generated
86 "C4514" # 'function': unreferenced inline function has been removed
87 "C4548" # expression before comma has no effect; expected expression with
88 # side-effect" caused by FD_* macros.
89 "C4610" # struct 'argument' can never be instantiated - user defined
90 # constructor required.
David Benjamin7acd6bc2016-05-02 12:57:01 -040091 "C4623" # default constructor was implicitly defined as deleted
David Benjamin3673be72015-02-11 15:12:05 -050092 "C4625" # copy constructor could not be generated because a base class
93 # copy constructor is inaccessible or deleted
94 "C4626" # assignment operator could not be generated because a base class
95 # assignment operator is inaccessible or deleted
David Benjamin96628432017-01-19 19:05:47 -050096 "C4668" # 'symbol' is not defined as a preprocessor macro, replacing with
97 # '0' for 'directives'
98 # Disable this because GTest uses it everywhere.
Brian Smithefed2212015-01-28 16:20:02 -080099 "C4706" # assignment within conditional expression
100 "C4710" # 'function': function not inlined
101 "C4711" # function 'function' selected for inline expansion
102 "C4800" # 'int' : forcing value to bool 'true' or 'false'
103 # (performance warning)
104 "C4820" # 'bytes' bytes padding added after construct 'member_name'
David Benjamin96628432017-01-19 19:05:47 -0500105 "C5026" # move constructor was implicitly defined as deleted
David Benjamin7acd6bc2016-05-02 12:57:01 -0400106 "C5027" # move assignment operator was implicitly defined as deleted
107 )
108 set(MSVC_LEVEL4_WARNINGS_LIST
109 # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
110 "C4265" # class has virtual functions, but destructor is not virtual
111 )
Brian Smithefed2212015-01-28 16:20:02 -0800112 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
113 ${MSVC_DISABLED_WARNINGS_LIST})
David Benjamin7acd6bc2016-05-02 12:57:01 -0400114 string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
115 ${MSVC_LEVEL4_WARNINGS_LIST})
Brian Smithdc6c1b82016-01-17 22:21:42 -1000116 set(CMAKE_C_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
117 set(CMAKE_CXX_FLAGS "-Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
David Benjamin72088222016-08-04 19:09:45 -0400118 set(CMAKE_ASM_NASM_FLAGS "-g cv8")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800119 add_definitions(-D_HAS_EXCEPTIONS=0)
Brian Smitha87de9b2015-01-28 20:34:47 -0800120 add_definitions(-DWIN32_LEAN_AND_MEAN)
David Benjamin0e434b92015-04-02 13:20:01 -0400121 add_definitions(-DNOMINMAX)
David Benjamin9b6ff442017-06-15 20:44:30 -0400122 # Allow use of fopen.
123 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
124 # VS 2017 and higher supports STL-only warning suppressions.
125 add_definitions("-D_STL_EXTRA_DISABLED_WARNINGS=4774 4987")
Adam Langley95c29f32014-06-20 12:00:00 -0700126endif()
127
David Benjaminb826c0d2015-02-28 00:00:44 -0500128if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
Brian Smith1d75c8b2015-01-23 17:25:44 -0800129 CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Adam Langley4a0f0c42015-01-28 16:37:10 -0800130 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
131 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
Adam Langley07100c62015-01-16 15:20:54 -0800132endif()
133
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400134if(CMAKE_COMPILER_IS_GNUCXX)
135 if ((CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.99") OR
136 CMAKE_CXX_COMPILER_ID MATCHES "Clang")
137 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
138 else()
139 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
140 endif()
141endif()
142
143# pthread_rwlock_t requires a feature flag.
144if(NOT WIN32)
145 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
Adam Langley6f2e7332015-05-15 12:01:29 -0700146endif()
147
Adam Langley9a4beb82015-11-09 13:57:26 -0800148if(FUZZ)
Alessandro Ghedinib6f69272016-09-28 22:14:01 +0100149 if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
150 message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
Adam Langley9a4beb82015-11-09 13:57:26 -0800151 endif()
152
David Benjaminec978dd2016-11-04 18:59:33 -0400153 add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
154 set(RUNNER_ARGS "-deterministic")
155
156 if(NOT NO_FUZZER_MODE)
157 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
158 set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
159 endif()
David Benjaminbc5b2a22016-03-01 22:57:32 -0500160
David Benjamin08ab59b2017-05-10 12:02:58 -0400161 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,trace-pc-guard")
162 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,trace-pc-guard")
Adam Langley9a4beb82015-11-09 13:57:26 -0800163 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
Adam Langley7104cc92015-11-10 15:00:51 -0800164 link_directories(.)
Adam Langley9a4beb82015-11-09 13:57:26 -0800165endif()
166
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700167add_definitions(-DBORINGSSL_IMPLEMENTATION)
168
David Benjamin507c1ee2015-01-28 00:50:21 -0500169if (BUILD_SHARED_LIBS)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800170 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
171 # Enable position-independent code globally. This is needed because
172 # some library targets are OBJECT libraries.
173 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
David Benjamin507c1ee2015-01-28 00:50:21 -0500174endif()
175
Adam Langley1bcd10c2016-12-16 10:48:23 -0800176if (MSAN)
177 if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
178 message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
179 endif()
180
181 if (ASAN)
182 message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
183 endif()
184
185 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
186 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
187 set(OPENSSL_NO_ASM "1")
188endif()
189
190if (ASAN)
191 if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
192 message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
193 endif()
194
David Benjamin0d3c9632017-02-28 14:58:00 -0500195 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
196 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 -0800197 set(OPENSSL_NO_ASM "1")
198endif()
199
David Benjamind035ab32016-12-27 12:15:56 -0500200if (GCOV)
201 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
202 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
203endif()
204
David Benjaminaff72a32017-04-06 23:26:04 -0400205if(FIPS)
206 add_definitions(-DBORINGSSL_FIPS)
Adam Langleyf64a6ee2017-05-17 13:05:50 -0700207 if(FIPS_BREAK_TEST)
208 add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
209 endif()
210 # Delocate does not work for ASan and MSan builds.
211 if(NOT ASAN AND NOT MSAN)
212 set(FIPS_DELOCATE "1")
213 endif()
David Benjaminaff72a32017-04-06 23:26:04 -0400214endif()
215
216# CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
217# architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
218# alone, and expects all architecture-specific logic to be conditioned within
219# the source files rather than the build. This does not work for our assembly
220# files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
221# builds.
222if (NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
223 list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
224 if (NOT ${NUM_ARCHES} EQUAL 1)
225 message(FATAL_ERROR "Universal binaries not supported.")
226 endif()
227 list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
228endif()
229
David Benjamin27b08e92015-05-04 15:16:57 -0400230if (OPENSSL_NO_ASM)
231 add_definitions(-DOPENSSL_NO_ASM)
232 set(ARCH "generic")
David Benjaminaff72a32017-04-06 23:26:04 -0400233elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
234 set(ARCH "x86_64")
235elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
236 set(ARCH "x86_64")
237elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
238 # cmake reports AMD64 on Windows, but we might be building for 32-bit.
239 if (CMAKE_CL_64)
240 set(ARCH "x86_64")
241 else()
242 set(ARCH "x86")
243 endif()
244elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
245 set(ARCH "x86")
246elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
247 set(ARCH "x86")
248elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
249 set(ARCH "x86")
250elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
251 set(ARCH "aarch64")
252elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
253 set(ARCH "aarch64")
254elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm*")
255 set(ARCH "arm")
256elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "mips")
257 # Just to avoid the “unknown processor” error.
258 set(ARCH "generic")
259elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le")
260 set(ARCH "ppc64le")
261else()
262 message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
David Benjamin27b08e92015-05-04 15:16:57 -0400263endif()
264
David Benjaminaff72a32017-04-06 23:26:04 -0400265if (ANDROID AND ${ARCH} STREQUAL "arm")
266 # The Android-NDK CMake files somehow fail to set the -march flag for
267 # assembly files. Without this flag, the compiler believes that it's
268 # building for ARMv5.
269 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -march=${CMAKE_SYSTEM_PROCESSOR}")
270endif()
271
272if (${ARCH} STREQUAL "x86" AND APPLE)
273 # With CMake 2.8.x, ${CMAKE_SYSTEM_PROCESSOR} evalutes to i386 on OS X,
274 # but clang defaults to 64-bit builds on OS X unless otherwise told.
275 # Set ARCH to x86_64 so clang and CMake agree. This is fixed in CMake 3.
276 set(ARCH "x86_64")
Adam Langleyfd499932017-04-04 14:21:43 -0700277endif()
278
David Benjamin96628432017-01-19 19:05:47 -0500279# Add minimal googletest targets. The provided one has many side-effects, and
280# googletest has a very straightforward build.
281add_library(gtest third_party/googletest/src/gtest-all.cc)
282target_include_directories(gtest PRIVATE third_party/googletest)
David Benjamin96628432017-01-19 19:05:47 -0500283
284include_directories(third_party/googletest/include)
285
David Benjamin301afaf2015-10-14 21:34:40 -0400286# Declare a dummy target to build all unit tests. Test targets should inject
287# themselves as dependencies next to the target definition.
288add_custom_target(all_tests)
289
David Benjamin3ecd0a52017-05-19 15:26:18 -0400290add_custom_command(
291 OUTPUT crypto_test_data.cc
292 COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go ${CRYPTO_TEST_DATA} >
293 ${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc
294 DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
295 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
296
297add_library(crypto_test_data OBJECT crypto_test_data.cc)
298
Adam Langley95c29f32014-06-20 12:00:00 -0700299add_subdirectory(crypto)
300add_subdirectory(ssl)
301add_subdirectory(ssl/test)
Martin Kreichgauer118355c2017-05-12 15:34:45 -0700302add_subdirectory(fipstools)
Adam Langley95c29f32014-06-20 12:00:00 -0700303add_subdirectory(tool)
Adam Langleyc004dfc2015-02-03 10:45:12 -0800304add_subdirectory(decrepit)
David Benjamin301afaf2015-10-14 21:34:40 -0400305
Adam Langley9a4beb82015-11-09 13:57:26 -0800306if(FUZZ)
David Benjamin1e5cb822017-05-10 16:07:56 -0400307 if(LIBFUZZER_FROM_DEPS)
308 file(GLOB LIBFUZZER_SOURCES "util/bot/libFuzzer/*.cpp")
309 add_library(Fuzzer STATIC ${LIBFUZZER_SOURCES})
310 # libFuzzer does not pass our aggressive warnings. It also must be built
311 # without -fsanitize-coverage options or clang crashes.
David Benjamin6fb16cc2017-07-14 11:02:39 -0400312 set_target_properties(Fuzzer PROPERTIES COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -Wno-missing-prototypes -fsanitize-coverage=0")
David Benjamin1e5cb822017-05-10 16:07:56 -0400313 endif()
314
Adam Langley9a4beb82015-11-09 13:57:26 -0800315 add_subdirectory(fuzz)
316endif()
317
David Benjamin301afaf2015-10-14 21:34:40 -0400318if (NOT ${CMAKE_VERSION} VERSION_LESS "3.2")
319 # USES_TERMINAL is only available in CMake 3.2 or later.
320 set(MAYBE_USES_TERMINAL USES_TERMINAL)
321endif()
322
323add_custom_target(
324 run_tests
325 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
326 ${CMAKE_BINARY_DIR}
Adam Langleyd5c72c82016-09-23 16:43:17 -0700327 COMMAND cd ssl/test/runner &&
328 ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
329 ${RUNNER_ARGS}
David Benjamin301afaf2015-10-14 21:34:40 -0400330 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
331 DEPENDS all_tests bssl_shim
332 ${MAYBE_USES_TERMINAL})