• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1cmake_minimum_required(VERSION 3.0)
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")
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()
175
176  if(CMAKE_COMPILER_IS_GNUCXX AND "4.8" VERSION_GREATER CMAKE_C_COMPILER_VERSION)
177    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-array-bounds")
178  endif()
179
180elseif(MSVC)
181  set(MSVC_DISABLED_WARNINGS_LIST
182      "C4061" # enumerator 'identifier' in switch of enum 'enumeration' is not
183              # explicitly handled by a case label
184              # Disable this because it flags even when there is a default.
185      "C4100" # 'exarg' : unreferenced formal parameter
186      "C4127" # conditional expression is constant
187      "C4200" # nonstandard extension used : zero-sized array in
188              # struct/union.
189      "C4204" # nonstandard extension used: non-constant aggregate initializer
190      "C4221" # nonstandard extension used : 'identifier' : cannot be
191              # initialized using address of automatic variable
192      "C4242" # 'function' : conversion from 'int' to 'uint8_t',
193              # possible loss of data
194      "C4244" # 'function' : conversion from 'int' to 'uint8_t',
195              # possible loss of data
196      "C4267" # conversion from 'size_t' to 'int', possible loss of data
197      "C4371" # layout of class may have changed from a previous version of the
198              # compiler due to better packing of member '...'
199      "C4388" # signed/unsigned mismatch
200      "C4296" # '>=' : expression is always true
201      "C4350" # behavior change: 'std::_Wrap_alloc...'
202      "C4365" # '=' : conversion from 'size_t' to 'int',
203              # signed/unsigned mismatch
204      "C4389" # '!=' : signed/unsigned mismatch
205      "C4464" # relative include path contains '..'
206      "C4510" # 'argument' : default constructor could not be generated
207      "C4512" # 'argument' : assignment operator could not be generated
208      "C4514" # 'function': unreferenced inline function has been removed
209      "C4548" # expression before comma has no effect; expected expression with
210              # side-effect" caused by FD_* macros.
211      "C4610" # struct 'argument' can never be instantiated - user defined
212              # constructor required.
213      "C4623" # default constructor was implicitly defined as deleted
214      "C4625" # copy constructor could not be generated because a base class
215              # copy constructor is inaccessible or deleted
216      "C4626" # assignment operator could not be generated because a base class
217              # assignment operator is inaccessible or deleted
218      "C4628" # digraphs not supported with -Ze
219      "C4668" # 'symbol' is not defined as a preprocessor macro, replacing with
220              # '0' for 'directives'
221              # Disable this because GTest uses it everywhere.
222      "C4706" # assignment within conditional expression
223      "C4710" # 'function': function not inlined
224      "C4711" # function 'function' selected for inline expansion
225      "C4800" # 'int' : forcing value to bool 'true' or 'false'
226              # (performance warning)
227      "C4820" # 'bytes' bytes padding added after construct 'member_name'
228      "C5026" # move constructor was implicitly defined as deleted
229      "C5027" # move assignment operator was implicitly defined as deleted
230      "C5045" # Compiler will insert Spectre mitigation for memory load if
231              # /Qspectre switch specified
232      )
233  set(MSVC_LEVEL4_WARNINGS_LIST
234      # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
235      "C4265" # class has virtual functions, but destructor is not virtual
236      )
237  string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
238                            ${MSVC_DISABLED_WARNINGS_LIST})
239  string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
240                            ${MSVC_LEVEL4_WARNINGS_LIST})
241  set(CMAKE_C_FLAGS   "-utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
242  set(CMAKE_CXX_FLAGS "-utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
243endif()
244
245if(WIN32)
246  add_definitions(-D_HAS_EXCEPTIONS=0)
247  add_definitions(-DWIN32_LEAN_AND_MEAN)
248  add_definitions(-DNOMINMAX)
249  # Allow use of fopen.
250  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
251  # VS 2017 and higher supports STL-only warning suppressions.
252  # A bug in CMake < 3.13.0 may cause the space in this value to
253  # cause issues when building with NASM. In that case, update CMake.
254  add_definitions("-D_STL_EXTRA_DISABLED_WARNINGS=4774 4987")
255endif()
256
257if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
258   CLANG)
259  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
260  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
261endif()
262
263if(CMAKE_COMPILER_IS_GNUCXX)
264  if((CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.99") OR CLANG)
265    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
266  else()
267    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
268  endif()
269endif()
270
271# pthread_rwlock_t requires a feature flag.
272if(NOT WIN32)
273  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
274endif()
275
276if(FUZZ)
277  if(NOT CLANG)
278    message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
279  endif()
280
281  if(CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0.0")
282    message(FATAL_ERROR "You need Clang ≥ 6.0.0")
283  endif()
284
285  add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
286  set(RUNNER_ARGS "-deterministic")
287
288  if(NOT NO_FUZZER_MODE)
289    add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
290    set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
291  endif()
292
293  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
294  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
295endif()
296
297add_definitions(-DBORINGSSL_IMPLEMENTATION)
298
299if(BUILD_SHARED_LIBS)
300  add_definitions(-DBORINGSSL_SHARED_LIBRARY)
301  # Enable position-independent code globally. This is needed because
302  # some library targets are OBJECT libraries.
303  set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
304endif()
305
306if(MSAN)
307  if(NOT CLANG)
308    message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
309  endif()
310
311  if(ASAN)
312    message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
313  endif()
314
315  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
316  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
317  set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
318endif()
319
320if(ASAN)
321  if(NOT CLANG)
322    message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
323  endif()
324
325  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
326  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
327endif()
328
329if(CFI)
330  if(NOT CLANG)
331    message(FATAL_ERROR "Cannot enable CFI unless using Clang")
332  endif()
333
334  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
335  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
336  # We use Chromium's copy of clang, which requires -fuse-ld=lld if building
337  # with -flto. That, in turn, can't handle -ggdb.
338  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
339  string(REPLACE "-ggdb" "-g" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
340  string(REPLACE "-ggdb" "-g" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
341  # -flto causes object files to contain LLVM bitcode. Mixing those with
342  # assembly output in the same static library breaks the linker.
343  set(OPENSSL_NO_ASM "1")
344endif()
345
346if(TSAN)
347  if(NOT CLANG)
348    message(FATAL_ERROR "Cannot enable TSAN unless using Clang")
349  endif()
350
351  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
352  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
353  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
354endif()
355
356if(UBSAN)
357  if(NOT CLANG)
358    message(FATAL_ERROR "Cannot enable UBSAN unless using Clang")
359  endif()
360
361  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
362  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
363  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
364
365  if(NOT UBSAN_RECOVER)
366    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize-recover=undefined")
367    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize-recover=undefined")
368    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-sanitize-recover=undefined")
369  endif()
370endif()
371
372if(GCOV)
373  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
374  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
375endif()
376
377if(FIPS)
378  add_definitions(-DBORINGSSL_FIPS)
379  if(FIPS_BREAK_TEST)
380    add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
381  endif()
382  # The FIPS integrity check does not work for ASan and MSan builds.
383  if(NOT ASAN AND NOT MSAN)
384    if(BUILD_SHARED_LIBS)
385      set(FIPS_SHARED "1")
386    else()
387      set(FIPS_DELOCATE "1")
388    endif()
389  endif()
390  if(FIPS_SHARED)
391    # The Android CMake files set -ffunction-sections and -fdata-sections,
392    # which is incompatible with FIPS_SHARED.
393    set(CMAKE_C_FLAGS
394        "${CMAKE_C_FLAGS} -fno-function-sections -fno-data-sections")
395    set(CMAKE_CXX_FLAGS
396        "${CMAKE_CXX_FLAGS} -fno-function-sections -fno-data-sections")
397  endif()
398endif()
399
400if(OPENSSL_SMALL)
401  add_definitions(-DOPENSSL_SMALL)
402endif()
403
404if(CONSTANT_TIME_VALIDATION)
405  add_definitions(-DBORINGSSL_CONSTANT_TIME_VALIDATION)
406  # Asserts will often test secret data.
407  add_definitions(-DNDEBUG)
408endif()
409
410function(go_executable dest package)
411  set(godeps "${CMAKE_SOURCE_DIR}/util/godeps.go")
412  if(${CMAKE_VERSION} VERSION_LESS "3.7" OR
413     NOT ${CMAKE_GENERATOR} STREQUAL "Ninja")
414    # The DEPFILE parameter to add_custom_command is new as of CMake 3.7 and
415    # only works with Ninja. Query the sources at configure time. Additionally,
416    # everything depends on go.mod. That affects what external packages to use.
417    execute_process(COMMAND ${GO_EXECUTABLE} run ${godeps} -format cmake
418                            -pkg ${package}
419                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
420                    OUTPUT_VARIABLE sources
421                    RESULT_VARIABLE godeps_result)
422    add_custom_command(OUTPUT ${dest}
423                       COMMAND ${GO_EXECUTABLE} build
424                               -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
425                       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
426                       DEPENDS ${sources} ${CMAKE_SOURCE_DIR}/go.mod)
427  else()
428    # Ninja expects the target in the depfile to match the output. This is a
429    # relative path from the build directory.
430    string(LENGTH "${CMAKE_BINARY_DIR}" root_dir_length)
431    math(EXPR root_dir_length "${root_dir_length} + 1")
432    string(SUBSTRING "${CMAKE_CURRENT_BINARY_DIR}" ${root_dir_length} -1 target)
433    set(target "${target}/${dest}")
434
435    set(depfile "${CMAKE_CURRENT_BINARY_DIR}/${dest}.d")
436    add_custom_command(OUTPUT ${dest}
437                       COMMAND ${GO_EXECUTABLE} build
438                               -o ${CMAKE_CURRENT_BINARY_DIR}/${dest} ${package}
439                       COMMAND ${GO_EXECUTABLE} run ${godeps} -format depfile
440                               -target ${target} -pkg ${package} -out ${depfile}
441                       WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
442                       DEPENDS ${godeps} ${CMAKE_SOURCE_DIR}/go.mod
443                       DEPFILE ${depfile})
444  endif()
445endfunction()
446
447# CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
448# architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
449# alone, and expects all architecture-specific logic to be conditioned within
450# the source files rather than the build. This does not work for our assembly
451# files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
452# builds.
453if(NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
454  list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
455  if(NOT ${NUM_ARCHES} EQUAL 1)
456    message(FATAL_ERROR "Universal binaries not supported.")
457  endif()
458  list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
459endif()
460
461if(OPENSSL_NO_ASM)
462  add_definitions(-DOPENSSL_NO_ASM)
463  set(ARCH "generic")
464elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
465  set(ARCH "x86_64")
466elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
467  set(ARCH "x86_64")
468elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
469  # cmake reports AMD64 on Windows, but we might be building for 32-bit.
470  if(CMAKE_CL_64)
471    set(ARCH "x86_64")
472  else()
473    set(ARCH "x86")
474  endif()
475elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
476  set(ARCH "x86")
477elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
478  set(ARCH "x86")
479elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
480  set(ARCH "x86")
481elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
482  set(ARCH "aarch64")
483elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
484  set(ARCH "aarch64")
485# Apple A12 Bionic chipset which is added in iPhone XS/XS Max/XR uses arm64e architecture.
486elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64e")
487  set(ARCH "aarch64")
488elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm*")
489  set(ARCH "arm")
490elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "mips")
491  # Just to avoid the “unknown processor” error.
492  set(ARCH "generic")
493elseif(${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le")
494  set(ARCH "ppc64le")
495else()
496  message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
497endif()
498
499if(ANDROID AND NOT ANDROID_NDK_REVISION AND ${ARCH} STREQUAL "arm")
500  # The third-party Android-NDK CMake files somehow fail to set the -march flag
501  # for assembly files. Without this flag, the compiler believes that it's
502  # building for ARMv5.
503  set(CMAKE_ASM_FLAGS "-march=${CMAKE_SYSTEM_PROCESSOR} ${CMAKE_ASM_FLAGS}")
504endif()
505
506if(USE_CUSTOM_LIBCXX)
507  if(NOT CLANG)
508    message(FATAL_ERROR "USE_CUSTOM_LIBCXX only supported with Clang")
509  endif()
510
511  # CMAKE_CXX_FLAGS ends up in the linker flags as well, so use
512  # add_compile_options. There does not appear to be a way to set
513  # language-specific compile-only flags.
514  add_compile_options("-nostdinc++")
515  set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -nostdlib++")
516  include_directories(
517    SYSTEM
518    util/bot/libcxx/include
519    util/bot/libcxxabi/include
520  )
521
522  # This is patterned after buildtools/third_party/libc++/BUILD.gn and
523  # buildtools/third_party/libc++abi/BUILD.gn in Chromium.
524
525  file(GLOB LIBCXX_SOURCES "util/bot/libcxx/src/*.cpp")
526  file(GLOB LIBCXXABI_SOURCES "util/bot/libcxxabi/src/*.cpp")
527
528  # This file is meant for exception-less builds.
529  list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_noexception.cpp")
530  # libc++ also defines new and delete.
531  list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/stdlib_new_delete.cpp")
532  if(TSAN)
533    # ThreadSanitizer tries to intercept these symbols. Skip them to avoid
534    # symbol conflicts.
535    list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_guard.cpp")
536  endif()
537
538  add_library(libcxxabi ${LIBCXXABI_SOURCES})
539  target_compile_definitions(
540    libcxxabi PRIVATE
541    -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS
542  )
543  set_target_properties(libcxxabi PROPERTIES COMPILE_FLAGS "-Wno-missing-prototypes -Wno-implicit-fallthrough")
544
545  add_library(libcxx ${LIBCXX_SOURCES})
546  if(ASAN OR MSAN OR TSAN)
547    # Sanitizers try to intercept new and delete.
548    target_compile_definitions(
549      libcxx PRIVATE
550      -D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
551    )
552  endif()
553  target_compile_definitions(
554    libcxx PRIVATE
555    -D_LIBCPP_BUILDING_LIBRARY
556    -DLIBCXX_BUILDING_LIBCXXABI
557  )
558  target_link_libraries(libcxx libcxxabi)
559endif()
560
561# Add minimal googletest targets. The provided one has many side-effects, and
562# googletest has a very straightforward build.
563add_library(boringssl_gtest third_party/googletest/src/gtest-all.cc)
564target_include_directories(boringssl_gtest PRIVATE third_party/googletest)
565
566include_directories(third_party/googletest/include)
567
568# Declare a dummy target to build all unit tests. Test targets should inject
569# themselves as dependencies next to the target definition.
570add_custom_target(all_tests)
571
572add_custom_command(
573  OUTPUT crypto_test_data.cc
574  COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go ${CRYPTO_TEST_DATA} >
575  ${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc
576  DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
577  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
578
579add_library(crypto_test_data OBJECT crypto_test_data.cc)
580
581add_subdirectory(crypto)
582add_subdirectory(ssl)
583add_subdirectory(ssl/test)
584add_subdirectory(tool)
585add_subdirectory(util/fipstools/cavp)
586add_subdirectory(util/fipstools/acvp/modulewrapper)
587add_subdirectory(decrepit)
588
589if(FUZZ)
590  if(LIBFUZZER_FROM_DEPS)
591    file(GLOB LIBFUZZER_SOURCES "util/bot/libFuzzer/*.cpp")
592    add_library(Fuzzer STATIC ${LIBFUZZER_SOURCES})
593    # libFuzzer does not pass our aggressive warnings. It also must be built
594    # without -fsanitize-coverage options or clang crashes.
595    set_target_properties(Fuzzer PROPERTIES COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -Wno-missing-prototypes -fsanitize-coverage=0")
596  endif()
597
598  add_subdirectory(fuzz)
599endif()
600
601if(NOT ${CMAKE_VERSION} VERSION_LESS "3.2")
602  # USES_TERMINAL is only available in CMake 3.2 or later.
603  set(MAYBE_USES_TERMINAL USES_TERMINAL)
604endif()
605
606if(UNIX AND NOT APPLE AND NOT ANDROID)
607  set(HANDSHAKER_ARGS "-handshaker-path" $<TARGET_FILE:handshaker>)
608endif()
609
610add_custom_target(
611    run_tests
612    COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
613            ${CMAKE_BINARY_DIR}
614    COMMAND cd ssl/test/runner &&
615            ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
616              ${HANDSHAKER_ARGS} ${RUNNER_ARGS}
617    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
618    DEPENDS all_tests bssl_shim handshaker
619    ${MAYBE_USES_TERMINAL})
620