CMakeLists.txt 23 KB

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