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.
 
 
 
 
 
 

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