CMakeLists.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 specifiy the scope of include
  10. # directories. That way, a target linking to a library (using the
  11. # target_link_librairies 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." OFF)
  43. option(UNSAFE_BUILD "Allow unsafe builds. These builds ARE NOT SECURE." OFF)
  44. # Dolphin: werror makes updating compilers painful
  45. option(MBEDTLS_FATAL_WARNINGS "Compiler warnings treated as errors" OFF)
  46. string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")
  47. string(REGEX MATCH "GNU" CMAKE_COMPILER_IS_GNU "${CMAKE_C_COMPILER_ID}")
  48. string(REGEX MATCH "IAR" CMAKE_COMPILER_IS_IAR "${CMAKE_C_COMPILER_ID}")
  49. string(REGEX MATCH "MSVC" CMAKE_COMPILER_IS_MSVC "${CMAKE_C_COMPILER_ID}")
  50. # the test suites currently have compile errors with MSVC
  51. if(CMAKE_COMPILER_IS_MSVC)
  52. option(ENABLE_TESTING "Build mbed TLS tests." OFF)
  53. else()
  54. option(ENABLE_TESTING "Build mbed TLS tests." OFF)
  55. endif()
  56. # Warning string - created as a list for compatibility with CMake 2.8
  57. set(WARNING_BORDER "*******************************************************\n")
  58. set(NULL_ENTROPY_WARN_L1 "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined!\n")
  59. set(NULL_ENTROPY_WARN_L2 "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES\n")
  60. set(NULL_ENTROPY_WARN_L3 "**** AND IS *NOT* SUITABLE FOR PRODUCTION USE\n")
  61. set(NULL_ENTROPY_WARNING "${WARNING_BORDER}"
  62. "${NULL_ENTROPY_WARN_L1}"
  63. "${NULL_ENTROPY_WARN_L2}"
  64. "${NULL_ENTROPY_WARN_L3}"
  65. "${WARNING_BORDER}")
  66. set(CTR_DRBG_128_BIT_KEY_WARN_L1 "**** WARNING! MBEDTLS_CTR_DRBG_USE_128_BIT_KEY defined!\n")
  67. set(CTR_DRBG_128_BIT_KEY_WARN_L2 "**** Using 128-bit keys for CTR_DRBG limits the security of generated\n")
  68. set(CTR_DRBG_128_BIT_KEY_WARN_L3 "**** keys and operations that use random values generated to 128-bit security\n")
  69. set(CTR_DRBG_128_BIT_KEY_WARNING "${WARNING_BORDER}"
  70. "${CTR_DRBG_128_BIT_KEY_WARN_L1}"
  71. "${CTR_DRBG_128_BIT_KEY_WARN_L2}"
  72. "${CTR_DRBG_128_BIT_KEY_WARN_L3}"
  73. "${WARNING_BORDER}")
  74. # Python 3 is only needed here to check for configuration warnings.
  75. if(NOT CMAKE_VERSION VERSION_LESS 3.15.0)
  76. set(Python3_FIND_STRATEGY LOCATION)
  77. find_package(Python3 COMPONENTS Interpreter)
  78. if(Python3_Interpreter_FOUND)
  79. set(MBEDTLS_PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
  80. endif()
  81. else()
  82. find_package(PythonInterp 3)
  83. if(PYTHONINTERP_FOUND)
  84. set(MBEDTLS_PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE})
  85. endif()
  86. endif()
  87. if(MBEDTLS_PYTHON_EXECUTABLE)
  88. # If 128-bit keys are configured for CTR_DRBG, display an appropriate warning
  89. 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
  90. RESULT_VARIABLE result)
  91. if(${result} EQUAL 0)
  92. message(WARNING ${CTR_DRBG_128_BIT_KEY_WARNING})
  93. endif()
  94. # If NULL Entropy is configured, display an appropriate warning
  95. 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
  96. RESULT_VARIABLE result)
  97. if(${result} EQUAL 0)
  98. message(WARNING ${NULL_ENTROPY_WARNING})
  99. if(NOT UNSAFE_BUILD)
  100. message(FATAL_ERROR "\
  101. \n\
  102. Warning! You have enabled MBEDTLS_TEST_NULL_ENTROPY. \
  103. This option is not safe for production use and negates all security \
  104. It is intended for development use only. \
  105. \n\
  106. To confirm you want to build with this option, re-run cmake with the \
  107. option: \n\
  108. cmake -DUNSAFE_BUILD=ON ")
  109. return()
  110. endif()
  111. endif()
  112. endif()
  113. # If this is the root project add longer list of available CMAKE_BUILD_TYPE values
  114. if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
  115. set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}
  116. CACHE STRING "Choose the type of build: None Debug Release Coverage ASan ASanDbg MemSan MemSanDbg Check CheckFull"
  117. FORCE)
  118. endif()
  119. # Create a symbolic link from ${base_name} in the binary directory
  120. # to the corresponding path in the source directory.
  121. function(link_to_source base_name)
  122. # Get OS dependent path to use in `execute_process`
  123. if (CMAKE_HOST_WIN32)
  124. #mklink is an internal command of cmd.exe it can only work with \
  125. string(REPLACE "/" "\\" link "${CMAKE_CURRENT_BINARY_DIR}/${base_name}")
  126. string(REPLACE "/" "\\" target "${CMAKE_CURRENT_SOURCE_DIR}/${base_name}")
  127. else()
  128. set(link "${CMAKE_CURRENT_BINARY_DIR}/${base_name}")
  129. set(target "${CMAKE_CURRENT_SOURCE_DIR}/${base_name}")
  130. endif()
  131. if (NOT EXISTS ${link})
  132. if (CMAKE_HOST_UNIX)
  133. set(command ln -s ${target} ${link})
  134. else()
  135. if (IS_DIRECTORY ${target})
  136. set(command cmd.exe /c mklink /j ${link} ${target})
  137. else()
  138. set(command cmd.exe /c mklink /h ${link} ${target})
  139. endif()
  140. endif()
  141. execute_process(COMMAND ${command}
  142. RESULT_VARIABLE result
  143. ERROR_VARIABLE output)
  144. if (NOT ${result} EQUAL 0)
  145. message(FATAL_ERROR "Could not create symbolic link for: ${target} --> ${output}")
  146. endif()
  147. endif()
  148. endfunction(link_to_source)
  149. string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")
  150. include(CheckCCompilerFlag)
  151. if(CMAKE_COMPILER_IS_GNU)
  152. # some warnings we want are not available with old GCC versions
  153. # note: starting with CMake 2.8 we could use CMAKE_C_COMPILER_VERSION
  154. execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
  155. OUTPUT_VARIABLE GCC_VERSION)
  156. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings")
  157. if (GCC_VERSION VERSION_GREATER 3.0 OR GCC_VERSION VERSION_EQUAL 3.0)
  158. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat=2 -Wno-format-nonliteral")
  159. endif()
  160. if (GCC_VERSION VERSION_GREATER 4.3 OR GCC_VERSION VERSION_EQUAL 4.3)
  161. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wvla")
  162. endif()
  163. if (GCC_VERSION VERSION_GREATER 4.5 OR GCC_VERSION VERSION_EQUAL 4.5)
  164. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wlogical-op")
  165. endif()
  166. if (GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8)
  167. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
  168. endif()
  169. if (GCC_VERSION VERSION_GREATER 5.0)
  170. CHECK_C_COMPILER_FLAG("-Wformat-signedness" C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS)
  171. if(C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS)
  172. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat-signedness")
  173. endif()
  174. endif()
  175. if (GCC_VERSION VERSION_GREATER 7.0 OR GCC_VERSION VERSION_EQUAL 7.0)
  176. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat-overflow=2 -Wformat-truncation")
  177. endif()
  178. set(CMAKE_C_FLAGS_RELEASE "-O2")
  179. set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
  180. set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
  181. set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3")
  182. 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")
  183. set(CMAKE_C_FLAGS_CHECK "-Os")
  184. set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual")
  185. endif(CMAKE_COMPILER_IS_GNU)
  186. if(CMAKE_COMPILER_IS_CLANG)
  187. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings -Wpointer-arith -Wimplicit-fallthrough -Wshadow -Wvla -Wformat=2 -Wno-format-nonliteral")
  188. set(CMAKE_C_FLAGS_RELEASE "-O2")
  189. set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
  190. set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
  191. set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3")
  192. 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")
  193. set(CMAKE_C_FLAGS_MEMSAN "-fsanitize=memory -O3")
  194. set(CMAKE_C_FLAGS_MEMSANDBG "-fsanitize=memory -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2")
  195. set(CMAKE_C_FLAGS_CHECK "-Os")
  196. endif(CMAKE_COMPILER_IS_CLANG)
  197. if(CMAKE_COMPILER_IS_IAR)
  198. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warn_about_c_style_casts --warnings_are_errors -Ohz")
  199. endif(CMAKE_COMPILER_IS_IAR)
  200. if(CMAKE_COMPILER_IS_MSVC)
  201. # Strictest warnings
  202. # Dolphin/MSVC: we want to disable all warnings for externals
  203. #set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3")
  204. endif(CMAKE_COMPILER_IS_MSVC)
  205. if(MBEDTLS_FATAL_WARNINGS)
  206. if(CMAKE_COMPILER_IS_MSVC)
  207. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
  208. endif(CMAKE_COMPILER_IS_MSVC)
  209. if(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU)
  210. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
  211. if(UNSAFE_BUILD)
  212. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=cpp")
  213. set(CMAKE_C_FLAGS_ASAN "${CMAKE_C_FLAGS_ASAN} -Wno-error=cpp")
  214. set(CMAKE_C_FLAGS_ASANDBG "${CMAKE_C_FLAGS_ASANDBG} -Wno-error=cpp")
  215. endif(UNSAFE_BUILD)
  216. endif(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU)
  217. endif(MBEDTLS_FATAL_WARNINGS)
  218. if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
  219. if(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG)
  220. set(CMAKE_SHARED_LINKER_FLAGS "--coverage")
  221. endif(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG)
  222. endif(CMAKE_BUILD_TYPE STREQUAL "Coverage")
  223. if(LIB_INSTALL_DIR)
  224. else()
  225. set(LIB_INSTALL_DIR lib)
  226. endif()
  227. if(ENABLE_ZLIB_SUPPORT)
  228. find_package(ZLIB)
  229. if(ZLIB_FOUND)
  230. include_directories(${ZLIB_INCLUDE_DIR})
  231. endif(ZLIB_FOUND)
  232. endif(ENABLE_ZLIB_SUPPORT)
  233. add_subdirectory(include)
  234. add_subdirectory(3rdparty)
  235. list(APPEND libs ${thirdparty_lib})
  236. add_subdirectory(library)