CMakeLists.txt 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. cmake_minimum_required(VERSION 3.10)
  2. # Defer enabling C and CXX languages.
  3. project(BoringSSL NONE)
  4. # Don't install BoringSSL to system directories by default; it has no stable
  5. # ABI. Instead, default to an "install" directory under the source.
  6. if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  7. set(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install CACHE PATH "" FORCE)
  8. endif()
  9. if(WIN32)
  10. # On Windows, prefer cl over gcc if both are available. By default most of
  11. # the CMake generators prefer gcc, even on Windows.
  12. set(CMAKE_GENERATOR_CC cl)
  13. endif()
  14. include(sources.cmake)
  15. include(cmake/go.cmake)
  16. include(cmake/perlasm.cmake)
  17. enable_language(C)
  18. enable_language(CXX)
  19. include(GNUInstallDirs)
  20. # CMake versions before 3.14 do not have default destination values. Executable
  21. # and library targets that use a default destination should include this
  22. # variable.
  23. if(CMAKE_VERSION VERSION_LESS "3.14")
  24. set(INSTALL_DESTINATION_DEFAULT
  25. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  26. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  27. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
  28. endif()
  29. # Wrap the CMake install function so we can disable it.
  30. set(INSTALL_ENABLED 1)
  31. function(install_if_enabled)
  32. if(INSTALL_ENABLED)
  33. install(${ARGV})
  34. endif()
  35. endfunction()
  36. if(ANDROID)
  37. # Android-NDK CMake files reconfigure the path and so Perl won't be found.
  38. # However, ninja will still find them in $PATH if we just name them.
  39. if(NOT PERL_EXECUTABLE)
  40. set(PERL_EXECUTABLE "perl")
  41. endif()
  42. else()
  43. find_package(Perl REQUIRED)
  44. endif()
  45. if(CMAKE_SYSTEM_NAME STREQUAL "Linux" AND NOT CMAKE_CROSSCOMPILING)
  46. find_package(PkgConfig QUIET)
  47. if (PkgConfig_FOUND)
  48. pkg_check_modules(LIBUNWIND libunwind-generic)
  49. if(LIBUNWIND_FOUND)
  50. add_definitions(-DBORINGSSL_HAVE_LIBUNWIND)
  51. else()
  52. message("libunwind not found. Disabling unwind tests.")
  53. endif()
  54. else()
  55. message("pkgconfig not found. Disabling unwind tests.")
  56. endif()
  57. endif()
  58. if(USE_CUSTOM_LIBCXX)
  59. set(BORINGSSL_ALLOW_CXX_RUNTIME 1)
  60. endif()
  61. if(BORINGSSL_ALLOW_CXX_RUNTIME)
  62. add_definitions(-DBORINGSSL_ALLOW_CXX_RUNTIME)
  63. endif()
  64. string(TOLOWER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE_LOWER)
  65. if(NOT FIPS)
  66. if(CMAKE_BUILD_TYPE_LOWER STREQUAL "relwithassert" OR
  67. NOT CMAKE_BUILD_TYPE_LOWER MATCHES "rel")
  68. add_definitions(-DBORINGSSL_DISPATCH_TEST)
  69. # CMake automatically connects include_directories to the NASM
  70. # command-line, but not add_definitions.
  71. set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -DBORINGSSL_DISPATCH_TEST")
  72. endif()
  73. endif()
  74. # Add a RelWithAsserts build configuration. It is the same as Release, except it
  75. # does not define NDEBUG, so asserts run.
  76. foreach(VAR CMAKE_C_FLAGS CMAKE_CXX_FLAGS CMAKE_ASM_FLAGS)
  77. string(REGEX REPLACE "(^| )[/-]DNDEBUG( |$)" " " "${VAR}_RELWITHASSERTS"
  78. "${${VAR}_RELEASE}")
  79. endforeach()
  80. if(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. # Use "symbol_prefix_include" to store generated header files
  86. include_directories(${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include)
  87. add_custom_command(
  88. OUTPUT symbol_prefix_include/boringssl_prefix_symbols.h
  89. symbol_prefix_include/boringssl_prefix_symbols_asm.h
  90. symbol_prefix_include/boringssl_prefix_symbols_nasm.inc
  91. COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include
  92. COMMAND ${GO_EXECUTABLE} run ${CMAKE_CURRENT_SOURCE_DIR}/util/make_prefix_headers.go -out ${CMAKE_CURRENT_BINARY_DIR}/symbol_prefix_include ${BORINGSSL_PREFIX_SYMBOLS}
  93. DEPENDS util/make_prefix_headers.go
  94. ${BORINGSSL_PREFIX_SYMBOLS})
  95. # add_dependencies needs a target, not a file, so we add an intermediate
  96. # target.
  97. add_custom_target(
  98. boringssl_prefix_symbols
  99. DEPENDS symbol_prefix_include/boringssl_prefix_symbols.h
  100. symbol_prefix_include/boringssl_prefix_symbols_asm.h
  101. symbol_prefix_include/boringssl_prefix_symbols_nasm.inc)
  102. elseif(BORINGSSL_PREFIX OR BORINGSSL_PREFIX_SYMBOLS)
  103. message(FATAL_ERROR "Must specify both or neither of BORINGSSL_PREFIX and BORINGSSL_PREFIX_SYMBOLS")
  104. else()
  105. add_custom_target(boringssl_prefix_symbols)
  106. endif()
  107. if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  108. set(CLANG 1)
  109. endif()
  110. if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
  111. set(EMSCRIPTEN 1)
  112. endif()
  113. set(CMAKE_CXX_STANDARD 14)
  114. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  115. set(CMAKE_C_STANDARD 11)
  116. set(CMAKE_C_STANDARD_REQUIRED ON)
  117. if(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 " -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings -Wvla -Wshadow -Wtype-limits")
  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. else()
  127. if(EMSCRIPTEN)
  128. # emscripten's emcc/clang does not accept the "-ggdb" flag.
  129. set(C_CXX_FLAGS "${C_CXX_FLAGS} -g")
  130. else()
  131. set(C_CXX_FLAGS "${C_CXX_FLAGS} -ggdb")
  132. endif()
  133. set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wall -fvisibility=hidden -fno-common")
  134. endif()
  135. if(CLANG)
  136. set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof -fcolor-diagnostics")
  137. else()
  138. # GCC (at least 4.8.4) has a bug where it'll find unreachable free() calls
  139. # and declare that the code is trying to free a stack pointer.
  140. set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-free-nonheap-object")
  141. endif()
  142. # -Wstring-concatenation was added in Clang 12.0.0, which corresponds to
  143. # AppleClang 13.0.0 per the table in
  144. # https://en.wikipedia.org/wiki/Xcode#Toolchain_versions
  145. if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND
  146. CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "12.0.0") OR
  147. (CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND
  148. CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "13.0.0"))
  149. set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wstring-concatenation")
  150. endif()
  151. if(CLANG OR CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "7.0.0")
  152. set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wimplicit-fallthrough")
  153. endif()
  154. if(CMAKE_COMPILER_IS_GNUCXX)
  155. set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wformat-signedness")
  156. endif()
  157. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
  158. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_CXX_FLAGS} -Wmissing-declarations")
  159. if(NOT MSVC AND NOT BORINGSSL_ALLOW_CXX_RUNTIME)
  160. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
  161. endif()
  162. # In GCC, -Wmissing-declarations is the C++ spelling of -Wmissing-prototypes
  163. # and using the wrong one is an error. In Clang, -Wmissing-prototypes is the
  164. # spelling for both and -Wmissing-declarations is some other warning.
  165. #
  166. # https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Warning-Options.html#Warning-Options
  167. # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-prototypes
  168. # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-declarations
  169. if(CLANG)
  170. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-prototypes")
  171. endif()
  172. elseif(MSVC)
  173. set(MSVC_DISABLED_WARNINGS_LIST
  174. "C4100" # 'exarg' : unreferenced formal parameter
  175. "C4127" # conditional expression is constant
  176. # C4204 and C4221 are C89-only restrictions which were dropped in C99, but
  177. # VS2017 warns about it. They can be removed when we require VS2019.
  178. "C4204" # nonstandard extension used: non-constant aggregate initializer
  179. "C4221" # nonstandard extension used : 'identifier' : cannot be
  180. # initialized using address of automatic variable
  181. "C4244" # 'function' : conversion from 'int' to 'uint8_t',
  182. # possible loss of data
  183. "C4267" # conversion from 'size_t' to 'int', possible loss of data
  184. "C4706" # assignment within conditional expression
  185. )
  186. string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
  187. ${MSVC_DISABLED_WARNINGS_LIST})
  188. set(CMAKE_C_FLAGS "-utf-8 -W4 -WX ${MSVC_DISABLED_WARNINGS_STR}")
  189. set(CMAKE_CXX_FLAGS "-utf-8 -W4 -WX ${MSVC_DISABLED_WARNINGS_STR}")
  190. endif()
  191. if(WIN32)
  192. add_definitions(-D_HAS_EXCEPTIONS=0)
  193. add_definitions(-DWIN32_LEAN_AND_MEAN)
  194. add_definitions(-DNOMINMAX)
  195. # Allow use of fopen.
  196. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  197. endif()
  198. # pthread_rwlock_t on Linux requires a feature flag. We limit this to Linux
  199. # because, on Apple platforms, it instead disables APIs we use. See compat(5)
  200. # and sys/cdefs.h. Reportedly, FreeBSD also breaks when this is set. See
  201. # https://crbug.com/boringssl/471.
  202. if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  203. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
  204. endif()
  205. if(FUZZ)
  206. if(NOT CLANG)
  207. message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
  208. endif()
  209. if(CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0.0")
  210. message(FATAL_ERROR "You need Clang ≥ 6.0.0")
  211. endif()
  212. add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  213. set(RUNNER_ARGS "-deterministic")
  214. if(NOT NO_FUZZER_MODE)
  215. add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
  216. set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
  217. endif()
  218. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
  219. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address,fuzzer-no-link -fsanitize-coverage=edge,indirect-calls")
  220. endif()
  221. add_definitions(-DBORINGSSL_IMPLEMENTATION)
  222. if(BUILD_SHARED_LIBS)
  223. add_definitions(-DBORINGSSL_SHARED_LIBRARY)
  224. # Enable position-independent code globally. This is needed because
  225. # some library targets are OBJECT libraries.
  226. set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
  227. endif()
  228. if(MSAN)
  229. if(NOT CLANG)
  230. message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
  231. endif()
  232. if(ASAN)
  233. message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
  234. endif()
  235. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
  236. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
  237. set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
  238. endif()
  239. if(ASAN)
  240. if(NOT CLANG)
  241. message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
  242. endif()
  243. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
  244. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
  245. endif()
  246. if(CFI)
  247. if(NOT CLANG)
  248. message(FATAL_ERROR "Cannot enable CFI unless using Clang")
  249. endif()
  250. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
  251. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -flto=thin")
  252. # We use Chromium's copy of clang, which requires -fuse-ld=lld if building
  253. # with -flto. That, in turn, can't handle -ggdb.
  254. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
  255. string(REPLACE "-ggdb" "-g" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  256. string(REPLACE "-ggdb" "-g" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  257. # -flto causes object files to contain LLVM bitcode. Mixing those with
  258. # assembly output in the same static library breaks the linker.
  259. set(OPENSSL_NO_ASM "1")
  260. endif()
  261. if(TSAN)
  262. if(NOT CLANG)
  263. message(FATAL_ERROR "Cannot enable TSAN unless using Clang")
  264. endif()
  265. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
  266. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
  267. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
  268. endif()
  269. if(UBSAN)
  270. if(NOT CLANG)
  271. message(FATAL_ERROR "Cannot enable UBSAN unless using Clang")
  272. endif()
  273. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined")
  274. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined")
  275. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=undefined")
  276. if(NOT UBSAN_RECOVER)
  277. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize-recover=undefined")
  278. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-sanitize-recover=undefined")
  279. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-sanitize-recover=undefined")
  280. endif()
  281. endif()
  282. if(GCOV)
  283. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
  284. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
  285. endif()
  286. if(FIPS)
  287. add_definitions(-DBORINGSSL_FIPS)
  288. if(FIPS_BREAK_TEST)
  289. add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
  290. endif()
  291. # The FIPS integrity check does not work for ASan and MSan builds.
  292. if(NOT ASAN AND NOT MSAN)
  293. if(BUILD_SHARED_LIBS)
  294. set(FIPS_SHARED "1")
  295. else()
  296. set(FIPS_DELOCATE "1")
  297. endif()
  298. endif()
  299. if(FIPS_SHARED)
  300. # The Android CMake files set -ffunction-sections and -fdata-sections,
  301. # which is incompatible with FIPS_SHARED.
  302. set(CMAKE_C_FLAGS
  303. "${CMAKE_C_FLAGS} -fno-function-sections -fno-data-sections")
  304. set(CMAKE_CXX_FLAGS
  305. "${CMAKE_CXX_FLAGS} -fno-function-sections -fno-data-sections")
  306. endif()
  307. endif()
  308. if(OPENSSL_SMALL)
  309. add_definitions(-DOPENSSL_SMALL)
  310. endif()
  311. if(CONSTANT_TIME_VALIDATION)
  312. add_definitions(-DBORINGSSL_CONSTANT_TIME_VALIDATION)
  313. # Asserts will often test secret data.
  314. add_definitions(-DNDEBUG)
  315. endif()
  316. if(MALLOC_FAILURE_TESTING)
  317. add_definitions(-DBORINGSSL_MALLOC_FAILURE_TESTING)
  318. endif()
  319. if(OPENSSL_NO_ASM)
  320. add_definitions(-DOPENSSL_NO_ASM)
  321. endif()
  322. if(FIPS_DELOCATE OR NOT OPENSSL_NO_ASM)
  323. # On x86 and x86_64 Windows, we use the NASM output.
  324. if(WIN32 AND CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64|x86_64|amd64|x86|i[3-6]86")
  325. enable_language(ASM_NASM)
  326. set(OPENSSL_NASM TRUE)
  327. set(CMAKE_ASM_NASM_FLAGS "${CMAKE_ASM_NASM_FLAGS} -gcv8")
  328. else()
  329. enable_language(ASM)
  330. set(OPENSSL_ASM TRUE)
  331. # Work around https://gitlab.kitware.com/cmake/cmake/-/issues/20771 in older
  332. # CMake versions.
  333. if(APPLE AND CMAKE_VERSION VERSION_LESS 3.19)
  334. if(CMAKE_OSX_SYSROOT)
  335. set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -isysroot \"${CMAKE_OSX_SYSROOT}\"")
  336. endif()
  337. foreach(arch ${CMAKE_OSX_ARCHITECTURES})
  338. set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -arch ${arch}")
  339. endforeach()
  340. endif()
  341. if(NOT WIN32)
  342. set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,--noexecstack")
  343. endif()
  344. # Clang's integerated assembler does not support debug symbols.
  345. if(NOT CMAKE_ASM_COMPILER_ID MATCHES "Clang")
  346. set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -Wa,-g")
  347. endif()
  348. endif()
  349. endif()
  350. if(OPENSSL_NO_SSE2_FOR_TESTING)
  351. add_definitions(-DOPENSSL_NO_SSE2_FOR_TESTING)
  352. endif()
  353. if(USE_CUSTOM_LIBCXX)
  354. if(NOT CLANG)
  355. message(FATAL_ERROR "USE_CUSTOM_LIBCXX only supported with Clang")
  356. endif()
  357. # CMake does not allow installing a library without installing dependencies.
  358. # If we installed libcrypto, we'd have to install our custom libc++, which
  359. # does not make sense. As this is a test-only configuration, disable
  360. # installing.
  361. set(INSTALL_ENABLED 0)
  362. # CMAKE_CXX_FLAGS ends up in the linker flags as well, so use
  363. # add_compile_options. There does not appear to be a way to set
  364. # language-specific compile-only flags.
  365. add_compile_options("-nostdinc++")
  366. set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -nostdlib++")
  367. include_directories(
  368. SYSTEM
  369. util/bot/libcxx-config
  370. util/bot/libcxx/include
  371. util/bot/libcxxabi/include
  372. )
  373. # This is patterned after buildtools/third_party/libc++/BUILD.gn and
  374. # buildtools/third_party/libc++abi/BUILD.gn in Chromium.
  375. file(GLOB LIBCXX_SOURCES "util/bot/libcxx/src/*.cpp")
  376. file(GLOB LIBCXXABI_SOURCES "util/bot/libcxxabi/src/*.cpp")
  377. # This file is meant for exception-less builds.
  378. list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_noexception.cpp")
  379. # libc++ also defines new and delete.
  380. list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/stdlib_new_delete.cpp")
  381. if(TSAN)
  382. # ThreadSanitizer tries to intercept these symbols. Skip them to avoid
  383. # symbol conflicts.
  384. list(REMOVE_ITEM LIBCXXABI_SOURCES "trunk/src/cxa_guard.cpp")
  385. endif()
  386. add_library(libcxxabi ${LIBCXXABI_SOURCES})
  387. target_compile_definitions(
  388. libcxxabi PRIVATE
  389. -D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS
  390. )
  391. add_library(libcxx ${LIBCXX_SOURCES})
  392. if(ASAN OR MSAN OR TSAN)
  393. # Sanitizers try to intercept new and delete.
  394. target_compile_definitions(
  395. libcxx PRIVATE
  396. -D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS
  397. )
  398. endif()
  399. target_compile_definitions(
  400. libcxx PRIVATE
  401. -D_LIBCPP_BUILDING_LIBRARY
  402. -DLIBCXX_BUILDING_LIBCXXABI
  403. )
  404. set_target_properties(
  405. libcxx libcxxabi PROPERTIES
  406. COMPILE_FLAGS "-Wno-missing-prototypes -Wno-implicit-fallthrough"
  407. # libc++ and libc++abi must be built in C++20 mode.
  408. CXX_STANDARD 20
  409. CXX_STANDARD_REQUIRED TRUE
  410. )
  411. # libc++abi depends on libc++ internal headers.
  412. set_property(TARGET libcxx libcxxabi APPEND PROPERTY INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}/util/bot/libcxx/src")
  413. target_link_libraries(libcxx libcxxabi)
  414. endif()
  415. # Add minimal googletest targets. The provided one has many side-effects, and
  416. # googletest has a very straightforward build.
  417. add_library(boringssl_gtest third_party/googletest/src/gtest-all.cc)
  418. if(USE_CUSTOM_LIBCXX)
  419. target_link_libraries(boringssl_gtest libcxx)
  420. endif()
  421. target_include_directories(
  422. boringssl_gtest
  423. PUBLIC third_party/googletest/include
  424. PRIVATE third_party/googletest
  425. )
  426. # Declare a dummy target to build all unit tests. Test targets should inject
  427. # themselves as dependencies next to the target definition.
  428. add_custom_target(all_tests)
  429. # On Windows, CRYPTO_TEST_DATA is too long to fit in command-line limits.
  430. # TODO(davidben): CMake 3.12 has a list(JOIN) command. Use that when we've
  431. # updated the minimum version.
  432. set(EMBED_TEST_DATA_ARGS "")
  433. foreach(arg ${CRYPTO_TEST_DATA})
  434. set(EMBED_TEST_DATA_ARGS "${EMBED_TEST_DATA_ARGS}${arg}\n")
  435. endforeach()
  436. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/embed_test_data_args.txt"
  437. "${EMBED_TEST_DATA_ARGS}")
  438. add_custom_command(
  439. OUTPUT crypto_test_data.cc
  440. COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go -file-list
  441. "${CMAKE_CURRENT_BINARY_DIR}/embed_test_data_args.txt" >
  442. "${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc"
  443. DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
  444. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  445. add_library(crypto_test_data OBJECT crypto_test_data.cc)
  446. add_subdirectory(crypto)
  447. add_subdirectory(ssl)
  448. add_subdirectory(ssl/test)
  449. add_subdirectory(tool)
  450. add_subdirectory(util/fipstools)
  451. add_subdirectory(util/fipstools/acvp/modulewrapper)
  452. add_subdirectory(decrepit)
  453. if(FUZZ)
  454. if(LIBFUZZER_FROM_DEPS)
  455. file(GLOB LIBFUZZER_SOURCES "util/bot/libFuzzer/*.cpp")
  456. add_library(Fuzzer STATIC ${LIBFUZZER_SOURCES})
  457. # libFuzzer does not pass our aggressive warnings. It also must be built
  458. # without -fsanitize-coverage options or clang crashes.
  459. set_target_properties(Fuzzer PROPERTIES COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -Wno-missing-prototypes -fsanitize-coverage=0")
  460. endif()
  461. add_subdirectory(fuzz)
  462. endif()
  463. if(RUST_BINDINGS)
  464. find_program(BINDGEN_EXECUTABLE bindgen)
  465. if(NOT BINDGEN_EXECUTABLE)
  466. message(FATAL_ERROR "Could not find bindgen but was asked to generate Rust bindings.")
  467. else()
  468. add_subdirectory(rust)
  469. endif()
  470. endif()
  471. if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
  472. set(HANDSHAKER_ARGS "-handshaker-path" $<TARGET_FILE:handshaker>)
  473. endif()
  474. if(FIPS)
  475. add_custom_target(
  476. acvp_tests
  477. COMMAND ${GO_EXECUTABLE} build -o ${CMAKE_CURRENT_BINARY_DIR}/acvptool
  478. boringssl.googlesource.com/boringssl/util/fipstools/acvp/acvptool
  479. COMMAND ${GO_EXECUTABLE} build -o ${CMAKE_CURRENT_BINARY_DIR}/testmodulewrapper
  480. boringssl.googlesource.com/boringssl/util/fipstools/acvp/acvptool/testmodulewrapper
  481. COMMAND cd util/fipstools/acvp/acvptool/test &&
  482. ${GO_EXECUTABLE} run check_expected.go
  483. -tool ${CMAKE_CURRENT_BINARY_DIR}/acvptool
  484. -module-wrappers modulewrapper:$<TARGET_FILE:modulewrapper>,testmodulewrapper:${CMAKE_CURRENT_BINARY_DIR}/testmodulewrapper
  485. -tests tests.json
  486. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  487. DEPENDS modulewrapper
  488. USES_TERMINAL)
  489. add_custom_target(
  490. fips_specific_tests_if_any
  491. DEPENDS acvp_tests
  492. )
  493. else()
  494. add_custom_target(fips_specific_tests_if_any)
  495. endif()
  496. # Read util/go_tests.txt into a CMake variable.
  497. file(READ util/go_tests.txt GO_TESTS)
  498. string(REPLACE "\n" ";" GO_TESTS "${GO_TESTS}")
  499. list(REMOVE_ITEM GO_TESTS "")
  500. set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
  501. util/go_tests.txt)
  502. add_custom_target(
  503. run_tests
  504. COMMAND ${GO_EXECUTABLE} test ${GO_TESTS}
  505. COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
  506. ${CMAKE_CURRENT_BINARY_DIR}
  507. COMMAND cd ssl/test/runner &&
  508. ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
  509. ${HANDSHAKER_ARGS} ${RUNNER_ARGS}
  510. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  511. DEPENDS all_tests bssl_shim handshaker fips_specific_tests_if_any
  512. USES_TERMINAL)
  513. install_if_enabled(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
  514. install_if_enabled(EXPORT OpenSSLTargets
  515. FILE OpenSSLTargets.cmake
  516. NAMESPACE OpenSSL::
  517. DESTINATION lib/cmake/OpenSSL
  518. )
  519. install_if_enabled(FILES cmake/OpenSSLConfig.cmake DESTINATION lib/cmake/OpenSSL)