CMakeLists.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. # https://cmake.org/cmake/help/latest/policy/CMP0011.html
  23. # Setting this policy is required in CMake >= 3.18.0, otherwise a warning is generated. The OLD
  24. # policy setting is deprecated, and will be removed in future versions.
  25. cmake_policy(SET CMP0011 NEW)
  26. # https://cmake.org/cmake/help/latest/policy/CMP0012.html
  27. # Setting the CMP0012 policy to NEW is required for FindPython3 to work with CMake 3.18.2
  28. # (there is a bug in this particular version), otherwise, setting the CMP0012 policy is required
  29. # for CMake versions >= 3.18.3 otherwise a deprecated warning is generated. The OLD policy setting
  30. # is deprecated and will be removed in future versions.
  31. cmake_policy(SET CMP0012 NEW)
  32. if(TEST_CPP)
  33. project("mbed TLS" C CXX)
  34. else()
  35. project("mbed TLS" C)
  36. endif()
  37. include(GNUInstallDirs)
  38. # Determine if mbed TLS is being built as a subproject using add_subdirectory()
  39. if(NOT DEFINED MBEDTLS_AS_SUBPROJECT)
  40. set(MBEDTLS_AS_SUBPROJECT ON)
  41. if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
  42. set(MBEDTLS_AS_SUBPROJECT OFF)
  43. endif()
  44. endif()
  45. # Set the project root directory.
  46. set(MBEDTLS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
  47. option(ENABLE_PROGRAMS "Build mbed TLS programs." ON)
  48. option(UNSAFE_BUILD "Allow unsafe builds. These builds ARE NOT SECURE." OFF)
  49. option(MBEDTLS_FATAL_WARNINGS "Compiler warnings treated as errors" ON)
  50. if(CMAKE_HOST_WIN32)
  51. option(GEN_FILES "Generate the auto-generated files as needed" OFF)
  52. else()
  53. option(GEN_FILES "Generate the auto-generated files as needed" OFF)
  54. endif()
  55. option(DISABLE_PACKAGE_CONFIG_AND_INSTALL "Disable package configuration, target export and installation" ${MBEDTLS_AS_SUBPROJECT})
  56. string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")
  57. string(REGEX MATCH "GNU" CMAKE_COMPILER_IS_GNU "${CMAKE_C_COMPILER_ID}")
  58. string(REGEX MATCH "IAR" CMAKE_COMPILER_IS_IAR "${CMAKE_C_COMPILER_ID}")
  59. string(REGEX MATCH "MSVC" CMAKE_COMPILER_IS_MSVC "${CMAKE_C_COMPILER_ID}")
  60. # the test suites currently have compile errors with MSVC
  61. if(CMAKE_COMPILER_IS_MSVC)
  62. option(ENABLE_TESTING "Build mbed TLS tests." OFF)
  63. else()
  64. option(ENABLE_TESTING "Build mbed TLS tests." ON)
  65. endif()
  66. # Warning string - created as a list for compatibility with CMake 2.8
  67. set(CTR_DRBG_128_BIT_KEY_WARN_L1 "**** WARNING! MBEDTLS_CTR_DRBG_USE_128_BIT_KEY defined!\n")
  68. set(CTR_DRBG_128_BIT_KEY_WARN_L2 "**** Using 128-bit keys for CTR_DRBG limits the security of generated\n")
  69. set(CTR_DRBG_128_BIT_KEY_WARN_L3 "**** keys and operations that use random values generated to 128-bit security\n")
  70. set(CTR_DRBG_128_BIT_KEY_WARNING "${WARNING_BORDER}"
  71. "${CTR_DRBG_128_BIT_KEY_WARN_L1}"
  72. "${CTR_DRBG_128_BIT_KEY_WARN_L2}"
  73. "${CTR_DRBG_128_BIT_KEY_WARN_L3}"
  74. "${WARNING_BORDER}")
  75. # Python 3 is only needed here to check for configuration warnings.
  76. if(NOT CMAKE_VERSION VERSION_LESS 3.15.0)
  77. set(Python3_FIND_STRATEGY LOCATION)
  78. find_package(Python3 COMPONENTS Interpreter)
  79. if(Python3_Interpreter_FOUND)
  80. set(MBEDTLS_PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
  81. endif()
  82. else()
  83. find_package(PythonInterp 3)
  84. if(PYTHONINTERP_FOUND)
  85. set(MBEDTLS_PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
  86. endif()
  87. endif()
  88. if(MBEDTLS_PYTHON_EXECUTABLE)
  89. # If 128-bit keys are configured for CTR_DRBG, display an appropriate warning
  90. 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
  91. RESULT_VARIABLE result)
  92. if(${result} EQUAL 0)
  93. message(WARNING ${CTR_DRBG_128_BIT_KEY_WARNING})
  94. endif()
  95. endif()
  96. # If this is the root project add longer list of available CMAKE_BUILD_TYPE values
  97. if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  98. set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}
  99. CACHE STRING "Choose the type of build: None Debug Release Coverage ASan ASanDbg MemSan MemSanDbg Check CheckFull"
  100. FORCE)
  101. endif()
  102. # Create a symbolic link from ${base_name} in the binary directory
  103. # to the corresponding path in the source directory.
  104. # Note: Copies the file(s) on Windows.
  105. function(link_to_source base_name)
  106. set(link "${CMAKE_CURRENT_BINARY_DIR}/${base_name}")
  107. set(target "${CMAKE_CURRENT_SOURCE_DIR}/${base_name}")
  108. # Linking to non-existent file is not desirable. At best you will have a
  109. # dangling link, but when building in tree, this can create a symbolic link
  110. # to itself.
  111. if (EXISTS ${target} AND NOT EXISTS ${link})
  112. if (CMAKE_HOST_UNIX)
  113. execute_process(COMMAND ln -s ${target} ${link}
  114. RESULT_VARIABLE result
  115. ERROR_VARIABLE output)
  116. if (NOT ${result} EQUAL 0)
  117. message(FATAL_ERROR "Could not create symbolic link for: ${target} --> ${output}")
  118. endif()
  119. else()
  120. if (IS_DIRECTORY ${target})
  121. file(GLOB_RECURSE files FOLLOW_SYMLINKS LIST_DIRECTORIES false RELATIVE ${target} "${target}/*")
  122. foreach(file IN LISTS files)
  123. configure_file("${target}/${file}" "${link}/${file}" COPYONLY)
  124. endforeach(file)
  125. else()
  126. configure_file(${target} ${link} COPYONLY)
  127. endif()
  128. endif()
  129. endif()
  130. endfunction(link_to_source)
  131. # Get the filename without the final extension (i.e. convert "a.b.c" to "a.b")
  132. function(get_name_without_last_ext dest_var full_name)
  133. # Split into a list on '.' (but a cmake list is just a ';'-separated string)
  134. string(REPLACE "." ";" ext_parts "${full_name}")
  135. # Remove the last item if there are more than one
  136. list(LENGTH ext_parts ext_parts_len)
  137. if (${ext_parts_len} GREATER "1")
  138. math(EXPR ext_parts_last_item "${ext_parts_len} - 1")
  139. list(REMOVE_AT ext_parts ${ext_parts_last_item})
  140. endif()
  141. # Convert back to a string by replacing separators with '.'
  142. string(REPLACE ";" "." no_ext_name "${ext_parts}")
  143. # Copy into the desired variable
  144. set(${dest_var} ${no_ext_name} PARENT_SCOPE)
  145. endfunction(get_name_without_last_ext)
  146. string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")
  147. include(CheckCCompilerFlag)
  148. set(CMAKE_C_EXTENSIONS OFF)
  149. set(CMAKE_C_STANDARD 99)
  150. if(CMAKE_COMPILER_IS_GNU)
  151. # some warnings we want are not available with old GCC versions
  152. # note: starting with CMake 2.8 we could use CMAKE_C_COMPILER_VERSION
  153. execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
  154. OUTPUT_VARIABLE GCC_VERSION)
  155. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings")
  156. if (GCC_VERSION VERSION_GREATER 3.0 OR GCC_VERSION VERSION_EQUAL 3.0)
  157. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat=2 -Wno-format-nonliteral")
  158. endif()
  159. if (GCC_VERSION VERSION_GREATER 4.3 OR GCC_VERSION VERSION_EQUAL 4.3)
  160. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wvla")
  161. endif()
  162. if (GCC_VERSION VERSION_GREATER 4.5 OR GCC_VERSION VERSION_EQUAL 4.5)
  163. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wlogical-op")
  164. endif()
  165. if (GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8)
  166. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
  167. endif()
  168. if (GCC_VERSION VERSION_GREATER 5.0)
  169. CHECK_C_COMPILER_FLAG("-Wformat-signedness" C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS)
  170. if(C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS)
  171. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat-signedness")
  172. endif()
  173. endif()
  174. if (GCC_VERSION VERSION_GREATER 7.0 OR GCC_VERSION VERSION_EQUAL 7.0)
  175. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat-overflow=2 -Wformat-truncation")
  176. endif()
  177. set(CMAKE_C_FLAGS_RELEASE "-O2")
  178. set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
  179. set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
  180. set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3")
  181. 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")
  182. set(CMAKE_C_FLAGS_CHECK "-Os")
  183. set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual")
  184. endif(CMAKE_COMPILER_IS_GNU)
  185. if(CMAKE_COMPILER_IS_CLANG)
  186. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings -Wpointer-arith -Wimplicit-fallthrough -Wshadow -Wvla -Wformat=2 -Wno-format-nonliteral")
  187. set(CMAKE_C_FLAGS_RELEASE "-O2")
  188. set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
  189. set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
  190. set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3")
  191. 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")
  192. set(CMAKE_C_FLAGS_MEMSAN "-fsanitize=memory -O3")
  193. set(CMAKE_C_FLAGS_MEMSANDBG "-fsanitize=memory -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2")
  194. set(CMAKE_C_FLAGS_CHECK "-Os")
  195. endif(CMAKE_COMPILER_IS_CLANG)
  196. if(CMAKE_COMPILER_IS_IAR)
  197. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warn_about_c_style_casts -Ohz")
  198. endif(CMAKE_COMPILER_IS_IAR)
  199. if(CMAKE_COMPILER_IS_MSVC)
  200. # Strictest warnings, UTF-8 source and execution charset
  201. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3 /utf-8")
  202. endif(CMAKE_COMPILER_IS_MSVC)
  203. if(MBEDTLS_FATAL_WARNINGS)
  204. if(CMAKE_COMPILER_IS_MSVC)
  205. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
  206. endif(CMAKE_COMPILER_IS_MSVC)
  207. if(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU)
  208. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
  209. if(UNSAFE_BUILD)
  210. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=cpp")
  211. set(CMAKE_C_FLAGS_ASAN "${CMAKE_C_FLAGS_ASAN} -Wno-error=cpp")
  212. set(CMAKE_C_FLAGS_ASANDBG "${CMAKE_C_FLAGS_ASANDBG} -Wno-error=cpp")
  213. endif(UNSAFE_BUILD)
  214. endif(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU)
  215. if (CMAKE_COMPILER_IS_IAR)
  216. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warning_are_errors")
  217. endif(CMAKE_COMPILER_IS_IAR)
  218. endif(MBEDTLS_FATAL_WARNINGS)
  219. if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
  220. if(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG)
  221. set(CMAKE_SHARED_LINKER_FLAGS "--coverage")
  222. endif(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG)
  223. endif(CMAKE_BUILD_TYPE STREQUAL "Coverage")
  224. if(LIB_INSTALL_DIR)
  225. set(CMAKE_INSTALL_LIBDIR "${LIB_INSTALL_DIR}")
  226. endif()
  227. add_subdirectory(include)
  228. add_subdirectory(3rdparty)
  229. add_subdirectory(library)
  230. #
  231. # The C files in tests/src directory contain test code shared among test suites
  232. # and programs. This shared test code is compiled and linked to test suites and
  233. # programs objects as a set of compiled objects. The compiled objects are NOT
  234. # built into a library that the test suite and program objects would link
  235. # against as they link against the mbedcrypto, mbedx509 and mbedtls libraries.
  236. # The reason is that such library is expected to have mutual dependencies with
  237. # the aforementioned libraries and that there is as of today no portable way of
  238. # handling such dependencies (only toolchain specific solutions).
  239. #
  240. # Thus the below definition of the `mbedtls_test` CMake library of objects
  241. # target. This library of objects is used by tests and programs CMake files
  242. # to define the test executables.
  243. #
  244. if(ENABLE_TESTING OR ENABLE_PROGRAMS)
  245. file(GLOB MBEDTLS_TEST_FILES
  246. ${CMAKE_CURRENT_SOURCE_DIR}/tests/src/*.c
  247. ${CMAKE_CURRENT_SOURCE_DIR}/tests/src/drivers/*.c)
  248. add_library(mbedtls_test OBJECT ${MBEDTLS_TEST_FILES})
  249. target_include_directories(mbedtls_test
  250. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tests/include
  251. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
  252. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/library)
  253. file(GLOB MBEDTLS_TEST_HELPER_FILES
  254. ${CMAKE_CURRENT_SOURCE_DIR}/tests/src/test_helpers/*.c)
  255. add_library(mbedtls_test_helpers OBJECT ${MBEDTLS_TEST_HELPER_FILES})
  256. target_include_directories(mbedtls_test_helpers
  257. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tests/include
  258. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include
  259. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/library
  260. PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/everest/include)
  261. endif()
  262. if(ENABLE_PROGRAMS)
  263. add_subdirectory(programs)
  264. endif()
  265. ADD_CUSTOM_TARGET(${MBEDTLS_TARGET_PREFIX}apidoc
  266. COMMAND doxygen mbedtls.doxyfile
  267. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doxygen)
  268. if(ENABLE_TESTING)
  269. enable_testing()
  270. add_subdirectory(tests)
  271. # additional convenience targets for Unix only
  272. if(UNIX)
  273. # For coverage testing:
  274. # 1. Build with:
  275. # cmake -D CMAKE_BUILD_TYPE=Coverage /path/to/source && make
  276. # 2. Run the relevant tests for the part of the code you're interested in.
  277. # For the reference coverage measurement, see
  278. # tests/scripts/basic-build-test.sh
  279. # 3. Run scripts/lcov.sh to generate an HTML report.
  280. ADD_CUSTOM_TARGET(lcov
  281. COMMAND scripts/lcov.sh
  282. )
  283. ADD_CUSTOM_TARGET(memcheck
  284. COMMAND sed -i.bak s+/usr/bin/valgrind+`which valgrind`+ DartConfiguration.tcl
  285. COMMAND ctest -O memcheck.log -D ExperimentalMemCheck
  286. COMMAND tail -n1 memcheck.log | grep 'Memory checking results:' > /dev/null
  287. COMMAND rm -f memcheck.log
  288. COMMAND mv DartConfiguration.tcl.bak DartConfiguration.tcl
  289. )
  290. endif(UNIX)
  291. # Make scripts needed for testing available in an out-of-source build.
  292. if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
  293. link_to_source(scripts)
  294. # Copy (don't link) DartConfiguration.tcl, needed for memcheck, to
  295. # keep things simple with the sed commands in the memcheck target.
  296. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/DartConfiguration.tcl
  297. ${CMAKE_CURRENT_BINARY_DIR}/DartConfiguration.tcl COPYONLY)
  298. endif()
  299. endif()
  300. if(NOT DISABLE_PACKAGE_CONFIG_AND_INSTALL)
  301. configure_package_config_file(
  302. "cmake/MbedTLSConfig.cmake.in"
  303. "cmake/MbedTLSConfig.cmake"
  304. INSTALL_DESTINATION "cmake")
  305. write_basic_package_version_file(
  306. "cmake/MbedTLSConfigVersion.cmake"
  307. COMPATIBILITY SameMajorVersion
  308. VERSION 3.4.0)
  309. install(
  310. FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake/MbedTLSConfig.cmake"
  311. "${CMAKE_CURRENT_BINARY_DIR}/cmake/MbedTLSConfigVersion.cmake"
  312. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/MbedTLS")
  313. export(
  314. EXPORT MbedTLSTargets
  315. NAMESPACE MbedTLS::
  316. FILE "cmake/MbedTLSTargets.cmake")
  317. install(
  318. EXPORT MbedTLSTargets
  319. NAMESPACE MbedTLS::
  320. DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/MbedTLS"
  321. FILE "MbedTLSTargets.cmake")
  322. if(CMAKE_VERSION VERSION_GREATER 3.15 OR CMAKE_VERSION VERSION_EQUAL 3.15)
  323. # Do not export the package by default
  324. cmake_policy(SET CMP0090 NEW)
  325. # Make this package visible to the system
  326. export(PACKAGE MbedTLS)
  327. endif()
  328. endif()