• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1cmake_minimum_required(VERSION 3.12)
2
3# Defer enabling C and CXX languages.
4project(BoringSSL NONE)
5
6# 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
12if(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
18include(sources.cmake)
19include(cmake/go.cmake)
20include(cmake/paths.cmake)
21include(cmake/perlasm.cmake)
22
23enable_language(C)
24enable_language(CXX)
25
26include(GNUInstallDirs)
27
28# 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
46if(ANDROID)
47  # 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.
49  if(NOT PERL_EXECUTABLE)
50    set(PERL_EXECUTABLE "perl")
51  endif()
52else()
53  find_package(Perl REQUIRED)
54endif()
55
56if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT CMAKE_CROSSCOMPILING)
57  find_package(PkgConfig QUIET)
58  if (PkgConfig_FOUND)
59    pkg_check_modules(LIBUNWIND libunwind-generic>=1.3.0)
60    if(LIBUNWIND_FOUND)
61      add_definitions(-DBORINGSSL_HAVE_LIBUNWIND)
62    else()
63      message("libunwind not found. Disabling unwind tests.")
64    endif()
65  else()
66    message("pkgconfig not found. Disabling unwind tests.")
67  endif()
68endif()
69
70if(USE_CUSTOM_LIBCXX)
71  set(BORINGSSL_ALLOW_CXX_RUNTIME 1)
72endif()
73
74if(BORINGSSL_ALLOW_CXX_RUNTIME)
75  add_definitions(-DBORINGSSL_ALLOW_CXX_RUNTIME)
76endif()
77
78string(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()
87endif()
88
89# 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
96if(BORINGSSL_PREFIX AND BORINGSSL_PREFIX_SYMBOLS)
97  add_definitions(-DBORINGSSL_PREFIX=${BORINGSSL_PREFIX})
98  # 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}")
101
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
111            ${BORINGSSL_PREFIX_SYMBOLS})
112
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)
120elseif(BORINGSSL_PREFIX OR BORINGSSL_PREFIX_SYMBOLS)
121  message(FATAL_ERROR "Must specify both or neither of BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS")
122else()
123  add_custom_target(boringssl_prefix_symbols)
124endif()
125
126if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
127  set(CLANG 1)
128endif()
129
130if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
131  set(EMSCRIPTEN 1)
132endif()
133
134set(CMAKE_CXX_STANDARD 14)
135set(CMAKE_CXX_STANDARD_REQUIRED ON)
136set(CMAKE_C_STANDARD 11)
137set(CMAKE_C_STANDARD_REQUIRED ON)
138
139if(CMAKE_COMPILER_IS_GNUCXX OR CLANG)
140  # Note clang-cl is odd and sets both CLANG and MSVC. We base our configuration
141  # primarily on our normal Clang one.
142  # 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")
144  if(MSVC)
145    # 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")
149  else()
150    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")
158  endif()
159
160  if(CLANG)
161    set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof -fcolor-diagnostics")
162  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")
166  endif()
167
168  # -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
178  if(CLANG OR CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0.0")
179    set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wimplicit-fallthrough")
180  endif()
181
182  if(CMAKE_COMPILER_IS_GNUCXX)
183    set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wformat-signedness")
184  endif()
185
186  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
187  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_CXX_FLAGS} -Wmissing-declarations")
188
189  if(NOT MSVC AND NOT BORINGSSL_ALLOW_CXX_RUNTIME)
190    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
191  endif()
192
193  # 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
200  if(CLANG)
201    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-prototypes")
202  endif()
203elseif(MSVC)
204  set(MSVC_DISABLED_WARNINGS_LIST
205      "C4100" # 'exarg' : unreferenced formal parameter
206      "C4127" # conditional expression is constant
207      "C4244" # 'function' : conversion from 'int' to 'uint8_t',
208              # possible loss of data
209      "C4267" # conversion from 'size_t' to 'int', possible loss of data
210      "C4706" # assignment within conditional expression
211      )
212  string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
213                            ${MSVC_DISABLED_WARNINGS_LIST})
214  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}")
216endif()
217
218if(WIN32)
219  add_definitions(-D_HAS_EXCEPTIONS=0)
220  add_definitions(-DWIN32_LEAN_AND_MEAN)
221  add_definitions(-DNOMINMAX)
222  # Allow use of fopen.
223  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
224endif()
225
226# 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")
231  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
232endif()
233
234if(FUZZ)
235  if(NOT CLANG)
236    message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
237  endif()
238
239  if(CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0.0")
240    message(FATAL_ERROR "You need Clang ≥ 6.0.0")
241  endif()
242
243  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()
250
251  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")
253endif()
254
255add_definitions(-DBORINGSSL_IMPLEMENTATION)
256
257if(BUILD_SHARED_LIBS)
258  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)
262endif()
263
264if(MSAN)
265  if(NOT CLANG)
266    message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
267  endif()
268
269  if(ASAN)
270    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")
275  set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
276endif()
277
278if(ASAN)
279  if(NOT CLANG)
280    message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
281  endif()
282
283  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")
285endif()
286
287if(CFI)
288  if(NOT CLANG)
289    message(FATAL_ERROR "Cannot enable CFI unless using Clang")
290  endif()
291
292  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")
294  # 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
304if(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
314if(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
330if(GCOV)
331  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
335if(FIPS)
336  add_definitions(-DBORINGSSL_FIPS)
337  if(FIPS_BREAK_TEST)
338    add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
339  endif()
340  # The FIPS integrity check does not work for ASan and MSan builds.
341  if(NOT ASAN AND NOT MSAN)
342    if(BUILD_SHARED_LIBS)
343      set(FIPS_SHARED "1")
344    else()
345      set(FIPS_DELOCATE "1")
346    endif()
347  endif()
348  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()
356endif()
357
358if(OPENSSL_SMALL)
359  add_definitions(-DOPENSSL_SMALL)
360endif()
361
362if(CONSTANT_TIME_VALIDATION)
363  add_definitions(-DBORINGSSL_CONSTANT_TIME_VALIDATION)
364  # Asserts will often test secret data.
365  add_definitions(-DNDEBUG)
366endif()
367
368if(MALLOC_FAILURE_TESTING)
369  add_definitions(-DBORINGSSL_MALLOC_FAILURE_TESTING)
370endif()
371
372if(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)
385    # 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)
388      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()
402  endif()
403endif()
404
405if(OPENSSL_NO_SSE2_FOR_TESTING)
406  add_definitions(-DOPENSSL_NO_SSE2_FOR_TESTING)
407endif()
408
409if(USE_CUSTOM_LIBCXX)
410  if(NOT CLANG)
411    message(FATAL_ERROR "USE_CUSTOM_LIBCXX only supported with Clang")
412  endif()
413
414  # 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
420  # 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
427    util/bot/libcxx-config
428    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")
442  if(TSAN)
443    # 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  )
453
454  add_library(libcxx ${LIBCXX_SOURCES})
455  if(ASAN OR MSAN OR TSAN)
456    # 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  )
467  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.
475  set_property(TARGET libcxx libcxxabi APPEND PROPERTY INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/util/bot/libcxx/src")
476  target_link_libraries(libcxx libcxxabi)
477endif()
478
479# Add minimal googletest targets. The provided one has many side-effects, and
480# googletest has a very straightforward build.
481add_library(
482  boringssl_gtest
483  third_party/googletest/googlemock/src/gmock-all.cc
484  third_party/googletest/googletest/src/gtest-all.cc
485)
486if(USE_CUSTOM_LIBCXX)
487  target_link_libraries(boringssl_gtest libcxx)
488endif()
489target_include_directories(
490    boringssl_gtest
491    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
497)
498
499# 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
503# 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
513add_custom_command(
514  OUTPUT crypto_test_data.cc
515  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"
518  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
523add_subdirectory(crypto)
524add_subdirectory(ssl)
525add_subdirectory(ssl/test)
526add_subdirectory(util/fipstools)
527add_subdirectory(util/fipstools/acvp/modulewrapper)
528add_subdirectory(decrepit)
529
530add_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
541# 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>)
549target_link_libraries(crypto_test test_support_lib boringssl_gtest crypto)
550add_dependencies(all_tests crypto_test)
551
552add_executable(ssl_test ${SSL_TEST_SOURCES})
553target_link_libraries(ssl_test test_support_lib boringssl_gtest ssl crypto)
554add_dependencies(all_tests ssl_test)
555
556add_executable(decrepit_test ${DECREPIT_TEST_SOURCES})
557target_link_libraries(decrepit_test test_support_lib boringssl_gtest
558                      decrepit crypto)
559add_dependencies(all_tests decrepit_test)
560
561if(APPLE)
562  set(PKI_CXX_FLAGS "-fno-aligned-new")
563endif()
564
565add_library(pki ${PKI_SOURCES})
566target_compile_definitions(pki PRIVATE _BORINGSSL_LIBPKI_)
567target_link_libraries(pki crypto)
568
569add_executable(pki_test ${PKI_TEST_SOURCES})
570target_compile_definitions(pki_test PRIVATE _BORINGSSL_LIBPKI_)
571target_link_libraries(pki_test test_support_lib boringssl_gtest pki crypto)
572add_dependencies(all_tests pki_test)
573
574# The PKI library requires C++17.
575set_target_properties(
576  pki pki_test
577  PROPERTIES
578  CXX_STANDARD 17
579  CXX_STANDARD_REQUIRED YES
580  COMPILE_FLAGS "${PKI_CXX_FLAGS}")
581
582add_executable(bssl ${BSSL_SOURCES})
583install_if_enabled(TARGETS bssl DESTINATION ${INSTALL_DESTINATION_DEFAULT})
584target_link_libraries(bssl ssl crypto)
585
586# Historically, targets were built in subdirectories. For compatibility with
587# existing tools, we, for now, copy the targets into the subdirectories. This
588# will be removed sometime in 2024.
589copy_post_build(crypto crypto_test urandom_test)
590copy_post_build(ssl ssl_test)
591copy_post_build(decrepit decrepit_test)
592copy_post_build(tool bssl)
593
594if(FUZZ)
595  if(LIBFUZZER_FROM_DEPS)
596    file(GLOB LIBFUZZER_SOURCES "util/bot/libFuzzer/*.cpp")
597    add_library(Fuzzer STATIC ${LIBFUZZER_SOURCES})
598    # libFuzzer does not pass our aggressive warnings. It also must be built
599    # without -fsanitize-coverage options or clang crashes.
600    set_target_properties(Fuzzer PROPERTIES COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -Wno-missing-prototypes -fsanitize-coverage=0")
601  endif()
602
603  add_subdirectory(fuzz)
604endif()
605
606if(RUST_BINDINGS)
607  find_program(BINDGEN_EXECUTABLE bindgen)
608  if(NOT BINDGEN_EXECUTABLE)
609    message(FATAL_ERROR "Could not find bindgen but was asked to generate Rust bindings.")
610  else()
611    add_subdirectory(rust)
612  endif()
613endif()
614
615if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
616  set(HANDSHAKER_ARGS "-handshaker-path" $<TARGET_FILE:handshaker>)
617endif()
618
619if(FIPS)
620  add_custom_target(
621    acvp_tests
622    COMMAND ${GO_EXECUTABLE} build -o ${CMAKE_CURRENT_BINARY_DIR}/acvptool
623            boringssl.googlesource.com/boringssl/util/fipstools/acvp/acvptool
624    COMMAND ${GO_EXECUTABLE} build -o ${CMAKE_CURRENT_BINARY_DIR}/testmodulewrapper
625            boringssl.googlesource.com/boringssl/util/fipstools/acvp/acvptool/testmodulewrapper
626    COMMAND cd util/fipstools/acvp/acvptool/test &&
627            ${GO_EXECUTABLE} run check_expected.go
628            -tool ${CMAKE_CURRENT_BINARY_DIR}/acvptool
629            -module-wrappers modulewrapper:$<TARGET_FILE:modulewrapper>,testmodulewrapper:${CMAKE_CURRENT_BINARY_DIR}/testmodulewrapper
630            -tests tests.json
631    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
632    DEPENDS modulewrapper
633    USES_TERMINAL)
634
635  add_custom_target(
636    fips_specific_tests_if_any
637    DEPENDS acvp_tests
638  )
639else()
640  add_custom_target(fips_specific_tests_if_any)
641endif()
642
643file(STRINGS util/go_tests.txt GO_TESTS)
644set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
645             util/go_tests.txt)
646
647add_custom_target(
648    run_tests
649    COMMAND ${GO_EXECUTABLE} test ${GO_TESTS}
650    COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
651            ${CMAKE_CURRENT_BINARY_DIR}
652    COMMAND cd ssl/test/runner &&
653            ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
654              ${HANDSHAKER_ARGS} ${RUNNER_ARGS}
655    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
656    DEPENDS all_tests bssl_shim handshaker fips_specific_tests_if_any
657    USES_TERMINAL)
658
659install_if_enabled(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
660
661install_if_enabled(EXPORT OpenSSLTargets
662  FILE OpenSSLTargets.cmake
663  NAMESPACE OpenSSL::
664  DESTINATION lib/cmake/OpenSSL
665)
666install_if_enabled(FILES cmake/OpenSSLConfig.cmake DESTINATION lib/cmake/OpenSSL)
667