CMakeLists.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #
  2. # CMake build system design considerations:
  3. #
  4. # - Include directories:
  5. # + Do not define include directories globally using the include_directories
  6. # command but rather at the target level using the
  7. # target_include_directories command. That way, it is easier to guarantee
  8. # that targets are built using the proper list of include directories.
  9. # + Use the PUBLIC and PRIVATE keywords to specify the scope of include
  10. # directories. That way, a target linking to a library (using the
  11. # target_link_libraries command) inherits from the library PUBLIC include
  12. # directories and not from the PRIVATE ones.
  13. # + Note: there is currently one remaining include_directories command in the
  14. # CMake files. It is related to ZLIB support which is planned to be removed.
  15. # When the support is removed, the associated include_directories command
  16. # will be removed as well as this note.
  17. # - MBEDTLS_TARGET_PREFIX: CMake targets are designed to be alterable by calling
  18. # CMake in order to avoid target name clashes, via the use of
  19. # MBEDTLS_TARGET_PREFIX. The value of this variable is prefixed to the
  20. # mbedtls, mbedx509, mbedcrypto and apidoc targets.
  21. #
  22. cmake_minimum_required(VERSION 2.8.12)
  23. # https://cmake.org/cmake/help/latest/policy/CMP0011.html
  24. # Setting this policy is required in CMake >= 3.18.0, otherwise a warning is generated. The OLD
  25. # policy setting is deprecated, and will be removed in future versions.
  26. cmake_policy(SET CMP0011 NEW)
  27. # https://cmake.org/cmake/help/latest/policy/CMP0012.html
  28. # Setting the CMP0012 policy to NEW is required for FindPython3 to work with CMake 3.18.2
  29. # (there is a bug in this particular version), otherwise, setting the CMP0012 policy is required
  30. # for CMake versions >= 3.18.3 otherwise a deprecated warning is generated. The OLD policy setting
  31. # is deprecated and will be removed in future versions.
  32. cmake_policy(SET CMP0012 NEW)
  33. if(TEST_CPP)
  34. project("mbed TLS" C CXX)
  35. else()
  36. project("mbed TLS" C)
  37. endif()
  38. # Set the project root directory.
  39. set(MBEDTLS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  40. option(USE_PKCS11_HELPER_LIBRARY "Build mbed TLS with the pkcs11-helper library." OFF)
  41. option(ENABLE_ZLIB_SUPPORT "Build mbed TLS with zlib library." OFF)
  42. option(ENABLE_PROGRAMS "Build mbed TLS programs." ON)
  43. option(UNSAFE_BUILD "Allow unsafe builds. These builds ARE NOT SECURE." OFF)
  44. option(MBEDTLS_FATAL_WARNINGS "Compiler warnings treated as errors" ON)
  45. string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")
  46. string(REGEX MATCH "GNU" CMAKE_COMPILER_IS_GNU "${CMAKE_C_COMPILER_ID}")
  47. string(REGEX MATCH "IAR" CMAKE_COMPILER_IS_IAR "${CMAKE_C_COMPILER_ID}")
  48. string(REGEX MATCH "MSVC" CMAKE_COMPILER_IS_MSVC "${CMAKE_C_COMPILER_ID}")
  49. # the test suites currently have compile errors with MSVC
  50. if(CMAKE_COMPILER_IS_MSVC)
  51. option(ENABLE_TESTING "Build mbed TLS tests." OFF)
  52. else()
  53. option(ENABLE_TESTING "Build mbed TLS tests." ON)
  54. endif()
  55. set(ENABLE_PROGRAMS OFF)
  56. set(ENABLE_TESTING OFF)
  57. set(MBEDTLS_FATAL_WARNINGS OFF)
  58. # Warning string - created as a list for compatibility with CMake 2.8
  59. set(WARNING_BORDER "*******************************************************\n")
  60. set(NULL_ENTROPY_WARN_L1 "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined!\n")
  61. set(NULL_ENTROPY_WARN_L2 "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES\n")
  62. set(NULL_ENTROPY_WARN_L3 "**** AND IS *NOT* SUITABLE FOR PRODUCTION USE\n")
  63. set(NULL_ENTROPY_WARNING "${WARNING_BORDER}"
  64. "${NULL_ENTROPY_WARN_L1}"
  65. "${NULL_ENTROPY_WARN_L2}"
  66. "${NULL_ENTROPY_WARN_L3}"
  67. "${WARNING_BORDER}")
  68. set(CTR_DRBG_128_BIT_KEY_WARN_L1 "**** WARNING! MBEDTLS_CTR_DRBG_USE_128_BIT_KEY defined!\n")
  69. set(CTR_DRBG_128_BIT_KEY_WARN_L2 "**** Using 128-bit keys for CTR_DRBG limits the security of generated\n")
  70. set(CTR_DRBG_128_BIT_KEY_WARN_L3 "**** keys and operations that use random values generated to 128-bit security\n")
  71. set(CTR_DRBG_128_BIT_KEY_WARNING "${WARNING_BORDER}"
  72. "${CTR_DRBG_128_BIT_KEY_WARN_L1}"
  73. "${CTR_DRBG_128_BIT_KEY_WARN_L2}"
  74. "${CTR_DRBG_128_BIT_KEY_WARN_L3}"
  75. "${WARNING_BORDER}")
  76. # Python 3 is only needed here to check for configuration warnings.
  77. if(NOT CMAKE_VERSION VERSION_LESS 3.15.0)
  78. set(Python3_FIND_STRATEGY LOCATION)
  79. find_package(Python3 COMPONENTS Interpreter)
  80. if(Python3_Interpreter_FOUND)
  81. set(MBEDTLS_PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
  82. endif()
  83. else()
  84. find_package(PythonInterp 3)
  85. if(PYTHONINTERP_FOUND)
  86. set(MBEDTLS_PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
  87. endif()
  88. endif()
  89. if(MBEDTLS_PYTHON_EXECUTABLE)
  90. # If 128-bit keys are configured for CTR_DRBG, display an appropriate warning
  91. execute_process(COMMAND ${MBEDTLS_PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/config.py -f ${CMAKE_CURRENT_SOURCE_DIR}/include/mbedtls/config.h get MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
  92. RESULT_VARIABLE result)
  93. if(${result} EQUAL 0)
  94. message(WARNING ${CTR_DRBG_128_BIT_KEY_WARNING})
  95. endif()
  96. # If NULL Entropy is configured, display an appropriate warning
  97. execute_process(COMMAND ${MBEDTLS_PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/config.py -f ${CMAKE_CURRENT_SOURCE_DIR}/include/mbedtls/config.h get MBEDTLS_TEST_NULL_ENTROPY
  98. RESULT_VARIABLE result)
  99. if(${result} EQUAL 0)
  100. message(WARNING ${NULL_ENTROPY_WARNING})
  101. if(NOT UNSAFE_BUILD)
  102. message(FATAL_ERROR "\
  103. \n\
  104. Warning! You have enabled MBEDTLS_TEST_NULL_ENTROPY. \
  105. This option is not safe for production use and negates all security \
  106. It is intended for development use only. \
  107. \n\
  108. To confirm you want to build with this option, re-run cmake with the \
  109. option: \n\
  110. cmake -DUNSAFE_BUILD=ON ")
  111. return()
  112. endif()
  113. endif()
  114. endif()
  115. # If this is the root project add longer list of available CMAKE_BUILD_TYPE values
  116. if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  117. set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}
  118. CACHE STRING "Choose the type of build: None Debug Release Coverage ASan ASanDbg MemSan MemSanDbg Check CheckFull"
  119. FORCE)
  120. endif()
  121. # Create a symbolic link from ${base_name} in the binary directory
  122. # to the corresponding path in the source directory.
  123. function(link_to_source base_name)
  124. # Get OS dependent path to use in `execute_process`
  125. if (CMAKE_HOST_WIN32)
  126. #mklink is an internal command of cmd.exe it can only work with \
  127. string(REPLACE "/" "\\" link "${CMAKE_CURRENT_BINARY_DIR}/${base_name}")
  128. string(REPLACE "/" "\\" target "${CMAKE_CURRENT_SOURCE_DIR}/${base_name}")
  129. else()
  130. set(link "${CMAKE_CURRENT_BINARY_DIR}/${base_name}")
  131. set(target "${CMAKE_CURRENT_SOURCE_DIR}/${base_name}")
  132. endif()
  133. if (NOT EXISTS ${link})
  134. if (CMAKE_HOST_UNIX)
  135. set(command ln -s ${target} ${link})
  136. else()
  137. if (IS_DIRECTORY ${target})
  138. set(command cmd.exe /c mklink /j ${link} ${target})
  139. else()
  140. set(command cmd.exe /c mklink /h ${link} ${target})
  141. endif()
  142. endif()
  143. execute_process(COMMAND ${command}
  144. RESULT_VARIABLE result
  145. ERROR_VARIABLE output)
  146. if (NOT ${result} EQUAL 0)
  147. message(FATAL_ERROR "Could not create symbolic link for: ${target} --> ${output}")
  148. endif()
  149. endif()
  150. endfunction(link_to_source)
  151. string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")
  152. include(CheckCCompilerFlag)
  153. if(CMAKE_COMPILER_IS_GNU)
  154. # some warnings we want are not available with old GCC versions
  155. # note: starting with CMake 2.8 we could use CMAKE_C_COMPILER_VERSION
  156. execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
  157. OUTPUT_VARIABLE GCC_VERSION)
  158. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings")
  159. if (GCC_VERSION VERSION_GREATER 3.0 OR GCC_VERSION VERSION_EQUAL 3.0)
  160. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat=2 -Wno-format-nonliteral")
  161. endif()
  162. if (GCC_VERSION VERSION_GREATER 4.3 OR GCC_VERSION VERSION_EQUAL 4.3)
  163. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wvla")
  164. endif()
  165. if (GCC_VERSION VERSION_GREATER 4.5 OR GCC_VERSION VERSION_EQUAL 4.5)
  166. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wlogical-op")
  167. endif()
  168. if (GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8)
  169. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
  170. endif()
  171. if (GCC_VERSION VERSION_GREATER 5.0)
  172. CHECK_C_COMPILER_FLAG("-Wformat-signedness" C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS)
  173. if(C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS)
  174. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat-signedness")
  175. endif()
  176. endif()
  177. if (GCC_VERSION VERSION_GREATER 7.0 OR GCC_VERSION VERSION_EQUAL 7.0)
  178. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat-overflow=2 -Wformat-truncation")
  179. endif()
  180. set(CMAKE_C_FLAGS_RELEASE "-O2")
  181. set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
  182. set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
  183. set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3")
  184. set(CMAKE_C_FLAGS_ASANDBG "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls")
  185. set(CMAKE_C_FLAGS_CHECK "-Os")
  186. set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual")
  187. endif(CMAKE_COMPILER_IS_GNU)
  188. if(CMAKE_COMPILER_IS_CLANG)
  189. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings -Wpointer-arith -Wimplicit-fallthrough -Wshadow -Wvla -Wformat=2 -Wno-format-nonliteral")
  190. set(CMAKE_C_FLAGS_RELEASE "-O2")
  191. set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
  192. set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
  193. set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3")
  194. set(CMAKE_C_FLAGS_ASANDBG "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls")
  195. set(CMAKE_C_FLAGS_MEMSAN "-fsanitize=memory -O3")
  196. set(CMAKE_C_FLAGS_MEMSANDBG "-fsanitize=memory -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2")
  197. set(CMAKE_C_FLAGS_CHECK "-Os")
  198. endif(CMAKE_COMPILER_IS_CLANG)
  199. if(CMAKE_COMPILER_IS_IAR)
  200. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warn_about_c_style_casts -Ohz")
  201. endif(CMAKE_COMPILER_IS_IAR)
  202. if(CMAKE_COMPILER_IS_MSVC)
  203. # Strictest warnings, UTF-8 source and execution charset
  204. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3 /utf-8")
  205. endif(CMAKE_COMPILER_IS_MSVC)
  206. if(MBEDTLS_FATAL_WARNINGS)
  207. if(CMAKE_COMPILER_IS_MSVC)
  208. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
  209. endif(CMAKE_COMPILER_IS_MSVC)
  210. if(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU)
  211. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
  212. if(UNSAFE_BUILD)
  213. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=cpp")
  214. set(CMAKE_C_FLAGS_ASAN "${CMAKE_C_FLAGS_ASAN} -Wno-error=cpp")
  215. set(CMAKE_C_FLAGS_ASANDBG "${CMAKE_C_FLAGS_ASANDBG} -Wno-error=cpp")
  216. endif(UNSAFE_BUILD)
  217. endif(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU)
  218. if (CMAKE_COMPILER_IS_IAR)
  219. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warning_are_errors")
  220. endif(CMAKE_COMPILER_IS_IAR)
  221. endif(MBEDTLS_FATAL_WARNINGS)
  222. if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
  223. if(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG)
  224. set(CMAKE_SHARED_LINKER_FLAGS "--coverage")
  225. endif(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG)
  226. endif(CMAKE_BUILD_TYPE STREQUAL "Coverage")
  227. if(LIB_INSTALL_DIR)
  228. else()
  229. set(LIB_INSTALL_DIR lib)
  230. endif()
  231. if(ENABLE_ZLIB_SUPPORT)
  232. find_package(ZLIB)
  233. if(ZLIB_FOUND)
  234. include_directories(${ZLIB_INCLUDE_DIR})
  235. endif(ZLIB_FOUND)
  236. endif(ENABLE_ZLIB_SUPPORT)
  237. add_subdirectory(include)
  238. #add_subdirectory(3rdparty)
  239. #list(APPEND libs ${thirdparty_lib})
  240. add_subdirectory(library)
  241. #
  242. # The C files in tests/src directory contain test code shared among test suites
  243. # and programs. This shared test code is compiled and linked to test suites and
  244. # programs objects as a set of compiled objects. The compiled objects are NOT
  245. # built into a library that the test suite and program objects would link
  246. # against as they link against the mbedcrypto, mbedx509 and mbedtls libraries.
  247. # The reason is that such library is expected to have mutual dependencies with
  248. # the aforementioned libraries and that there is as of today no portable way of
  249. # handling such dependencies (only toolchain specific solutions).
  250. #
  251. # Thus the below definition of the `mbedtls_test` CMake library of objects
  252. # target. This library of objects is used by tests and programs CMake files
  253. # to define the test executables.
  254. #
  255. if(ENABLE_TESTING OR ENABLE_PROGRAMS)
  256. file(GLOB MBEDTLS_TEST_FILES ${CMAKE_CURRENT_SOURCE_DIR}/tests/src/*.c ${CMAKE_CURRENT_SOURCE_DIR}/tests/src/drivers/*.c)
  257. add_library(mbedtls_test OBJECT ${MBEDTLS_TEST_FILES})
  258. target_include_directories(mbedtls_test
  259. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tests/include
  260. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
  261. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/library)
  262. endif()
  263. if(ENABLE_PROGRAMS)
  264. add_subdirectory(programs)
  265. endif()
  266. ADD_CUSTOM_TARGET(${MBEDTLS_TARGET_PREFIX}apidoc
  267. COMMAND doxygen mbedtls.doxyfile
  268. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doxygen)
  269. if(ENABLE_TESTING)
  270. enable_testing()
  271. add_subdirectory(tests)
  272. # additional convenience targets for Unix only
  273. if(UNIX)
  274. ADD_CUSTOM_TARGET(covtest
  275. COMMAND make test
  276. COMMAND programs/test/selftest
  277. COMMAND tests/compat.sh
  278. COMMAND tests/ssl-opt.sh
  279. )
  280. ADD_CUSTOM_TARGET(lcov
  281. COMMAND rm -rf Coverage
  282. COMMAND lcov --capture --initial --directory library/CMakeFiles/mbedtls.dir -o files.info
  283. COMMAND lcov --capture --directory library/CMakeFiles/mbedtls.dir -o tests.info
  284. COMMAND lcov --add-tracefile files.info --add-tracefile tests.info -o all.info
  285. COMMAND lcov --remove all.info -o final.info '*.h'
  286. COMMAND gendesc tests/Descriptions.txt -o descriptions
  287. COMMAND genhtml --title "mbed TLS" --description-file descriptions --keep-descriptions --legend --no-branch-coverage -o Coverage final.info
  288. COMMAND rm -f files.info tests.info all.info final.info descriptions
  289. )
  290. ADD_CUSTOM_TARGET(memcheck
  291. COMMAND sed -i.bak s+/usr/bin/valgrind+`which valgrind`+ DartConfiguration.tcl
  292. COMMAND ctest -O memcheck.log -D ExperimentalMemCheck
  293. COMMAND tail -n1 memcheck.log | grep 'Memory checking results:' > /dev/null
  294. COMMAND rm -f memcheck.log
  295. COMMAND mv DartConfiguration.tcl.bak DartConfiguration.tcl
  296. )
  297. endif(UNIX)
  298. # Make scripts needed for testing available in an out-of-source build.
  299. if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
  300. link_to_source(scripts)
  301. # Copy (don't link) DartConfiguration.tcl, needed for memcheck, to
  302. # keep things simple with the sed commands in the memcheck target.
  303. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/DartConfiguration.tcl
  304. ${CMAKE_CURRENT_BINARY_DIR}/DartConfiguration.tcl COPYONLY)
  305. endif()
  306. endif()