CMakeLists.txt 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. cmake_minimum_required(VERSION 2.6)
  2. project("mbed TLS" C)
  3. option(USE_PKCS11_HELPER_LIBRARY "Build mbed TLS with the pkcs11-helper library." OFF)
  4. option(ENABLE_ZLIB_SUPPORT "Build mbed TLS with zlib library." OFF)
  5. option(ENABLE_PROGRAMS "Build mbed TLS programs." ON)
  6. option(UNSAFE_BUILD "Allow unsafe builds. These builds ARE NOT SECURE." OFF)
  7. # the test suites currently have compile errors with MSVC
  8. if(MSVC)
  9. option(ENABLE_TESTING "Build mbed TLS tests." OFF)
  10. else()
  11. option(ENABLE_TESTING "Build mbed TLS tests." ON)
  12. endif()
  13. # Warning string - created as a list for compatibility with CMake 2.8
  14. set(WARNING_BORDER "*******************************************************\n")
  15. set(NULL_ENTROPY_WARN_L1 "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined!\n")
  16. set(NULL_ENTROPY_WARN_L2 "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES\n")
  17. set(NULL_ENTROPY_WARN_L3 "**** AND IS *NOT* SUITABLE FOR PRODUCTION USE\n")
  18. set(NULL_ENTROPY_WARNING "${WARNING_BORDER}"
  19. "${NULL_ENTROPY_WARN_L1}"
  20. "${NULL_ENTROPY_WARN_L2}"
  21. "${NULL_ENTROPY_WARN_L3}"
  22. "${WARNING_BORDER}")
  23. find_package(Perl)
  24. if(PERL_FOUND)
  25. # If NULL Entropy is configured, display an appropriate warning
  26. execute_process(COMMAND ${PERL_EXECUTABLE} ${CMAKE_SOURCE_DIR}/scripts/config.pl -f ${CMAKE_SOURCE_DIR}/include/mbedtls/config.h get MBEDTLS_TEST_NULL_ENTROPY
  27. RESULT_VARIABLE result)
  28. if(${result} EQUAL 0)
  29. message(WARNING ${NULL_ENTROPY_WARNING})
  30. if(NOT UNSAFE_BUILD)
  31. message(FATAL_ERROR "\
  32. \n\
  33. Warning! You have enabled MBEDTLS_TEST_NULL_ENTROPY. \
  34. This option is not safe for production use and negates all security \
  35. It is intended for development use only. \
  36. \n\
  37. To confirm you want to build with this option, re-run cmake with the \
  38. option: \n\
  39. cmake -DUNSAFE_BUILD=ON ")
  40. return()
  41. endif()
  42. endif()
  43. endif()
  44. set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}
  45. CACHE STRING "Choose the type of build: None Debug Release Coverage ASan ASanDbg MemSan MemSanDbg Check CheckFull"
  46. FORCE)
  47. string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")
  48. if(CMAKE_COMPILER_IS_GNUCC)
  49. # some warnings we want are not available with old GCC versions
  50. # note: starting with CMake 2.8 we could use CMAKE_C_COMPILER_VERSION
  51. execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
  52. OUTPUT_VARIABLE GCC_VERSION)
  53. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -W -Wdeclaration-after-statement -Wwrite-strings")
  54. if (GCC_VERSION VERSION_GREATER 4.5 OR GCC_VERSION VERSION_EQUAL 4.5)
  55. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wlogical-op")
  56. endif()
  57. if (GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8)
  58. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
  59. endif()
  60. set(CMAKE_C_FLAGS_RELEASE "-O2")
  61. set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
  62. set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
  63. set(CMAKE_C_FLAGS_ASAN "-Werror -fsanitize=address -fno-common -O3")
  64. set(CMAKE_C_FLAGS_ASANDBG "-Werror -fsanitize=address -fno-common -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls ")
  65. set(CMAKE_C_FLAGS_CHECK "-Werror -Os")
  66. set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual")
  67. endif(CMAKE_COMPILER_IS_GNUCC)
  68. if(CMAKE_COMPILER_IS_CLANG)
  69. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -W -Wdeclaration-after-statement -Wwrite-strings -Wpointer-arith -Wimplicit-fallthrough -Wshadow")
  70. set(CMAKE_C_FLAGS_RELEASE "-O2")
  71. set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
  72. set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
  73. set(CMAKE_C_FLAGS_ASAN "-Werror -fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover -O3")
  74. set(CMAKE_C_FLAGS_ASANDBG "-Werror -fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls ")
  75. set(CMAKE_C_FLAGS_MEMSAN "-Werror -fsanitize=memory -O3")
  76. set(CMAKE_C_FLAGS_MEMSANDBG "-Werror -fsanitize=memory -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2")
  77. set(CMAKE_C_FLAGS_CHECK "-Werror -Os")
  78. endif(CMAKE_COMPILER_IS_CLANG)
  79. if(MSVC)
  80. # Strictest warnings, and treat as errors
  81. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3")
  82. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX")
  83. endif(MSVC)
  84. if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
  85. if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
  86. set(CMAKE_SHARED_LINKER_FLAGS "--coverage")
  87. endif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_CLANG)
  88. endif(CMAKE_BUILD_TYPE STREQUAL "Coverage")
  89. if(LIB_INSTALL_DIR)
  90. else()
  91. set(LIB_INSTALL_DIR lib)
  92. endif()
  93. include_directories(include/)
  94. if(ENABLE_ZLIB_SUPPORT)
  95. find_package(ZLIB)
  96. if(ZLIB_FOUND)
  97. include_directories(${ZLIB_INCLUDE_DIR})
  98. endif(ZLIB_FOUND)
  99. endif(ENABLE_ZLIB_SUPPORT)
  100. add_subdirectory(library)
  101. add_subdirectory(include)
  102. if(ENABLE_PROGRAMS)
  103. add_subdirectory(programs)
  104. endif()
  105. ADD_CUSTOM_TARGET(apidoc
  106. COMMAND doxygen doxygen/mbedtls.doxyfile
  107. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
  108. if(ENABLE_TESTING)
  109. enable_testing()
  110. add_subdirectory(tests)
  111. # additional convenience targets for Unix only
  112. if(UNIX)
  113. ADD_CUSTOM_TARGET(covtest
  114. COMMAND make test
  115. COMMAND programs/test/selftest
  116. COMMAND tests/compat.sh
  117. COMMAND tests/ssl-opt.sh
  118. )
  119. ADD_CUSTOM_TARGET(lcov
  120. COMMAND rm -rf Coverage
  121. COMMAND lcov --capture --initial --directory library/CMakeFiles/mbedtls.dir -o files.info
  122. COMMAND lcov --capture --directory library/CMakeFiles/mbedtls.dir -o tests.info
  123. COMMAND lcov --add-tracefile files.info --add-tracefile tests.info -o all.info
  124. COMMAND lcov --remove all.info -o final.info '*.h'
  125. COMMAND gendesc tests/Descriptions.txt -o descriptions
  126. COMMAND genhtml --title "mbed TLS" --description-file descriptions --keep-descriptions --legend --no-branch-coverage -o Coverage final.info
  127. COMMAND rm -f files.info tests.info all.info final.info descriptions
  128. )
  129. ADD_CUSTOM_TARGET(memcheck
  130. COMMAND sed -i.bak s+/usr/bin/valgrind+`which valgrind`+ DartConfiguration.tcl
  131. COMMAND ctest -O memcheck.log -D ExperimentalMemCheck
  132. COMMAND tail -n1 memcheck.log | grep 'Memory checking results:' > /dev/null
  133. COMMAND rm -f memcheck.log
  134. COMMAND mv DartConfiguration.tcl.bak DartConfiguration.tcl
  135. )
  136. endif(UNIX)
  137. endif()