CMakeLists.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. # - MBEDTLS_TARGET_PREFIX: CMake targets are designed to be alterable by calling
  14. # CMake in order to avoid target name clashes, via the use of
  15. # MBEDTLS_TARGET_PREFIX. The value of this variable is prefixed to the
  16. # mbedtls, mbedx509, mbedcrypto and apidoc targets.
  17. #
  18. # We specify a minimum requirement of 3.10.2, but for now use 3.5.1 here
  19. # until our infrastructure catches up.
  20. cmake_minimum_required(VERSION 3.5.1)
  21. include(CMakePackageConfigHelpers)
  22. # Include convenience functions for printing properties and variables, like
  23. # cmake_print_properties(), cmake_print_variables().
  24. include(CMakePrintHelpers)
  25. # https://cmake.org/cmake/help/latest/policy/CMP0011.html
  26. # Setting this policy is required in CMake >= 3.18.0, otherwise a warning is generated. The OLD
  27. # policy setting is deprecated, and will be removed in future versions.
  28. cmake_policy(SET CMP0011 NEW)
  29. # https://cmake.org/cmake/help/latest/policy/CMP0012.html
  30. # Setting the CMP0012 policy to NEW is required for FindPython3 to work with CMake 3.18.2
  31. # (there is a bug in this particular version), otherwise, setting the CMP0012 policy is required
  32. # for CMake versions >= 3.18.3 otherwise a deprecated warning is generated. The OLD policy setting
  33. # is deprecated and will be removed in future versions.
  34. cmake_policy(SET CMP0012 NEW)
  35. if(TEST_CPP)
  36. project("Mbed TLS"
  37. LANGUAGES C CXX
  38. VERSION 3.6.5
  39. )
  40. else()
  41. project("Mbed TLS"
  42. LANGUAGES C
  43. VERSION 3.6.5
  44. )
  45. endif()
  46. include(GNUInstallDirs)
  47. # Determine if Mbed TLS is being built as a subproject using add_subdirectory()
  48. if(NOT DEFINED MBEDTLS_AS_SUBPROJECT)
  49. set(MBEDTLS_AS_SUBPROJECT ON)
  50. if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  51. set(MBEDTLS_AS_SUBPROJECT OFF)
  52. endif()
  53. endif()
  54. # Set the project root directory.
  55. set(MBEDTLS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  56. set(MBEDTLS_FRAMEWORK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/framework)
  57. option(ENABLE_PROGRAMS "Build Mbed TLS programs." ON)
  58. option(UNSAFE_BUILD "Allow unsafe builds. These builds ARE NOT SECURE." OFF)
  59. option(MBEDTLS_FATAL_WARNINGS "Compiler warnings treated as errors" ON)
  60. if(CMAKE_HOST_WIN32)
  61. # N.B. The comment on the next line is significant! If you change it,
  62. # edit the sed command in prepare_release.sh that modifies
  63. # CMakeLists.txt.
  64. option(GEN_FILES "Generate the auto-generated files as needed" OFF) # off in development
  65. else()
  66. option(GEN_FILES "Generate the auto-generated files as needed" OFF)
  67. endif()
  68. option(DISABLE_PACKAGE_CONFIG_AND_INSTALL "Disable package configuration, target export and installation" ${MBEDTLS_AS_SUBPROJECT})
  69. if (CMAKE_C_SIMULATE_ID)
  70. set(COMPILER_ID ${CMAKE_C_SIMULATE_ID})
  71. else()
  72. set(COMPILER_ID ${CMAKE_C_COMPILER_ID})
  73. endif(CMAKE_C_SIMULATE_ID)
  74. string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${COMPILER_ID}")
  75. string(REGEX MATCH "GNU" CMAKE_COMPILER_IS_GNU "${COMPILER_ID}")
  76. string(REGEX MATCH "IAR" CMAKE_COMPILER_IS_IAR "${COMPILER_ID}")
  77. string(REGEX MATCH "MSVC" CMAKE_COMPILER_IS_MSVC "${COMPILER_ID}")
  78. # the test suites currently have compile errors with MSVC
  79. if(CMAKE_COMPILER_IS_MSVC)
  80. option(ENABLE_TESTING "Build Mbed TLS tests." OFF)
  81. else()
  82. option(ENABLE_TESTING "Build Mbed TLS tests." ON)
  83. endif()
  84. # Warning string - created as a list for compatibility with CMake 2.8
  85. set(CTR_DRBG_128_BIT_KEY_WARN_L1 "**** WARNING! MBEDTLS_CTR_DRBG_USE_128_BIT_KEY defined!\n")
  86. set(CTR_DRBG_128_BIT_KEY_WARN_L2 "**** Using 128-bit keys for CTR_DRBG limits the security of generated\n")
  87. set(CTR_DRBG_128_BIT_KEY_WARN_L3 "**** keys and operations that use random values generated to 128-bit security\n")
  88. set(CTR_DRBG_128_BIT_KEY_WARNING "${WARNING_BORDER}"
  89. "${CTR_DRBG_128_BIT_KEY_WARN_L1}"
  90. "${CTR_DRBG_128_BIT_KEY_WARN_L2}"
  91. "${CTR_DRBG_128_BIT_KEY_WARN_L3}"
  92. "${WARNING_BORDER}")
  93. # Python 3 is only needed here to check for configuration warnings.
  94. if(NOT CMAKE_VERSION VERSION_LESS 3.15.0)
  95. set(Python3_FIND_STRATEGY LOCATION)
  96. find_package(Python3 COMPONENTS Interpreter)
  97. if(Python3_Interpreter_FOUND)
  98. set(MBEDTLS_PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
  99. endif()
  100. else()
  101. find_package(PythonInterp 3)
  102. if(PYTHONINTERP_FOUND)
  103. set(MBEDTLS_PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
  104. endif()
  105. endif()
  106. if(MBEDTLS_PYTHON_EXECUTABLE)
  107. # If 128-bit keys are configured for CTR_DRBG, display an appropriate warning
  108. execute_process(COMMAND ${MBEDTLS_PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/config.py -f ${CMAKE_CURRENT_SOURCE_DIR}/include/mbedtls/mbedtls_config.h get MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
  109. RESULT_VARIABLE result)
  110. if(${result} EQUAL 0)
  111. message(WARNING ${CTR_DRBG_128_BIT_KEY_WARNING})
  112. endif()
  113. endif()
  114. # We now potentially need to link all executables against PThreads, if available
  115. set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
  116. set(THREADS_PREFER_PTHREAD_FLAG TRUE)
  117. find_package(Threads)
  118. # If this is the root project add longer list of available CMAKE_BUILD_TYPE values
  119. if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  120. set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}
  121. CACHE STRING "Choose the type of build: None Debug Release Coverage ASan ASanDbg MemSan MemSanDbg Check CheckFull TSan TSanDbg"
  122. FORCE)
  123. endif()
  124. # Make MBEDTLS_CONFIG_FILE and MBEDTLS_USER_CONFIG_FILE into PATHs
  125. set(MBEDTLS_CONFIG_FILE "" CACHE FILEPATH "Mbed TLS config file (overrides default).")
  126. set(MBEDTLS_USER_CONFIG_FILE "" CACHE FILEPATH "Mbed TLS user config file (appended to default).")
  127. # Create a symbolic link from ${base_name} in the binary directory
  128. # to the corresponding path in the source directory.
  129. # Note: Copies the file(s) on Windows.
  130. function(link_to_source base_name)
  131. set(link "${CMAKE_CURRENT_BINARY_DIR}/${base_name}")
  132. set(target "${CMAKE_CURRENT_SOURCE_DIR}/${base_name}")
  133. # Linking to non-existent file is not desirable. At best you will have a
  134. # dangling link, but when building in tree, this can create a symbolic link
  135. # to itself.
  136. if (EXISTS ${target} AND NOT EXISTS ${link})
  137. if (CMAKE_HOST_UNIX)
  138. execute_process(COMMAND ln -s ${target} ${link}
  139. RESULT_VARIABLE result
  140. ERROR_VARIABLE output)
  141. if (NOT ${result} EQUAL 0)
  142. message(FATAL_ERROR "Could not create symbolic link for: ${target} --> ${output}")
  143. endif()
  144. else()
  145. if (IS_DIRECTORY ${target})
  146. file(GLOB_RECURSE files FOLLOW_SYMLINKS LIST_DIRECTORIES false RELATIVE ${target} "${target}/*")
  147. foreach(file IN LISTS files)
  148. configure_file("${target}/${file}" "${link}/${file}" COPYONLY)
  149. endforeach(file)
  150. else()
  151. configure_file(${target} ${link} COPYONLY)
  152. endif()
  153. endif()
  154. endif()
  155. endfunction(link_to_source)
  156. # Get the filename without the final extension (i.e. convert "a.b.c" to "a.b")
  157. function(get_name_without_last_ext dest_var full_name)
  158. # Split into a list on '.' (but a cmake list is just a ';'-separated string)
  159. string(REPLACE "." ";" ext_parts "${full_name}")
  160. # Remove the last item if there are more than one
  161. list(LENGTH ext_parts ext_parts_len)
  162. if (${ext_parts_len} GREATER "1")
  163. math(EXPR ext_parts_last_item "${ext_parts_len} - 1")
  164. list(REMOVE_AT ext_parts ${ext_parts_last_item})
  165. endif()
  166. # Convert back to a string by replacing separators with '.'
  167. string(REPLACE ";" "." no_ext_name "${ext_parts}")
  168. # Copy into the desired variable
  169. set(${dest_var} ${no_ext_name} PARENT_SCOPE)
  170. endfunction(get_name_without_last_ext)
  171. include(CheckCCompilerFlag)
  172. set(CMAKE_C_EXTENSIONS OFF)
  173. set(CMAKE_C_STANDARD 99)
  174. if(CMAKE_COMPILER_IS_GNU)
  175. # some warnings we want are not available with old GCC versions
  176. # note: starting with CMake 2.8 we could use CMAKE_C_COMPILER_VERSION
  177. execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
  178. OUTPUT_VARIABLE GCC_VERSION)
  179. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings -Wmissing-prototypes")
  180. if (GCC_VERSION VERSION_GREATER 3.0 OR GCC_VERSION VERSION_EQUAL 3.0)
  181. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat=2 -Wno-format-nonliteral")
  182. endif()
  183. if (GCC_VERSION VERSION_GREATER 4.3 OR GCC_VERSION VERSION_EQUAL 4.3)
  184. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wvla")
  185. endif()
  186. if (GCC_VERSION VERSION_GREATER 4.5 OR GCC_VERSION VERSION_EQUAL 4.5)
  187. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wlogical-op")
  188. endif()
  189. if (GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8)
  190. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
  191. endif()
  192. if (GCC_VERSION VERSION_GREATER 5.0)
  193. CHECK_C_COMPILER_FLAG("-Wformat-signedness" C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS)
  194. if(C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS)
  195. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat-signedness")
  196. endif()
  197. endif()
  198. if (GCC_VERSION VERSION_GREATER 7.0 OR GCC_VERSION VERSION_EQUAL 7.0)
  199. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat-overflow=2 -Wformat-truncation")
  200. endif()
  201. set(CMAKE_C_FLAGS_RELEASE "-O2")
  202. set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
  203. set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
  204. # Old GCC versions hit a performance problem with test_suite_pkwrite
  205. # "Private keey write check EC" tests when building with Asan+UBSan
  206. # and -O3: those tests take more than 100x time than normal, with
  207. # test_suite_pkwrite taking >3h on the CI. Observed with GCC 5.4 on
  208. # Ubuntu 16.04 x86_64 and GCC 6.5 on Ubuntu 18.04 x86_64.
  209. # GCC 7.5 and above on Ubuntu 18.04 appear fine.
  210. # To avoid the performance problem, we use -O2 when GCC version is lower than 7.0.
  211. # It doesn't slow down much even with modern compiler versions.
  212. if (GCC_VERSION VERSION_LESS 7.0)
  213. message(STATUS "USING O2")
  214. set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O2")
  215. else()
  216. message(STATUS "USING O3")
  217. set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3")
  218. endif()
  219. 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")
  220. set(CMAKE_C_FLAGS_TSAN "-fsanitize=thread -O3")
  221. set(CMAKE_C_FLAGS_TSANDBG "-fsanitize=thread -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls")
  222. set(CMAKE_C_FLAGS_CHECK "-Os")
  223. set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual")
  224. endif(CMAKE_COMPILER_IS_GNU)
  225. if(CMAKE_COMPILER_IS_CLANG)
  226. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings -Wmissing-prototypes -Wpointer-arith -Wimplicit-fallthrough -Wshadow -Wvla -Wformat=2 -Wno-format-nonliteral")
  227. set(CMAKE_C_FLAGS_RELEASE "-O2")
  228. set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
  229. set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
  230. set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3")
  231. 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")
  232. set(CMAKE_C_FLAGS_MEMSAN "-fsanitize=memory -O3")
  233. set(CMAKE_C_FLAGS_MEMSANDBG "-fsanitize=memory -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2")
  234. set(CMAKE_C_FLAGS_TSAN "-fsanitize=thread -O3")
  235. set(CMAKE_C_FLAGS_TSANDBG "-fsanitize=thread -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls")
  236. set(CMAKE_C_FLAGS_CHECK "-Os")
  237. endif(CMAKE_COMPILER_IS_CLANG)
  238. if(CMAKE_COMPILER_IS_IAR)
  239. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warn_about_c_style_casts")
  240. set(CMAKE_C_FLAGS_RELEASE "-Ohz")
  241. set(CMAKE_C_FLAGS_DEBUG "--debug -On")
  242. endif(CMAKE_COMPILER_IS_IAR)
  243. if(CMAKE_COMPILER_IS_MSVC)
  244. # Strictest warnings, UTF-8 source and execution charset
  245. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3 /utf-8")
  246. endif(CMAKE_COMPILER_IS_MSVC)
  247. if(MBEDTLS_FATAL_WARNINGS)
  248. if(CMAKE_COMPILER_IS_MSVC)
  249. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
  250. endif(CMAKE_COMPILER_IS_MSVC)
  251. if(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU)
  252. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
  253. if(UNSAFE_BUILD)
  254. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=cpp")
  255. set(CMAKE_C_FLAGS_ASAN "${CMAKE_C_FLAGS_ASAN} -Wno-error=cpp")
  256. set(CMAKE_C_FLAGS_ASANDBG "${CMAKE_C_FLAGS_ASANDBG} -Wno-error=cpp")
  257. endif(UNSAFE_BUILD)
  258. endif(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU)
  259. if (CMAKE_COMPILER_IS_IAR)
  260. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warnings_are_errors")
  261. endif(CMAKE_COMPILER_IS_IAR)
  262. endif(MBEDTLS_FATAL_WARNINGS)
  263. if(CMAKE_BUILD_TYPE STREQUAL "Check" AND TEST_CPP)
  264. set(CMAKE_CXX_STANDARD 11)
  265. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  266. set(CMAKE_CXX_EXTENSIONS OFF)
  267. if(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU)
  268. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic")
  269. endif()
  270. endif()
  271. if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
  272. if(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG)
  273. set(CMAKE_SHARED_LINKER_FLAGS "--coverage")
  274. endif(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG)
  275. endif(CMAKE_BUILD_TYPE STREQUAL "Coverage")
  276. if(LIB_INSTALL_DIR)
  277. set(CMAKE_INSTALL_LIBDIR "${LIB_INSTALL_DIR}")
  278. endif()
  279. if (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/framework/CMakeLists.txt")
  280. if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/.git")
  281. message(FATAL_ERROR "${CMAKE_CURRENT_SOURCE_DIR}CMakeLists.txt not found (and does appear to be a git checkout). Run `git submodule update --init` from the source tree to fetch the submodule contents.")
  282. else ()
  283. message(FATAL_ERROR "${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt not found (and does not appear to be a git checkout). Please ensure you have downloaded the right archive from the release page on GitHub.")
  284. endif()
  285. endif()
  286. add_subdirectory(framework)
  287. add_subdirectory(include)
  288. add_subdirectory(3rdparty)
  289. add_subdirectory(library)
  290. add_subdirectory(pkgconfig)
  291. #
  292. # The C files in framework/tests/src directory contain test code shared among test suites
  293. # and programs. This shared test code is compiled and linked to test suites and
  294. # programs objects as a set of compiled objects. The compiled objects are NOT
  295. # built into a library that the test suite and program objects would link
  296. # against as they link against the mbedcrypto, mbedx509 and mbedtls libraries.
  297. # The reason is that such library is expected to have mutual dependencies with
  298. # the aforementioned libraries and that there is as of today no portable way of
  299. # handling such dependencies (only toolchain specific solutions).
  300. #
  301. # Thus the below definition of the `mbedtls_test` CMake library of objects
  302. # target. This library of objects is used by tests and programs CMake files
  303. # to define the test executables.
  304. #
  305. if(ENABLE_TESTING OR ENABLE_PROGRAMS)
  306. file(GLOB MBEDTLS_TEST_FILES
  307. ${CMAKE_CURRENT_SOURCE_DIR}/tests/src/*.c
  308. ${CMAKE_CURRENT_SOURCE_DIR}/framework/tests/src/*.c
  309. ${CMAKE_CURRENT_SOURCE_DIR}/framework/tests/src/drivers/*.c)
  310. add_library(mbedtls_test OBJECT ${MBEDTLS_TEST_FILES})
  311. if(GEN_FILES)
  312. add_custom_command(
  313. OUTPUT
  314. ${CMAKE_CURRENT_BINARY_DIR}/tests/include/test/test_keys.h
  315. COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/tests/include/test
  316. COMMAND
  317. "${MBEDTLS_PYTHON_EXECUTABLE}"
  318. "${CMAKE_CURRENT_SOURCE_DIR}/framework/scripts/generate_test_keys.py"
  319. "--output"
  320. "${CMAKE_CURRENT_BINARY_DIR}/tests/include/test/test_keys.h"
  321. DEPENDS
  322. ${CMAKE_CURRENT_SOURCE_DIR}/framework/scripts/generate_test_keys.py
  323. )
  324. add_custom_target(test_keys_header
  325. DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tests/include/test/test_keys.h)
  326. add_custom_command(
  327. OUTPUT
  328. ${CMAKE_CURRENT_BINARY_DIR}/tests/include/test/test_certs.h
  329. COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/tests/include/test
  330. WORKING_DIRECTORY
  331. ${CMAKE_CURRENT_SOURCE_DIR}/tests
  332. COMMAND
  333. "${MBEDTLS_PYTHON_EXECUTABLE}"
  334. "${CMAKE_CURRENT_SOURCE_DIR}/framework/scripts/generate_test_cert_macros.py"
  335. "--output"
  336. "${CMAKE_CURRENT_BINARY_DIR}/tests/include/test/test_certs.h"
  337. DEPENDS
  338. ${CMAKE_CURRENT_SOURCE_DIR}/framework/scripts/generate_test_cert_macros.py
  339. )
  340. add_custom_target(test_certs_header DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/tests/include/test/test_certs.h)
  341. add_dependencies(mbedtls_test test_keys_header test_certs_header)
  342. endif()
  343. target_include_directories(mbedtls_test
  344. PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/tests/include
  345. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/framework/tests/include
  346. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tests/include
  347. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
  348. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/library)
  349. # Request C11, needed for memory poisoning tests
  350. set_target_properties(mbedtls_test PROPERTIES C_STANDARD 11)
  351. file(GLOB MBEDTLS_TEST_HELPER_FILES
  352. ${CMAKE_CURRENT_SOURCE_DIR}/tests/src/test_helpers/*.c)
  353. add_library(mbedtls_test_helpers OBJECT ${MBEDTLS_TEST_HELPER_FILES})
  354. target_include_directories(mbedtls_test_helpers
  355. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/framework/tests/include
  356. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tests/include
  357. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
  358. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/library
  359. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/everest/include)
  360. # Pass-through MBEDTLS_CONFIG_FILE and MBEDTLS_USER_CONFIG_FILE
  361. if(MBEDTLS_CONFIG_FILE)
  362. target_compile_definitions(mbedtls_test
  363. PUBLIC MBEDTLS_CONFIG_FILE="${MBEDTLS_CONFIG_FILE}")
  364. target_compile_definitions(mbedtls_test_helpers
  365. PUBLIC MBEDTLS_CONFIG_FILE="${MBEDTLS_CONFIG_FILE}")
  366. endif()
  367. if(MBEDTLS_USER_CONFIG_FILE)
  368. target_compile_definitions(mbedtls_test
  369. PUBLIC MBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}")
  370. target_compile_definitions(mbedtls_test_helpers
  371. PUBLIC MBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}")
  372. endif()
  373. endif()
  374. if(ENABLE_PROGRAMS)
  375. set(ssl_opt_target "${MBEDTLS_TARGET_PREFIX}ssl-opt")
  376. add_custom_target(${ssl_opt_target})
  377. add_subdirectory(programs)
  378. endif()
  379. ADD_CUSTOM_TARGET(${MBEDTLS_TARGET_PREFIX}apidoc
  380. COMMAND doxygen mbedtls.doxyfile
  381. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doxygen)
  382. if(ENABLE_TESTING)
  383. enable_testing()
  384. add_subdirectory(tests)
  385. # additional convenience targets for Unix only
  386. if(UNIX)
  387. # For coverage testing:
  388. # 1. Build with:
  389. # cmake -D CMAKE_BUILD_TYPE=Coverage /path/to/source && make
  390. # 2. Run the relevant tests for the part of the code you're interested in.
  391. # For the reference coverage measurement, see
  392. # tests/scripts/basic-build-test.sh
  393. # 3. Run scripts/lcov.sh to generate an HTML report.
  394. ADD_CUSTOM_TARGET(lcov
  395. COMMAND scripts/lcov.sh
  396. )
  397. ADD_CUSTOM_TARGET(memcheck
  398. COMMAND sed -i.bak s+/usr/bin/valgrind+`which valgrind`+ DartConfiguration.tcl
  399. COMMAND ctest -O memcheck.log -D ExperimentalMemCheck
  400. COMMAND tail -n1 memcheck.log | grep 'Memory checking results:' > /dev/null
  401. COMMAND rm -f memcheck.log
  402. COMMAND mv DartConfiguration.tcl.bak DartConfiguration.tcl
  403. )
  404. endif(UNIX)
  405. # Make scripts needed for testing available in an out-of-source build.
  406. if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
  407. link_to_source(scripts)
  408. # Copy (don't link) DartConfiguration.tcl, needed for memcheck, to
  409. # keep things simple with the sed commands in the memcheck target.
  410. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/DartConfiguration.tcl
  411. ${CMAKE_CURRENT_BINARY_DIR}/DartConfiguration.tcl COPYONLY)
  412. endif()
  413. endif()
  414. if(NOT DISABLE_PACKAGE_CONFIG_AND_INSTALL)
  415. configure_package_config_file(
  416. "cmake/MbedTLSConfig.cmake.in"
  417. "cmake/MbedTLSConfig.cmake"
  418. INSTALL_DESTINATION "cmake")
  419. write_basic_package_version_file(
  420. "cmake/MbedTLSConfigVersion.cmake"
  421. COMPATIBILITY SameMajorVersion
  422. VERSION 3.6.5)
  423. install(
  424. FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake/MbedTLSConfig.cmake"
  425. "${CMAKE_CURRENT_BINARY_DIR}/cmake/MbedTLSConfigVersion.cmake"
  426. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/MbedTLS")
  427. export(
  428. EXPORT MbedTLSTargets
  429. NAMESPACE MbedTLS::
  430. FILE "cmake/MbedTLSTargets.cmake")
  431. install(
  432. EXPORT MbedTLSTargets
  433. NAMESPACE MbedTLS::
  434. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/MbedTLS"
  435. FILE "MbedTLSTargets.cmake")
  436. if(CMAKE_VERSION VERSION_GREATER 3.15 OR CMAKE_VERSION VERSION_EQUAL 3.15)
  437. # Do not export the package by default
  438. cmake_policy(SET CMP0090 NEW)
  439. # Make this package visible to the system
  440. export(PACKAGE MbedTLS)
  441. endif()
  442. endif()