• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1cmake_minimum_required(VERSION 3.10)
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)
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  set(C_CXX_FLAGS "-Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings -Wvla -Wshadow -Wtype-limits")
143  if(MSVC)
144    # 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")
148  else()
149    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")
157  endif()
158
159  if(CLANG)
160    set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof -fcolor-diagnostics")
161  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")
165  endif()
166
167  # -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
177  if(CLANG OR CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0.0")
178    set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wimplicit-fallthrough")
179  endif()
180
181  if(CMAKE_COMPILER_IS_GNUCXX)
182    set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wformat-signedness")
183  endif()
184
185  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
186  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_CXX_FLAGS} -Wmissing-declarations")
187
188  if(NOT MSVC AND NOT BORINGSSL_ALLOW_CXX_RUNTIME)
189    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
190  endif()
191
192  # 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
199  if(CLANG)
200    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-prototypes")
201  endif()
202elseif(MSVC)
203  set(MSVC_DISABLED_WARNINGS_LIST
204      "C4100" # 'exarg' : unreferenced formal parameter
205      "C4127" # conditional expression is constant
206      # C4204 and C4221 are C89-only restrictions which were dropped in C99, but
207      # VS2017 warns about it. They can be removed when we require VS2019.
208      "C4204" # nonstandard extension used: non-constant aggregate initializer
209      "C4221" # nonstandard extension used : 'identifier' : cannot be
210              # initialized using address of automatic variable
211      "C4244" # 'function' : conversion from 'int' to 'uint8_t',
212              # possible loss of data
213      "C4267" # conversion from 'size_t' to 'int', possible loss of data
214      "C4706" # assignment within conditional expression
215      )
216  string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
217                            ${MSVC_DISABLED_WARNINGS_LIST})
218  set(CMAKE_C_FLAGS   "-utf-8 -W4 -WX ${MSVC_DISABLED_WARNINGS_STR}")
219  set(CMAKE_CXX_FLAGS "-utf-8 -W4 -WX ${MSVC_DISABLED_WARNINGS_STR}")
220endif()
221
222if(WIN32)
223  add_definitions(-D_HAS_EXCEPTIONS=0)
224  add_definitions(-DWIN32_LEAN_AND_MEAN)
225  add_definitions(-DNOMINMAX)
226  # Allow use of fopen.
227  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
228endif()
229
230# pthread_rwlock_t on Linux requires a feature flag. We limit this to Linux
231# because, on Apple platforms, it instead disables APIs we use. See compat(5)
232# and sys/cdefs.h. Reportedly, FreeBSD also breaks when this is set. See
233# https://crbug.com/boringssl/471.
234if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
235  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
236endif()
237
238if(FUZZ)
239  if(NOT CLANG)
240    message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
241  endif()
242
243  if(CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0.0")
244    message(FATAL_ERROR "You need Clang ≥ 6.0.0")
245  endif()
246
247  add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
248  set(RUNNER_ARGS "-deterministic")
249
250  if(NOT NO_FUZZER_MODE)
251    add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
252    set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
253  endif()
254
255  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
256  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
257endif()
258
259add_definitions(-DBORINGSSL_IMPLEMENTATION)
260
261if(BUILD_SHARED_LIBS)
262  add_definitions(-DBORINGSSL_SHARED_LIBRARY)
263  # Enable position-independent code globally. This is needed because
264  # some library targets are OBJECT libraries.
265  set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
266endif()
267
268if(MSAN)
269  if(NOT CLANG)
270    message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
271  endif()
272
273  if(ASAN)
274    message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
275  endif()
276
277  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
278  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
279  set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
280endif()
281
282if(ASAN)
283  if(NOT CLANG)
284    message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
285  endif()
286
287  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
288  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
289endif()
290
291if(CFI)
292  if(NOT CLANG)
293    message(FATAL_ERROR "Cannot enable CFI unless using Clang")
294  endif()
295
296  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
297  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
298  # We use Chromium's copy of clang, which requires -fuse-ld=lld if building
299  # with -flto. That, in turn, can't handle -ggdb.
300  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
301  string(REPLACE "-ggdb" "-g" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
302  string(REPLACE "-ggdb" "-g" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
303  # -flto causes object files to contain LLVM bitcode. Mixing those with
304  # assembly output in the same static library breaks the linker.
305  set(OPENSSL_NO_ASM "1")
306endif()
307
308if(TSAN)
309  if(NOT CLANG)
310    message(FATAL_ERROR "Cannot enable TSAN unless using Clang")
311  endif()
312
313  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
314  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
315  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
316endif()
317
318if(UBSAN)
319  if(NOT CLANG)
320    message(FATAL_ERROR "Cannot enable UBSAN unless using Clang")
321  endif()
322
323  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
324  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
325  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
326
327  if(NOT UBSAN_RECOVER)
328    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize-recover=undefined")
329    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize-recover=undefined")
330    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-sanitize-recover=undefined")
331  endif()
332endif()
333
334if(GCOV)
335  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
336  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
337endif()
338
339if(FIPS)
340  add_definitions(-DBORINGSSL_FIPS)
341  if(FIPS_BREAK_TEST)
342    add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
343  endif()
344  # The FIPS integrity check does not work for ASan and MSan builds.
345  if(NOT ASAN AND NOT MSAN)
346    if(BUILD_SHARED_LIBS)
347      set(FIPS_SHARED "1")
348    else()
349      set(FIPS_DELOCATE "1")
350    endif()
351  endif()
352  if(FIPS_SHARED)
353    # The Android CMake files set -ffunction-sections and -fdata-sections,
354    # which is incompatible with FIPS_SHARED.
355    set(CMAKE_C_FLAGS
356        "${CMAKE_C_FLAGS} -fno-function-sections -fno-data-sections")
357    set(CMAKE_CXX_FLAGS
358        "${CMAKE_CXX_FLAGS} -fno-function-sections -fno-data-sections")
359  endif()
360endif()
361
362if(OPENSSL_SMALL)
363  add_definitions(-DOPENSSL_SMALL)
364endif()
365
366if(CONSTANT_TIME_VALIDATION)
367  add_definitions(-DBORINGSSL_CONSTANT_TIME_VALIDATION)
368  # Asserts will often test secret data.
369  add_definitions(-DNDEBUG)
370endif()
371
372if(MALLOC_FAILURE_TESTING)
373  add_definitions(-DBORINGSSL_MALLOC_FAILURE_TESTING)
374endif()
375
376if(OPENSSL_NO_ASM)
377  add_definitions(-DOPENSSL_NO_ASM)
378endif()
379
380if(FIPS_DELOCATE OR NOT OPENSSL_NO_ASM)
381  # On x86 and x86_64 Windows, we use the NASM output.
382  if(WIN32 AND CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64|amd64|x86|i[3-6]86")
383    enable_language(ASM_NASM)
384    set(OPENSSL_NASM TRUE)
385    set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -gcv8")
386  else()
387    enable_language(ASM)
388    set(OPENSSL_ASM TRUE)
389    # Work around https://gitlab.kitware.com/cmake/cmake/-/issues/20771 in older
390    # CMake versions.
391    if(APPLE AND CMAKE_VERSION VERSION_LESS 3.19)
392      if(CMAKE_OSX_SYSROOT)
393        set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -isysroot \"${CMAKE_OSX_SYSROOT}\"")
394      endif()
395      foreach(arch ${CMAKE_OSX_ARCHITECTURES})
396        set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -arch ${arch}")
397      endforeach()
398    endif()
399    if(NOT WIN32)
400      set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
401    endif()
402    # Clang's integerated assembler does not support debug symbols.
403    if(NOT CMAKE_ASM_COMPILER_ID MATCHES "Clang")
404      set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,-g")
405    endif()
406  endif()
407endif()
408
409if(OPENSSL_NO_SSE2_FOR_TESTING)
410  add_definitions(-DOPENSSL_NO_SSE2_FOR_TESTING)
411endif()
412
413if(USE_CUSTOM_LIBCXX)
414  if(NOT CLANG)
415    message(FATAL_ERROR "USE_CUSTOM_LIBCXX only supported with Clang")
416  endif()
417
418  # CMake does not allow installing a library without installing dependencies.
419  # If we installed libcrypto, we'd have to install our custom libc++, which
420  # does not make sense. As this is a test-only configuration, disable
421  # installing.
422  set(INSTALL_ENABLED 0)
423
424  # CMAKE_CXX_FLAGS ends up in the linker flags as well, so use
425  # add_compile_options. There does not appear to be a way to set
426  # language-specific compile-only flags.
427  add_compile_options("-nostdinc++")
428  set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -nostdlib++")
429  include_directories(
430    SYSTEM
431    util/bot/libcxx-config
432    util/bot/libcxx/include
433    util/bot/libcxxabi/include
434  )
435
436  # This is patterned after buildtools/third_party/libc++/BUILD.gn and
437  # buildtools/third_party/libc++abi/BUILD.gn in Chromium.
438
439  file(GLOB LIBCXX_SOURCES "util/bot/libcxx/src/*.cpp")
440  file(GLOB LIBCXXABI_SOURCES "util/bot/libcxxabi/src/*.cpp")
441
442  # This file is meant for exception-less builds.
443  list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_noexception.cpp")
444  # libc++ also defines new and delete.
445  list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/stdlib_new_delete.cpp")
446  if(TSAN)
447    # ThreadSanitizer tries to intercept these symbols. Skip them to avoid
448    # symbol conflicts.
449    list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_guard.cpp")
450  endif()
451
452  add_library(libcxxabi ${LIBCXXABI_SOURCES})
453  target_compile_definitions(
454    libcxxabi PRIVATE
455    -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS
456  )
457
458  add_library(libcxx ${LIBCXX_SOURCES})
459  if(ASAN OR MSAN OR TSAN)
460    # Sanitizers try to intercept new and delete.
461    target_compile_definitions(
462      libcxx PRIVATE
463      -D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
464    )
465  endif()
466  target_compile_definitions(
467    libcxx PRIVATE
468    -D_LIBCPP_BUILDING_LIBRARY
469    -DLIBCXX_BUILDING_LIBCXXABI
470  )
471  set_target_properties(
472    libcxx libcxxabi PROPERTIES
473    COMPILE_FLAGS "-Wno-missing-prototypes -Wno-implicit-fallthrough"
474    # libc++ and libc++abi must be built in C++20 mode.
475    CXX_STANDARD 20
476    CXX_STANDARD_REQUIRED TRUE
477  )
478  # libc++abi depends on libc++ internal headers.
479  set_property(TARGET libcxx libcxxabi APPEND PROPERTY INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/util/bot/libcxx/src")
480  target_link_libraries(libcxx libcxxabi)
481endif()
482
483# Add minimal googletest targets. The provided one has many side-effects, and
484# googletest has a very straightforward build.
485add_library(boringssl_gtest third_party/googletest/src/gtest-all.cc)
486if(USE_CUSTOM_LIBCXX)
487  target_link_libraries(boringssl_gtest libcxx)
488endif()
489target_include_directories(
490    boringssl_gtest
491    PUBLIC third_party/googletest/include
492    PRIVATE third_party/googletest
493)
494
495# Declare a dummy target to build all unit tests. Test targets should inject
496# themselves as dependencies next to the target definition.
497add_custom_target(all_tests)
498
499# On Windows, CRYPTO_TEST_DATA is too long to fit in command-line limits.
500# TODO(davidben): CMake 3.12 has a list(JOIN) command. Use that when we've
501# updated the minimum version.
502set(EMBED_TEST_DATA_ARGS "")
503foreach(arg ${CRYPTO_TEST_DATA})
504  set(EMBED_TEST_DATA_ARGS "${EMBED_TEST_DATA_ARGS}${arg}\n")
505endforeach()
506file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/embed_test_data_args.txt"
507     "${EMBED_TEST_DATA_ARGS}")
508
509add_custom_command(
510  OUTPUT crypto_test_data.cc
511  COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go -file-list
512          "${CMAKE_CURRENT_BINARY_DIR}/embed_test_data_args.txt" >
513          "${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc"
514  DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
515  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
516
517add_library(crypto_test_data OBJECT crypto_test_data.cc)
518
519add_subdirectory(crypto)
520add_subdirectory(ssl)
521add_subdirectory(ssl/test)
522add_subdirectory(tool)
523add_subdirectory(util/fipstools)
524add_subdirectory(util/fipstools/acvp/modulewrapper)
525add_subdirectory(decrepit)
526
527if(FUZZ)
528  if(LIBFUZZER_FROM_DEPS)
529    file(GLOB LIBFUZZER_SOURCES "util/bot/libFuzzer/*.cpp")
530    add_library(Fuzzer STATIC ${LIBFUZZER_SOURCES})
531    # libFuzzer does not pass our aggressive warnings. It also must be built
532    # without -fsanitize-coverage options or clang crashes.
533    set_target_properties(Fuzzer PROPERTIES COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -Wno-missing-prototypes -fsanitize-coverage=0")
534  endif()
535
536  add_subdirectory(fuzz)
537endif()
538
539if(RUST_BINDINGS)
540  find_program(BINDGEN_EXECUTABLE bindgen)
541  if(NOT BINDGEN_EXECUTABLE)
542    message(FATAL_ERROR "Could not find bindgen but was asked to generate Rust bindings.")
543  else()
544    add_subdirectory(rust)
545  endif()
546endif()
547
548if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
549  set(HANDSHAKER_ARGS "-handshaker-path" $<TARGET_FILE:handshaker>)
550endif()
551
552if(FIPS)
553  add_custom_target(
554    acvp_tests
555    COMMAND ${GO_EXECUTABLE} build -o ${CMAKE_CURRENT_BINARY_DIR}/acvptool
556            boringssl.googlesource.com/boringssl/util/fipstools/acvp/acvptool
557    COMMAND ${GO_EXECUTABLE} build -o ${CMAKE_CURRENT_BINARY_DIR}/testmodulewrapper
558            boringssl.googlesource.com/boringssl/util/fipstools/acvp/acvptool/testmodulewrapper
559    COMMAND cd util/fipstools/acvp/acvptool/test &&
560            ${GO_EXECUTABLE} run check_expected.go
561            -tool ${CMAKE_CURRENT_BINARY_DIR}/acvptool
562            -module-wrappers modulewrapper:$<TARGET_FILE:modulewrapper>,testmodulewrapper:${CMAKE_CURRENT_BINARY_DIR}/testmodulewrapper
563            -tests tests.json
564    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
565    DEPENDS modulewrapper
566    USES_TERMINAL)
567
568  add_custom_target(
569    fips_specific_tests_if_any
570    DEPENDS acvp_tests
571  )
572else()
573  add_custom_target(fips_specific_tests_if_any)
574endif()
575
576# Read util/go_tests.txt into a CMake variable.
577file(READ util/go_tests.txt GO_TESTS)
578string(REPLACE "\n" ";" GO_TESTS "${GO_TESTS}")
579list(REMOVE_ITEM GO_TESTS "")
580set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
581             util/go_tests.txt)
582
583add_custom_target(
584    run_tests
585    COMMAND ${GO_EXECUTABLE} test ${GO_TESTS}
586    COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
587            ${CMAKE_CURRENT_BINARY_DIR}
588    COMMAND cd ssl/test/runner &&
589            ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
590              ${HANDSHAKER_ARGS} ${RUNNER_ARGS}
591    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
592    DEPENDS all_tests bssl_shim handshaker fips_specific_tests_if_any
593    USES_TERMINAL)
594
595install_if_enabled(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
596
597install_if_enabled(EXPORT OpenSSLTargets
598  FILE OpenSSLTargets.cmake
599  NAMESPACE OpenSSL::
600  DESTINATION lib/cmake/OpenSSL
601)
602install_if_enabled(FILES cmake/OpenSSLConfig.cmake DESTINATION lib/cmake/OpenSSL)
603