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.
 
 
 
 
 
 

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