blob: 4c7926d00c17d47a3d5cee86e680d62a13e9892a [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.
David Benjamina9a4c6d2023-12-01 16:21:40 -0500142 set(C_CXX_FLAGS "-Werror -Wformat=2 -Wsign-compare -Wwrite-strings -Wvla -Wshadow -Wtype-limits -Wmissing-field-initializers")
David Benjamin02afbd32017-10-05 15:04:08 -0400143 if(MSVC)
David Benjamin8d67f6f2018-01-05 15:43:09 -0500144 # clang-cl sets different default warnings than clang. It also treats -Wall
145 # as -Weverything, to match MSVC. Instead -W3 is the alias for -Wall.
146 # See http://llvm.org/viewvc/llvm-project?view=revision&revision=319116
147 set(C_CXX_FLAGS "${C_CXX_FLAGS} -W3 -Wno-unused-parameter -fmsc-version=1900")
David Benjamin02afbd32017-10-05 15:04:08 -0400148 else()
Alexei Lozovsky356a9a02019-04-10 14:50:55 +0300149 if(EMSCRIPTEN)
150 # emscripten's emcc/clang does not accept the "-ggdb" flag.
151 set(C_CXX_FLAGS "${C_CXX_FLAGS} -g")
152 else()
153 set(C_CXX_FLAGS "${C_CXX_FLAGS} -ggdb")
154 endif()
155
156 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wall -fvisibility=hidden -fno-common")
David Benjamin02afbd32017-10-05 15:04:08 -0400157 endif()
158
159 if(CLANG)
David Benjamin9fb6fea2017-07-31 14:03:38 -0400160 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof -fcolor-diagnostics")
Adam Langley5c38c052017-04-28 14:47:06 -0700161 else()
162 # GCC (at least 4.8.4) has a bug where it'll find unreachable free() calls
163 # and declare that the code is trying to free a stack pointer.
164 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-free-nonheap-object")
David Benjamin9b7d8362016-08-29 14:59:31 -0400165 endif()
Vincent Batts60931e22017-09-20 11:51:54 -0400166
David Benjamin7d1fc2b2022-10-14 08:44:15 -0400167 # -Wstring-concatenation was added in Clang 12.0.0, which corresponds to
168 # AppleClang 13.0.0 per the table in
169 # https://en.wikipedia.org/wiki/Xcode#Toolchain_versions
170 if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND
171 CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "12.0.0") OR
172 (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND
173 CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "13.0.0"))
174 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wstring-concatenation")
175 endif()
176
David Benjaminc8d31372022-07-12 11:46:03 -0400177 if(CLANG OR CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0.0")
Vincent Batts60931e22017-09-20 11:51:54 -0400178 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wimplicit-fallthrough")
179 endif()
180
David Benjamind7936c22021-12-15 17:25:10 -0500181 if(CMAKE_COMPILER_IS_GNUCXX)
182 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wformat-signedness")
183 endif()
184
Adam Langley01867872016-06-30 14:16:59 -0700185 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
David Benjamin02afbd32017-10-05 15:04:08 -0400186 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_CXX_FLAGS} -Wmissing-declarations")
David Benjamin2507d9e2017-07-26 11:39:38 -0400187
David Benjamina9281712022-05-20 12:41:15 -0400188 if(NOT MSVC AND NOT BORINGSSL_ALLOW_CXX_RUNTIME)
189 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
David Benjamin2507d9e2017-07-26 11:39:38 -0400190 endif()
191
David Benjamin4a8d1f32017-07-13 17:53:07 -0400192 # In GCC, -Wmissing-declarations is the C++ spelling of -Wmissing-prototypes
193 # and using the wrong one is an error. In Clang, -Wmissing-prototypes is the
194 # spelling for both and -Wmissing-declarations is some other warning.
195 #
196 # https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Warning-Options.html#Warning-Options
197 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-prototypes
198 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-declarations
David Benjamin02afbd32017-10-05 15:04:08 -0400199 if(CLANG)
Vincent Batts60931e22017-09-20 11:51:54 -0400200 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-prototypes")
David Benjamin4a8d1f32017-07-13 17:53:07 -0400201 endif()
Adam Langley95c29f32014-06-20 12:00:00 -0700202elseif(MSVC)
Brian Smithefed2212015-01-28 16:20:02 -0800203 set(MSVC_DISABLED_WARNINGS_LIST
204 "C4100" # 'exarg' : unreferenced formal parameter
205 "C4127" # conditional expression is constant
Brian Smithefed2212015-01-28 16:20:02 -0800206 "C4244" # 'function' : conversion from 'int' to 'uint8_t',
207 # possible loss of data
David Benjamin3673be72015-02-11 15:12:05 -0500208 "C4267" # conversion from 'size_t' to 'int', possible loss of data
Brian Smithefed2212015-01-28 16:20:02 -0800209 "C4706" # assignment within conditional expression
David Benjamin7acd6bc2016-05-02 12:57:01 -0400210 )
Brian Smithefed2212015-01-28 16:20:02 -0800211 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
212 ${MSVC_DISABLED_WARNINGS_LIST})
David Benjamin3251ca12023-01-12 16:04:58 -0500213 set(CMAKE_C_FLAGS "-utf-8 -W4 -WX ${MSVC_DISABLED_WARNINGS_STR}")
214 set(CMAKE_CXX_FLAGS "-utf-8 -W4 -WX ${MSVC_DISABLED_WARNINGS_STR}")
David Benjamine2568c42017-08-16 15:25:27 -0400215endif()
216
217if(WIN32)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800218 add_definitions(-D_HAS_EXCEPTIONS=0)
Brian Smitha87de9b2015-01-28 20:34:47 -0800219 add_definitions(-DWIN32_LEAN_AND_MEAN)
David Benjamin0e434b92015-04-02 13:20:01 -0400220 add_definitions(-DNOMINMAX)
David Benjamin9b6ff442017-06-15 20:44:30 -0400221 # Allow use of fopen.
222 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
Adam Langley95c29f32014-06-20 12:00:00 -0700223endif()
224
David Benjamin387f8202022-01-25 12:16:48 -0500225# pthread_rwlock_t on Linux requires a feature flag. We limit this to Linux
226# because, on Apple platforms, it instead disables APIs we use. See compat(5)
227# and sys/cdefs.h. Reportedly, FreeBSD also breaks when this is set. See
228# https://crbug.com/boringssl/471.
229if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
David Benjamin2e8ba2d2016-06-09 16:22:26 -0400230 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
Adam Langley6f2e7332015-05-15 12:01:29 -0700231endif()
232
Adam Langley9a4beb82015-11-09 13:57:26 -0800233if(FUZZ)
David Benjamin02afbd32017-10-05 15:04:08 -0400234 if(NOT CLANG)
Alessandro Ghedinib6f69272016-09-28 22:14:01 +0100235 message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
Adam Langley9a4beb82015-11-09 13:57:26 -0800236 endif()
237
Adam Langley9c969bf2018-08-24 10:46:01 -0700238 if(CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0.0")
239 message(FATAL_ERROR "You need Clang ≥ 6.0.0")
240 endif()
241
David Benjaminec978dd2016-11-04 18:59:33 -0400242 add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
243 set(RUNNER_ARGS "-deterministic")
244
245 if(NOT NO_FUZZER_MODE)
246 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
247 set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
248 endif()
David Benjaminbc5b2a22016-03-01 22:57:32 -0500249
Adam Langley9c969bf2018-08-24 10:46:01 -0700250 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
251 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
Adam Langley9a4beb82015-11-09 13:57:26 -0800252endif()
253
Adam Langleyeb7d2ed2014-07-30 16:02:14 -0700254add_definitions(-DBORINGSSL_IMPLEMENTATION)
255
David Benjamine6fd1252018-08-10 10:30:55 -0500256if(BUILD_SHARED_LIBS)
Adam Langley4a0f0c42015-01-28 16:37:10 -0800257 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
258 # Enable position-independent code globally. This is needed because
259 # some library targets are OBJECT libraries.
260 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
David Benjamin507c1ee2015-01-28 00:50:21 -0500261endif()
262
David Benjamine6fd1252018-08-10 10:30:55 -0500263if(MSAN)
David Benjamin02afbd32017-10-05 15:04:08 -0400264 if(NOT CLANG)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800265 message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
266 endif()
267
David Benjamine6fd1252018-08-10 10:30:55 -0500268 if(ASAN)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800269 message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
270 endif()
271
272 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
273 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
Adam Langleye77c27d2018-09-07 11:20:23 -0700274 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
Adam Langley1bcd10c2016-12-16 10:48:23 -0800275endif()
276
David Benjamine6fd1252018-08-10 10:30:55 -0500277if(ASAN)
David Benjamin02afbd32017-10-05 15:04:08 -0400278 if(NOT CLANG)
Adam Langley1bcd10c2016-12-16 10:48:23 -0800279 message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
280 endif()
281
David Benjamin0d3c9632017-02-28 14:58:00 -0500282 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
283 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 -0800284endif()
285
David Benjaminc367ee52017-11-21 08:16:42 -0500286if(CFI)
287 if(NOT CLANG)
288 message(FATAL_ERROR "Cannot enable CFI unless using Clang")
289 endif()
290
David Benjamin66508982018-09-22 16:19:15 -0500291 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
292 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
David Benjaminc367ee52017-11-21 08:16:42 -0500293 # We use Chromium's copy of clang, which requires -fuse-ld=lld if building
294 # with -flto. That, in turn, can't handle -ggdb.
295 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
296 string(REPLACE "-ggdb" "-g" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
297 string(REPLACE "-ggdb" "-g" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
298 # -flto causes object files to contain LLVM bitcode. Mixing those with
299 # assembly output in the same static library breaks the linker.
300 set(OPENSSL_NO_ASM "1")
301endif()
302
David Benjamin5852cfc2018-07-19 17:50:45 -0400303if(TSAN)
304 if(NOT CLANG)
305 message(FATAL_ERROR "Cannot enable TSAN unless using Clang")
306 endif()
307
308 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
309 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
310 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
311endif()
312
David Benjaminfc27a192019-01-21 07:25:12 +0000313if(UBSAN)
314 if(NOT CLANG)
315 message(FATAL_ERROR "Cannot enable UBSAN unless using Clang")
316 endif()
317
318 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
319 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
320 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
321
322 if(NOT UBSAN_RECOVER)
323 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize-recover=undefined")
324 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize-recover=undefined")
325 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-sanitize-recover=undefined")
326 endif()
327endif()
328
David Benjamine6fd1252018-08-10 10:30:55 -0500329if(GCOV)
David Benjamind035ab32016-12-27 12:15:56 -0500330 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
331 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
332endif()
333
David Benjaminaff72a32017-04-06 23:26:04 -0400334if(FIPS)
335 add_definitions(-DBORINGSSL_FIPS)
Adam Langleyf64a6ee2017-05-17 13:05:50 -0700336 if(FIPS_BREAK_TEST)
337 add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
338 endif()
Adam Langleyd72e47f2019-05-09 16:39:49 -0700339 # The FIPS integrity check does not work for ASan and MSan builds.
Adam Langleyf64a6ee2017-05-17 13:05:50 -0700340 if(NOT ASAN AND NOT MSAN)
Adam Langleyd72e47f2019-05-09 16:39:49 -0700341 if(BUILD_SHARED_LIBS)
342 set(FIPS_SHARED "1")
343 else()
344 set(FIPS_DELOCATE "1")
345 endif()
Adam Langleyf64a6ee2017-05-17 13:05:50 -0700346 endif()
David Benjamine481d942019-10-18 16:43:12 -0400347 if(FIPS_SHARED)
348 # The Android CMake files set -ffunction-sections and -fdata-sections,
349 # which is incompatible with FIPS_SHARED.
350 set(CMAKE_C_FLAGS
351 "${CMAKE_C_FLAGS} -fno-function-sections -fno-data-sections")
352 set(CMAKE_CXX_FLAGS
353 "${CMAKE_CXX_FLAGS} -fno-function-sections -fno-data-sections")
354 endif()
David Benjaminaff72a32017-04-06 23:26:04 -0400355endif()
356
David Benjamin6291af42018-03-23 13:49:27 -0400357if(OPENSSL_SMALL)
358 add_definitions(-DOPENSSL_SMALL)
359endif()
360
Adam Langleya6a049a2018-12-06 17:15:58 -0800361if(CONSTANT_TIME_VALIDATION)
362 add_definitions(-DBORINGSSL_CONSTANT_TIME_VALIDATION)
363 # Asserts will often test secret data.
364 add_definitions(-DNDEBUG)
365endif()
366
David Benjamin582904f2023-02-04 18:30:36 -0500367if(MALLOC_FAILURE_TESTING)
368 add_definitions(-DBORINGSSL_MALLOC_FAILURE_TESTING)
369endif()
370
David Benjamin19676212023-01-25 10:03:53 -0500371if(OPENSSL_NO_ASM)
372 add_definitions(-DOPENSSL_NO_ASM)
373endif()
374
375if(FIPS_DELOCATE OR NOT OPENSSL_NO_ASM)
376 # On x86 and x86_64 Windows, we use the NASM output.
377 if(WIN32 AND CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64|amd64|x86|i[3-6]86")
378 enable_language(ASM_NASM)
379 set(OPENSSL_NASM TRUE)
380 set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -gcv8")
381 else()
382 enable_language(ASM)
383 set(OPENSSL_ASM TRUE)
David Benjamin61266e42023-01-29 15:53:30 -0500384 # Work around https://gitlab.kitware.com/cmake/cmake/-/issues/20771 in older
385 # CMake versions.
386 if(APPLE AND CMAKE_VERSION VERSION_LESS 3.19)
David Benjamin19676212023-01-25 10:03:53 -0500387 if(CMAKE_OSX_SYSROOT)
388 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -isysroot \"${CMAKE_OSX_SYSROOT}\"")
389 endif()
390 foreach(arch ${CMAKE_OSX_ARCHITECTURES})
391 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -arch ${arch}")
392 endforeach()
393 endif()
394 if(NOT WIN32)
395 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
396 endif()
397 # Clang's integerated assembler does not support debug symbols.
398 if(NOT CMAKE_ASM_COMPILER_ID MATCHES "Clang")
399 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,-g")
400 endif()
David Benjaminaff72a32017-04-06 23:26:04 -0400401 endif()
David Benjaminaff72a32017-04-06 23:26:04 -0400402endif()
403
David Benjamin6887d5e2019-12-17 13:56:30 -0500404if(OPENSSL_NO_SSE2_FOR_TESTING)
405 add_definitions(-DOPENSSL_NO_SSE2_FOR_TESTING)
406endif()
407
David Benjamine6fd1252018-08-10 10:30:55 -0500408if(USE_CUSTOM_LIBCXX)
409 if(NOT CLANG)
David Benjamine9ae99b2018-08-09 15:33:07 -0500410 message(FATAL_ERROR "USE_CUSTOM_LIBCXX only supported with Clang")
411 endif()
412
David Benjamine5f72662023-01-29 18:59:45 -0500413 # CMake does not allow installing a library without installing dependencies.
414 # If we installed libcrypto, we'd have to install our custom libc++, which
415 # does not make sense. As this is a test-only configuration, disable
416 # installing.
417 set(INSTALL_ENABLED 0)
418
David Benjamine9ae99b2018-08-09 15:33:07 -0500419 # CMAKE_CXX_FLAGS ends up in the linker flags as well, so use
420 # add_compile_options. There does not appear to be a way to set
421 # language-specific compile-only flags.
422 add_compile_options("-nostdinc++")
423 set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -nostdlib++")
424 include_directories(
425 SYSTEM
David Benjamin16b3af72021-10-26 17:07:39 -0400426 util/bot/libcxx-config
David Benjamine9ae99b2018-08-09 15:33:07 -0500427 util/bot/libcxx/include
428 util/bot/libcxxabi/include
429 )
430
431 # This is patterned after buildtools/third_party/libc++/BUILD.gn and
432 # buildtools/third_party/libc++abi/BUILD.gn in Chromium.
433
434 file(GLOB LIBCXX_SOURCES "util/bot/libcxx/src/*.cpp")
435 file(GLOB LIBCXXABI_SOURCES "util/bot/libcxxabi/src/*.cpp")
436
437 # This file is meant for exception-less builds.
438 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_noexception.cpp")
439 # libc++ also defines new and delete.
440 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/stdlib_new_delete.cpp")
David Benjamine6fd1252018-08-10 10:30:55 -0500441 if(TSAN)
David Benjamine9ae99b2018-08-09 15:33:07 -0500442 # ThreadSanitizer tries to intercept these symbols. Skip them to avoid
443 # symbol conflicts.
444 list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_guard.cpp")
445 endif()
446
447 add_library(libcxxabi ${LIBCXXABI_SOURCES})
448 target_compile_definitions(
449 libcxxabi PRIVATE
450 -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS
451 )
David Benjamine9ae99b2018-08-09 15:33:07 -0500452
453 add_library(libcxx ${LIBCXX_SOURCES})
David Benjamine6fd1252018-08-10 10:30:55 -0500454 if(ASAN OR MSAN OR TSAN)
David Benjamine9ae99b2018-08-09 15:33:07 -0500455 # Sanitizers try to intercept new and delete.
456 target_compile_definitions(
457 libcxx PRIVATE
458 -D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
459 )
460 endif()
461 target_compile_definitions(
462 libcxx PRIVATE
463 -D_LIBCPP_BUILDING_LIBRARY
464 -DLIBCXX_BUILDING_LIBCXXABI
465 )
David Benjaminaa72a6c2022-11-21 15:27:15 -0500466 set_target_properties(
467 libcxx libcxxabi PROPERTIES
468 COMPILE_FLAGS "-Wno-missing-prototypes -Wno-implicit-fallthrough"
469 # libc++ and libc++abi must be built in C++20 mode.
470 CXX_STANDARD 20
471 CXX_STANDARD_REQUIRED TRUE
472 )
473 # libc++abi depends on libc++ internal headers.
David Benjamin8c4ec3b2023-02-27 12:46:12 -0500474 set_property(TARGET libcxx libcxxabi APPEND PROPERTY INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/util/bot/libcxx/src")
David Benjamine9ae99b2018-08-09 15:33:07 -0500475 target_link_libraries(libcxx libcxxabi)
476endif()
477
David Benjamin96628432017-01-19 19:05:47 -0500478# Add minimal googletest targets. The provided one has many side-effects, and
479# googletest has a very straightforward build.
David Benjaminfa343af2023-09-01 21:23:01 -0400480add_library(
481 boringssl_gtest
482 third_party/googletest/googlemock/src/gmock-all.cc
483 third_party/googletest/googletest/src/gtest-all.cc
484)
David Benjamin987dff12023-02-15 11:04:01 -0500485if(USE_CUSTOM_LIBCXX)
486 target_link_libraries(boringssl_gtest libcxx)
487endif()
David Benjamin8bc06cf2023-02-04 20:05:04 -0500488target_include_directories(
489 boringssl_gtest
David Benjaminfa343af2023-09-01 21:23:01 -0400490 PUBLIC
491 third_party/googletest/googlemock/include
492 third_party/googletest/googletest/include
493 PRIVATE
494 third_party/googletest/googlemock
495 third_party/googletest/googletest
David Benjamin8bc06cf2023-02-04 20:05:04 -0500496)
David Benjamin96628432017-01-19 19:05:47 -0500497
David Benjamin301afaf2015-10-14 21:34:40 -0400498# Declare a dummy target to build all unit tests. Test targets should inject
499# themselves as dependencies next to the target definition.
500add_custom_target(all_tests)
501
David Benjamincbac9c32020-06-18 16:14:24 -0400502# On Windows, CRYPTO_TEST_DATA is too long to fit in command-line limits.
503# TODO(davidben): CMake 3.12 has a list(JOIN) command. Use that when we've
504# updated the minimum version.
505set(EMBED_TEST_DATA_ARGS "")
506foreach(arg ${CRYPTO_TEST_DATA})
507 set(EMBED_TEST_DATA_ARGS "${EMBED_TEST_DATA_ARGS}${arg}\n")
508endforeach()
509file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/embed_test_data_args.txt"
510 "${EMBED_TEST_DATA_ARGS}")
511
David Benjamin3ecd0a52017-05-19 15:26:18 -0400512add_custom_command(
513 OUTPUT crypto_test_data.cc
David Benjamincbac9c32020-06-18 16:14:24 -0400514 COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go -file-list
515 "${CMAKE_CURRENT_BINARY_DIR}/embed_test_data_args.txt" >
516 "${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc"
David Benjamin3ecd0a52017-05-19 15:26:18 -0400517 DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
518 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
519
520add_library(crypto_test_data OBJECT crypto_test_data.cc)
521
Adam Langley95c29f32014-06-20 12:00:00 -0700522add_subdirectory(crypto)
523add_subdirectory(ssl)
524add_subdirectory(ssl/test)
Adam Langleyea9fb942022-02-07 12:32:09 -0800525add_subdirectory(util/fipstools)
Adam Langleyb7f0c1b2019-07-09 18:02:14 -0700526add_subdirectory(util/fipstools/acvp/modulewrapper)
Adam Langleyc004dfc2015-02-03 10:45:12 -0800527add_subdirectory(decrepit)
David Benjamin9bbf0d92023-06-30 13:17:03 -0400528
David Benjamin847a3952023-06-30 15:05:24 -0400529add_library(test_support_lib STATIC ${TEST_SUPPORT_SOURCES})
530if (LIBUNWIND_FOUND)
531 target_compile_options(test_support_lib PRIVATE ${LIBUNWIND_CFLAGS_OTHER})
532 target_include_directories(test_support_lib PRIVATE ${LIBUNWIND_INCLUDE_DIRS})
533 target_link_libraries(test_support_lib ${LIBUNWIND_LDFLAGS})
534endif()
535if(WIN32)
536 target_link_libraries(test_support_lib dbghelp)
537endif()
538target_link_libraries(test_support_lib boringssl_gtest crypto)
539
David Benjamin197b5712023-06-30 14:10:42 -0400540# urandom_test is a separate binary because it needs to be able to observe the
541# PRNG initialisation, which means that it can't have other tests running before
542# it does.
543add_executable(urandom_test ${URANDOM_TEST_SOURCES})
544target_link_libraries(urandom_test test_support_lib boringssl_gtest crypto)
545add_dependencies(all_tests urandom_test)
546
547add_executable(crypto_test ${CRYPTO_TEST_SOURCES} $<TARGET_OBJECTS:crypto_test_data>)
David Benjamin847a3952023-06-30 15:05:24 -0400548target_link_libraries(crypto_test test_support_lib boringssl_gtest crypto)
David Benjamin197b5712023-06-30 14:10:42 -0400549add_dependencies(all_tests crypto_test)
550
551add_executable(ssl_test ${SSL_TEST_SOURCES})
David Benjamin847a3952023-06-30 15:05:24 -0400552target_link_libraries(ssl_test test_support_lib boringssl_gtest ssl crypto)
David Benjamin197b5712023-06-30 14:10:42 -0400553add_dependencies(all_tests ssl_test)
554
555add_executable(decrepit_test ${DECREPIT_TEST_SOURCES})
David Benjamin847a3952023-06-30 15:05:24 -0400556target_link_libraries(decrepit_test test_support_lib boringssl_gtest
David Benjamin197b5712023-06-30 14:10:42 -0400557 decrepit crypto)
558add_dependencies(all_tests decrepit_test)
559
David Benjamin9bbf0d92023-06-30 13:17:03 -0400560if(APPLE)
561 set(PKI_CXX_FLAGS "-fno-aligned-new")
562endif()
563
564add_library(pki ${PKI_SOURCES})
David Benjamin9bbf0d92023-06-30 13:17:03 -0400565target_link_libraries(pki crypto)
566
567add_executable(pki_test ${PKI_TEST_SOURCES})
David Benjamin847a3952023-06-30 15:05:24 -0400568target_link_libraries(pki_test test_support_lib boringssl_gtest pki crypto)
David Benjamin9bbf0d92023-06-30 13:17:03 -0400569add_dependencies(all_tests pki_test)
570
571# The PKI library requires C++17.
572set_target_properties(
573 pki pki_test
574 PROPERTIES
575 CXX_STANDARD 17
576 CXX_STANDARD_REQUIRED YES
577 COMPILE_FLAGS "${PKI_CXX_FLAGS}")
David Benjamin301afaf2015-10-14 21:34:40 -0400578
David Benjamin99ce1e02023-06-30 15:43:24 -0400579add_executable(bssl ${BSSL_SOURCES})
580install_if_enabled(TARGETS bssl DESTINATION ${INSTALL_DESTINATION_DEFAULT})
581target_link_libraries(bssl ssl crypto)
582
David Benjamin197b5712023-06-30 14:10:42 -0400583# Historically, targets were built in subdirectories. For compatibility with
584# existing tools, we, for now, copy the targets into the subdirectories. This
585# will be removed sometime in 2024.
586copy_post_build(crypto crypto_test urandom_test)
587copy_post_build(ssl ssl_test)
588copy_post_build(decrepit decrepit_test)
David Benjamin99ce1e02023-06-30 15:43:24 -0400589copy_post_build(tool bssl)
David Benjamin197b5712023-06-30 14:10:42 -0400590
Adam Langley9a4beb82015-11-09 13:57:26 -0800591if(FUZZ)
David Benjamin6c597be2019-01-07 15:13:04 -0600592 if(LIBFUZZER_FROM_DEPS)
593 file(GLOB LIBFUZZER_SOURCES "util/bot/libFuzzer/*.cpp")
594 add_library(Fuzzer STATIC ${LIBFUZZER_SOURCES})
595 # libFuzzer does not pass our aggressive warnings. It also must be built
596 # without -fsanitize-coverage options or clang crashes.
597 set_target_properties(Fuzzer PROPERTIES COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -Wno-missing-prototypes -fsanitize-coverage=0")
598 endif()
599
Adam Langley9a4beb82015-11-09 13:57:26 -0800600 add_subdirectory(fuzz)
601endif()
602
Benjamin Brittainea46caf2022-01-20 15:50:12 -0500603if(RUST_BINDINGS)
604 find_program(BINDGEN_EXECUTABLE bindgen)
605 if(NOT BINDGEN_EXECUTABLE)
606 message(FATAL_ERROR "Could not find bindgen but was asked to generate Rust bindings.")
607 else()
608 add_subdirectory(rust)
609 endif()
610endif()
611
David Benjamin0f4454c2022-01-26 14:17:08 -0500612if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
David Benjamin17dc94e2018-08-10 09:05:20 -0500613 set(HANDSHAKER_ARGS "-handshaker-path" $<TARGET_FILE:handshaker>)
614endif()
615
Adam Langley4f75b762020-12-17 14:06:25 -0800616if(FIPS)
617 add_custom_target(
618 acvp_tests
David Benjamin8c4ec3b2023-02-27 12:46:12 -0500619 COMMAND ${GO_EXECUTABLE} build -o ${CMAKE_CURRENT_BINARY_DIR}/acvptool
Adam Langley4f75b762020-12-17 14:06:25 -0800620 boringssl.googlesource.com/boringssl/util/fipstools/acvp/acvptool
David Benjamin8c4ec3b2023-02-27 12:46:12 -0500621 COMMAND ${GO_EXECUTABLE} build -o ${CMAKE_CURRENT_BINARY_DIR}/testmodulewrapper
Adam Langley4f75b762020-12-17 14:06:25 -0800622 boringssl.googlesource.com/boringssl/util/fipstools/acvp/acvptool/testmodulewrapper
623 COMMAND cd util/fipstools/acvp/acvptool/test &&
624 ${GO_EXECUTABLE} run check_expected.go
David Benjamin8c4ec3b2023-02-27 12:46:12 -0500625 -tool ${CMAKE_CURRENT_BINARY_DIR}/acvptool
626 -module-wrappers modulewrapper:$<TARGET_FILE:modulewrapper>,testmodulewrapper:${CMAKE_CURRENT_BINARY_DIR}/testmodulewrapper
Adam Langley4f75b762020-12-17 14:06:25 -0800627 -tests tests.json
David Benjamin8c4ec3b2023-02-27 12:46:12 -0500628 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
Adam Langley4f75b762020-12-17 14:06:25 -0800629 DEPENDS modulewrapper
630 USES_TERMINAL)
631
632 add_custom_target(
633 fips_specific_tests_if_any
634 DEPENDS acvp_tests
635 )
636else()
637 add_custom_target(fips_specific_tests_if_any)
638endif()
639
David Benjamin49bfec52023-06-30 13:07:24 -0400640file(STRINGS util/go_tests.txt GO_TESTS)
David Benjaminf53ca9f2023-02-24 09:07:36 -0500641set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
642 util/go_tests.txt)
643
David Benjamin301afaf2015-10-14 21:34:40 -0400644add_custom_target(
645 run_tests
David Benjamine3a5fac2023-02-27 16:36:24 -0500646 COMMAND ${GO_EXECUTABLE} test ${GO_TESTS}
David Benjamin301afaf2015-10-14 21:34:40 -0400647 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
David Benjamin8c4ec3b2023-02-27 12:46:12 -0500648 ${CMAKE_CURRENT_BINARY_DIR}
Adam Langleyd5c72c82016-09-23 16:43:17 -0700649 COMMAND cd ssl/test/runner &&
650 ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
David Benjamin17dc94e2018-08-10 09:05:20 -0500651 ${HANDSHAKER_ARGS} ${RUNNER_ARGS}
David Benjamin8c4ec3b2023-02-27 12:46:12 -0500652 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
Adam Langley4f75b762020-12-17 14:06:25 -0800653 DEPENDS all_tests bssl_shim handshaker fips_specific_tests_if_any
David Benjamin81a998a2020-05-26 11:35:58 -0400654 USES_TERMINAL)
Daniel Thornburgh2fc6d382022-04-04 11:23:56 -0700655
David Benjamine5f72662023-01-29 18:59:45 -0500656install_if_enabled(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
Daniel Thornburgh2fc6d382022-04-04 11:23:56 -0700657
David Benjamine5f72662023-01-29 18:59:45 -0500658install_if_enabled(EXPORT OpenSSLTargets
Daniel Thornburgh2fc6d382022-04-04 11:23:56 -0700659 FILE OpenSSLTargets.cmake
660 NAMESPACE OpenSSL::
661 DESTINATION lib/cmake/OpenSSL
662)
David Benjamine5f72662023-01-29 18:59:45 -0500663install_if_enabled(FILES cmake/OpenSSLConfig.cmake DESTINATION lib/cmake/OpenSSL)