Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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