You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

419 lines
16 KiB

  1. cmake_minimum_required (VERSION 2.8.11)
  2. # Report AppleClang separately from Clang. Their version numbers are different.
  3. # https://cmake.org/cmake/help/v3.0/policy/CMP0025.html
  4. if(POLICY CMP0025)
  5. cmake_policy(SET CMP0025 NEW)
  6. endif()
  7. # Defer enabling C and CXX languages.
  8. project (BoringSSL NONE)
  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. enable_language(C)
  16. enable_language(CXX)
  17. if(ANDROID)
  18. # Android-NDK CMake files reconfigure the path and so Go and Perl won't be
  19. # found. However, ninja will still find them in $PATH if we just name them.
  20. if(NOT PERL_EXECUTABLE)
  21. set(PERL_EXECUTABLE "perl")
  22. endif()
  23. if(NOT GO_EXECUTABLE)
  24. set(GO_EXECUTABLE "go")
  25. endif()
  26. else()
  27. find_package(Perl REQUIRED)
  28. find_program(GO_EXECUTABLE go)
  29. endif()
  30. if (NOT GO_EXECUTABLE)
  31. message(FATAL_ERROR "Could not find Go")
  32. endif()
  33. if (BORINGSSL_ALLOW_CXX_RUNTIME)
  34. add_definitions(-DBORINGSSL_ALLOW_CXX_RUNTIME)
  35. endif()
  36. if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  37. set(CLANG 1)
  38. endif()
  39. if(CMAKE_COMPILER_IS_GNUCXX OR CLANG)
  40. # Note clang-cl is odd and sets both CLANG and MSVC. We base our configuration
  41. # primarily on our normal Clang one.
  42. set(C_CXX_FLAGS "-Werror -Wformat=2 -Wsign-compare -Wmissing-field-initializers -Wwrite-strings")
  43. if(MSVC)
  44. # clang-cl sets different default warnings than clang. It also treats -Wall
  45. # as -Weverything, to match MSVC. Instead -W3 is the alias for -Wall.
  46. # See http://llvm.org/viewvc/llvm-project?view=revision&revision=319116
  47. set(C_CXX_FLAGS "${C_CXX_FLAGS} -W3 -Wno-unused-parameter -fmsc-version=1900")
  48. # googletest suppresses warning C4996 via a pragma, but clang-cl does not
  49. # honor it. Suppress it here to compensate. See https://crbug.com/772117.
  50. set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-deprecated-declarations")
  51. else()
  52. set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wall -ggdb -fvisibility=hidden -fno-common")
  53. endif()
  54. if(CLANG)
  55. set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wnewline-eof -fcolor-diagnostics")
  56. else()
  57. # GCC (at least 4.8.4) has a bug where it'll find unreachable free() calls
  58. # and declare that the code is trying to free a stack pointer.
  59. set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wno-free-nonheap-object")
  60. endif()
  61. if(CLANG OR NOT "7.0.0" VERSION_GREATER CMAKE_C_COMPILER_VERSION)
  62. set(C_CXX_FLAGS "${C_CXX_FLAGS} -Wimplicit-fallthrough")
  63. endif()
  64. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${C_CXX_FLAGS} -Wmissing-prototypes -Wold-style-definition -Wstrict-prototypes")
  65. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${C_CXX_FLAGS} -Wmissing-declarations")
  66. if(NOT MSVC)
  67. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  68. if(APPLE)
  69. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
  70. endif()
  71. if(NOT BORINGSSL_ALLOW_CXX_RUNTIME)
  72. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")
  73. endif()
  74. endif()
  75. # In GCC, -Wmissing-declarations is the C++ spelling of -Wmissing-prototypes
  76. # and using the wrong one is an error. In Clang, -Wmissing-prototypes is the
  77. # spelling for both and -Wmissing-declarations is some other warning.
  78. #
  79. # https://gcc.gnu.org/onlinedocs/gcc-7.1.0/gcc/Warning-Options.html#Warning-Options
  80. # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-prototypes
  81. # https://clang.llvm.org/docs/DiagnosticsReference.html#wmissing-declarations
  82. if(CLANG)
  83. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-prototypes")
  84. endif()
  85. if(CMAKE_COMPILER_IS_GNUCXX AND "4.8" VERSION_GREATER CMAKE_C_COMPILER_VERSION)
  86. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-array-bounds")
  87. endif()
  88. elseif(MSVC)
  89. set(MSVC_DISABLED_WARNINGS_LIST
  90. "C4061" # enumerator 'identifier' in switch of enum 'enumeration' is not
  91. # explicitly handled by a case label
  92. # Disable this because it flags even when there is a default.
  93. "C4100" # 'exarg' : unreferenced formal parameter
  94. "C4127" # conditional expression is constant
  95. "C4200" # nonstandard extension used : zero-sized array in
  96. # struct/union.
  97. "C4204" # nonstandard extension used: non-constant aggregate initializer
  98. "C4221" # nonstandard extension used : 'identifier' : cannot be
  99. # initialized using address of automatic variable
  100. "C4242" # 'function' : conversion from 'int' to 'uint8_t',
  101. # possible loss of data
  102. "C4244" # 'function' : conversion from 'int' to 'uint8_t',
  103. # possible loss of data
  104. "C4267" # conversion from 'size_t' to 'int', possible loss of data
  105. "C4371" # layout of class may have changed from a previous version of the
  106. # compiler due to better packing of member '...'
  107. "C4388" # signed/unsigned mismatch
  108. "C4296" # '>=' : expression is always true
  109. "C4350" # behavior change: 'std::_Wrap_alloc...'
  110. "C4365" # '=' : conversion from 'size_t' to 'int',
  111. # signed/unsigned mismatch
  112. "C4389" # '!=' : signed/unsigned mismatch
  113. "C4464" # relative include path contains '..'
  114. "C4510" # 'argument' : default constructor could not be generated
  115. "C4512" # 'argument' : assignment operator could not be generated
  116. "C4514" # 'function': unreferenced inline function has been removed
  117. "C4548" # expression before comma has no effect; expected expression with
  118. # side-effect" caused by FD_* macros.
  119. "C4610" # struct 'argument' can never be instantiated - user defined
  120. # constructor required.
  121. "C4623" # default constructor was implicitly defined as deleted
  122. "C4625" # copy constructor could not be generated because a base class
  123. # copy constructor is inaccessible or deleted
  124. "C4626" # assignment operator could not be generated because a base class
  125. # assignment operator is inaccessible or deleted
  126. "C4668" # 'symbol' is not defined as a preprocessor macro, replacing with
  127. # '0' for 'directives'
  128. # Disable this because GTest uses it everywhere.
  129. "C4706" # assignment within conditional expression
  130. "C4710" # 'function': function not inlined
  131. "C4711" # function 'function' selected for inline expansion
  132. "C4800" # 'int' : forcing value to bool 'true' or 'false'
  133. # (performance warning)
  134. "C4820" # 'bytes' bytes padding added after construct 'member_name'
  135. "C5026" # move constructor was implicitly defined as deleted
  136. "C5027" # move assignment operator was implicitly defined as deleted
  137. "C5045" # Compiler will insert Spectre mitigation for memory load if
  138. # /Qspectre switch specified
  139. )
  140. set(MSVC_LEVEL4_WARNINGS_LIST
  141. # See https://connect.microsoft.com/VisualStudio/feedback/details/1217660/warning-c4265-when-using-functional-header
  142. "C4265" # class has virtual functions, but destructor is not virtual
  143. )
  144. string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR
  145. ${MSVC_DISABLED_WARNINGS_LIST})
  146. string(REPLACE "C" " -w4" MSVC_LEVEL4_WARNINGS_STR
  147. ${MSVC_LEVEL4_WARNINGS_LIST})
  148. set(CMAKE_C_FLAGS "-utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
  149. set(CMAKE_CXX_FLAGS "-utf-8 -Wall -WX ${MSVC_DISABLED_WARNINGS_STR} ${MSVC_LEVEL4_WARNINGS_STR}")
  150. endif()
  151. if(WIN32)
  152. add_definitions(-D_HAS_EXCEPTIONS=0)
  153. add_definitions(-DWIN32_LEAN_AND_MEAN)
  154. add_definitions(-DNOMINMAX)
  155. # Allow use of fopen.
  156. add_definitions(-D_CRT_SECURE_NO_WARNINGS)
  157. # VS 2017 and higher supports STL-only warning suppressions. Manually add to
  158. # C++ only to work around a CMake quoting bug when using NASM with the Visual
  159. # Studio generator. This will be fixed in CMake 3.13.0. See
  160. # https://gitlab.kitware.com/cmake/cmake/merge_requests/2179
  161. add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-D_STL_EXTRA_DISABLED_WARNINGS=4774\ 4987>)
  162. endif()
  163. if((CMAKE_COMPILER_IS_GNUCXX AND CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.7.99") OR
  164. CLANG)
  165. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
  166. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wshadow")
  167. endif()
  168. if(CMAKE_COMPILER_IS_GNUCXX)
  169. if ((CMAKE_C_COMPILER_VERSION VERSION_GREATER "4.8.99") OR CLANG)
  170. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
  171. else()
  172. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99")
  173. endif()
  174. endif()
  175. # pthread_rwlock_t requires a feature flag.
  176. if(NOT WIN32)
  177. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_XOPEN_SOURCE=700")
  178. endif()
  179. if(FUZZ)
  180. if(NOT CLANG)
  181. message(FATAL_ERROR "You need to build with Clang for fuzzing to work")
  182. endif()
  183. add_definitions(-DBORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  184. set(RUNNER_ARGS "-deterministic")
  185. if(NOT NO_FUZZER_MODE)
  186. add_definitions(-DBORINGSSL_UNSAFE_FUZZER_MODE)
  187. set(RUNNER_ARGS ${RUNNER_ARGS} "-fuzzer" "-shim-config" "fuzzer_mode.json")
  188. endif()
  189. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,trace-pc-guard")
  190. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-coverage=edge,indirect-calls,trace-pc-guard")
  191. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address")
  192. link_directories(.)
  193. endif()
  194. add_definitions(-DBORINGSSL_IMPLEMENTATION)
  195. if (BUILD_SHARED_LIBS)
  196. add_definitions(-DBORINGSSL_SHARED_LIBRARY)
  197. # Enable position-independent code globally. This is needed because
  198. # some library targets are OBJECT libraries.
  199. set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
  200. endif()
  201. if (MSAN)
  202. if(NOT CLANG)
  203. message(FATAL_ERROR "Cannot enable MSAN unless using Clang")
  204. endif()
  205. if (ASAN)
  206. message(FATAL_ERROR "ASAN and MSAN are mutually exclusive")
  207. endif()
  208. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
  209. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer")
  210. set(OPENSSL_NO_ASM "1")
  211. endif()
  212. if (ASAN)
  213. if(NOT CLANG)
  214. message(FATAL_ERROR "Cannot enable ASAN unless using Clang")
  215. endif()
  216. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
  217. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fsanitize-address-use-after-scope -fno-omit-frame-pointer")
  218. set(OPENSSL_NO_ASM "1")
  219. endif()
  220. if(CFI)
  221. if(NOT CLANG)
  222. message(FATAL_ERROR "Cannot enable CFI unless using Clang")
  223. endif()
  224. # TODO(crbug.com/785442): Remove -fsanitize-cfi-icall-generalize-pointers.
  225. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -fsanitize-cfi-icall-generalize-pointers -flto")
  226. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=cfi -fno-sanitize-trap=cfi -fsanitize-cfi-icall-generalize-pointers -flto")
  227. # We use Chromium's copy of clang, which requires -fuse-ld=lld if building
  228. # with -flto. That, in turn, can't handle -ggdb.
  229. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
  230. string(REPLACE "-ggdb" "-g" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  231. string(REPLACE "-ggdb" "-g" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  232. # -flto causes object files to contain LLVM bitcode. Mixing those with
  233. # assembly output in the same static library breaks the linker.
  234. set(OPENSSL_NO_ASM "1")
  235. endif()
  236. if(TSAN)
  237. if(NOT CLANG)
  238. message(FATAL_ERROR "Cannot enable TSAN unless using Clang")
  239. endif()
  240. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
  241. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
  242. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=thread")
  243. endif()
  244. if (GCOV)
  245. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
  246. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
  247. endif()
  248. if(FIPS)
  249. add_definitions(-DBORINGSSL_FIPS)
  250. if(FIPS_BREAK_TEST)
  251. add_definitions("-DBORINGSSL_FIPS_BREAK_${FIPS_BREAK_TEST}=1")
  252. endif()
  253. # Delocate does not work for ASan and MSan builds.
  254. if(NOT ASAN AND NOT MSAN)
  255. set(FIPS_DELOCATE "1")
  256. endif()
  257. endif()
  258. if(OPENSSL_SMALL)
  259. add_definitions(-DOPENSSL_SMALL)
  260. endif()
  261. # CMake's iOS support uses Apple's multiple-architecture toolchain. It takes an
  262. # architecture list from CMAKE_OSX_ARCHITECTURES, leaves CMAKE_SYSTEM_PROCESSOR
  263. # alone, and expects all architecture-specific logic to be conditioned within
  264. # the source files rather than the build. This does not work for our assembly
  265. # files, so we fix CMAKE_SYSTEM_PROCESSOR and only support single-architecture
  266. # builds.
  267. if (NOT OPENSSL_NO_ASM AND CMAKE_OSX_ARCHITECTURES)
  268. list(LENGTH CMAKE_OSX_ARCHITECTURES NUM_ARCHES)
  269. if (NOT ${NUM_ARCHES} EQUAL 1)
  270. message(FATAL_ERROR "Universal binaries not supported.")
  271. endif()
  272. list(GET CMAKE_OSX_ARCHITECTURES 0 CMAKE_SYSTEM_PROCESSOR)
  273. endif()
  274. if (OPENSSL_NO_ASM)
  275. add_definitions(-DOPENSSL_NO_ASM)
  276. set(ARCH "generic")
  277. elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64")
  278. set(ARCH "x86_64")
  279. elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "amd64")
  280. set(ARCH "x86_64")
  281. elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64")
  282. # cmake reports AMD64 on Windows, but we might be building for 32-bit.
  283. if (CMAKE_CL_64)
  284. set(ARCH "x86_64")
  285. else()
  286. set(ARCH "x86")
  287. endif()
  288. elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86")
  289. set(ARCH "x86")
  290. elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i386")
  291. set(ARCH "x86")
  292. elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "i686")
  293. set(ARCH "x86")
  294. elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
  295. set(ARCH "aarch64")
  296. elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
  297. set(ARCH "aarch64")
  298. elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^arm*")
  299. set(ARCH "arm")
  300. elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "mips")
  301. # Just to avoid the “unknown processor” error.
  302. set(ARCH "generic")
  303. elseif (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "ppc64le")
  304. set(ARCH "ppc64le")
  305. else()
  306. message(FATAL_ERROR "Unknown processor:" ${CMAKE_SYSTEM_PROCESSOR})
  307. endif()
  308. if (ANDROID AND NOT ANDROID_NDK_REVISION AND ${ARCH} STREQUAL "arm")
  309. # The third-party Android-NDK CMake files somehow fail to set the -march flag
  310. # for assembly files. Without this flag, the compiler believes that it's
  311. # building for ARMv5.
  312. set(CMAKE_ASM_FLAGS "-march=${CMAKE_SYSTEM_PROCESSOR} ${CMAKE_ASM_FLAGS}")
  313. endif()
  314. if (${ARCH} STREQUAL "x86" AND APPLE AND ${CMAKE_VERSION} VERSION_LESS "3.0")
  315. # With CMake 2.8.x, ${CMAKE_SYSTEM_PROCESSOR} evalutes to i386 on OS X,
  316. # but clang defaults to 64-bit builds on OS X unless otherwise told.
  317. # Set ARCH to x86_64 so clang and CMake agree. This is fixed in CMake 3.
  318. set(ARCH "x86_64")
  319. endif()
  320. # Add minimal googletest targets. The provided one has many side-effects, and
  321. # googletest has a very straightforward build.
  322. add_library(boringssl_gtest third_party/googletest/src/gtest-all.cc)
  323. target_include_directories(boringssl_gtest PRIVATE third_party/googletest)
  324. include_directories(third_party/googletest/include)
  325. # Declare a dummy target to build all unit tests. Test targets should inject
  326. # themselves as dependencies next to the target definition.
  327. add_custom_target(all_tests)
  328. add_custom_command(
  329. OUTPUT crypto_test_data.cc
  330. COMMAND ${GO_EXECUTABLE} run util/embed_test_data.go ${CRYPTO_TEST_DATA} >
  331. ${CMAKE_CURRENT_BINARY_DIR}/crypto_test_data.cc
  332. DEPENDS util/embed_test_data.go ${CRYPTO_TEST_DATA}
  333. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  334. add_library(crypto_test_data OBJECT crypto_test_data.cc)
  335. add_subdirectory(crypto)
  336. add_subdirectory(third_party/fiat)
  337. add_subdirectory(ssl)
  338. add_subdirectory(ssl/test)
  339. add_subdirectory(fipstools)
  340. add_subdirectory(tool)
  341. add_subdirectory(decrepit)
  342. if(FUZZ)
  343. if(LIBFUZZER_FROM_DEPS)
  344. file(GLOB LIBFUZZER_SOURCES "util/bot/libFuzzer/*.cpp")
  345. add_library(Fuzzer STATIC ${LIBFUZZER_SOURCES})
  346. # libFuzzer does not pass our aggressive warnings. It also must be built
  347. # without -fsanitize-coverage options or clang crashes.
  348. set_target_properties(Fuzzer PROPERTIES COMPILE_FLAGS "-Wno-shadow -Wno-format-nonliteral -Wno-missing-prototypes -fsanitize-coverage=0")
  349. endif()
  350. add_subdirectory(fuzz)
  351. endif()
  352. if (NOT ${CMAKE_VERSION} VERSION_LESS "3.2")
  353. # USES_TERMINAL is only available in CMake 3.2 or later.
  354. set(MAYBE_USES_TERMINAL USES_TERMINAL)
  355. endif()
  356. add_custom_target(
  357. run_tests
  358. COMMAND ${GO_EXECUTABLE} run util/all_tests.go -build-dir
  359. ${CMAKE_BINARY_DIR}
  360. COMMAND cd ssl/test/runner &&
  361. ${GO_EXECUTABLE} test -shim-path $<TARGET_FILE:bssl_shim>
  362. ${RUNNER_ARGS}
  363. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  364. DEPENDS all_tests bssl_shim
  365. ${MAYBE_USES_TERMINAL})