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.
 
 
 
 
 
 

371 line
14 KiB

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