• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1cmake_minimum_required(VERSION 3.5)
2
3# Defer enabling C and CXX languages.
4project(BoringSSL NONE)
5
6if(WIN32)
7  # On Windows, prefer cl over gcc if both are available. By default most of
8  # the CMake generators prefer gcc, even on Windows.
9  set(CMAKE_GENERATOR_CC cl)
10endif()
11
12include(sources.cmake)
13
14enable_language(C)
15enable_language(CXX)
16
17# This is a dummy target which all other targets depend on (manually - see other
18# CMakeLists.txt files). This gives us a hook to add any targets which need to
19# run before all other targets.
20add_custom_target(global_target)
21
22if(ANDROID)
23  # Android-NDK CMake files reconfigure the path and so Go and Perl won't be
24  # found. However, ninja will still find them in $PATH if we just name them.
25  if(NOT PERL_EXECUTABLE)
26    set(PERL_EXECUTABLE "perl")
27  endif()
28  if(NOT GO_EXECUTABLE)
29    set(GO_EXECUTABLE "go")
30  endif()
31else()
32  find_package(Perl REQUIRED)
33  find_program(GO_EXECUTABLE go)
34endif()
35
36if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT CMAKE_CROSSCOMPILING)
37  find_package(PkgConfig QUIET)
38  if (PkgConfig_FOUND)
39    pkg_check_modules(LIBUNWIND libunwind-generic)
40    if(LIBUNWIND_FOUND)
41      add_definitions(-DBORINGSSL_HAVE_LIBUNWIND)
42    else()
43      message("libunwind not found. Disabling unwind tests.")
44    endif()
45  else()
46    message("pkgconfig not found. Disabling unwind tests.")
47  endif()
48endif()
49
50if(NOT GO_EXECUTABLE)
51  message(FATAL_ERROR "Could not find Go")
52endif()
53
54if(USE_CUSTOM_LIBCXX)
55  set(BORINGSSL_ALLOW_CXX_RUNTIME 1)
56endif()
57
58if(BORINGSSL_ALLOW_CXX_RUNTIME)
59  add_definitions(-DBORINGSSL_ALLOW_CXX_RUNTIME)
60endif()
61
62string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
63if(NOT FIPS)
64  if(CMAKE_BUILD_TYPE_LOWER STREQUAL "relwithassert" OR
65     NOT CMAKE_BUILD_TYPE_LOWER MATCHES "rel")
66    add_definitions(-DBORINGSSL_DISPATCH_TEST)
67    # CMake automatically connects include_directories to the NASM
68    # command-line, but not add_definitions.
69    set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -DBORINGSSL_DISPATCH_TEST")
70  endif()
71endif()
72
73# Add a RelWithAsserts build configuration. It is the same as Release, except it
74# does not define NDEBUG, so asserts run.
75foreach(VAR CMAKE_C_FLAGS CMAKE_CXX_FLAGS CMAKE_ASM_FLAGS)
76  string(REGEX REPLACE "(^| )[/-]DNDEBUG( |$)" " " "${VAR}_RELWITHASSERTS"
77         "${${VAR}_RELEASE}")
78endforeach()
79
80if(BORINGSSL_PREFIX AND BORINGSSL_PREFIX_SYMBOLS)
81  add_definitions(-DBORINGSSL_PREFIX=${BORINGSSL_PREFIX})
82  # CMake automatically connects include_directories to the NASM command-line,
83  # but not add_definitions.
84  set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -DBORINGSSL_PREFIX=${BORINGSSL_PREFIX}")
85
86  # Use "symbol_prefix_include" to store generated header files
87  include_directories(${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include)
88  add_custom_command(
89    OUTPUT symbol_prefix_include/boringssl_prefix_symbols.h
90           symbol_prefix_include/boringssl_prefix_symbols_asm.h
91           symbol_prefix_include/boringssl_prefix_symbols_nasm.inc
92    COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include
93    COMMAND ${GO_EXECUTABLE} run ${CMAKE_CURRENT_SOURCE_DIR}/util/make_prefix_headers.go -out ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include ${BORINGSSL_PREFIX_SYMBOLS}
94    DEPENDS util/make_prefix_headers.go
95            ${CMAKE_BINARY_DIR}/${BORINGSSL_PREFIX_SYMBOLS})
96
97  # add_dependencies needs a target, not a file, so we add an intermediate
98  # target.
99  add_custom_target(
100    boringssl_prefix_symbols
101    DEPENDS symbol_prefix_include/boringssl_prefix_symbols.h
102            symbol_prefix_include/boringssl_prefix_symbols_asm.h
103            symbol_prefix_include/boringssl_prefix_symbols_nasm.inc)
104  add_dependencies(global_target boringssl_prefix_symbols)
105elseif(BORINGSSL_PREFIX OR BORINGSSL_PREFIX_SYMBOLS)
106  message(FATAL_ERROR "Must specify both or neither of BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS")
107endif()
108
109if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
110  set(CLANG 1)
111endif()
112
113if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
114  set(EMSCRIPTEN 1)
115endif()
116
117if(CMAKE_COMPILER_IS_GNUCXX OR CLANG)
118  # Note clang-cl is odd and sets both CLANG and MSVC. We base our configuration
119  # primarily on our normal Clang one.
120  set(C_CXX_FLAGS "-Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings -Wvla -Wshadow")
121  if(MSVC)
122    # clang-cl sets different default warnings than clang. It also treats -Wall
123    # as -Weverything, to match MSVC. Instead -W3 is the alias for -Wall.
124    # See http://llvm.org/viewvc/llvm-project?view=revision&revision=319116
125    set(C_CXX_FLAGS "${C_CXX_FLAGS} -W3 -Wno-unused-parameter -fmsc-version=1900")
126    # googletest suppresses warning C4996 via a pragma, but clang-cl does not
127    # honor it. Suppress it here to compensate. See https://crbug.com/772117.
128    set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-deprecated-declarations")
129  else()
130    if(EMSCRIPTEN)
131      # emscripten's emcc/clang does not accept the "-ggdb" flag.
132      set(C_CXX_FLAGS "${C_CXX_FLAGS} -g")
133    else()
134      set(C_CXX_FLAGS "${C_CXX_FLAGS} -ggdb")
135    endif()
136
137    set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wall -fvisibility=hidden -fno-common")
138  endif()
139
140  if(CLANG)
141    set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof -fcolor-diagnostics")
142  else()
143    # GCC (at least 4.8.4) has a bug where it'll find unreachable free() calls
144    # and declare that the code is trying to free a stack pointer.
145    set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-free-nonheap-object")
146  endif()
147
148  if(CLANG OR NOT "7.0.0" VERSION_GREATER CMAKE_C_COMPILER_VERSION)
149    set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wimplicit-fallthrough")
150  endif()
151
152  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
153  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_CXX_FLAGS} -Wmissing-declarations")
154
155  if(NOT MSVC)
156    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
157    if(APPLE)
158      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
159    endif()
160    if(NOT BORINGSSL_ALLOW_CXX_RUNTIME)
161      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
162    endif()
163  endif()
164
165  # In GCC, -Wmissing-declarations is the C++ spelling of -Wmissing-prototypes
166  # and using the wrong one is an error. In Clang, -Wmissing-prototypes is the
167  # spelling for both and -Wmissing-declarations is some other warning.
168  #
169  # https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Warning-Options.html#Warning-Options
170  # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-prototypes
171  # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-declarations
172  if(CLANG)
173    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-prototypes")
174  endif()
175elseif(MSVC)
176  set(MSVC_DISABLED_WARNINGS_LIST
177      "C4061" # enumerator 'identifier' in switch of enum 'enumeration' is not
178              # explicitly handled by a case label
179              # Disable this because it flags even when there is a default.
180      "C4100" # 'exarg' : unreferenced formal parameter
181      "C4127" # conditional expression is constant
182      "C4200" # nonstandard extension used : zero-sized array in
183              # struct/union.
184      "C4204" # nonstandard extension used: non-constant aggregate initializer
185      "C4221" # nonstandard extension used : 'identifier' : cannot be
186              # initialized using address of automatic variable
187      "C4242" # 'function' : conversion from 'int' to 'uint8_t',
188              # possible loss of data
189      "C4244" # 'function' : conversion from 'int' to 'uint8_t',
190              # possible loss of data
191      "C4267" # conversion from 'size_t' to 'int', possible loss of data
192      "C4371" # layout of class may have changed from a previous version of the
193              # compiler due to better packing of member '...'
194      "C4388" # signed/unsigned mismatch
195      "C4296" # '>=' : expression is always true
196      "C4350" # behavior change: 'std::_Wrap_alloc...'
197      "C4365" # '=' : conversion from 'size_t' to 'int',
198              # signed/unsigned mismatch
199      "C4389" # '!=' : signed/unsigned mismatch
200      "C4464" # relative include path contains '..'
201      "C4510" # 'argument' : default constructor could not be generated
202      "C4512" # 'argument' : assignment operator could not be generated
203      "C4514" # 'function': unreferenced inline function has been removed
204      "C4548" # expression before comma has no effect; expected expression with
205              # side-effect" caused by FD_* macros.
206      "C4610" # struct 'argument' can never be instantiated - user defined
207              # constructor required.
208      "C4623" # default constructor was implicitly defined as deleted
209      "C4625" # copy constructor could not be generated because a base class
210              # copy constructor is inaccessible or deleted
211      "C4626" # assignment operator could not be generated because a base class
212              # assignment operator is inaccessible or deleted
213      "C4628" # digraphs not supported with -Ze
214      "C4668" # 'symbol' is not defined as a preprocessor macro, replacing with
215              # '0' for 'directives'
216              # Disable this because GTest uses it everywhere.
217      "C4706" # assignment within conditional expression
218      "C4710" # 'function': function not inlined
219      "C4711" # function 'function' selected for inline expansion
220      "C4800" # 'int' : forcing value to bool 'true' or 'false'
221              # (performance warning)
222      "C4820" # 'bytes' bytes padding added after construct 'member_name'
223      "C5026" # move constructor was implicitly defined as deleted
224      "C5027" # move assignment operator was implicitly defined as deleted
225      "C5045" # Compiler will insert Spectre mitigation for memory load if
226              # /Qspectre switch specified
227      )
228  set(MSVC_LEVEL4_WARNINGS_LIST
229      # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
230      "C4265" # class has virtual functions, but destructor is not virtual
231      )
232  string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
233                            ${MSVC_DISABLED_WARNINGS_LIST})
234  string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
235                            ${MSVC_LEVEL4_WARNINGS_LIST})
236  set(CMAKE_C_FLAGS   "-utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
237  set(CMAKE_CXX_FLAGS "-utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
238endif()
239
240if(WIN32)
241  add_definitions(-D_HAS_EXCEPTIONS=0)
242  add_definitions(-DWIN32_LEAN_AND_MEAN)
243  add_definitions(-DNOMINMAX)
244  # Allow use of fopen.
245  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
246  # VS 2017 and higher supports STL-only warning suppressions.
247  # A bug in CMake < 3.13.0 may cause the space in this value to
248  # cause issues when building with NASM. In that case, update CMake.
249  add_definitions("-D_STL_EXTRA_DISABLED_WARNINGS=4774 4987")
250endif()
251
252if(CMAKE_COMPILER_IS_GNUCXX)
253  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
254endif()
255
256# pthread_rwlock_t requires a feature flag.
257if(NOT WIN32)
258  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
259endif()
260
261if(FUZZ)
262  if(NOT CLANG)
263    message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
264  endif()
265
266  if(CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0.0")
267    message(FATAL_ERROR "You need Clang ≥ 6.0.0")
268  endif()
269
270  add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
271  set(RUNNER_ARGS "-deterministic")
272
273  if(NOT NO_FUZZER_MODE)
274    add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
275    set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
276  endif()
277
278  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
279  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
280endif()
281
282add_definitions(-DBORINGSSL_IMPLEMENTATION)
283
284if(BUILD_SHARED_LIBS)
285  add_definitions(-DBORINGSSL_SHARED_LIBRARY)
286  # Enable position-independent code globally. This is needed because
287  # some library targets are OBJECT libraries.
288  set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
289endif()
290
291if(MSAN)
292  if(NOT CLANG)
293    message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
294  endif()
295
296  if(ASAN)
297    message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
298  endif()
299
300  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
301  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
302  set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
303endif()
304
305if(ASAN)
306  if(NOT CLANG)
307    message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
308  endif()
309
310  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
311  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
312endif()
313
314if(CFI)
315  if(NOT CLANG)
316    message(FATAL_ERROR "Cannot enable CFI unless using Clang")
317  endif()
318
319  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
320  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
321  # We use Chromium's copy of clang, which requires -fuse-ld=lld if building
322  # with -flto. That, in turn, can't handle -ggdb.
323  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
324  string(REPLACE "-ggdb" "-g" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
325  string(REPLACE "-ggdb" "-g" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
326  # -flto causes object files to contain LLVM bitcode. Mixing those with
327  # assembly output in the same static library breaks the linker.
328  set(OPENSSL_NO_ASM "1")
329endif()
330
331if(TSAN)
332  if(NOT CLANG)
333    message(FATAL_ERROR "Cannot enable TSAN unless using Clang")
334  endif()
335
336  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
337  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
338  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
339endif()
340
341if(UBSAN)
342  if(NOT CLANG)
343    message(FATAL_ERROR "Cannot enable UBSAN unless using Clang")
344  endif()
345
346  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
347  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
348  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
349
350  if(NOT UBSAN_RECOVER)
351    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize-recover=undefined")
352    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize-recover=undefined")
353    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-sanitize-recover=undefined")
354  endif()
355endif()
356
357if(GCOV)
358  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
359  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
360endif()
361
362if(FIPS)
363  add_definitions(-DBORINGSSL_FIPS)
364  if(FIPS_BREAK_TEST)
365    add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
366  endif()
367  # The FIPS integrity check does not work for ASan and MSan builds.
368  if(NOT ASAN AND NOT MSAN)
369    if(BUILD_SHARED_LIBS)
370      set(FIPS_SHARED "1")
371    else()
372      set(FIPS_DELOCATE "1")
373    endif()
374  endif()
375  if(FIPS_SHARED)
376    # The Android CMake files set -ffunction-sections and -fdata-sections,
377    # which is incompatible with FIPS_SHARED.
378    set(CMAKE_C_FLAGS
379        "${CMAKE_C_FLAGS} -fno-function-sections -fno-data-sections")
380    set(CMAKE_CXX_FLAGS
381        "${CMAKE_CXX_FLAGS} -fno-function-sections -fno-data-sections")
382  endif()
383endif()
384
385if(OPENSSL_SMALL)
386  add_definitions(-DOPENSSL_SMALL)
387endif()
388
389if(CONSTANT_TIME_VALIDATION)
390  add_definitions(-DBORINGSSL_CONSTANT_TIME_VALIDATION)
391  # Asserts will often test secret data.
392  add_definitions(-DNDEBUG)
393endif()
394
395function(go_executable dest package)
396  set(godeps "${CMAKE_SOURCE_DIR}/util/godeps.go")
397  if(CMAKE_VERSION VERSION_LESS "3.7" OR NOT CMAKE_GENERATOR STREQUAL "Ninja")
398    # The DEPFILE parameter to add_custom_command is new as of CMake 3.7 and
399    # only works with Ninja. Query the sources at configure time. Additionally,
400    # everything depends on go.mod. That affects what external packages to use.
401    execute_process(COMMAND ${GO_EXECUTABLE} run ${godeps} -format cmake
402                            -pkg ${package}
403                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
404                    OUTPUT_VARIABLE sources
405                    RESULT_VARIABLE godeps_result)
406    add_custom_command(OUTPUT ${dest}
407                       COMMAND ${GO_EXECUTABLE} build
408                               -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
409                       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
410                       DEPENDS ${sources} ${CMAKE_SOURCE_DIR}/go.mod)
411  else()
412    # Ninja expects the target in the depfile to match the output. This is a
413    # relative path from the build directory.
414    string(LENGTH "${CMAKE_BINARY_DIR}" root_dir_length)
415    math(EXPR root_dir_length "${root_dir_length} + 1")
416    string(SUBSTRING "${CMAKE_CURRENT_BINARY_DIR}" ${root_dir_length} -1 target)
417    set(target "${target}/${dest}")
418
419    set(depfile "${CMAKE_CURRENT_BINARY_DIR}/${dest}.d")
420    add_custom_command(OUTPUT ${dest}
421                       COMMAND ${GO_EXECUTABLE} build
422                               -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
423                       COMMAND ${GO_EXECUTABLE} run ${godeps} -format depfile
424                               -target ${target} -pkg ${package} -out ${depfile}
425                       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
426                       DEPENDS ${godeps} ${CMAKE_SOURCE_DIR}/go.mod
427                       DEPFILE ${depfile})
428  endif()
429endfunction()
430
431# CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
432# architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
433# alone, and expects all architecture-specific logic to be conditioned within
434# the source files rather than the build. This does not work for our assembly
435# files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
436# builds.
437if(NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
438  list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
439  if(NOT NUM_ARCHES EQUAL 1)
440    message(FATAL_ERROR "Universal binaries not supported.")
441  endif()
442  list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
443endif()
444
445if(OPENSSL_NO_SSE2_FOR_TESTING)
446  add_definitions(-DOPENSSL_NO_SSE2_FOR_TESTING)
447endif()
448
449if(OPENSSL_NO_ASM)
450  add_definitions(-DOPENSSL_NO_ASM)
451  set(ARCH "generic")
452elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
453  set(ARCH "x86_64")
454elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64")
455  set(ARCH "x86_64")
456elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "AMD64")
457  # cmake reports AMD64 on Windows, but we might be building for 32-bit.
458  if(CMAKE_SIZEOF_VOID_P EQUAL 8)
459    set(ARCH "x86_64")
460  else()
461    set(ARCH "x86")
462  endif()
463elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")
464  set(ARCH "x86")
465elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "i386")
466  set(ARCH "x86")
467elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "i686")
468  set(ARCH "x86")
469elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
470  set(ARCH "aarch64")
471elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64")
472  set(ARCH "aarch64")
473elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
474  set(ARCH "aarch64")
475# Apple A12 Bionic chipset which is added in iPhone XS/XS Max/XR uses arm64e architecture.
476elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64e")
477  set(ARCH "aarch64")
478elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm*")
479  set(ARCH "arm")
480elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "mips")
481  # Just to avoid the “unknown processor” error.
482  set(ARCH "generic")
483elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "ppc64le")
484  set(ARCH "ppc64le")
485else()
486  message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
487endif()
488
489if(ANDROID AND NOT ANDROID_NDK_REVISION AND ARCH STREQUAL "arm")
490  # The third-party Android-NDK CMake files somehow fail to set the -march flag
491  # for assembly files. Without this flag, the compiler believes that it's
492  # building for ARMv5.
493  set(CMAKE_ASM_FLAGS "-march=${CMAKE_SYSTEM_PROCESSOR} ${CMAKE_ASM_FLAGS}")
494endif()
495
496if(USE_CUSTOM_LIBCXX)
497  if(NOT CLANG)
498    message(FATAL_ERROR "USE_CUSTOM_LIBCXX only supported with Clang")
499  endif()
500
501  # CMAKE_CXX_FLAGS ends up in the linker flags as well, so use
502  # add_compile_options. There does not appear to be a way to set
503  # language-specific compile-only flags.
504  add_compile_options("-nostdinc++")
505  set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -nostdlib++")
506  include_directories(
507    SYSTEM
508    util/bot/libcxx/include
509    util/bot/libcxxabi/include
510  )
511
512  # This is patterned after buildtools/third_party/libc++/BUILD.gn and
513  # buildtools/third_party/libc++abi/BUILD.gn in Chromium.
514
515  file(GLOB LIBCXX_SOURCES "util/bot/libcxx/src/*.cpp")
516  file(GLOB LIBCXXABI_SOURCES "util/bot/libcxxabi/src/*.cpp")
517
518  # This file is meant for exception-less builds.
519  list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_noexception.cpp")
520  # libc++ also defines new and delete.
521  list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/stdlib_new_delete.cpp")
522  if(TSAN)
523    # ThreadSanitizer tries to intercept these symbols. Skip them to avoid
524    # symbol conflicts.
525    list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_guard.cpp")
526  endif()
527
528  add_library(libcxxabi ${LIBCXXABI_SOURCES})
529  target_compile_definitions(
530    libcxxabi PRIVATE
531    -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS
532  )
533  set_target_properties(libcxxabi PROPERTIES COMPILE_FLAGS "-Wno-missing-prototypes -Wno-implicit-fallthrough")
534
535  add_library(libcxx ${LIBCXX_SOURCES})
536  if(ASAN OR MSAN OR TSAN)
537    # Sanitizers try to intercept new and delete.
538    target_compile_definitions(
539      libcxx PRIVATE
540      -D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
541    )
542  endif()
543  target_compile_definitions(
544    libcxx PRIVATE
545    -D_LIBCPP_BUILDING_LIBRARY
546    -DLIBCXX_BUILDING_LIBCXXABI
547  )
548  target_link_libraries(libcxx libcxxabi)
549endif()
550
551# Add minimal googletest targets. The provided one has many side-effects, and
552# googletest has a very straightforward build.
553add_library(boringssl_gtest third_party/googletest/src/gtest-all.cc)
554target_include_directories(boringssl_gtest PRIVATE third_party/googletest)
555
556include_directories(third_party/googletest/include)
557
558# Declare a dummy target to build all unit tests. Test targets should inject
559# themselves as dependencies next to the target definition.
560add_custom_target(all_tests)
561
562# On Windows, CRYPTO_TEST_DATA is too long to fit in command-line limits.
563# TODO(davidben): CMake 3.12 has a list(JOIN) command. Use that when we've
564# updated the minimum version.
565set(EMBED_TEST_DATA_ARGS "")
566foreach(arg ${CRYPTO_TEST_DATA})
567  set(EMBED_TEST_DATA_ARGS "${EMBED_TEST_DATA_ARGS}${arg}\n")
568endforeach()
569file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/embed_test_data_args.txt"
570     "${EMBED_TEST_DATA_ARGS}")
571
572add_custom_command(
573  OUTPUT crypto_test_data.cc
574  COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go -file-list
575          "${CMAKE_CURRENT_BINARY_DIR}/embed_test_data_args.txt" >
576          "${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc"
577  DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
578  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
579
580add_library(crypto_test_data OBJECT crypto_test_data.cc)
581
582add_subdirectory(crypto)
583add_subdirectory(ssl)
584add_subdirectory(ssl/test)
585add_subdirectory(tool)
586add_subdirectory(util/fipstools/cavp)
587add_subdirectory(util/fipstools/acvp/modulewrapper)
588add_subdirectory(decrepit)
589
590if(FUZZ)
591  if(LIBFUZZER_FROM_DEPS)
592    file(GLOB LIBFUZZER_SOURCES "util/bot/libFuzzer/*.cpp")
593    add_library(Fuzzer STATIC ${LIBFUZZER_SOURCES})
594    # libFuzzer does not pass our aggressive warnings. It also must be built
595    # without -fsanitize-coverage options or clang crashes.
596    set_target_properties(Fuzzer PROPERTIES COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -Wno-missing-prototypes -fsanitize-coverage=0")
597  endif()
598
599  add_subdirectory(fuzz)
600endif()
601
602if(UNIX AND NOT APPLE AND NOT ANDROID)
603  set(HANDSHAKER_ARGS "-handshaker-path" $<TARGET_FILE:handshaker>)
604endif()
605
606if(FIPS)
607  add_custom_target(
608    acvp_tests
609    COMMAND ${GO_EXECUTABLE} build -o ${CMAKE_BINARY_DIR}/acvptool
610            boringssl.googlesource.com/boringssl/util/fipstools/acvp/acvptool
611    COMMAND ${GO_EXECUTABLE} build -o ${CMAKE_BINARY_DIR}/testmodulewrapper
612            boringssl.googlesource.com/boringssl/util/fipstools/acvp/acvptool/testmodulewrapper
613    COMMAND cd util/fipstools/acvp/acvptool/test &&
614            ${GO_EXECUTABLE} run check_expected.go
615            -tool ${CMAKE_BINARY_DIR}/acvptool
616            -module-wrappers modulewrapper:$<TARGET_FILE:modulewrapper>,testmodulewrapper:${CMAKE_BINARY_DIR}/testmodulewrapper
617            -tests tests.json
618    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
619    DEPENDS modulewrapper
620    USES_TERMINAL)
621
622  add_custom_target(
623    fips_specific_tests_if_any
624    DEPENDS acvp_tests
625  )
626else()
627  add_custom_target(fips_specific_tests_if_any)
628endif()
629
630add_custom_target(
631    run_tests
632    COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
633            ${CMAKE_BINARY_DIR}
634    COMMAND cd ssl/test/runner &&
635            ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
636              ${HANDSHAKER_ARGS} ${RUNNER_ARGS}
637    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
638    DEPENDS all_tests bssl_shim handshaker fips_specific_tests_if_any
639    USES_TERMINAL)
640