Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/CMakeLists.txt

raw
1 cmake_minimum_required(VERSION 3.5..3.31)
2
3 if(POLICY CMP0091)
4 cmake_policy(SET CMP0091 NEW)
5 endif()
6
7 set(SOFTWARE_NAME "awslc")
8 set(SOFTWARE_VERSION "1.73.0")
9 set(ABI_VERSION 0)
10 set(CRYPTO_LIB_NAME "crypto")
11 set(SSL_LIB_NAME "ssl")
12 set(OPENSSL_VERSION_NUMBER "0x1010107f")
13
14 # Note this is set to 1.1.1 for compatibility purposes
15 # with projects that use the pkg-config information to determine
16 # the OpenSSL version.
17 set(REPORTED_PKGCONFIG_VERSION "1.1.1")
18
19 # Defer enabling C and CXX languages.
20 project(AWSLC VERSION "${SOFTWARE_VERSION}" LANGUAGES NONE)
21
22 if(DEFINED ENABLE_PRE_SONAME_BUILD)
23 message(WARNING "ENABLE_PRE_SONAME_BUILD option will be deprecated in a future release. Please see ENABLE_DIST_PKG and ENABLE_DIST_PKG_OPENSSL_SHIM")
24 endif()
25
26 option(BUILD_TESTING "Build all test targets for AWS-LC" ON)
27 option(BUILD_LIBSSL "Build libssl for AWS-LC" ON)
28 option(BUILD_TOOL "Build bssl tool for AWS-LC" ON)
29 option(DISABLE_PERL "Disable Perl for AWS-LC" OFF)
30 option(DISABLE_GO "Disable Go for AWS-LC" OFF)
31 # Keeping this flag for now, for compatibility with existing build configs.
32 option(ENABLE_FIPS_ENTROPY_CPU_JITTER "Enable FIPS entropy source: CPU Jitter" OFF)
33 option(ENABLE_DATA_INDEPENDENT_TIMING "Enable automatic setting/resetting Data-Independent Timing
34 (DIT) flag in cryptographic functions. Currently only applicable to Arm64 (except on Windows)" OFF)
35 option(ENABLE_PRE_SONAME_BUILD "Build AWS-LC without SONAME configuration for shared library builds" ON)
36 option(ENABLE_SOURCE_MODIFICATION "Allow the build to update files in the source directory. This is typically done to update versioning." ON)
37 option(DISABLE_CPU_JITTER_ENTROPY "Disable usage of CPU Jitter Entropy as an entropy source. This option cannot be used with the FIPS build. With this configuration, randomness generation might not use two independent entropy sources." OFF)
38 option(GENERATE_RUST_BINDINGS "Generate Rust bindings using bindgen-cli" OFF)
39 option(ENABLE_DIST_PKG "Enables a set of packaging that take highest precedence to any other packaging configuration i.e. ENABLE_PRE_SONAME_BUILD" OFF)
40 option(ENABLE_DIST_PKG_OPENSSL_SHIM "Controls whether the OpenSSL shim components are installed when ENABLE_DIST_PKG is enabled" OFF)
41
42 if(MSVC)
43 # On Windows, prefer cl over gcc if both are available. By default most of
44 # the CMake generators prefer gcc, even on Windows.
45 set(CMAKE_GENERATOR_CC cl)
46 endif()
47
48 include(sources.cmake)
49 include(TestBigEndian)
50 include(CheckCCompilerFlag)
51
52 macro(add_flag_if_supported VARIABLE FLAG)
53 # Create a safe, unique variable name to cache the result of the check
54 string(MAKE_C_IDENTIFIER "HAVE_C_FLAG_${FLAG}" _CHECK_VAR_NAME)
55
56 # Keep configure output tidy
57 set(CMAKE_REQUIRED_QUIET ${CMAKE_NOT_VERBOSE})
58
59 # Run the check only once to speed up re-configuration
60 if(NOT DEFINED ${_CHECK_VAR_NAME})
61 check_c_compiler_flag("${FLAG}" ${_CHECK_VAR_NAME})
62 endif()
63
64 # If the check passed, append the flag to the specified variable
65 if(${_CHECK_VAR_NAME})
66 set(${VARIABLE} "${${VARIABLE}} ${FLAG}")
67 message(STATUS "Compiler supports '${FLAG}', adding to ${VARIABLE}")
68 endif()
69 endmacro()
70
71 if(POLICY CMP0077)
72 cmake_policy(SET CMP0077 NEW) #option does nothing when a normal variable of the same name exists.
73 endif()
74
75 set(RUST_BINDINGS_TARGET_VERSION "1.70" CACHE STRING "Minimum Rust version for generated bindings")
76
77 include(cmake/go.cmake)
78
79 if(ENABLE_DIST_PKG_OPENSSL_SHIM AND NOT ENABLE_DIST_PKG)
80 message(FATAL_ERROR "ENABLE_DIST_PKG_OPENSSL_SHIM requires ENABLE_DIST_PKG to be enabled and will be ignored.")
81 endif()
82
83 if(ENABLE_DIST_PKG)
84 if(NOT UNIX OR APPLE)
85 message(FATAL_ERROR "ENABLE_DIST_PKG is not supported on macOS or Windows and will be ignored.")
86 else()
87 set(SET_LIB_SONAME 1)
88 set(COHABITANT_HEADERS 1)
89 if(ENABLE_DIST_PKG_OPENSSL_SHIM)
90 set(INSTALL_OPENSSL_SHIM 1)
91 else()
92 set(INSTALL_OPENSSL_SHIM 0)
93 endif()
94 endif()
95 elseif(NOT ENABLE_PRE_SONAME_BUILD AND BUILD_SHARED_LIBS AND UNIX AND NOT APPLE)
96 set(SET_LIB_SONAME 1)
97 set(COHABITANT_HEADERS 0)
98 set(INSTALL_OPENSSL_SHIM 1)
99 else()
100 set(SET_LIB_SONAME 0)
101 set(COHABITANT_HEADERS 0)
102 set(INSTALL_OPENSSL_SHIM 1)
103 endif()
104
105 message(STATUS "SET_LIB_SONAME: ${SET_LIB_SONAME}")
106 message(STATUS "COHABITANT_HEADERS: ${COHABITANT_HEADERS}")
107 message(STATUS "INSTALL_OPENSSL_SHIM: ${INSTALL_OPENSSL_SHIM}")
108
109 if(SET_LIB_SONAME)
110 set(CRYPTO_LIB_NAME "${CRYPTO_LIB_NAME}-${SOFTWARE_NAME}")
111 set(SSL_LIB_NAME "${SSL_LIB_NAME}-${SOFTWARE_NAME}")
112 endif()
113
114 enable_language(C)
115
116 if (NOT WIN32 AND NOT APPLE)
117 include(GNUInstallDirs)
118 else()
119 if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
120 set(CMAKE_INSTALL_LIBDIR "lib")
121 endif()
122 if(NOT DEFINED CMAKE_INSTALL_INCLUDEDIR)
123 set(CMAKE_INSTALL_INCLUDEDIR "include")
124 endif()
125 if(NOT DEFINED CMAKE_INSTALL_BINDIR)
126 set(CMAKE_INSTALL_BINDIR "bin")
127 endif()
128 endif()
129
130 # Set the install include directory based on whether a prefix subdirectory is desired
131 if(COHABITANT_HEADERS)
132 set(AWSLC_INSTALL_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}/aws-lc")
133 else()
134 set(AWSLC_INSTALL_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}")
135 endif()
136
137 function(target_add_awslc_include_paths)
138 set(options EXCLUDE_PREFIX_HEADERS)
139 set(oneValueArgs TARGET SCOPE)
140 set(multiValueArgs)
141
142 if(CMAKE_VERSION VERSION_LESS "3.7")
143 cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
144 else()
145 cmake_parse_arguments(PARSE_ARGV 0 arg "${options}" "${oneValueArgs}" "${multiValueArgs}")
146 endif()
147
148 if(arg_UNPARSED_ARGUMENTS)
149 message(FATAL_ERROR "Unparsed arguments to target_add_awslc_include_paths: ${arg_UNPARSED_ARGUMENTS}")
150 endif()
151
152 if(arg_KEYWORDS_MISSING_VALUES)
153 message(FATAL_ERROR "Missing value(s) for keyword(s) to target_add_awslc_include_paths: ${arg_KEYWORDS_MISSING_VALUES}")
154 endif()
155
156 if(EXCLUDE_PREFIX_HEADERS)
157 set(INCLUDE_PREFIX_HEADERS 0)
158 else()
159 set(INCLUDE_PREFIX_HEADERS 1)
160 endif()
161
162 add_dependencies(${arg_TARGET} boringssl_prefix_symbols)
163 target_include_directories(${arg_TARGET} BEFORE ${arg_SCOPE}
164 $<$<BOOL:${INCLUDE_PREFIX_HEADERS}>:$<BUILD_INTERFACE:${AWSLC_BINARY_DIR}/symbol_prefix_include>>
165 $<BUILD_INTERFACE:${AWSLC_SOURCE_DIR}/include>
166 $<INSTALL_INTERFACE:${AWSLC_INSTALL_INCLUDEDIR}>)
167 endfunction()
168
169 # Validate Rust bindings prerequisites
170 if(GENERATE_RUST_BINDINGS)
171 find_program(BINDGEN_EXECUTABLE NAMES bindgen)
172 if(NOT BINDGEN_EXECUTABLE)
173 message(FATAL_ERROR "GENERATE_RUST_BINDINGS is enabled but bindgen-cli was not found. "
174 "Install it with: cargo install --force --locked bindgen-cli")
175 endif()
176
177 # Verify minimum version (0.69.5)
178 execute_process(
179 COMMAND ${BINDGEN_EXECUTABLE} --version
180 OUTPUT_VARIABLE BINDGEN_VERSION_OUTPUT
181 OUTPUT_STRIP_TRAILING_WHITESPACE
182 RESULT_VARIABLE BINDGEN_VERSION_RESULT
183 )
184
185 if(NOT BINDGEN_VERSION_RESULT EQUAL 0)
186 message(FATAL_ERROR "Failed to get bindgen version")
187 endif()
188
189 # Extract version number (format: "bindgen X.Y.Z")
190 string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" BINDGEN_VERSION "${BINDGEN_VERSION_OUTPUT}")
191
192 if(BINDGEN_VERSION VERSION_LESS "0.69.5")
193 message(FATAL_ERROR "bindgen version ${BINDGEN_VERSION} is too old. "
194 "Minimum required version is 0.69.5. "
195 "Upgrade with: cargo install --force --locked bindgen-cli")
196 endif()
197
198 message(STATUS "Found bindgen-cli: ${BINDGEN_EXECUTABLE} (version ${BINDGEN_VERSION})")
199 message(STATUS "Rust bindings target version: ${RUST_BINDINGS_TARGET_VERSION}")
200
201 # Check for rustfmt (required for --formatter rustfmt option)
202 find_program(RUSTFMT_EXECUTABLE NAMES rustfmt)
203 if(NOT RUSTFMT_EXECUTABLE)
204 message(FATAL_ERROR "GENERATE_RUST_BINDINGS requires rustfmt but it was not found. "
205 "Install it with: rustup component add rustfmt")
206 endif()
207 message(STATUS "Found rustfmt: ${RUSTFMT_EXECUTABLE}")
208 endif()
209
210 if(DISABLE_CPU_JITTER_ENTROPY)
211 if(FIPS)
212 message(FATAL_ERROR "Cannot opt-out of CPU Jitter for the FIPS build")
213 endif()
214 add_definitions(-DDISABLE_CPU_JITTER_ENTROPY)
215 message(STATUS "Entropy source configuration: CPU Jitter opt-out")
216 message(STATUS "Entropy source configured: Dynamic (default: Operating system)")
217 else()
218 # The validated entropy source will always be configured to be CPU Jitter
219 # Entropy by default; because it's the root of the Tree DRBG.
220 message(STATUS "Entropy source configured: Dynamic (default: CPU Jitter)")
221 endif()
222
223 if(${CMAKE_SYSTEM_NAME} STREQUAL "OpenBSD")
224 # OpenBSD by defaults links with --execute-only this is problematic for two reasons:
225 # 1. The FIPS shared and static builds need to compute the module signature hash by reading the .text section
226 # 2. s2n-bignum x86 assembly sources still have references to static data in the .text section
227 if(NOT BUILD_SHARED_LIBS)
228 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--no-execute-only")
229 else()
230 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-execute-only")
231 endif()
232 endif()
233
234 if(CMAKE_HOST_SYSTEM_NAME STREQUAL "SunOS" AND NOT CMAKE_CROSSCOMPILING)
235 # Determine if the host is running an illumos distribution:
236 execute_process(COMMAND /usr/bin/uname -o OUTPUT_VARIABLE UNAME_O
237 OUTPUT_STRIP_TRAILING_WHITESPACE)
238
239 if (UNAME_O STREQUAL "illumos")
240 set(HOST_ILLUMOS 1)
241 endif()
242
243 if (UNAME_O STREQUAL "Solaris")
244 set(HOST_SOLARIS 1)
245 endif()
246
247 if (HOST_ILLUMOS)
248 #
249 # illumos systems require linking libsocket and libnsl to get various
250 # networking routines sometimes found in libc on other platforms:
251 #
252 if(NOT BUILD_SHARED_LIBS)
253 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lsocket -lnsl")
254 else()
255 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lsocket -lnsl")
256 endif()
257 endif()
258 endif()
259
260 # Tests and libssl both require the CXX language to be enabled. If a consumer
261 # chooses to disable building the tests and libssl, do not enable CXX
262 if(BUILD_TESTING OR BUILD_LIBSSL)
263 enable_language(CXX)
264 endif()
265
266 if(ENABLE_CLANG_TIDY)
267 find_program(CLANG_TIDY_CMD NAMES clang-tidy)
268 set(CMAKE_VERBOSE_MAKEFILE ON)
269
270 if(NOT CLANG_TIDY_CMD)
271 message(WARNING "clang-tidy is not found!")
272 set(CMAKE_CXX_CLANG_TIDY "" CACHE STRING "" FORCE)
273 else()
274 message(STATUS "clang-tidy found: ${CLANG_TIDY_CMD}")
275 set(CLANG_TIDY_EXTRA_ARGS
276 "-extra-arg=-Wno-unknown-warning-option"
277 )
278 set(CMAKE_C_CLANG_TIDY ${CLANG_TIDY_CMD} "--config-file=${AWSLC_SOURCE_DIR}/.clang-tidy")
279 set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_CMD} "--config-file=${AWSLC_SOURCE_DIR}/.clang-tidy")
280 endif()
281 endif()
282
283 if (NOT DEFINED CMAKE_CXX_STANDARD)
284 set(CMAKE_CXX_STANDARD 11)
285 endif()
286 set(CMAKE_CXX_STANDARD_REQUIRED ON)
287 if (NOT DEFINED CMAKE_C_STANDARD)
288 try_compile(
289 RESULT
290 ${AWSLC_BINARY_DIR}
291 SOURCES "${CMAKE_CURRENT_LIST_DIR}/tests/compiler_features_tests/c11.c"
292 COMPILE_DEFINITIONS -c -std=c11)
293 if(RESULT)
294 set(CMAKE_C_STANDARD 11)
295 else()
296 set(CMAKE_C_STANDARD 99)
297 endif()
298 endif ()
299 set(CMAKE_C_STANDARD_REQUIRED ON)
300
301 if(CMAKE_C_COMPILER_ID MATCHES "Clang")
302 set(CLANG 1)
303 elseif(CMAKE_C_COMPILER_ID MATCHES "GNU")
304 set(GCC 1)
305 endif()
306
307 install(DIRECTORY include/openssl
308 DESTINATION ${AWSLC_INSTALL_INCLUDEDIR}
309 COMPONENT Development
310 PATTERN boringssl_prefix_symbols.h EXCLUDE
311 PATTERN boringssl_prefix_symbols_asm.h EXCLUDE
312 PATTERN boringssl_prefix_symbols_nasm.inc EXCLUDE
313 PATTERN "*.in" EXCLUDE
314 )
315
316 if (TEST_SYSGENID_PATH)
317 message(STATUS "Setting AWSLC_VM_UBE_TESTING=1")
318 add_definitions(-DAWSLC_VM_UBE_TESTING=1)
319 message(STATUS "Setting AWSLC_SYSGENID_PATH=${TEST_SYSGENID_PATH}")
320 add_definitions(-DAWSLC_SYSGENID_PATH=\"${TEST_SYSGENID_PATH}\")
321 endif()
322
323 if(NOT DISABLE_PERL)
324 find_package(Perl REQUIRED)
325 endif()
326
327 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux" AND BUILD_TESTING AND NOT CMAKE_CROSSCOMPILING)
328 find_package(PkgConfig QUIET)
329 if (PkgConfig_FOUND)
330 pkg_check_modules(LIBUNWIND libunwind-generic)
331 if(LIBUNWIND_FOUND)
332 add_definitions(-DBORINGSSL_HAVE_LIBUNWIND)
333 else()
334 message("libunwind not found. Disabling unwind tests.")
335 endif()
336 else()
337 message("pkgconfig not found. Disabling unwind tests.")
338 endif()
339 endif()
340
341 if(NOT GO_EXECUTABLE)
342 message(STATUS "Go not found. Disabling some code generation and using pre-generated code in generated-src/")
343 endif()
344 if (NOT PERL_EXECUTABLE)
345 message(STATUS "Perl not found. Disabling some code generation and using pre-generated code in generated-src/")
346 endif()
347
348 set(GENERATE_CODE_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/generated-src")
349
350 if(USE_CUSTOM_LIBCXX)
351 set(BORINGSSL_ALLOW_CXX_RUNTIME 1)
352 endif()
353
354 if(BORINGSSL_ALLOW_CXX_RUNTIME)
355 add_definitions(-DBORINGSSL_ALLOW_CXX_RUNTIME)
356 endif()
357
358 string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
359 if(NOT FIPS)
360 if("${CMAKE_BUILD_TYPE_LOWER}" STREQUAL "relwithassert" OR
361 NOT CMAKE_BUILD_TYPE_LOWER MATCHES "rel")
362 add_definitions(-DBORINGSSL_DISPATCH_TEST)
363 # CMake automatically connects include_directories to the NASM
364 # command-line, but not add_definitions.
365 set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -DBORINGSSL_DISPATCH_TEST")
366 elseif(CMAKE_BUILD_TYPE_LOWER MATCHES "rel")
367 add_definitions(-DBORINGSSL_RELEASE_BUILD)
368 endif()
369 endif()
370
371 # Add a RelWithAsserts build configuration. It is the same as Release, except it
372 # does not define NDEBUG, so asserts run.
373 foreach(VAR CMAKE_C_FLAGS CMAKE_CXX_FLAGS CMAKE_ASM_FLAGS)
374 string(REGEX REPLACE "(^| )[/-]DNDEBUG( |$)" " " "${VAR}_RELWITHASSERTS"
375 "${${VAR}_RELEASE}")
376 endforeach()
377
378 if(BORINGSSL_PREFIX AND BORINGSSL_PREFIX_SYMBOLS AND GO_EXECUTABLE)
379
380 message(STATUS "Prefix build configured: building headers using prefix \"${BORINGSSL_PREFIX}\" and symbols file \"${BORINGSSL_PREFIX_SYMBOLS}\"")
381
382 if(IS_ABSOLUTE ${BORINGSSL_PREFIX_SYMBOLS})
383 set(BORINGSSL_PREFIX_SYMBOLS_PATH ${BORINGSSL_PREFIX_SYMBOLS})
384 else()
385 set(BORINGSSL_PREFIX_SYMBOLS_PATH ${AWSLC_BINARY_DIR}/${BORINGSSL_PREFIX_SYMBOLS})
386 endif()
387
388 add_custom_command(
389 OUTPUT symbol_prefix_include/openssl/boringssl_prefix_symbols.h
390 symbol_prefix_include/openssl/boringssl_prefix_symbols_asm.h
391 symbol_prefix_include/openssl/boringssl_prefix_symbols_nasm.inc
392 COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include/openssl
393 COMMAND ${GO_EXECUTABLE} run ${CMAKE_CURRENT_SOURCE_DIR}/util/make_prefix_headers.go -out ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include/openssl -prefix ${BORINGSSL_PREFIX} ${BORINGSSL_PREFIX_SYMBOLS_PATH}
394 COMMAND ${CMAKE_COMMAND} -E remove
395 ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include/openssl/boringssl_prefix_symbols.h.bak
396 ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include/openssl/boringssl_prefix_symbols_asm.h.bak
397 ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include/openssl/boringssl_prefix_symbols_nasm.inc.bak
398 DEPENDS util/make_prefix_headers.go
399 ${BORINGSSL_PREFIX_SYMBOLS_PATH})
400 # add_dependencies needs a target, not a file, so we add an intermediate
401 # target.
402 add_custom_target(
403 boringssl_prefix_symbols
404 DEPENDS symbol_prefix_include/openssl/boringssl_prefix_symbols.h
405 symbol_prefix_include/openssl/boringssl_prefix_symbols_asm.h
406 symbol_prefix_include/openssl/boringssl_prefix_symbols_nasm.inc)
407
408 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include/openssl
409 DESTINATION ${AWSLC_INSTALL_INCLUDEDIR}
410 COMPONENT Development
411 )
412 elseif(BORINGSSL_PREFIX AND BORINGSSL_PREFIX_HEADERS)
413
414 message(STATUS "Prefix build configured: performing build using prefix \"${BORINGSSL_PREFIX}\" and headers path \"${BORINGSSL_PREFIX_HEADERS}\"")
415
416 if(IS_ABSOLUTE ${BORINGSSL_PREFIX_HEADERS})
417 set(BORINGSSL_PREFIX_HEADERS_PATH ${BORINGSSL_PREFIX_HEADERS})
418 else()
419 set(BORINGSSL_PREFIX_HEADERS_PATH ${AWSLC_BINARY_DIR}/${BORINGSSL_PREFIX_HEADERS})
420 endif()
421
422 file(COPY ${BORINGSSL_PREFIX_HEADERS_PATH}/openssl/boringssl_prefix_symbols.h DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include/openssl)
423 file(COPY ${BORINGSSL_PREFIX_HEADERS_PATH}/openssl/boringssl_prefix_symbols_asm.h DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include/openssl)
424 file(COPY ${BORINGSSL_PREFIX_HEADERS_PATH}/openssl/boringssl_prefix_symbols_nasm.inc DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include/openssl)
425
426 add_custom_target(boringssl_prefix_symbols)
427
428 install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include/openssl
429 DESTINATION ${AWSLC_INSTALL_INCLUDEDIR}
430 COMPONENT Development
431 )
432 elseif(BORINGSSL_PREFIX AND BORINGSSL_PREFIX_SYMBOLS AND NOT GO_EXECUTABLE)
433 # This case is reached when both BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS are set,
434 # but GO_EXECUTABLE is not available (checked after the first if-branch).
435 message(FATAL_ERROR "Must have Go installed when using BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS")
436 elseif(BORINGSSL_PREFIX OR BORINGSSL_PREFIX_SYMBOLS)
437 # Only one of BORINGSSL_PREFIX or BORINGSSL_PREFIX_SYMBOLS is set
438 message(FATAL_ERROR "Must specify both or neither of BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS")
439 else()
440 add_custom_target(boringssl_prefix_symbols)
441
442 install(DIRECTORY include/openssl
443 DESTINATION ${AWSLC_INSTALL_INCLUDEDIR}
444 COMPONENT Development
445 FILES_MATCHING
446 PATTERN boringssl_prefix_symbols.h
447 PATTERN boringssl_prefix_symbols_asm.h
448 PATTERN boringssl_prefix_symbols_nasm.inc
449 )
450 endif()
451
452 # Rust bindings generation
453 if(GENERATE_RUST_BINDINGS)
454 include(cmake/rust_bindings.cmake)
455
456 # Generate the rust_wrapper.h header from template
457 generate_rust_wrapper_header()
458
459 set(RUST_BINDINGS_OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/rust/aws_lc_bindings.rs")
460
461 # Build list of include directories for bindgen.
462 # Prefix symbols header is intentionally excluded; see cmake/rust_bindings.cmake.
463 set(RUST_BINDINGS_INCLUDE_DIRS
464 "${CMAKE_CURRENT_BINARY_DIR}/include" # Generated rust_wrapper.h location
465 "${AWSLC_SOURCE_DIR}/include" # Main AWS-LC headers (unprefixed)
466 )
467
468 # Conditionally set optional arguments for generate_rust_bindings
469 if(BORINGSSL_PREFIX)
470 set(_rust_prefix_arg PREFIX "${BORINGSSL_PREFIX}")
471 else()
472 set(_rust_prefix_arg "")
473 endif()
474
475 if(BUILD_LIBSSL)
476 set(_rust_ssl_arg INCLUDE_SSL)
477 else()
478 set(_rust_ssl_arg "")
479 endif()
480
481 generate_rust_bindings(
482 OUTPUT_FILE "${RUST_BINDINGS_OUTPUT}"
483 INCLUDE_DIRS ${RUST_BINDINGS_INCLUDE_DIRS}
484 ${_rust_prefix_arg}
485 ${_rust_ssl_arg}
486 )
487
488 # Install the generated Rust bindings.
489 # The rust_bindings target is not part of ALL, so we build it on-demand during install.
490 install(CODE "execute_process(COMMAND \"${CMAKE_COMMAND}\" --build \"${CMAKE_BINARY_DIR}\" --target rust_bindings)")
491
492 install(
493 FILES "${CMAKE_CURRENT_BINARY_DIR}/rust/aws_lc_bindings.rs"
494 DESTINATION "share/rust"
495 COMPONENT Development
496 )
497
498 # Install the generated rust_wrapper.h header
499 install(
500 FILES "${CMAKE_CURRENT_BINARY_DIR}/include/rust_wrapper.h"
501 DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
502 COMPONENT Development
503 )
504 endif()
505
506
507 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten")
508 set(EMSCRIPTEN 1)
509 endif()
510
511 # Checks if test file compiles. If so, sets flag_to_set both as a CMake variable
512 # and as a preprocessor define. Additional arguments are interpreted as CFLAGS
513 # for the attempted compilation.
514 macro(check_compiler file_to_test flag_to_set)
515 try_compile(
516 ${flag_to_set} # Use CMake variable of the same name as the C flag to allow later reference
517 ${AWSLC_BINARY_DIR}
518 SOURCES "${AWSLC_SOURCE_DIR}/tests/compiler_features_tests/${file_to_test}"
519 COMPILE_DEFINITIONS "-Werror ${ARGN}"
520 OUTPUT_VARIABLE ERROR_MESSAGE)
521 if(${flag_to_set})
522 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D${flag_to_set}")
523 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D${flag_to_set}")
524 message(STATUS "${file_to_test} probe is positive, enabling ${flag_to_set}")
525 else()
526 message(STATUS "${file_to_test} probe is negative, NOT enabling ${flag_to_set}:")
527 # Some build applications use regexes on build output to highlight build
528 # errors. Below, we modify a compiler error message to avoid that a negative
529 # probe is considered an error in such build applications.
530 if(FEATURE_PROBE_VERBOSE)
531 string(REPLACE ": error:"
532 ": compiler_error:" ERROR_MESSAGE_PROCESSED
533 ${ERROR_MESSAGE})
534 message(STATUS " ${ERROR_MESSAGE_PROCESSED}")
535 else()
536 message(STATUS "Use -DFEATURE_PROBE_VERBOSE=ON to view feature probe error output.")
537 endif()
538 endif()
539 endmacro()
540
541 macro(check_run file_to_test flag_to_set compile_flags)
542 message(STATUS "Run check_run file_to_test '${file_to_test}', "
543 "flag_to_set '${flag_to_set}', "
544 "and compile_flags '${compile_flags}'.")
545 try_run(
546 ${flag_to_set}
547 COMPILE_RESULT
548 "${CMAKE_CURRENT_BINARY_DIR}"
549 "${CMAKE_CURRENT_LIST_DIR}/tests/compiler_features_tests/${file_to_test}"
550 COMPILE_DEFINITIONS "${compile_flags}"
551 OUTPUT_VARIABLE COMPILE_AND_RUN_OUTPUT)
552 if (NOT COMPILE_RESULT)
553 message(WARNING "COMPILE_AND_RUN_OUTPUT ${COMPILE_AND_RUN_OUTPUT}")
554 endif()
555 endmacro()
556
557 # Some ancient assemblers don't know about AVX instructions, which is an
558 # assumption we make for some of the assembly implementations. This flag
559 # can be set to handle such cases.
560 option(MY_ASSEMBLER_IS_TOO_OLD_FOR_AVX "Exclude AVX code from the build" OFF)
561
562 # Some assemblers know about AVX but not ADX, AVX2 or AVX512 instructions, e.g. gcc 4.8.2.
563 # This flag can be set to handle such cases.
564 # Note:
565 # * Although this flag name implies an effect on AVX512 instructions, it's also
566 # intended to avoid generating ADX and AVX2 instructions.
567 # * This flag name has "512AVX" instead of "AVX512" so that it doesn't
568 # include the entire flag -DMY_ASSEMBLER_IS_TOO_OLD_FOR_AVX and match
569 # to it in the Perl files checks.
570 option(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX "Exclude AVX512 code from the build" OFF)
571
572 if(MY_ASSEMBLER_IS_TOO_OLD_FOR_AVX)
573 add_definitions(-DMY_ASSEMBLER_IS_TOO_OLD_FOR_AVX)
574 add_definitions(-DMY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX)
575 set(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX ON)
576 message(STATUS "MY_ASSEMBLER_IS_TOO_OLD_FOR_AVX selected, removing all AVX optimisations")
577 endif()
578
579 if(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX)
580 add_definitions(-DMY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX)
581 message(STATUS "MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX selected, removing ADX, AVX2 and AVX512 optimisations")
582 endif()
583
584 if (GCC)
585 # All versions of GCC that AWS-LC supports has this warning
586 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wredundant-decls")
587
588 # Detect if memcmp is wrongly stripped like strcmp.
589 # If exists, let CMake generate a warning.
590 # memcmp bug link https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95189.
591 # CMake try_run requires these variables must be preset.
592 # https://cmake.org/cmake/help/latest/command/try_run.html
593 set(MEMCMP_INVALID_STRIPPED "")
594 set(MEMCMP_INVALID_STRIPPED__TRYRUN_OUTPUT "")
595
596 # See https://gitlab.kitware.com/cmake/cmake/-/issues/16920 for why we set this to 2
597 # During the `try_run` commands invoked by `check_run` a test program is run for the threading library.
598 # This test is run expecting two threads, and this variable isn't set, for some systems
599 # (particularly for systems using cross-compilation) causing our test to run without 2 threads and fail.
600 # We set this variable here to allow these builds to succeed and keep these tests around to make sure
601 # that we properly detect the compiler bugs that we are trying to test for with these `try_run` calls.
602
603 set(THREADS_PTHREAD_ARG "2" CACHE STRING "Forcibly set by CMakeLists.txt." FORCE)
604
605 if (CMAKE_BUILD_TYPE_LOWER MATCHES "release")
606 # CMAKE_C_FLAGS_RELEASE enables `-O3`.
607 check_run(memcmp_invalid_stripped_check.c MEMCMP_INVALID_STRIPPED "${CMAKE_C_FLAGS_RELEASE}")
608 elseif(CMAKE_BUILD_TYPE_LOWER MATCHES "relwithdebinfo")
609 # CMAKE_C_FLAGS_RELEASE enables `-O2`.
610 check_run(memcmp_invalid_stripped_check.c MEMCMP_INVALID_STRIPPED "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
611 endif()
612
613 if (MEMCMP_INVALID_STRIPPED)
614 message(WARNING "Currently, GCC ${CMAKE_C_COMPILER_VERSION} is not supported due to a memcmp related bug reported in "
615 "https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95189.\n"
616 "We strongly recommend against using the GCC ${CMAKE_C_COMPILER_VERSION} compiler.")
617 endif ()
618 endif ()
619
620 if(GCC OR CLANG)
621 # Note clang-cl is odd and sets both CLANG and MSVC. We base our configuration
622 # primarily on our normal Clang one.
623 if (NOT CMAKE_VERSION VERSION_GREATER "3.1.0")
624 message(STATUS "Adding CMAKE_C_FLAGS -std=c${CMAKE_C_STANDARD}")
625 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c${CMAKE_C_STANDARD}")
626 else()
627 message(STATUS "Setting CMAKE_C_STANDARD=${CMAKE_C_STANDARD}")
628 endif()
629
630 # TODO(CryptoAlg-759): enable '-Wpedantic' if awslc has to follow c99 spec.
631 if(CLANG OR (GCC AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.1.3"))
632 # GCC 4.1.3 and below do not support all of these flags or they raise false positives.
633 if (MSVC)
634 # clang-cl sets different default warnings than clang. It also treats -Wall
635 # as -Weverything, to match MSVC. Instead -W3 is the alias for -Wall.
636 # See http://llvm.org/viewvc/llvm-project?view=revision&revision=319116
637 set(C_CXX_FLAGS "${C_CXX_FLAGS} -W3 -fmsc-version=1900")
638 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-deprecated-declarations")
639 else()
640 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra")
641 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wall -fvisibility=hidden -fno-common")
642 if(CLANG AND MINGW)
643 set(C_CXX_FLAGS "${C_CXX_FLAGS} -fms-extensions")
644 endif()
645 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wno-newline-eof")
646 endif()
647 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wunused -Wcomment -Wchar-subscripts -Wuninitialized -Wshadow")
648 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wwrite-strings -Wformat-security -Wunused-result -Wno-overlength-strings")
649 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wvla -Wtype-limits -Wno-unused-parameter")
650 if(CLANG)
651 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-c11-extensions")
652 endif()
653 endif()
654 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings")
655
656 # GCC 8.x added a warning called -Wcast-function-type to the -Wextra umbrella.
657 # Also suppress for all clang versions supporting this warning.
658 add_flag_if_supported(CMAKE_C_FLAGS "-Wno-cast-function-type")
659
660 if(CMAKE_BUILD_TYPE_LOWER STREQUAL "debug" OR CMAKE_BUILD_TYPE_LOWER STREQUAL "relwithdebinfo")
661 if (MSVC)
662 set(C_CXX_FLAGS "${C_CXX_FLAGS} /Zi")
663 else()
664 if(EMSCRIPTEN)
665 # emscripten's emcc/clang does not accept the "-ggdb" flag.
666 set(C_CXX_FLAGS "${C_CXX_FLAGS} -g")
667 else()
668 set(C_CXX_FLAGS "${C_CXX_FLAGS} -ggdb")
669 endif()
670 endif()
671 endif()
672
673 if(MINGW AND NOT CLANG)
674 # Some MinGW compilers set _WIN32_WINNT to an older version (Windows Server 2003)
675 # See: https://learn.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt?view=msvc-170
676 # Support Windows 7 and later.
677 add_definitions(-D_WIN32_WINNT=_WIN32_WINNT_WIN7)
678 endif()
679
680 if(CLANG)
681 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof -fcolor-diagnostics")
682 else()
683 # GCC (at least 4.8.4) has a bug where it'll find unreachable free() calls
684 # and declare that the code is trying to free a stack pointer. GCC 4.1.3 and lower
685 # doesn't support this flag and can't use it.
686 add_flag_if_supported(C_CXX_FLAGS "-Wno-free-nonheap-object")
687 # GCC (from at least 4.8) does not include -Wmissing-braces in -Wall due to Bug 25137.
688 # This warning is turned on everywhere internally however, so we have to define it here
689 # to check that our changes don't break the build.
690 # See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25137
691 add_flag_if_supported(C_CXX_FLAGS "-Wmissing-braces")
692 endif()
693
694 # -Wstring-concatenation was added in Clang 12.0.0, which corresponds to
695 # AppleClang 13.0.0 per the table in
696 # https://en.wikipedia.org/wiki/Xcode#Toolchain_versions
697 add_flag_if_supported(C_CXX_FLAGS "-Wstring-concatenation")
698
699 add_flag_if_supported(C_CXX_FLAGS "-Wimplicit-fallthrough")
700
701 if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER "5")
702 set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wformat-signedness")
703 endif()
704
705 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
706 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_CXX_FLAGS} -Wmissing-declarations")
707
708 if(NOT MSVC)
709 if (NOT CMAKE_CXX_STANDARD)
710 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
711 endif ()
712 if(APPLE)
713 set(CMAKE_MACOSX_RPATH 1)
714 endif()
715 if(NOT BORINGSSL_ALLOW_CXX_RUNTIME)
716 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
717 endif()
718 endif()
719
720 # In GCC, -Wmissing-declarations is the C++ spelling of -Wmissing-prototypes
721 # and using the wrong one is an error. In Clang, -Wmissing-prototypes is the
722 # spelling for both and -Wmissing-declarations is some other warning.
723 #
724 # https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Warning-Options.html#Warning-Options
725 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-prototypes
726 # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-declarations
727 if(CLANG)
728 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-prototypes")
729 endif()
730
731 if(GCC AND "4.8" VERSION_GREATER CMAKE_C_COMPILER_VERSION AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.1.3")
732 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-array-bounds")
733 endif()
734
735 check_compiler("linux_random_h.c" HAVE_LINUX_RANDOM_H)
736 if(NOT HAVE_LINUX_RANDOM_H)
737 check_compiler("linux_random_h.c" AWS_LC_URANDOM_NEEDS_U32 "-DDEFINE_U32")
738 if(AWS_LC_URANDOM_NEEDS_U32)
739 add_definitions("-DHAVE_LINUX_RANDOM_H")
740 endif()
741 endif()
742
743 check_compiler("stdalign_check.c" AWS_LC_STDALIGN_AVAILABLE)
744 check_compiler("builtin_swap_check.c" AWS_LC_BUILTIN_SWAP_SUPPORTED)
745
746 elseif(MSVC)
747 set(MSVC_DISABLED_WARNINGS_LIST
748 "C4061" # enumerator 'identifier' in switch of enum 'enumeration' is not
749 # explicitly handled by a case label
750 # Disable this because it flags even when there is a default.
751 "C4100" # 'exarg' : unreferenced formal parameter
752 "C4127" # conditional expression is constant
753 "C4146" # unary minus operator applied to unsigned type, result still unsigned
754 "C4200" # nonstandard extension used : zero-sized array in
755 # struct/union.
756 "C4204" # nonstandard extension used: non-constant aggregate initializer
757 "C4221" # nonstandard extension used : 'identifier' : cannot be
758 # initialized using address of automatic variable
759 "C4242" # 'function' : conversion from 'int' to 'uint8_t',
760 # possible loss of data
761 "C4244" # 'function' : conversion from 'int' to 'uint8_t',
762 # possible loss of data
763 "C4267" # conversion from 'size_t' to 'int', possible loss of data
764 "C4291" # 'void *operator new(std::size_t,void *) throw()': no matching
765 # operator delete found; memory will not be freed if initialization
766 # throws an exception
767 "C4296" # '>=' : expression is always true
768 "C4371" # layout of class may have changed from a previous version of the
769 # compiler due to better packing of member '...'
770 "C4388" # signed/unsigned mismatch
771 "C4350" # behavior change: 'std::_Wrap_alloc...'
772 "C4365" # '=' : conversion from 'size_t' to 'int',
773 # signed/unsigned mismatch
774 "C4389" # '!=' : signed/unsigned mismatch
775 "C4464" # relative include path contains '..'
776 "C4510" # 'argument' : default constructor could not be generated
777 "C4512" # 'argument' : assignment operator could not be generated
778 "C4514" # 'function': unreferenced inline function has been removed
779 "C4548" # expression before comma has no effect; expected expression with
780 # side-effect" caused by FD_* macros.
781 "C4571" # Informational: catch(...) semantics changed since Visual C++ 7.1;
782 # structured exceptions (SEH) are no longer caught
783 "C4610" # struct 'argument' can never be instantiated - user defined
784 # constructor required.
785 "C4623" # default constructor was implicitly defined as deleted
786 "C4625" # copy constructor could not be generated because a base class
787 # copy constructor is inaccessible or deleted
788 "C4626" # assignment operator could not be generated because a base class
789 # assignment operator is inaccessible or deleted
790 "C4628" # digraphs not supported with -Ze
791 "C4668" # 'symbol' is not defined as a preprocessor macro, replacing with
792 # '0' for 'directives'
793 # Disable this because GTest uses it everywhere.
794 "C4706" # assignment within conditional expression
795 "C4710" # 'function': function not inlined
796 "C4711" # function 'function' selected for inline expansion
797 "C4800" # 'int' : forcing value to bool 'true' or 'false'
798 # (performance warning)
799 "C4820" # 'bytes' bytes padding added after construct 'member_name'
800 "C5026" # move constructor was implicitly defined as deleted
801 "C5027" # move assignment operator was implicitly defined as deleted
802 "C5039" # pointer or reference to potentially throwing function passed to 'extern "C"' function under -EHc
803 "C5045" # Compiler will insert Spectre mitigation for memory load if
804 # /Qspectre switch specified
805 "C4255" # no function prototype given: converting '()' to '(void)'
806 "C4152" # non standard extension, function/data ptr conversion in expression
807 # used in bcm.c to check functions are inside the FIPS module memory region
808 "C4295" # array is too small to include a terminating null character
809 "C4701" # potentially uninitialized local
810 "C4505" # unreferenced local function has been removed
811 "C4702" # unreachable code in bcm.c power on tests
812 "C4191" # unsafe conversion from 'type of expression' to 'type required'
813 # TODO remove once https://github.com/openssl/openssl/issues/18957 is resolved
814 "C4996" # deprecated warnings using low level functions in OpenSSL 3.0
815 "C4746" # consider using __iso_volatile_load/store intrinsic functions
816 "C5264" # 'variable-name': 'const' variable is not used
817 "C5266" # 'const' qualifier on return type has no effect
818 "C5267" # definition of implicit copy constructor for '...' is deprecated because it has a user-provided destructor
819 )
820 set(MSVC_LEVEL4_WARNINGS_LIST
821 # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
822 "C4265" # class has virtual functions, but destructor is not virtual
823 )
824 string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
825 ${MSVC_DISABLED_WARNINGS_LIST})
826 string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
827 ${MSVC_LEVEL4_WARNINGS_LIST})
828 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
829 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
830 endif()
831
832 # If we're using MSVC on Windows in FIPS mode with RelWithDebInfo then we want to override some of the default RelWithDebInfo flags.
833 # This fixes the problem we run into with RelWithDebInfo and FIPS mode on Windows where the FIPS module wouldn't span the expected symbol.
834 if(MSVC AND CMAKE_BUILD_TYPE_LOWER MATCHES "relwithdebinfo" AND FIPS)
835 # /Zi requires the /debug flag for executables/libraries that we want .pdb files for.
836 # We want to replace the default /debug flag with /DEBUG:FULL, to explicitly make sure that the .pdb files can be used on machines other than one on which it's built.
837 string(REPLACE "/debug" "/DEBUG:FULL" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}")
838 string(REPLACE "/debug" "/DEBUG:FULL" CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")
839
840 # The /debug flag also turns off the /OPT linker flag so we want to turn them back on across the board.
841 set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /OPT:REF,ICF,LBR")
842 set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /OPT:REF,ICF,LBR")
843 endif()
844
845 if(WIN32)
846 add_definitions(-D_HAS_EXCEPTIONS=0)
847 add_definitions(-DWIN32_LEAN_AND_MEAN)
848 add_definitions(-DNOMINMAX)
849 # Allow use of fopen.
850 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
851 # VS 2017 and higher supports STL-only warning suppressions.
852 # _STL_EXTRA_DISABLED_WARNINGS is only consumed by MSVC STL headers,
853 # so scope the definition to C++. Using add_compile_options with a
854 # CMake-escaped space keeps the value as a single argument under
855 # Ninja + clang-cl, where add_definitions with a space-containing
856 # value is not quoted robustly (see aws/aws-lc-rs#981).
857 add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-D_STL_EXTRA_DISABLED_WARNINGS=4774\ 4987>)
858 endif()
859
860 add_flag_if_supported(C_CXX_FLAGS "-Wshadow")
861
862 # pthread_rwlock_t on Linux requires a feature flag. We limit this to Linux
863 # because, on Apple platforms, it instead disables APIs we use. See compat(5)
864 # and sys/cdefs.h. Reportedly, FreeBSD also breaks when this is set. See
865 # https://crbug.com/boringssl/471.
866 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
867 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
868 endif()
869
870 if(FUZZ)
871 if(NOT CLANG)
872 message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
873 endif()
874
875 if(CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0.0")
876 message(FATAL_ERROR "You need Clang ≥ 6.0.0")
877 endif()
878
879 add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
880 set(RUNNER_ARGS "-deterministic")
881
882 if(NOT NO_FUZZER_MODE)
883 add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
884 set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
885 endif()
886
887 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
888 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
889 endif()
890
891 if(SSL_RUNNER_SHIM_CONFIG)
892 set(RUNNER_ARGS ${RUNNER_ARGS} "-shim-config" "${AWSLC_SOURCE_DIR}/${SSL_RUNNER_SHIM_CONFIG}")
893 endif()
894
895 if(BUILD_SHARED_LIBS)
896 add_definitions(-DBORINGSSL_SHARED_LIBRARY)
897 # Enable position-independent code globally. This is needed because
898 # some library targets are OBJECT libraries.
899 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
900 elseif(NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
901 set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
902 endif()
903
904 if(MSAN)
905 if(NOT CLANG)
906 message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
907 endif()
908
909 if(ASAN)
910 message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
911 endif()
912
913 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
914 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
915 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
916 endif()
917
918 if(ASAN)
919 if(NOT CLANG)
920 message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
921 endif()
922
923 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
924 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
925 endif()
926
927 if(CFI)
928 if(NOT CLANG)
929 message(FATAL_ERROR "Cannot enable CFI unless using Clang")
930 endif()
931
932 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
933 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
934 # We use Chromium's copy of clang, which requires -fuse-ld=lld if building
935 # with -flto. That, in turn, can't handle -ggdb.
936 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
937 string(REPLACE "-ggdb" "-g" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
938 string(REPLACE "-ggdb" "-g" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
939 # -flto causes object files to contain LLVM bitcode. Mixing those with
940 # assembly output in the same static library breaks the linker.
941 set(OPENSSL_NO_ASM "1")
942 endif()
943
944 if(TSAN)
945 if(NOT CLANG)
946 message(FATAL_ERROR "Cannot enable TSAN unless using Clang")
947 endif()
948
949 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
950 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
951 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
952 endif()
953
954 if(UBSAN)
955 if(NOT CLANG)
956 message(FATAL_ERROR "Cannot enable UBSAN unless using Clang")
957 endif()
958
959 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
960 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
961 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
962
963 if(NOT UBSAN_RECOVER)
964 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize-recover=undefined")
965 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize-recover=undefined")
966 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-sanitize-recover=undefined")
967 endif()
968 endif()
969
970 if(GCOV)
971 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage -O0")
972 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage -O0")
973 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
974 endif()
975
976 if(KEEP_ASM_LOCAL_SYMBOLS)
977 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,-L")
978 endif()
979
980 if(FIPS)
981 if(NOT GO_EXECUTABLE OR NOT PERL_EXECUTABLE)
982 message(FATAL_ERROR "Building AWS-LC for FIPS requires Go and Perl")
983 endif()
984
985 if(NOT BUILD_SHARED_LIBS AND (WIN32 OR APPLE))
986 message(FATAL_ERROR "Static FIPS build of AWS-LC is supported only on Linux")
987 endif()
988
989 if(CMAKE_C_FLAGS MATCHES "(^| )-DAWSLC_FIPS_FAILURE_CALLBACK($| )" AND BUILD_SHARED_LIBS)
990 message(FATAL_ERROR "AWSLC_FIPS_FAILURE_CALLBACK only supported with the static library build of AWS-LC")
991 endif()
992
993 # The FIPS integrity check does not work for ASan and MSan builds.
994 if(NOT ASAN AND NOT MSAN)
995 if(BUILD_SHARED_LIBS)
996 set(FIPS_SHARED "1")
997 else()
998 set(FIPS_DELOCATE "1")
999 endif()
1000 endif()
1001
1002 add_definitions(-DBORINGSSL_FIPS)
1003 endif()
1004
1005 # The Android CMake files set -ffunction-sections and -fdata-sections, which
1006 # is incompatible with FIPS_SHARED.
1007 if(FIPS_SHARED AND ANDROID)
1008 set(CMAKE_C_FLAGS
1009 "${CMAKE_C_FLAGS} -fno-function-sections -fno-data-sections")
1010 set(CMAKE_CXX_FLAGS
1011 "${CMAKE_CXX_FLAGS} -fno-function-sections -fno-data-sections")
1012 endif()
1013
1014 if(OPENSSL_SMALL)
1015 add_definitions(-DOPENSSL_SMALL)
1016 endif()
1017
1018 if(CONSTANT_TIME_VALIDATION)
1019 add_definitions(-DBORINGSSL_CONSTANT_TIME_VALIDATION)
1020 endif()
1021
1022 # CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
1023 # architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
1024 # alone, and expects all architecture-specific logic to be conditioned within
1025 # the source files rather than the build. This does not work for our assembly
1026 # files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
1027 # builds.
1028 if(NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
1029 list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
1030 if(NOT NUM_ARCHES EQUAL 1)
1031 message(FATAL_ERROR "Universal binaries not supported.")
1032 endif()
1033 list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
1034 endif()
1035
1036 if(MALLOC_FAILURE_TESTING)
1037 add_definitions(-DBORINGSSL_MALLOC_FAILURE_TESTING)
1038 endif()
1039
1040 TEST_BIG_ENDIAN(BIG_ENDIAN)
1041
1042 if(OPENSSL_NO_SSE2_FOR_TESTING)
1043 add_definitions(-DOPENSSL_NO_SSE2_FOR_TESTING)
1044 endif()
1045
1046 if(HOST_ILLUMOS OR HOST_SOLARIS)
1047 #
1048 # CMAKE_SYSTEM_PROCESSOR unfortunately comes from the output of "uname -p",
1049 # which on illumos systems emits "i386". Instead, use the value from
1050 # "isainfo -n", which prints "the name of the native instruction set used by
1051 # portable applications"; e.g., "amd64".
1052 #
1053 execute_process(COMMAND /usr/bin/isainfo -n OUTPUT_VARIABLE
1054 CMAKE_SYSTEM_PROCESSOR_LOWER OUTPUT_STRIP_TRAILING_WHITESPACE)
1055 else()
1056 # Some consumers might use upper-case (e.g.) "X86" or "X86_64".
1057 # Matching below is based on lower-case.
1058 string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" CMAKE_SYSTEM_PROCESSOR_LOWER)
1059 endif()
1060
1061 if(OPENSSL_NO_ASM)
1062 add_definitions(-DOPENSSL_NO_ASM)
1063 set(ARCH "generic")
1064 elseif(CMAKE_SYSTEM_PROCESSOR_LOWER MATCHES "x86_64|amd64")
1065 # If ARCH is originally detected as 64-bit, perform an additional check
1066 # to determine whether to build as 32-bit or 64-bit. This happens in some
1067 # cases such as when building in Docker, where the host-level architecture is 64-bit
1068 # but the Docker image should result in building for a 32-bit architecture.
1069 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
1070 set(ARCH "x86_64")
1071 else()
1072 set(ARCH "x86")
1073 endif()
1074 elseif(CMAKE_SYSTEM_PROCESSOR_LOWER MATCHES "x86|i386|i686")
1075 set(ARCH "x86")
1076 elseif(CMAKE_SYSTEM_PROCESSOR_LOWER MATCHES "arm64.*|aarch64")
1077 # If ARCH is originally detected as 64-bit, perform an additional check
1078 # to determine whether to build as 32-bit or 64-bit. This happens in some
1079 # cases such as when building on an 64-bit CPU where the OS is 32-bit.
1080 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
1081 set(ARCH "aarch64")
1082 else()
1083 set(ARCH "arm")
1084 endif()
1085 elseif(CMAKE_SYSTEM_PROCESSOR_LOWER MATCHES "^arm*")
1086 set(ARCH "arm")
1087 elseif(CMAKE_SYSTEM_PROCESSOR_LOWER MATCHES "powerpc64le|ppc64le")
1088 set(ARCH "ppc64le")
1089 elseif(CMAKE_SYSTEM_PROCESSOR_LOWER STREQUAL "riscv64")
1090 set(ARCH "riscv64")
1091 elseif(CMAKE_SYSTEM_PROCESSOR_LOWER STREQUAL "s390x")
1092 set(ARCH "s390x")
1093 elseif(CMAKE_SYSTEM_PROCESSOR_LOWER STREQUAL "loongarch64")
1094 set(ARCH "loongarch64")
1095 else()
1096 message(STATUS "Unknown processor found. Using generic implementations. Processor: " ${CMAKE_SYSTEM_PROCESSOR})
1097 set(ARCH "generic")
1098 endif()
1099
1100 if(ARCH STREQUAL "aarch64" AND CMAKE_GENERATOR MATCHES "Visual Studio" AND NOT "${CMAKE_VS_PLATFORM_TOOLSET}" MATCHES "ClangCL")
1101 message(FATAL_ERROR "AWS-LC Windows/ARM64 assembly code requires ClangCL. Current toolset: ${CMAKE_VS_PLATFORM_TOOLSET}")
1102 endif()
1103
1104 if(WIN32 AND FIPS AND (CMAKE_BUILD_TYPE_LOWER STREQUAL "debug" OR (CMAKE_BUILD_TYPE_LOWER STREQUAL "relwithdebinfo" AND ARCH STREQUAL "aarch64")))
1105 message(FATAL_ERROR "Windows Debug and RelWithDebInfo builds are not supported with FIPS, use Release")
1106 endif()
1107
1108 # If target ARCH is 32-bit x86, ensure SSE2 is enabled since it's used by the optimized assembly.
1109 # To build for targets that do not support SSE2, use the `OPENSSL_NO_ASM` flag.
1110 if(ARCH STREQUAL "x86" AND NOT OPENSSL_NO_SSE2_FOR_TESTING)
1111 # Most compilers enable SSE2 in 32-bit x86 by default, but in some cases GCC and Clang don't.
1112 # See: https://github.com/aws/aws-lc/commit/6fe8dcbe96e580ea85233fdb98a142e42951b70b
1113 if(GCC OR CLANG)
1114 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse2")
1115 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse2")
1116 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -msse2")
1117 endif()
1118 endif()
1119
1120 if(ENABLE_DATA_INDEPENDENT_TIMING)
1121 add_definitions(-DENABLE_AUTO_SET_RESET_DIT)
1122 endif()
1123
1124 if(USE_CUSTOM_LIBCXX)
1125 if(NOT CLANG)
1126 message(FATAL_ERROR "USE_CUSTOM_LIBCXX only supported with Clang")
1127 endif()
1128
1129 # The docker images set an environment variable to the llvm project directory which the sandbox builds will use,
1130 # you can also pass in the llvm project path as a CMake parameter which takes precedence over the environment variable
1131 if(DEFINED ENV{LLVM_PROJECT_HOME} AND NOT LLVM_PROJECT_HOME)
1132 set(LLVM_PROJECT_HOME $ENV{LLVM_PROJECT_HOME})
1133 endif()
1134
1135 if(NOT LLVM_PROJECT_HOME)
1136 message(FATAL "Could not find path to LLVM project, must set LLVM_PROJECT_HOME environment variable or pass in -DLLVM_PROJECT_HOME")
1137 endif()
1138
1139 # CMAKE_CXX_FLAGS ends up in the linker flags as well, so use
1140 # add_compile_options. There does not appear to be a way to set
1141 # language-specific compile-only flags.
1142 add_compile_options("-nostdinc++")
1143 set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -nostdlib++")
1144 include_directories(
1145 SYSTEM
1146 util/bot/libcxx-config
1147 ${LLVM_PROJECT_HOME}/libcxx/include
1148 ${LLVM_PROJECT_HOME}/libcxxabi/include
1149 )
1150
1151 # This is patterned after buildtools/third_party/libc++/BUILD.gn and
1152 # buildtools/third_party/libc++abi/BUILD.gn in Chromium.
1153
1154 file(GLOB LIBCXX_SOURCES "${LLVM_PROJECT_HOME}/libcxx/src/*.cpp" "${LLVM_PROJECT_HOME}/libcxx/src/ryu/*.cpp")
1155 file(GLOB LIBCXXABI_SOURCES "${LLVM_PROJECT_HOME}/libcxxabi/src/*.cpp")
1156
1157 # This file is meant for exception-less builds.
1158 list(REMOVE_ITEM LIBCXXABI_SOURCES "${LLVM_PROJECT_HOME}/libcxxabi/src/cxa_noexception.cpp")
1159
1160 # libc++ also defines new and delete.
1161 list(REMOVE_ITEM LIBCXXABI_SOURCES "${LLVM_PROJECT_HOME}/libcxxabi/src/stdlib_new_delete.cpp")
1162 if(TSAN)
1163 # ThreadSanitizer tries to intercept these symbols. Skip them to avoid
1164 # symbol conflicts.
1165 list(REMOVE_ITEM LIBCXXABI_SOURCES "${LLVM_PROJECT_HOME}/libcxxabi/src/cxa_guard.cpp")
1166 endif()
1167
1168 add_library(libcxxabi ${LIBCXXABI_SOURCES})
1169 target_compile_definitions(
1170 libcxxabi PRIVATE
1171 -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS
1172 )
1173
1174 add_library(libcxx ${LIBCXX_SOURCES})
1175 if(ASAN OR MSAN OR TSAN)
1176 # Sanitizers try to intercept new and delete.
1177 target_compile_definitions(
1178 libcxx PRIVATE
1179 -D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
1180 )
1181 endif()
1182 target_compile_definitions(
1183 libcxx PRIVATE
1184 -D_LIBCPP_BUILDING_LIBRARY
1185 -DLIBCXX_BUILDING_LIBCXXABI
1186 )
1187 set_target_properties(
1188 libcxx libcxxabi PROPERTIES
1189 COMPILE_FLAGS "-Wno-missing-prototypes -Wno-implicit-fallthrough"
1190 # libc++ and libc++abi must be built in C++23 mode.
1191 # LLVM 19+ added expected.cpp which requires C++23.
1192 CXX_STANDARD 23
1193 CXX_STANDARD_REQUIRED TRUE
1194 )
1195 # libc++abi depends on libc++ internal headers.
1196 set_property(TARGET libcxx libcxxabi APPEND PROPERTY INCLUDE_DIRECTORIES "${LLVM_PROJECT_HOME}/libcxx/src")
1197 target_link_libraries(libcxx libcxxabi)
1198 endif()
1199
1200 if(BUILD_TESTING)
1201 # Add minimal googletest targets. The provided one has many side-effects, and
1202 # googletest has a very straightforward build.
1203 add_library(boringssl_gtest third_party/googletest/src/gtest-all.cc)
1204 if(USE_CUSTOM_LIBCXX)
1205 target_link_libraries(boringssl_gtest libcxx)
1206 endif()
1207 if(BUILD_SHARED_LIBS)
1208 # This is needed for the Windows build to correctly annotate GTest's API with __declspec(dllexport)
1209 target_compile_options(boringssl_gtest PRIVATE -DGTEST_CREATE_SHARED_LIBRARY=1)
1210 endif()
1211 target_include_directories(
1212 boringssl_gtest
1213 PUBLIC third_party/googletest/include
1214 PRIVATE third_party/googletest
1215 )
1216
1217 # Declare a dummy target to build all unit tests. Test targets should inject
1218 # themselves as dependencies next to the target definition.
1219 add_custom_target(all_tests)
1220
1221 # On Windows, CRYPTO_TEST_DATA is too long to fit in command-line limits.
1222 # TODO(davidben): CMake 3.12 has a list(JOIN) command. Use that when we've
1223 # updated the minimum version.
1224 set(EMBED_TEST_DATA_ARGS "")
1225 foreach(arg ${CRYPTO_TEST_DATA})
1226 set(EMBED_TEST_DATA_ARGS "${EMBED_TEST_DATA_ARGS}${arg}\n")
1227 endforeach()
1228 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/embed_test_data_args.txt"
1229 "${EMBED_TEST_DATA_ARGS}")
1230
1231 if(GO_EXECUTABLE)
1232 add_custom_command(
1233 OUTPUT crypto_test_data.cc
1234 COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go -file-list
1235 "${CMAKE_CURRENT_BINARY_DIR}/embed_test_data_args.txt" >
1236 "${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc"
1237 DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
1238 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
1239 else()
1240 add_custom_command(
1241 OUTPUT crypto_test_data.cc
1242 COMMAND ${CMAKE_COMMAND} -E tar "jxvf" ${GENERATE_CODE_ROOT}/crypto_test_data.cc.tar.bz2
1243 DEPENDS ${GENERATE_CODE_ROOT}/crypto_test_data.cc.tar.bz2
1244 WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
1245 endif()
1246 add_library(crypto_test_data OBJECT crypto_test_data.cc)
1247
1248 # sets tests to compile an so executable file for testing in our Android app CI.
1249 if(ANDROIDTESTRUNNER)
1250 set(TEST_EXECUTABLE_EXT ".so")
1251 endif()
1252
1253 set(CRYPTO_TEST_EXEC crypto_test${TEST_EXECUTABLE_EXT})
1254 set(RANDOM_TEST_EXEC urandom_test${TEST_EXECUTABLE_EXT})
1255 set(SSL_TEST_EXEC ssl_test${TEST_EXECUTABLE_EXT})
1256 set(MEM_TEST_EXEC mem_test${TEST_EXECUTABLE_EXT})
1257 set(MEM_SET_TEST_EXEC mem_set_test${TEST_EXECUTABLE_EXT})
1258 set(INTEGRATION_TEST_EXEC integration_test${TEST_EXECUTABLE_EXT})
1259 set(DYNAMIC_LOADING_TEST_EXEC dynamic_loading_test${TEST_EXECUTABLE_EXT})
1260 set(RWLOCK_STATIC_INIT_TEST_EXEC rwlock_static_init${TEST_EXECUTABLE_EXT})
1261 set(RANDOM_ISOLATED_EXEC rand_isolated_test${TEST_EXECUTABLE_EXT})
1262 set(TREE_DRBG_JITTER_ENTROPY_ISOLATED_EXEC tree_drbg_jitter_entropy_isolated_test${TEST_EXECUTABLE_EXT})
1263
1264 add_subdirectory(util/fipstools/acvp/modulewrapper)
1265
1266 macro(set_test_location executable_name)
1267 if(CMAKE_GENERATOR MATCHES "Visual Studio")
1268 message(NOTICE "Location for ${executable_name} is: ${CMAKE_CURRENT_BINARY_DIR}")
1269 # Set the output directory for the executable
1270 set_target_properties(${executable_name} PROPERTIES
1271 RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}"
1272 RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_BINARY_DIR}"
1273 RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_BINARY_DIR}"
1274 RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_CURRENT_BINARY_DIR}"
1275 RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL "${CMAKE_CURRENT_BINARY_DIR}"
1276 )
1277 endif()
1278 endmacro()
1279 endif()
1280
1281 # By default, include jitter entropy.
1282 if(NOT DISABLE_CPU_JITTER_ENTROPY)
1283 add_subdirectory(third_party/jitterentropy)
1284 endif()
1285
1286 # CMAKE_SYSTEM_NAME is "Generic" for embedded OSes:
1287 # https://cmake.org/cmake/help/book/mastering-cmake/chapter/Cross%20Compiling%20With%20CMake.html#toolchain-files
1288 #
1289 # For now we assume embedded OSes do not have threads. Additionally, the Threads
1290 # package does not work with Android, but Android does not require any extra
1291 # parameters to link pthreads. Emscripten provides its own pthread implementation
1292 # via Web Workers and SharedArrayBuffer, configured through compiler flags.
1293 if(NOT CMAKE_SYSTEM_NAME MATCHES "^(Generic|Android|Emscripten)$")
1294 find_package(Threads REQUIRED)
1295 set(AWSLC_LINK_THREADS TRUE)
1296 # CMAKE_THREAD_LIBS_INIT contains the actual linker flags (e.g., -lpthread)
1297 # set by find_package(Threads). Use this for pkgconfig instead of the
1298 # imported target name.
1299 set(PKGCONFIG_LIBS_PRIVATE "${CMAKE_THREAD_LIBS_INIT}")
1300 else()
1301 set(AWSLC_LINK_THREADS FALSE)
1302 set(PKGCONFIG_LIBS_PRIVATE "")
1303 endif()
1304
1305
1306 add_subdirectory(crypto)
1307 if(BUILD_LIBSSL)
1308 add_subdirectory(ssl)
1309 if(BUILD_TOOL)
1310 add_subdirectory(tool)
1311 add_subdirectory(tool-openssl)
1312 endif()
1313 endif()
1314 add_subdirectory(util/fipstools)
1315
1316 if(FUZZ)
1317 if(LIBFUZZER_FROM_DEPS)
1318 file(GLOB LIBFUZZER_SOURCES "util/bot/libFuzzer/*.cpp")
1319 add_library(Fuzzer STATIC ${LIBFUZZER_SOURCES})
1320 # libFuzzer does not pass our aggressive warnings. It also must be built
1321 # without -fsanitize-coverage options or clang crashes.
1322 set_target_properties(Fuzzer PROPERTIES COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -Wno-missing-prototypes -fsanitize-coverage=0")
1323 endif()
1324
1325 add_subdirectory(fuzz)
1326 endif()
1327
1328
1329 if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
1330 set(HANDSHAKER_ARGS "-handshaker-path" $<TARGET_FILE:handshaker>)
1331 set(SSL_TRANSFER_ARGS "-ssl-transfer-test-file" ${AWSLC_SOURCE_DIR}/ssl/test/runner/ssl_transfer/test_case_names.txt)
1332 if(DEFINED ENV{AWS_LC_SSL_RUNNER_START_INDEX})
1333 set(AWS_LC_SSL_RUNNER_INDEX_FILTER "-test-case-start-index" $ENV{AWS_LC_SSL_RUNNER_START_INDEX})
1334 endif()
1335 if(DEFINED ENV{AWS_LC_SSL_RUNNER_END_INDEX})
1336 set(AWS_LC_SSL_RUNNER_INDEX_FILTER "${AWS_LC_SSL_RUNNER_INDEX_FILTER}" "-test-case-end-index" $ENV{AWS_LC_SSL_RUNNER_END_INDEX})
1337 endif()
1338 endif()
1339
1340 # Define GO_TEST_TIMEOUT based on env variable.
1341 # This is needed because sanitizer test in aarch64 takes 45 mins, which exceeds `go test` default timeout(10m).
1342 # https://golang.org/pkg/cmd/go/internal/test/
1343 if(DEFINED ENV{AWS_LC_GO_TEST_TIMEOUT})
1344 set(GO_TEST_TIMEOUT "$ENV{AWS_LC_GO_TEST_TIMEOUT}")
1345 else()
1346 set(GO_TEST_TIMEOUT "10m")
1347 endif()
1348
1349 # Allow overriding the SSL test runner's idle timeout (default in runner.go is 30s).
1350 # Useful for resource-constrained environments (e.g. FreeBSD VMs) where the shim
1351 # may be slow to respond.
1352 if(DEFINED ENV{AWS_LC_SSL_RUNNER_IDLE_TIMEOUT})
1353 set(RUNNER_ARGS ${RUNNER_ARGS} "-idle-timeout" "$ENV{AWS_LC_SSL_RUNNER_IDLE_TIMEOUT}")
1354 endif()
1355
1356 # Allow enabling retry on runner-side TCP timeouts.
1357 # Useful for resource-constrained environments (e.g. emulated FreeBSD VMs) where
1358 # transient timeouts do not indicate real bugs.
1359 if(DEFINED ENV{AWS_LC_SSL_RUNNER_RETRY_ON_TIMEOUT})
1360 set(RUNNER_ARGS ${RUNNER_ARGS} "-retry-on-timeout")
1361 endif()
1362
1363 if (NOT ${CMAKE_VERSION} VERSION_LESS "3.2")
1364 # USES_TERMINAL is only available in CMake 3.2 or later.
1365 set(MAYBE_USES_TERMINAL USES_TERMINAL)
1366 endif()
1367
1368 if(BUILD_TESTING)
1369 if(GO_EXECUTABLE)
1370 if(FIPS)
1371 if(WIN32)
1372 set(ACVP_TOOL ${AWSLC_BINARY_DIR}/acvptool.exe)
1373 set(TEST_WRAPPER ${AWSLC_BINARY_DIR}/testmodulewrapper.exe)
1374 else()
1375 set(ACVP_TOOL ${AWSLC_BINARY_DIR}/acvptool)
1376 set(TEST_WRAPPER ${AWSLC_BINARY_DIR}/testmodulewrapper)
1377 endif()
1378
1379 # Read util/go_fips_tests.txt into a CMake variable.
1380 file(READ util/go_fips_tests.txt GO_FIPS_TESTS)
1381 string(REPLACE "\n" ";" GO_FIPS_TESTS "${GO_FIPS_TESTS}")
1382 list(REMOVE_ITEM GO_FIPS_TESTS "")
1383 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
1384 util/go_fips_tests.txt)
1385
1386 if(WIN32)
1387 # Delocator doesn't support Windows. So, this test would fail.
1388 string(REGEX REPLACE "./util/fipstools/delocate" "" GO_FIPS_TESTS "${GO_FIPS_TESTS}")
1389 endif()
1390
1391 # We want to run the SHA tests iteratively because they contain Large Data Tests, which make
1392 # us hash anywhere from 1-8 gigabytes of memory, so running multiple at the same time cause a
1393 # failure on most machines due to a lack of memory.
1394 set(acvp_sha_test_commands)
1395 file(GLOB acvp_sha_tests "${AWSLC_SOURCE_DIR}/util/fipstools/acvp/acvptool/test/sha-tests/*.json")
1396 foreach(test_path ${acvp_sha_tests})
1397 get_filename_component(test_name ${test_path} NAME)
1398 list(APPEND acvp_sha_test_commands
1399 COMMAND ${GO_EXECUTABLE} run check_expected.go
1400 -tool ${ACVP_TOOL}
1401 -module-wrappers modulewrapper:$<TARGET_FILE:modulewrapper>,testmodulewrapper:${TEST_WRAPPER}
1402 -tests "sha-tests/${test_name}")
1403 endforeach()
1404
1405 add_custom_target(
1406 build_acvp_tool
1407 COMMAND ${GO_EXECUTABLE} build -o ${ACVP_TOOL}
1408 github.com/aws/aws-lc/util/fipstools/acvp/acvptool
1409 COMMAND ${GO_EXECUTABLE} build -o ${TEST_WRAPPER}
1410 github.com/aws/aws-lc/util/fipstools/acvp/acvptool/testmodulewrapper
1411 WORKING_DIRECTORY ${AWSLC_SOURCE_DIR}
1412 DEPENDS modulewrapper
1413 ${MAYBE_USES_TERMINAL})
1414
1415 add_custom_target(
1416 acvp_tests
1417 COMMAND ${CMAKE_COMMAND} -E echo
1418 COMMAND ${CMAKE_COMMAND} -E echo "Running ACVP tests"
1419 COMMAND ${GO_EXECUTABLE} run check_expected.go
1420 -tool ${ACVP_TOOL}
1421 -module-wrappers modulewrapper:$<TARGET_FILE:modulewrapper>,testmodulewrapper:${TEST_WRAPPER}
1422 -tests tests.json
1423 ${acvp_sha_test_commands}
1424 WORKING_DIRECTORY ${AWSLC_SOURCE_DIR}/util/fipstools/acvp/acvptool/test
1425 DEPENDS build_acvp_tool
1426 ${MAYBE_USES_TERMINAL})
1427
1428 add_custom_target(
1429 fips_specific_tests_if_any
1430 DEPENDS acvp_tests
1431 )
1432 else()
1433 add_custom_target(fips_specific_tests_if_any)
1434 endif()
1435
1436 # Add macho parser tests if FIPS and on MacOS
1437 if(FIPS AND APPLE)
1438 add_custom_target(
1439 macho_parser_tests
1440 COMMAND ./util/fipstools/inject_hash/macho_parser/tests/test_macho_parser
1441 WORKING_DIRECTORY ${AWSLC_BINARY_DIR}
1442 DEPENDS test_macho_parser
1443 )
1444 add_dependencies(fips_specific_tests_if_any macho_parser_tests)
1445 endif()
1446
1447 # Read util/go_tests.txt into a CMake variable.
1448 file(READ util/go_tests.txt GO_TESTS)
1449 foreach(fips_specific_test ${GO_FIPS_TESTS})
1450 set(GO_TESTS "${GO_TESTS}\n${fips_specific_test}")
1451 endforeach()
1452 string(REPLACE "\n" ";" GO_TESTS "${GO_TESTS}")
1453 list(REMOVE_ITEM GO_TESTS "")
1454 set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
1455 util/go_tests.txt)
1456
1457 if(BUILD_LIBSSL)
1458 add_custom_target(
1459 run_ssl_runner_tests
1460 COMMAND ${CMAKE_COMMAND} -E echo "Running SSL tests"
1461 COMMAND cd ssl/test/runner &&
1462 ${GO_EXECUTABLE} test -timeout ${GO_TEST_TIMEOUT} -shim-path $<TARGET_FILE:bssl_shim>
1463 ${HANDSHAKER_ARGS} ${RUNNER_ARGS} ${AWS_LC_SSL_RUNNER_INDEX_FILTER} ${SSL_TRANSFER_ARGS}
1464 WORKING_DIRECTORY ${AWSLC_SOURCE_DIR}
1465 DEPENDS bssl_shim handshaker fips_specific_tests_if_any
1466 ${MAYBE_USES_TERMINAL})
1467
1468 add_custom_target(
1469 run_ssl_runner_tests_valgrind
1470 COMMAND ${CMAKE_COMMAND} -E echo "Running SSL tests"
1471 COMMAND cd ssl/test/runner &&
1472 ${GO_EXECUTABLE} test -timeout ${GO_TEST_TIMEOUT} -shim-path $<TARGET_FILE:bssl_shim> -valgrind
1473 ${HANDSHAKER_ARGS} ${RUNNER_ARGS} ${AWS_LC_SSL_RUNNER_INDEX_FILTER} ${SSL_TRANSFER_ARGS}
1474 WORKING_DIRECTORY ${AWSLC_SOURCE_DIR}
1475 DEPENDS bssl_shim handshaker fips_specific_tests_if_any
1476 ${MAYBE_USES_TERMINAL})
1477
1478 add_custom_target(
1479 run_tests
1480 COMMAND ${CMAKE_COMMAND} -E echo
1481 COMMAND ${CMAKE_COMMAND} -E echo "Running Go tests"
1482 COMMAND ${GO_EXECUTABLE} test ${GO_TESTS}
1483 COMMAND ${CMAKE_COMMAND} -E echo
1484 COMMAND ${CMAKE_COMMAND} -E echo "Running unit tests"
1485 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
1486 ${AWSLC_BINARY_DIR}
1487 WORKING_DIRECTORY ${AWSLC_SOURCE_DIR}
1488 DEPENDS all_tests run_ssl_runner_tests
1489 ${MAYBE_USES_TERMINAL})
1490 else()
1491 add_custom_target(
1492 run_tests
1493 COMMAND ${CMAKE_COMMAND} -E echo "Running Go tests"
1494 COMMAND ${GO_EXECUTABLE} test ${GO_TESTS}
1495 COMMAND ${CMAKE_COMMAND} -E echo
1496 COMMAND ${CMAKE_COMMAND} -E echo "Running unit tests"
1497 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
1498 ${AWSLC_BINARY_DIR} -ssl-tests=false
1499 WORKING_DIRECTORY ${AWSLC_SOURCE_DIR}
1500 DEPENDS all_tests fips_specific_tests_if_any
1501 ${MAYBE_USES_TERMINAL}
1502 )
1503 endif()
1504
1505 add_custom_target(
1506 run_tests_valgrind
1507 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
1508 ${AWSLC_BINARY_DIR} -valgrind=true -valgrind-supp-dir="tests/ci"
1509 WORKING_DIRECTORY ${AWSLC_SOURCE_DIR}
1510 DEPENDS all_tests
1511 ${MAYBE_USES_TERMINAL})
1512
1513 add_custom_target(
1514 run_tests_with_sde
1515 COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
1516 ${AWSLC_BINARY_DIR} -sde=true -sde-path="$ENV{SDEROOT}/sde"
1517 WORKING_DIRECTORY ${AWSLC_SOURCE_DIR}
1518 DEPENDS all_tests
1519 ${MAYBE_USES_TERMINAL})
1520 else()
1521 if(BUILD_LIBSSL)
1522 add_custom_target(
1523 run_minimal_tests
1524 COMMAND crypto/crypto_test
1525 COMMAND crypto/urandom_test
1526 COMMAND ssl/ssl_test
1527 WORKING_DIRECTORY ${AWSLC_BINARY_DIR}
1528 DEPENDS all_tests
1529 ${MAYBE_USES_TERMINAL})
1530 else()
1531 add_custom_target(
1532 run_minimal_tests
1533 COMMAND crypto/crypto_test
1534 COMMAND crypto/urandom_test
1535 WORKING_DIRECTORY ${AWSLC_BINARY_DIR}
1536 DEPENDS all_tests
1537 ${MAYBE_USES_TERMINAL}
1538 )
1539 endif()
1540 endif()
1541 endif()
1542
1543 if(NOT MSVC AND NOT CLANG AND NOT GCC)
1544 message(STATUS "Alternative compiler '${CMAKE_C_COMPILER_ID}' detected. Not all flags may be set, check final options with 'cmake --build . -- VERBOSE=1'")
1545 endif()
1546
1547 include(cmake/JoinPaths.cmake)
1548 join_paths(LIBDIR_FOR_PC_FILE "\${prefix}" "${CMAKE_INSTALL_LIBDIR}")
1549 join_paths(INCLUDEDIR_FOR_PC_FILE "\${prefix}" "${AWSLC_INSTALL_INCLUDEDIR}")
1550
1551 function(install_pkgconfig_file)
1552 set(options "")
1553 set(oneValueArgs TEMPLATE DEST)
1554 set(multiValueArgs)
1555 if(CMAKE_VERSION VERSION_LESS "3.7")
1556 cmake_parse_arguments(arg "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
1557 else()
1558 cmake_parse_arguments(PARSE_ARGV 0 arg "${options}" "${oneValueArgs}" "${multiValueArgs}")
1559 endif()
1560
1561 configure_file(pkgconfig/${arg_TEMPLATE} ${CMAKE_CURRENT_BINARY_DIR}/pkgconfig/${arg_DEST} @ONLY)
1562 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/pkgconfig/${arg_DEST} DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
1563 endfunction()
1564
1565 if(ENABLE_SOURCE_MODIFICATION)
1566 configure_file(include/openssl/base.h.in ${AWSLC_SOURCE_DIR}/include/openssl/base.h @ONLY)
1567 configure_file(include/openssl/opensslv.h.in ${AWSLC_SOURCE_DIR}/include/openssl/opensslv.h @ONLY)
1568 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/util/check-linkage.sh.in")
1569 configure_file(util/check-linkage.sh.in check-linkage.sh @ONLY)
1570 endif()
1571 endif()
1572
1573 install_pkgconfig_file(TEMPLATE product.pc.in DEST aws-lc.pc)
1574 install_pkgconfig_file(TEMPLATE libcrypto.pc.in DEST lib${CRYPTO_LIB_NAME}.pc)
1575 install_pkgconfig_file(TEMPLATE libssl.pc.in DEST lib${SSL_LIB_NAME}.pc)
1576
1577 if(INSTALL_OPENSSL_SHIM)
1578 install_pkgconfig_file(TEMPLATE product.pc.in DEST openssl.pc)
1579
1580 # Create OpenSSL compatibility symlinks
1581 if(BUILD_SHARED_LIBS)
1582 if(SET_LIB_SONAME)
1583 # When SONAME build is enabled, libraries have -awslc suffix
1584 install(CODE "
1585 execute_process(COMMAND \${CMAKE_COMMAND} -E create_symlink
1586 lib${CRYPTO_LIB_NAME}.so \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libcrypto.so\")
1587 execute_process(COMMAND \${CMAKE_COMMAND} -E create_symlink
1588 lib${SSL_LIB_NAME}.so \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libssl.so\")
1589 ")
1590 endif()
1591 else()
1592 if(SET_LIB_SONAME)
1593 # When SONAME build is enabled, libraries have -awslc suffix
1594 install(CODE "
1595 execute_process(COMMAND \${CMAKE_COMMAND} -E create_symlink
1596 lib${CRYPTO_LIB_NAME}.a \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libcrypto.a\")
1597 execute_process(COMMAND \${CMAKE_COMMAND} -E create_symlink
1598 lib${SSL_LIB_NAME}.a \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/libssl.a\")
1599 " COMPONENT Development)
1600 endif()
1601 endif()
1602
1603 if(COHABITANT_HEADERS)
1604 # Always create the include directory symlink for OpenSSL compatibility
1605 install(CODE "
1606 execute_process(COMMAND \${CMAKE_COMMAND} -E create_symlink
1607 aws-lc/openssl \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_INCLUDEDIR}/openssl\")
1608 " COMPONENT Development)
1609 endif()
1610 endif()