CMakeLists.txt 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. cmake_minimum_required(VERSION 2.6)
  2. if(TEST_CPP)
  3. project("mbed TLS" C CXX)
  4. else()
  5. project("mbed TLS" C)
  6. endif()
  7. option(USE_PKCS11_HELPER_LIBRARY "Build mbed TLS with the pkcs11-helper library." OFF)
  8. option(ENABLE_ZLIB_SUPPORT "Build mbed TLS with zlib library." OFF)
  9. option(ENABLE_PROGRAMS "Build mbed TLS programs." ON)
  10. option(UNSAFE_BUILD "Allow unsafe builds. These builds ARE NOT SECURE." OFF)
  11. string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")
  12. string(REGEX MATCH "GNU" CMAKE_COMPILER_IS_GNU "${CMAKE_C_COMPILER_ID}")
  13. string(REGEX MATCH "IAR" CMAKE_COMPILER_IS_IAR "${CMAKE_C_COMPILER_ID}")
  14. string(REGEX MATCH "MSVC" CMAKE_COMPILER_IS_MSVC "${CMAKE_C_COMPILER_ID}")
  15. # the test suites currently have compile errors with MSVC
  16. if(CMAKE_COMPILER_IS_MSVC)
  17. option(ENABLE_TESTING "Build mbed TLS tests." OFF)
  18. else()
  19. option(ENABLE_TESTING "Build mbed TLS tests." ON)
  20. endif()
  21. # Warning string - created as a list for compatibility with CMake 2.8
  22. set(WARNING_BORDER "*******************************************************\n")
  23. set(NULL_ENTROPY_WARN_L1 "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined!\n")
  24. set(NULL_ENTROPY_WARN_L2 "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES\n")
  25. set(NULL_ENTROPY_WARN_L3 "**** AND IS *NOT* SUITABLE FOR PRODUCTION USE\n")
  26. set(NULL_ENTROPY_WARNING "${WARNING_BORDER}"
  27. "${NULL_ENTROPY_WARN_L1}"
  28. "${NULL_ENTROPY_WARN_L2}"
  29. "${NULL_ENTROPY_WARN_L3}"
  30. "${WARNING_BORDER}")
  31. set(CTR_DRBG_128_BIT_KEY_WARN_L1 "**** WARNING! MBEDTLS_CTR_DRBG_USE_128_BIT_KEY defined!\n")
  32. set(CTR_DRBG_128_BIT_KEY_WARN_L2 "**** Using 128-bit keys for CTR_DRBG limits the security of generated\n")
  33. set(CTR_DRBG_128_BIT_KEY_WARN_L3 "**** keys and operations that use random values generated to 128-bit security\n")
  34. set(CTR_DRBG_128_BIT_KEY_WARNING "${WARNING_BORDER}"
  35. "${CTR_DRBG_128_BIT_KEY_WARN_L1}"
  36. "${CTR_DRBG_128_BIT_KEY_WARN_L2}"
  37. "${CTR_DRBG_128_BIT_KEY_WARN_L3}"
  38. "${WARNING_BORDER}")
  39. find_package(PythonInterp)
  40. find_package(Perl)
  41. if(PERL_FOUND)
  42. # If 128-bit keys are configured for CTR_DRBG, display an appropriate warning
  43. execute_process(COMMAND ${PERL_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/config.pl -f ${CMAKE_CURRENT_SOURCE_DIR}/include/mbedtls/config.h get MBEDTLS_CTR_DRBG_USE_128_BIT_KEY
  44. RESULT_VARIABLE result)
  45. if(${result} EQUAL 0)
  46. message(WARNING ${CTR_DRBG_128_BIT_KEY_WARNING})
  47. endif()
  48. # If NULL Entropy is configured, display an appropriate warning
  49. execute_process(COMMAND ${PERL_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/config.pl -f ${CMAKE_CURRENT_SOURCE_DIR}/include/mbedtls/config.h get MBEDTLS_TEST_NULL_ENTROPY
  50. RESULT_VARIABLE result)
  51. if(${result} EQUAL 0)
  52. message(WARNING ${NULL_ENTROPY_WARNING})
  53. if(NOT UNSAFE_BUILD)
  54. message(FATAL_ERROR "\
  55. \n\
  56. Warning! You have enabled MBEDTLS_TEST_NULL_ENTROPY. \
  57. This option is not safe for production use and negates all security \
  58. It is intended for development use only. \
  59. \n\
  60. To confirm you want to build with this option, re-run cmake with the \
  61. option: \n\
  62. cmake -DUNSAFE_BUILD=ON ")
  63. return()
  64. endif()
  65. endif()
  66. endif()
  67. set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}
  68. CACHE STRING "Choose the type of build: None Debug Release Coverage ASan ASanDbg MemSan MemSanDbg Check CheckFull"
  69. FORCE)
  70. # Create a symbolic link from ${base_name} in the binary directory
  71. # to the corresponding path in the source directory.
  72. function(link_to_source base_name)
  73. # Get OS dependent path to use in `execute_process`
  74. if (CMAKE_HOST_WIN32)
  75. #mklink is an internal command of cmd.exe it can only work with \
  76. string(REPLACE "/" "\\" link "${CMAKE_CURRENT_BINARY_DIR}/${base_name}")
  77. string(REPLACE "/" "\\" target "${CMAKE_CURRENT_SOURCE_DIR}/${base_name}")
  78. else()
  79. set(link "${CMAKE_CURRENT_BINARY_DIR}/${base_name}")
  80. set(target "${CMAKE_CURRENT_SOURCE_DIR}/${base_name}")
  81. endif()
  82. if (NOT EXISTS ${link})
  83. if (CMAKE_HOST_UNIX)
  84. set(command ln -s ${target} ${link})
  85. else()
  86. if (IS_DIRECTORY ${target})
  87. set(command cmd.exe /c mklink /j ${link} ${target})
  88. else()
  89. set(command cmd.exe /c mklink /h ${link} ${target})
  90. endif()
  91. endif()
  92. execute_process(COMMAND ${command}
  93. RESULT_VARIABLE result
  94. ERROR_VARIABLE output)
  95. if (NOT ${result} EQUAL 0)
  96. message(FATAL_ERROR "Could not create symbolic link for: ${target} --> ${output}")
  97. endif()
  98. endif()
  99. endfunction(link_to_source)
  100. string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")
  101. if(CMAKE_COMPILER_IS_GNU)
  102. # some warnings we want are not available with old GCC versions
  103. # note: starting with CMake 2.8 we could use CMAKE_C_COMPILER_VERSION
  104. execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
  105. OUTPUT_VARIABLE GCC_VERSION)
  106. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -W -Wdeclaration-after-statement -Wwrite-strings")
  107. if (GCC_VERSION VERSION_GREATER 4.5 OR GCC_VERSION VERSION_EQUAL 4.5)
  108. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wlogical-op")
  109. endif()
  110. if (GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8)
  111. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
  112. endif()
  113. set(CMAKE_C_FLAGS_RELEASE "-O2")
  114. set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
  115. set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
  116. set(CMAKE_C_FLAGS_ASAN "-Werror -fsanitize=address -fno-common -O3")
  117. set(CMAKE_C_FLAGS_ASANDBG "-Werror -fsanitize=address -fno-common -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls ")
  118. set(CMAKE_C_FLAGS_CHECK "-Werror -Os")
  119. set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual")
  120. endif(CMAKE_COMPILER_IS_GNU)
  121. if(CMAKE_COMPILER_IS_CLANG)
  122. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -W -Wdeclaration-after-statement -Wwrite-strings -Wpointer-arith -Wimplicit-fallthrough -Wshadow")
  123. set(CMAKE_C_FLAGS_RELEASE "-O2")
  124. set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
  125. set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
  126. set(CMAKE_C_FLAGS_ASAN "-Werror -fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3")
  127. set(CMAKE_C_FLAGS_ASANDBG "-Werror -fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls ")
  128. set(CMAKE_C_FLAGS_MEMSAN "-Werror -fsanitize=memory -O3")
  129. set(CMAKE_C_FLAGS_MEMSANDBG "-Werror -fsanitize=memory -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2")
  130. set(CMAKE_C_FLAGS_CHECK "-Werror -Os")
  131. endif(CMAKE_COMPILER_IS_CLANG)
  132. if(CMAKE_COMPILER_IS_IAR)
  133. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warn_about_c_style_casts --warnings_are_errors -Ohz")
  134. endif(CMAKE_COMPILER_IS_IAR)
  135. if(CMAKE_COMPILER_IS_MSVC)
  136. # Strictest warnings, and treat as errors
  137. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3")
  138. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
  139. endif(CMAKE_COMPILER_IS_MSVC)
  140. if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
  141. if(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG)
  142. set(CMAKE_SHARED_LINKER_FLAGS "--coverage")
  143. endif(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG)
  144. endif(CMAKE_BUILD_TYPE STREQUAL "Coverage")
  145. if(LIB_INSTALL_DIR)
  146. else()
  147. set(LIB_INSTALL_DIR lib)
  148. endif()
  149. include_directories(include/)
  150. if(ENABLE_ZLIB_SUPPORT)
  151. find_package(ZLIB)
  152. if(ZLIB_FOUND)
  153. include_directories(${ZLIB_INCLUDE_DIR})
  154. endif(ZLIB_FOUND)
  155. endif(ENABLE_ZLIB_SUPPORT)
  156. add_subdirectory(library)
  157. add_subdirectory(include)
  158. if(ENABLE_PROGRAMS)
  159. add_subdirectory(programs)
  160. endif()
  161. ADD_CUSTOM_TARGET(apidoc
  162. COMMAND doxygen mbedtls.doxyfile
  163. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doxygen)
  164. if(ENABLE_TESTING)
  165. enable_testing()
  166. add_subdirectory(tests)
  167. # additional convenience targets for Unix only
  168. if(UNIX)
  169. ADD_CUSTOM_TARGET(covtest
  170. COMMAND make test
  171. COMMAND programs/test/selftest
  172. COMMAND tests/compat.sh
  173. COMMAND tests/ssl-opt.sh
  174. )
  175. ADD_CUSTOM_TARGET(lcov
  176. COMMAND rm -rf Coverage
  177. COMMAND lcov --capture --initial --directory library/CMakeFiles/mbedtls.dir -o files.info
  178. COMMAND lcov --capture --directory library/CMakeFiles/mbedtls.dir -o tests.info
  179. COMMAND lcov --add-tracefile files.info --add-tracefile tests.info -o all.info
  180. COMMAND lcov --remove all.info -o final.info '*.h'
  181. COMMAND gendesc tests/Descriptions.txt -o descriptions
  182. COMMAND genhtml --title "mbed TLS" --description-file descriptions --keep-descriptions --legend --no-branch-coverage -o Coverage final.info
  183. COMMAND rm -f files.info tests.info all.info final.info descriptions
  184. )
  185. ADD_CUSTOM_TARGET(memcheck
  186. COMMAND sed -i.bak s+/usr/bin/valgrind+`which valgrind`+ DartConfiguration.tcl
  187. COMMAND ctest -O memcheck.log -D ExperimentalMemCheck
  188. COMMAND tail -n1 memcheck.log | grep 'Memory checking results:' > /dev/null
  189. COMMAND rm -f memcheck.log
  190. COMMAND mv DartConfiguration.tcl.bak DartConfiguration.tcl
  191. )
  192. endif(UNIX)
  193. # Make scripts needed for testing available in an out-of-source build.
  194. if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
  195. link_to_source(scripts)
  196. # Copy (don't link) DartConfiguration.tcl, needed for memcheck, to
  197. # keep things simple with the sed commands in the memcheck target.
  198. configure_file(${CMAKE_CURRENT_SOURCE_DIR}/DartConfiguration.tcl
  199. ${CMAKE_CURRENT_BINARY_DIR}/DartConfiguration.tcl COPYONLY)
  200. endif()
  201. endif()