blob: 2c2eb8800f184ab19673c06ee7ae7afcfafc470d [file] [log] [blame]
David Benjamina1843d62023-09-17 10:17:50 -04001cmake_minimum_required(VERSION 3.12)
David Benjamin6979c7e2017-12-04 17:19:49 -05002
David Benjamin6b34d542016-02-05 21:58:39 -05003# Defer enabling C and CXX languages.
David Benjamine6fd1252018-08-10 10:30:55 -05004project(BoringSSL NONE)
David Benjamin6b34d542016-02-05 21:58:39 -05005
Daniel Thornburgh2fc6d382022-04-04 11:23:56 -07006# Don't install BoringSSL to system directories by default; it has no stable
7# ABI. Instead, default to an "install" directory under the source.
8if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
9 set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install CACHE PATH "" FORCE)
10endif()
11
David Benjamin6b34d542016-02-05 21:58:39 -050012if(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)
David Benjamin261ec612023-01-29 16:34:48 -050019include(cmake/go.cmake)
David Benjaminb0b1f9d2023-04-19 17:18:27 -040020include(cmake/paths.cmake)
David Benjamin19676212023-01-25 10:03:53 -050021include(cmake/perlasm.cmake)
David Benjamin3ecd0a52017-05-19 15:26:18 -040022
David Benjamin6b34d542016-02-05 21:58:39 -050023enable_language(C)
24enable_language(CXX)
Adam Langley95c29f32014-06-20 12:00:00 -070025
Daniel Thornburgh48f79472022-04-25 20:52:43 +000026include(GNUInstallDirs)
27
David Benjamine5f72662023-01-29 18:59:45 -050028# CMake versions before 3.14 do not have default destination values. Executable
29# and library targets that use a default destination should include this
30# variable.
31if(CMAKE_VERSION VERSION_LESS "3.14")
32 set(INSTALL_DESTINATION_DEFAULT
33 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
34 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
35 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
36endif()
37
38# Wrap the CMake install function so we can disable it.
39set(INSTALL_ENABLED 1)
40function(install_if_enabled)
41 if(INSTALL_ENABLED)
42 install(${ARGV})
43 endif()
44endfunction()
45
Adam Langley843ab662015-04-28 17:46:58 -070046if(ANDROID)
David Benjamin261ec612023-01-29 16:34:48 -050047 # Android-NDK CMake files reconfigure the path and so Perl won't be found.
48 # However, ninja will still find them in $PATH if we just name them.
Tamas Berghammer5693e422016-05-19 14:28:14 +010049 if(NOT PERL_EXECUTABLE)
50 set(PERL_EXECUTABLE "perl")
51 endif()
Adam Langley843ab662015-04-28 17:46:58 -070052else()
53 find_package(Perl REQUIRED)
Adam Langley843ab662015-04-28 17:46:58 -070054endif()
David Benjamin3ce3c362015-02-23 13:06:19 -050055
David Benjaminf36c3ad2019-01-15 12:45:57 -060056if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT CMAKE_CROSSCOMPILING)
Adam Langley9dfaf252019-01-04 07:53:25 -080057 find_package(PkgConfig QUIET)
58 if (PkgConfig_FOUND)
David Benjaminf86dd182023-08-29 16:51:53 -040059 pkg_check_modules(LIBUNWIND libunwind-generic>=1.3.0)
Adam Langley9dfaf252019-01-04 07:53:25 -080060 if(LIBUNWIND_FOUND)
61 add_definitions(-DBORINGSSL_HAVE_LIBUNWIND)
62 else()
63 message("libunwind not found. Disabling unwind tests.")
64 endif()
David Benjamin17d553d2018-12-21 17:58:36 -060065 else()
Adam Langley9dfaf252019-01-04 07:53:25 -080066 message("pkgconfig not found. Disabling unwind tests.")
David Benjamin17d553d2018-12-21 17:58:36 -060067 endif()
68endif()
69
David Benjamine6fd1252018-08-10 10:30:55 -050070if(USE_CUSTOM_LIBCXX)
David Benjamine9ae99b2018-08-09 15:33:07 -050071 set(BORINGSSL_ALLOW_CXX_RUNTIME 1)
72endif()
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070073
David Benjamine6fd1252018-08-10 10:30:55 -050074if(BORINGSSL_ALLOW_CXX_RUNTIME)
David Benjamin2507d9e2017-07-26 11:39:38 -040075 add_definitions(-DBORINGSSL_ALLOW_CXX_RUNTIME)
76endif()
77
David Benjamin0de64a72019-09-26 15:59:40 -040078string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
79if(NOT FIPS)
80 if(CMAKE_BUILD_TYPE_LOWER STREQUAL "relwithassert" OR
81 NOT CMAKE_BUILD_TYPE_LOWER MATCHES "rel")
82 add_definitions(-DBORINGSSL_DISPATCH_TEST)
83 # CMake automatically connects include_directories to the NASM
84 # command-line, but not add_definitions.
85 set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -DBORINGSSL_DISPATCH_TEST")
86 endif()
Adam Langleyc1615712018-11-27 14:07:12 -080087endif()
88
David Benjaminc47f7932019-01-22 19:49:35 -060089# Add a RelWithAsserts build configuration. It is the same as Release, except it
90# does not define NDEBUG, so asserts run.
91foreach(VAR CMAKE_C_FLAGS CMAKE_CXX_FLAGS CMAKE_ASM_FLAGS)
92 string(REGEX REPLACE "(^| )[/-]DNDEBUG( |$)" " " "${VAR}_RELWITHASSERTS"
93 "${${VAR}_RELEASE}")
94endforeach()
95
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070096if(BORINGSSL_PREFIX AND BORINGSSL_PREFIX_SYMBOLS)
97 add_definitions(-DBORINGSSL_PREFIX=${BORINGSSL_PREFIX})
David Benjamin8c23d3a2018-11-25 15:58:02 -060098 # CMake automatically connects include_directories to the NASM command-line,
99 # but not add_definitions.
100 set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -DBORINGSSL_PREFIX=${BORINGSSL_PREFIX}")
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700101
102 # Use "symbol_prefix_include" to store generated header files
103 include_directories(${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include)
104 add_custom_command(
105 OUTPUT symbol_prefix_include/boringssl_prefix_symbols.h
106 symbol_prefix_include/boringssl_prefix_symbols_asm.h
107 symbol_prefix_include/boringssl_prefix_symbols_nasm.inc
108 COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include
109 COMMAND ${GO_EXECUTABLE} run ${CMAKE_CURRENT_SOURCE_DIR}/util/make_prefix_headers.go -out ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include ${BORINGSSL_PREFIX_SYMBOLS}
110 DEPENDS util/make_prefix_headers.go
David Benjamin8c75ed02023-02-05 13:44:26 -0500111 ${BORINGSSL_PREFIX_SYMBOLS})
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700112
113 # add_dependencies needs a target, not a file, so we add an intermediate
114 # target.
115 add_custom_target(
116 boringssl_prefix_symbols
117 DEPENDS symbol_prefix_include/boringssl_prefix_symbols.h
118 symbol_prefix_include/boringssl_prefix_symbols_asm.h
119 symbol_prefix_include/boringssl_prefix_symbols_nasm.inc)
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700120elseif(BORINGSSL_PREFIX OR BORINGSSL_PREFIX_SYMBOLS)
121 message(FATAL_ERROR "Must specify both or neither of BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS")
David Benjamin8c75ed02023-02-05 13:44:26 -0500122else()
123 add_custom_target(boringssl_prefix_symbols)
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700124endif()
125
David Benjamin02afbd32017-10-05 15:04:08 -0400126if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
127 set(CLANG 1)
128endif()
Vincent Batts60931e22017-09-20 11:51:54 -0400129
Alexei Lozovsky356a9a02019-04-10 14:50:55 +0300130if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
131 set(EMSCRIPTEN 1)
132endif()
133
David Benjamina9281712022-05-20 12:41:15 -0400134set(CMAKE_CXX_STANDARD 14)
135set(CMAKE_CXX_STANDARD_REQUIRED ON)
David Benjaminc371b8b2023-01-25 17:14:35 -0500136set(CMAKE_C_STANDARD 11)
137set(CMAKE_C_STANDARD_REQUIRED ON)
David Benjamina9281712022-05-20 12:41:15 -0400138
David Benjamin02afbd32017-10-05 15:04:08 -0400139if(CMAKE_COMPILER_IS_GNUCXX OR CLANG)
140 # Note clang-cl is odd and sets both CLANG and MSVC. We base our configuration
David Benjamin8d67f6f2018-01-05 15:43:09 -0500141 # primarily on our normal Clang one.
Bob Beckbc97b7a2023-04-18 08:35:15 -0600142 # TODO(bbe) took out -Wmissing-field-initializers for pki - fix and put back or disable only for pki
143 set(C_CXX_FLAGS "-Werror -Wformat=2 -Wsign-compare -Wwrite-strings -Wvla -Wshadow -Wtype-limits")
David Benjamin02afbd32017-10-05 15:04:08 -0400144 if(MSVC)
David Benjamin8d67f6f2018-01-05 15:43:09 -0500145 # clang-cl sets different default warnings than clang. It also treats -Wall
146 # as -Weverything, to match MSVC. Instead -W3 is the alias for -Wall.
147 # See http://llvm.org/viewvc/llvm-project?view=revision&revision=319116
148 set(C_CXX_FLAGS "${C_CXX_FLAGS} -W3 -Wno-unused-parameter -fmsc-version=1900")
David Benjamin02afbd32017-10-05 15:04:08 -0400149 else()
Alexei Lozovsky356a9a02019-04-10 14:50:55 +0300150 if(EMSCRIPTEN)
151 # emscripten's emcc/clang does not accept the "-ggdb" flag.
152 set(C_CXX_FLAGS "${C_CXX_FLAGS} -g")
153 else()
154 set(C_CXX_FLAGS "${C_CXX_FLAGS} -ggdb")
155 endif()
156
157 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wall -fvisibility=hidden -fno-common")
David Benjamin02afbd32017-10-05 15:04:08 -0400158 endif()
159
160 if(CLANG)
David Benjamin9fb6fea2017-07-31 14:03:38 -0400161 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof -fcolor-diagnostics")
Adam Langley5c38c052017-04-28 14:47:06 -0700162 else()
163 # GCC (at least 4.8.4) has a bug where it'll find unreachable free() calls
164 # and declare that the code is trying to free a stack pointer.
165 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-free-nonheap-object")
David Benjamin9b7d8362016-08-29 14:59:31 -0400166 endif()
Vincent Batts60931e22017-09-20 11:51:54 -0400167
David Benjamin7d1fc2b2022-10-14 08:44:15 -0400168 # -Wstring-concatenation was added in Clang 12.0.0, which corresponds to
169 # AppleClang 13.0.0 per the table in
170 # https://en.wikipedia.org/wiki/Xcode#Toolchain_versions
171 if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND
172 CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "12.0.0") OR
173 (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND
174 CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "13.0.0"))
175 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wstring-concatenation")
176 endif()
177
David Benjaminc8d31372022-07-12 11:46:03 -0400178 if(CLANG OR CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0.0")
Vincent Batts60931e22017-09-20 11:51:54 -0400179 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wimplicit-fallthrough")
180 endif()
181
David Benjamind7936c22021-12-15 17:25:10 -0500182 if(CMAKE_COMPILER_IS_GNUCXX)
183 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wformat-signedness")
184 endif()
185
Adam Langley01867872016-06-30 14:16:59 -0700186 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
David Benjamin02afbd32017-10-05 15:04:08 -0400187 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_CXX_FLAGS} -Wmissing-declarations")
David Benjamin2507d9e2017-07-26 11:39:38 -0400188
David Benjamina9281712022-05-20 12:41:15 -0400189 if(NOT MSVC AND NOT BORINGSSL_ALLOW_CXX_RUNTIME)
190 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
David Benjamin2507d9e2017-07-26 11:39:38 -0400191 endif()
192
David Benjamin4a8d1f32017-07-13 17:53:07 -0400193 # In GCC, -Wmissing-declarations is the C++ spelling of -Wmissing-prototypes
194 # and using the wrong one is an error. In Clang, -Wmissing-prototypes is the
195 # spelling for both and -Wmissing-declarations is some other warning.
196 #
197 # https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Warning-Options.html#Warning-Options
198 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-prototypes
199 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-declarations
David Benjamin02afbd32017-10-05 15:04:08 -0400200 if(CLANG)
Vincent Batts60931e22017-09-20 11:51:54 -0400201 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-prototypes")
David Benjamin4a8d1f32017-07-13 17:53:07 -0400202 endif()
Adam Langley95c29f32014-06-20 12:00:00 -0700203elseif(MSVC)
Brian Smithefed2212015-01-28 16:20:02 -0800204 set(MSVC_DISABLED_WARNINGS_LIST
205 "C4100" # 'exarg' : unreferenced formal parameter
206 "C4127" # conditional expression is constant
Brian Smithefed2212015-01-28 16:20:02 -0800207 "C4244" # 'function' : conversion from 'int' to 'uint8_t',
208 # possible loss of data
David Benjamin3673be72015-02-11 15:12:05 -0500209 "C4267" # conversion from 'size_t' to 'int', possible loss of data
Brian Smithefed2212015-01-28 16:20:02 -0800210 "C4706" # assignment within conditional expression
David Benjamin7acd6bc2016-05-02 12:57:01 -0400211 )
Brian Smithefed2212015-01-28 16:20:02 -0800212 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
213 ${MSVC_DISABLED_WARNINGS_LIST})
David Benjamin3251ca12023-01-12 16:04:58 -0500214 set(CMAKE_C_FLAGS "-utf-8 -W4 -WX ${MSVC_DISABLED_WARNINGS_STR}")
215 set(CMAKE_CXX_FLAGS "-utf-8 -W4 -WX ${MSVC_DISABLED_WARNINGS_STR}")
David Benjamine2568c42017-08-16 15:25:27 -0400216endif()
217
218if(WIN32)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800219 add_definitions(-D_HAS_EXCEPTIONS=0)
Brian Smitha87de9b2015-01-28 20:34:47 -0800220 add_definitions(-DWIN32_LEAN_AND_MEAN)
David Benjamin0e434b92015-04-02 13:20:01 -0400221 add_definitions(-DNOMINMAX)
David Benjamin9b6ff442017-06-15 20:44:30 -0400222 # Allow use of fopen.
223 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
Adam Langley95c29f32014-06-20 12:00:00 -0700224endif()
225
David Benjamin387f8202022-01-25 12:16:48 -0500226# pthread_rwlock_t on Linux requires a feature flag. We limit this to Linux
227# because, on Apple platforms, it instead disables APIs we use. See compat(5)
228# and sys/cdefs.h. Reportedly, FreeBSD also breaks when this is set. See
229# https://crbug.com/boringssl/471.
230if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400231 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
Adam Langley6f2e7332015-05-15 12:01:29 -0700232endif()
233
Adam Langley9a4beb82015-11-09 13:57:26 -0800234if(FUZZ)
David Benjamin02afbd32017-10-05 15:04:08 -0400235 if(NOT CLANG)
Alessandro Ghedinib6f69272016-09-28 22:14:01 +0100236 message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
Adam Langley9a4beb82015-11-09 13:57:26 -0800237 endif()
238
Adam Langley9c969bf2018-08-24 10:46:01 -0700239 if(CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0.0")
240 message(FATAL_ERROR "You need Clang ≥ 6.0.0")
241 endif()
242
David Benjaminec978dd2016-11-04 18:59:33 -0400243 add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
244 set(RUNNER_ARGS "-deterministic")
245
246 if(NOT NO_FUZZER_MODE)
247 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
248 set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
249 endif()
David Benjaminbc5b2a22016-03-01 22:57:32 -0500250
Adam Langley9c969bf2018-08-24 10:46:01 -0700251 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
252 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
Adam Langley9a4beb82015-11-09 13:57:26 -0800253endif()
254
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700255add_definitions(-DBORINGSSL_IMPLEMENTATION)
256
David Benjamine6fd1252018-08-10 10:30:55 -0500257if(BUILD_SHARED_LIBS)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800258 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
259 # Enable position-independent code globally. This is needed because
260 # some library targets are OBJECT libraries.
261 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
David Benjamin507c1ee2015-01-28 00:50:21 -0500262endif()
263
David Benjamine6fd1252018-08-10 10:30:55 -0500264if(MSAN)
David Benjamin02afbd32017-10-05 15:04:08 -0400265 if(NOT CLANG)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800266 message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
267 endif()
268
David Benjamine6fd1252018-08-10 10:30:55 -0500269 if(ASAN)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800270 message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
271 endif()
272
273 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
274 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
Adam Langleye77c27d2018-09-07 11:20:23 -0700275 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
Adam Langley1bcd10c2016-12-16 10:48:23 -0800276endif()
277
David Benjamine6fd1252018-08-10 10:30:55 -0500278if(ASAN)
David Benjamin02afbd32017-10-05 15:04:08 -0400279 if(NOT CLANG)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800280 message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
281 endif()
282
David Benjamin0d3c9632017-02-28 14:58:00 -0500283 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
284 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 -0800285endif()
286
David Benjaminc367ee52017-11-21 08:16:42 -0500287if(CFI)
288 if(NOT CLANG)
289 message(FATAL_ERROR "Cannot enable CFI unless using Clang")
290 endif()
291
David Benjamin66508982018-09-22 16:19:15 -0500292 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
293 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
David Benjaminc367ee52017-11-21 08:16:42 -0500294 # We use Chromium's copy of clang, which requires -fuse-ld=lld if building
295 # with -flto. That, in turn, can't handle -ggdb.
296 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
297 string(REPLACE "-ggdb" "-g" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
298 string(REPLACE "-ggdb" "-g" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
299 # -flto causes object files to contain LLVM bitcode. Mixing those with
300 # assembly output in the same static library breaks the linker.
301 set(OPENSSL_NO_ASM "1")
302endif()
303
David Benjamin5852cfc2018-07-19 17:50:45 -0400304if(TSAN)
305 if(NOT CLANG)
306 message(FATAL_ERROR "Cannot enable TSAN unless using Clang")
307 endif()
308
309 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
310 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
311 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
312endif()
313
David Benjaminfc27a192019-01-21 07:25:12 +0000314if(UBSAN)
315 if(NOT CLANG)
316 message(FATAL_ERROR "Cannot enable UBSAN unless using Clang")
317 endif()
318
319 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
320 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
321 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
322
323 if(NOT UBSAN_RECOVER)
324 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize-recover=undefined")
325 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize-recover=undefined")
326 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-sanitize-recover=undefined")
327 endif()
328endif()
329
David Benjamine6fd1252018-08-10 10:30:55 -0500330if(GCOV)
David Benjamind035ab32016-12-27 12:15:56 -0500331 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
332 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
333endif()
334
David Benjaminaff72a32017-04-06 23:26:04 -0400335if(FIPS)
336 add_definitions(-DBORINGSSL_FIPS)
Adam Langleyf64a6ee2017-05-17 13:05:50 -0700337 if(FIPS_BREAK_TEST)
338 add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
339 endif()
Adam Langleyd72e47f2019-05-09 16:39:49 -0700340 # The FIPS integrity check does not work for ASan and MSan builds.
Adam Langleyf64a6ee2017-05-17 13:05:50 -0700341 if(NOT ASAN AND NOT MSAN)
Adam Langleyd72e47f2019-05-09 16:39:49 -0700342 if(BUILD_SHARED_LIBS)
343 set(FIPS_SHARED "1")
344 else()
345 set(FIPS_DELOCATE "1")
346 endif()
Adam Langleyf64a6ee2017-05-17 13:05:50 -0700347 endif()
David Benjamine481d942019-10-18 16:43:12 -0400348 if(FIPS_SHARED)
349 # The Android CMake files set -ffunction-sections and -fdata-sections,
350 # which is incompatible with FIPS_SHARED.
351 set(CMAKE_C_FLAGS
352 "${CMAKE_C_FLAGS} -fno-function-sections -fno-data-sections")
353 set(CMAKE_CXX_FLAGS
354 "${CMAKE_CXX_FLAGS} -fno-function-sections -fno-data-sections")
355 endif()
David Benjaminaff72a32017-04-06 23:26:04 -0400356endif()
357
David Benjamin6291af42018-03-23 13:49:27 -0400358if(OPENSSL_SMALL)
359 add_definitions(-DOPENSSL_SMALL)
360endif()
361
Adam Langleya6a049a2018-12-06 17:15:58 -0800362if(CONSTANT_TIME_VALIDATION)
363 add_definitions(-DBORINGSSL_CONSTANT_TIME_VALIDATION)
364 # Asserts will often test secret data.
365 add_definitions(-DNDEBUG)
366endif()
367
David Benjamin582904f2023-02-04 18:30:36 -0500368if(MALLOC_FAILURE_TESTING)
369 add_definitions(-DBORINGSSL_MALLOC_FAILURE_TESTING)
370endif()
371
David Benjamin19676212023-01-25 10:03:53 -0500372if(OPENSSL_NO_ASM)
373 add_definitions(-DOPENSSL_NO_ASM)
374endif()
375
376if(FIPS_DELOCATE OR NOT OPENSSL_NO_ASM)
377 # On x86 and x86_64 Windows, we use the NASM output.
378 if(WIN32 AND CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64|amd64|x86|i[3-6]86")
379 enable_language(ASM_NASM)
380 set(OPENSSL_NASM TRUE)
381 set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -gcv8")
382 else()
383 enable_language(ASM)
384 set(OPENSSL_ASM TRUE)
David Benjamin61266e42023-01-29 15:53:30 -0500385 # Work around https://gitlab.kitware.com/cmake/cmake/-/issues/20771 in older
386 # CMake versions.
387 if(APPLE AND CMAKE_VERSION VERSION_LESS 3.19)
David Benjamin19676212023-01-25 10:03:53 -0500388 if(CMAKE_OSX_SYSROOT)
389 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -isysroot \"${CMAKE_OSX_SYSROOT}\"")
390 endif()
391 foreach(arch ${CMAKE_OSX_ARCHITECTURES})
392 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -arch ${arch}")
393 endforeach()
394 endif()
395 if(NOT WIN32)
396 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
397 endif()
398 # Clang's integerated assembler does not support debug symbols.
399 if(NOT CMAKE_ASM_COMPILER_ID MATCHES "Clang")
400 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,-g")
401 endif()
David Benjaminaff72a32017-04-06 23:26:04 -0400402 endif()
David Benjaminaff72a32017-04-06 23:26:04 -0400403endif()
404
David Benjamin6887d5e2019-12-17 13:56:30 -0500405if(OPENSSL_NO_SSE2_FOR_TESTING)
406 add_definitions(-DOPENSSL_NO_SSE2_FOR_TESTING)
407endif()
408
David Benjamine6fd1252018-08-10 10:30:55 -0500409if(USE_CUSTOM_LIBCXX)
410 if(NOT CLANG)
David Benjamine9ae99b2018-08-09 15:33:07 -0500411 message(FATAL_ERROR "USE_CUSTOM_LIBCXX only supported with Clang")
412 endif()
413
David Benjamine5f72662023-01-29 18:59:45 -0500414 # CMake does not allow installing a library without installing dependencies.
415 # If we installed libcrypto, we'd have to install our custom libc++, which
416 # does not make sense. As this is a test-only configuration, disable
417 # installing.
418 set(INSTALL_ENABLED 0)
419
David Benjamine9ae99b2018-08-09 15:33:07 -0500420 # CMAKE_CXX_FLAGS ends up in the linker flags as well, so use
421 # add_compile_options. There does not appear to be a way to set
422 # language-specific compile-only flags.
423 add_compile_options("-nostdinc++")
424 set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -nostdlib++")
425 include_directories(
426 SYSTEM
David Benjamin16b3af72021-10-26 17:07:39 -0400427 util/bot/libcxx-config
David Benjamine9ae99b2018-08-09 15:33:07 -0500428 util/bot/libcxx/include
429 util/bot/libcxxabi/include
430 )
431
432 # This is patterned after buildtools/third_party/libc++/BUILD.gn and
433 # buildtools/third_party/libc++abi/BUILD.gn in Chromium.
434
435 file(GLOB LIBCXX_SOURCES "util/bot/libcxx/src/*.cpp")
436 file(GLOB LIBCXXABI_SOURCES "util/bot/libcxxabi/src/*.cpp")
437
438 # This file is meant for exception-less builds.
439 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_noexception.cpp")
440 # libc++ also defines new and delete.
441 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/stdlib_new_delete.cpp")
David Benjamine6fd1252018-08-10 10:30:55 -0500442 if(TSAN)
David Benjamine9ae99b2018-08-09 15:33:07 -0500443 # ThreadSanitizer tries to intercept these symbols. Skip them to avoid
444 # symbol conflicts.
445 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_guard.cpp")
446 endif()
447
448 add_library(libcxxabi ${LIBCXXABI_SOURCES})
449 target_compile_definitions(
450 libcxxabi PRIVATE
451 -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS
452 )
David Benjamine9ae99b2018-08-09 15:33:07 -0500453
454 add_library(libcxx ${LIBCXX_SOURCES})
David Benjamine6fd1252018-08-10 10:30:55 -0500455 if(ASAN OR MSAN OR TSAN)
David Benjamine9ae99b2018-08-09 15:33:07 -0500456 # Sanitizers try to intercept new and delete.
457 target_compile_definitions(
458 libcxx PRIVATE
459 -D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
460 )
461 endif()
462 target_compile_definitions(
463 libcxx PRIVATE
464 -D_LIBCPP_BUILDING_LIBRARY
465 -DLIBCXX_BUILDING_LIBCXXABI
466 )
David Benjaminaa72a6c2022-11-21 15:27:15 -0500467 set_target_properties(
468 libcxx libcxxabi PROPERTIES
469 COMPILE_FLAGS "-Wno-missing-prototypes -Wno-implicit-fallthrough"
470 # libc++ and libc++abi must be built in C++20 mode.
471 CXX_STANDARD 20
472 CXX_STANDARD_REQUIRED TRUE
473 )
474 # libc++abi depends on libc++ internal headers.
David Benjamin8c4ec3b2023-02-27 12:46:12 -0500475 set_property(TARGET libcxx libcxxabi APPEND PROPERTY INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/util/bot/libcxx/src")
David Benjamine9ae99b2018-08-09 15:33:07 -0500476 target_link_libraries(libcxx libcxxabi)
477endif()
478
David Benjamin96628432017-01-19 19:05:47 -0500479# Add minimal googletest targets. The provided one has many side-effects, and
480# googletest has a very straightforward build.
David Benjaminfa343af2023-09-01 21:23:01 -0400481add_library(
482 boringssl_gtest
483 third_party/googletest/googlemock/src/gmock-all.cc
484 third_party/googletest/googletest/src/gtest-all.cc
485)
David Benjamin987dff12023-02-15 11:04:01 -0500486if(USE_CUSTOM_LIBCXX)
487 target_link_libraries(boringssl_gtest libcxx)
488endif()
David Benjamin8bc06cf2023-02-04 20:05:04 -0500489target_include_directories(
490 boringssl_gtest
David Benjaminfa343af2023-09-01 21:23:01 -0400491 PUBLIC
492 third_party/googletest/googlemock/include
493 third_party/googletest/googletest/include
494 PRIVATE
495 third_party/googletest/googlemock
496 third_party/googletest/googletest
David Benjamin8bc06cf2023-02-04 20:05:04 -0500497)
David Benjamin96628432017-01-19 19:05:47 -0500498
David Benjamin301afaf2015-10-14 21:34:40 -0400499# Declare a dummy target to build all unit tests. Test targets should inject
500# themselves as dependencies next to the target definition.
501add_custom_target(all_tests)
502
David Benjamincbac9c32020-06-18 16:14:24 -0400503# On Windows, CRYPTO_TEST_DATA is too long to fit in command-line limits.
504# TODO(davidben): CMake 3.12 has a list(JOIN) command. Use that when we've
505# updated the minimum version.
506set(EMBED_TEST_DATA_ARGS "")
507foreach(arg ${CRYPTO_TEST_DATA})
508 set(EMBED_TEST_DATA_ARGS "${EMBED_TEST_DATA_ARGS}${arg}\n")
509endforeach()
510file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/embed_test_data_args.txt"
511 "${EMBED_TEST_DATA_ARGS}")
512
David Benjamin3ecd0a52017-05-19 15:26:18 -0400513add_custom_command(
514 OUTPUT crypto_test_data.cc
David Benjamincbac9c32020-06-18 16:14:24 -0400515 COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go -file-list
516 "${CMAKE_CURRENT_BINARY_DIR}/embed_test_data_args.txt" >
517 "${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc"
David Benjamin3ecd0a52017-05-19 15:26:18 -0400518 DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
519 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
520
521add_library(crypto_test_data OBJECT crypto_test_data.cc)
522
Adam Langley95c29f32014-06-20 12:00:00 -0700523add_subdirectory(crypto)
524add_subdirectory(ssl)
525add_subdirectory(ssl/test)
Adam Langleyea9fb942022-02-07 12:32:09 -0800526add_subdirectory(util/fipstools)
Adam Langleyb7f0c1b2019-07-09 18:02:14 -0700527add_subdirectory(util/fipstools/acvp/modulewrapper)
Adam Langleyc004dfc2015-02-03 10:45:12 -0800528add_subdirectory(decrepit)
David Benjamin9bbf0d92023-06-30 13:17:03 -0400529
David Benjamin847a3952023-06-30 15:05:24 -0400530add_library(test_support_lib STATIC ${TEST_SUPPORT_SOURCES})
531if (LIBUNWIND_FOUND)
532 target_compile_options(test_support_lib PRIVATE ${LIBUNWIND_CFLAGS_OTHER})
533 target_include_directories(test_support_lib PRIVATE ${LIBUNWIND_INCLUDE_DIRS})
534 target_link_libraries(test_support_lib ${LIBUNWIND_LDFLAGS})
535endif()
536if(WIN32)
537 target_link_libraries(test_support_lib dbghelp)
538endif()
539target_link_libraries(test_support_lib boringssl_gtest crypto)
540
David Benjamin197b5712023-06-30 14:10:42 -0400541# urandom_test is a separate binary because it needs to be able to observe the
542# PRNG initialisation, which means that it can't have other tests running before
543# it does.
544add_executable(urandom_test ${URANDOM_TEST_SOURCES})
545target_link_libraries(urandom_test test_support_lib boringssl_gtest crypto)
546add_dependencies(all_tests urandom_test)
547
548add_executable(crypto_test ${CRYPTO_TEST_SOURCES} $<TARGET_OBJECTS:crypto_test_data>)
David Benjamin847a3952023-06-30 15:05:24 -0400549target_link_libraries(crypto_test test_support_lib boringssl_gtest crypto)
David Benjamin197b5712023-06-30 14:10:42 -0400550add_dependencies(all_tests crypto_test)
551
552add_executable(ssl_test ${SSL_TEST_SOURCES})
David Benjamin847a3952023-06-30 15:05:24 -0400553target_link_libraries(ssl_test test_support_lib boringssl_gtest ssl crypto)
David Benjamin197b5712023-06-30 14:10:42 -0400554add_dependencies(all_tests ssl_test)
555
556add_executable(decrepit_test ${DECREPIT_TEST_SOURCES})
David Benjamin847a3952023-06-30 15:05:24 -0400557target_link_libraries(decrepit_test test_support_lib boringssl_gtest
David Benjamin197b5712023-06-30 14:10:42 -0400558 decrepit crypto)
559add_dependencies(all_tests decrepit_test)
560
David Benjamin9bbf0d92023-06-30 13:17:03 -0400561if(APPLE)
562 set(PKI_CXX_FLAGS "-fno-aligned-new")
563endif()
564
565add_library(pki ${PKI_SOURCES})
David Benjamin9bbf0d92023-06-30 13:17:03 -0400566target_link_libraries(pki crypto)
567
568add_executable(pki_test ${PKI_TEST_SOURCES})
David Benjamin847a3952023-06-30 15:05:24 -0400569target_link_libraries(pki_test test_support_lib boringssl_gtest pki crypto)
David Benjamin9bbf0d92023-06-30 13:17:03 -0400570add_dependencies(all_tests pki_test)
571
572# The PKI library requires C++17.
573set_target_properties(
574 pki pki_test
575 PROPERTIES
576 CXX_STANDARD 17
577 CXX_STANDARD_REQUIRED YES
578 COMPILE_FLAGS "${PKI_CXX_FLAGS}")
David Benjamin301afaf2015-10-14 21:34:40 -0400579
David Benjamin99ce1e02023-06-30 15:43:24 -0400580add_executable(bssl ${BSSL_SOURCES})
581install_if_enabled(TARGETS bssl DESTINATION ${INSTALL_DESTINATION_DEFAULT})
582target_link_libraries(bssl ssl crypto)
583
David Benjamin197b5712023-06-30 14:10:42 -0400584# Historically, targets were built in subdirectories. For compatibility with
585# existing tools, we, for now, copy the targets into the subdirectories. This
586# will be removed sometime in 2024.
587copy_post_build(crypto crypto_test urandom_test)
588copy_post_build(ssl ssl_test)
589copy_post_build(decrepit decrepit_test)
David Benjamin99ce1e02023-06-30 15:43:24 -0400590copy_post_build(tool bssl)
David Benjamin197b5712023-06-30 14:10:42 -0400591
Adam Langley9a4beb82015-11-09 13:57:26 -0800592if(FUZZ)
David Benjamin6c597be2019-01-07 15:13:04 -0600593 if(LIBFUZZER_FROM_DEPS)
594 file(GLOB LIBFUZZER_SOURCES "util/bot/libFuzzer/*.cpp")
595 add_library(Fuzzer STATIC ${LIBFUZZER_SOURCES})
596 # libFuzzer does not pass our aggressive warnings. It also must be built
597 # without -fsanitize-coverage options or clang crashes.
598 set_target_properties(Fuzzer PROPERTIES COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -Wno-missing-prototypes -fsanitize-coverage=0")
599 endif()
600
Adam Langley9a4beb82015-11-09 13:57:26 -0800601 add_subdirectory(fuzz)
602endif()
603
Benjamin Brittainea46caf2022-01-20 15:50:12 -0500604if(RUST_BINDINGS)
605 find_program(BINDGEN_EXECUTABLE bindgen)
606 if(NOT BINDGEN_EXECUTABLE)
607 message(FATAL_ERROR "Could not find bindgen but was asked to generate Rust bindings.")
608 else()
609 add_subdirectory(rust)
610 endif()
611endif()
612
David Benjamin0f4454c2022-01-26 14:17:08 -0500613if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
David Benjamin17dc94e2018-08-10 09:05:20 -0500614 set(HANDSHAKER_ARGS "-handshaker-path" $<TARGET_FILE:handshaker>)
615endif()
616
Adam Langley4f75b762020-12-17 14:06:25 -0800617if(FIPS)
618 add_custom_target(
619 acvp_tests
David Benjamin8c4ec3b2023-02-27 12:46:12 -0500620 COMMAND ${GO_EXECUTABLE} build -o ${CMAKE_CURRENT_BINARY_DIR}/acvptool
Adam Langley4f75b762020-12-17 14:06:25 -0800621 boringssl.googlesource.com/boringssl/util/fipstools/acvp/acvptool
David Benjamin8c4ec3b2023-02-27 12:46:12 -0500622 COMMAND ${GO_EXECUTABLE} build -o ${CMAKE_CURRENT_BINARY_DIR}/testmodulewrapper
Adam Langley4f75b762020-12-17 14:06:25 -0800623 boringssl.googlesource.com/boringssl/util/fipstools/acvp/acvptool/testmodulewrapper
624 COMMAND cd util/fipstools/acvp/acvptool/test &&
625 ${GO_EXECUTABLE} run check_expected.go
David Benjamin8c4ec3b2023-02-27 12:46:12 -0500626 -tool ${CMAKE_CURRENT_BINARY_DIR}/acvptool
627 -module-wrappers modulewrapper:$<TARGET_FILE:modulewrapper>,testmodulewrapper:${CMAKE_CURRENT_BINARY_DIR}/testmodulewrapper
Adam Langley4f75b762020-12-17 14:06:25 -0800628 -tests tests.json
David Benjamin8c4ec3b2023-02-27 12:46:12 -0500629 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
Adam Langley4f75b762020-12-17 14:06:25 -0800630 DEPENDS modulewrapper
631 USES_TERMINAL)
632
633 add_custom_target(
634 fips_specific_tests_if_any
635 DEPENDS acvp_tests
636 )
637else()
638 add_custom_target(fips_specific_tests_if_any)
639endif()
640
David Benjamin49bfec52023-06-30 13:07:24 -0400641file(STRINGS util/go_tests.txt GO_TESTS)
David Benjaminf53ca9f2023-02-24 09:07:36 -0500642set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
643 util/go_tests.txt)
644
David Benjamin301afaf2015-10-14 21:34:40 -0400645add_custom_target(
646 run_tests
David Benjamine3a5fac2023-02-27 16:36:24 -0500647 COMMAND ${GO_EXECUTABLE} test ${GO_TESTS}
David Benjamin301afaf2015-10-14 21:34:40 -0400648 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
David Benjamin8c4ec3b2023-02-27 12:46:12 -0500649 ${CMAKE_CURRENT_BINARY_DIR}
Adam Langleyd5c72c82016-09-23 16:43:17 -0700650 COMMAND cd ssl/test/runner &&
651 ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
David Benjamin17dc94e2018-08-10 09:05:20 -0500652 ${HANDSHAKER_ARGS} ${RUNNER_ARGS}
David Benjamin8c4ec3b2023-02-27 12:46:12 -0500653 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
Adam Langley4f75b762020-12-17 14:06:25 -0800654 DEPENDS all_tests bssl_shim handshaker fips_specific_tests_if_any
David Benjamin81a998a2020-05-26 11:35:58 -0400655 USES_TERMINAL)
Daniel Thornburgh2fc6d382022-04-04 11:23:56 -0700656
David Benjamine5f72662023-01-29 18:59:45 -0500657install_if_enabled(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
Daniel Thornburgh2fc6d382022-04-04 11:23:56 -0700658
David Benjamine5f72662023-01-29 18:59:45 -0500659install_if_enabled(EXPORT OpenSSLTargets
Daniel Thornburgh2fc6d382022-04-04 11:23:56 -0700660 FILE OpenSSLTargets.cmake
661 NAMESPACE OpenSSL::
662 DESTINATION lib/cmake/OpenSSL
663)
David Benjamine5f72662023-01-29 18:59:45 -0500664install_if_enabled(FILES cmake/OpenSSLConfig.cmake DESTINATION lib/cmake/OpenSSL)