blob: f46639a6e756cdcd267d66404d32afc06dbf9e8e [file] [log] [blame]
Paul Bakker367dae42009-06-28 21:50:27 +00001cmake_minimum_required(VERSION 2.6)
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +00002project(MBEDTLS C)
Paul Bakker367dae42009-06-28 21:50:27 +00003
Barry K. Nathancf975f52014-04-23 17:40:25 -07004string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")
Paul Bakker92bc8752013-12-30 17:56:23 +01005
Paul Bakker2015eac2011-07-27 16:52:28 +00006if(CMAKE_COMPILER_IS_GNUCC)
Manuel Pégourié-Gonnarda64b15e2015-07-19 16:00:04 +02007 # some warnings we want are not available with old GCC versions
8 # note: starting with CMake 2.8 we could use CMAKE_C_COMPILER_VERSION
9 execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
10 OUTPUT_VARIABLE GCC_VERSION)
11 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -W -Wdeclaration-after-statement -Wwrite-strings")
12 if (GCC_VERSION VERSION_GREATER 4.5 OR GCC_VERSION VERSION_EQUAL 4.5)
13 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wlogical-op")
14 endif()
Manuel Pégourié-Gonnarda67fd792015-08-27 12:02:40 +020015 if (GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8)
16 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow")
17 endif()
Manuel Pégourié-Gonnard705b70f2014-11-13 13:35:50 +010018 set(CMAKE_C_FLAGS_RELEASE "-O2")
19 set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
20 set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
21 set(CMAKE_C_FLAGS_ASAN "-Werror -fsanitize=address -fno-common -O3")
22 set(CMAKE_C_FLAGS_ASANDBG "-Werror -fsanitize=address -fno-common -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls ")
23 set(CMAKE_C_FLAGS_CHECK "-Werror -O1")
24 set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual")
Paul Bakker2015eac2011-07-27 16:52:28 +000025endif(CMAKE_COMPILER_IS_GNUCC)
Paul Bakker76f03112013-11-28 17:20:04 +010026
Paul Bakker92bc8752013-12-30 17:56:23 +010027if(CMAKE_COMPILER_IS_CLANG)
Manuel Pégourié-Gonnarda67fd792015-08-27 12:02:40 +020028 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -W -Wdeclaration-after-statement -Wwrite-strings -Wpointer-arith -Wshadow")
Manuel Pégourié-Gonnard705b70f2014-11-13 13:35:50 +010029 set(CMAKE_C_FLAGS_RELEASE "-O2")
30 set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
31 set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
32 set(CMAKE_C_FLAGS_ASAN "-Werror -fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover -O3")
33 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 ")
34 set(CMAKE_C_FLAGS_MEMSAN "-Werror -fsanitize=memory -O3")
35 set(CMAKE_C_FLAGS_MEMSANDBG "-Werror -fsanitize=memory -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2")
36 set(CMAKE_C_FLAGS_CHECK "-Werror -O1")
Paul Bakker92bc8752013-12-30 17:56:23 +010037endif(CMAKE_COMPILER_IS_CLANG)
38
Paul Bakkerad0db972013-12-30 14:09:27 +010039set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}
Manuel Pégourié-Gonnard705b70f2014-11-13 13:35:50 +010040 CACHE STRING "Choose the type of build: None Debug Release Coverage ASan ASanDbg MemSan MemSanDbg Check CheckFull"
Paul Bakkerad0db972013-12-30 14:09:27 +010041 FORCE)
Manuel Pégourié-Gonnard0933d1f2014-01-31 13:16:30 +010042
Paul Bakker396c52f2009-07-11 19:54:40 +000043if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
Paul Bakker2015eac2011-07-27 16:52:28 +000044 if(CMAKE_COMPILER_IS_GNUCC)
Manuel Pégourié-Gonnard0933d1f2014-01-31 13:16:30 +010045 set(CMAKE_SHARED_LINKER_FLAGS "--coverage")
Paul Bakker2015eac2011-07-27 16:52:28 +000046 endif(CMAKE_COMPILER_IS_GNUCC)
Manuel Pégourié-Gonnard0933d1f2014-01-31 13:16:30 +010047 if(CMAKE_COMPILER_IS_CLANG)
48 set(CMAKE_SHARED_LINKER_FLAGS "--coverage")
49 endif(CMAKE_COMPILER_IS_CLANG)
Paul Bakker396c52f2009-07-11 19:54:40 +000050endif(CMAKE_BUILD_TYPE STREQUAL "Coverage")
Paul Bakker367dae42009-06-28 21:50:27 +000051
Manuel Pégourié-Gonnardcfa9a452015-01-23 13:33:31 +000052option(USE_PKCS11_HELPER_LIBRARY "Build mbed TLS with the pkcs11-helper library." OFF)
Paul Bakkerb06819b2011-01-18 16:18:38 +000053
Manuel Pégourié-Gonnardcfa9a452015-01-23 13:33:31 +000054option(ENABLE_ZLIB_SUPPORT "Build mbed TLS with zlib library." OFF)
55option(ENABLE_PROGRAMS "Build mbed TLS programs." ON)
56option(ENABLE_TESTING "Build mbed TLS tests." ON)
Paul Bakker27f1cae2014-04-30 16:31:54 +020057
58if(ENABLE_TESTING)
59 enable_testing()
60endif()
Paul Bakker92eeea42012-07-03 15:10:33 +000061
Paul Bakker091e2872011-07-13 11:45:58 +000062if(LIB_INSTALL_DIR)
63else()
Paul Bakkerb3b49012011-12-11 11:28:52 +000064set(LIB_INSTALL_DIR lib)
Paul Bakker091e2872011-07-13 11:45:58 +000065endif()
66
Paul Bakker367dae42009-06-28 21:50:27 +000067include_directories(include/)
68
Paul Bakker92eeea42012-07-03 15:10:33 +000069if(ENABLE_ZLIB_SUPPORT)
70 find_package(ZLIB)
71
72 if(ZLIB_FOUND)
hasufell7c4a5532014-03-06 15:46:06 +010073 include_directories(${ZLIB_INCLUDE_DIR})
Paul Bakker92eeea42012-07-03 15:10:33 +000074 endif(ZLIB_FOUND)
75endif(ENABLE_ZLIB_SUPPORT)
76
Paul Bakker367dae42009-06-28 21:50:27 +000077add_subdirectory(library)
Paul Bakker547f73d2011-01-05 15:07:54 +000078add_subdirectory(include)
Paul Bakker2015eac2011-07-27 16:52:28 +000079
Paul Bakker27f1cae2014-04-30 16:31:54 +020080if(ENABLE_TESTING)
81 if(CMAKE_COMPILER_IS_GNUCC)
82 add_subdirectory(tests)
83 endif(CMAKE_COMPILER_IS_GNUCC)
84 if(CMAKE_COMPILER_IS_CLANG)
85 add_subdirectory(tests)
86 endif(CMAKE_COMPILER_IS_CLANG)
87endif()
Paul Bakker2015eac2011-07-27 16:52:28 +000088
Paul Bakkerdf5024c2014-03-26 13:27:51 +010089if(ENABLE_PROGRAMS)
90 add_subdirectory(programs)
91endif()
Paul Bakkerccba9bc2011-01-05 15:30:32 +000092
93ADD_CUSTOM_TARGET(apidoc
Manuel Pégourié-Gonnardf234ff82015-01-22 17:01:27 +000094 COMMAND doxygen doxygen/mbedtls.doxyfile
Paul Bakkerccba9bc2011-01-05 15:30:32 +000095 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Manuel Pégourié-Gonnard7669f282013-09-07 16:52:42 +020096
Paul Bakker27f1cae2014-04-30 16:31:54 +020097if(ENABLE_TESTING)
98 ADD_CUSTOM_TARGET(test-ref-config
Manuel Pégourié-Gonnard6529ff02014-01-31 13:41:07 +010099 COMMAND tests/scripts/test-ref-configs.pl
100 )
101
Paul Bakker27f1cae2014-04-30 16:31:54 +0200102 ADD_CUSTOM_TARGET(covtest
Manuel Pégourié-Gonnard61137df2014-02-24 11:57:36 +0100103 COMMAND make test
104 COMMAND programs/test/selftest
105 COMMAND cd tests && ./compat.sh
106 COMMAND cd tests && ./ssl-opt.sh
107 )
108
Paul Bakker27f1cae2014-04-30 16:31:54 +0200109 ADD_CUSTOM_TARGET(lcov
Manuel Pégourié-Gonnard720375e2014-02-24 12:39:18 +0100110 COMMAND rm -rf Coverage
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000111 COMMAND lcov --capture --initial --directory library/CMakeFiles/mbedtls.dir -o files.info
112 COMMAND lcov --capture --directory library/CMakeFiles/mbedtls.dir -o tests.info
Manuel Pégourié-Gonnarde41072e2014-06-16 16:24:24 +0200113 COMMAND lcov --add-tracefile files.info --add-tracefile tests.info -o all.info
114 COMMAND lcov --remove all.info -o final.info '*.h'
Manuel Pégourié-Gonnard720375e2014-02-24 12:39:18 +0100115 COMMAND gendesc tests/Descriptions.txt -o descriptions
Manuel Pégourié-Gonnardafe8f532015-01-23 17:40:33 +0000116 COMMAND genhtml --title "mbed TLS" --description-file descriptions --keep-descriptions --legend --no-branch-coverage -o Coverage final.info
Manuel Pégourié-Gonnarde41072e2014-06-16 16:24:24 +0200117 COMMAND rm -f files.info tests.info all.info final.info descriptions
Manuel Pégourié-Gonnard546d86c2014-01-31 16:19:43 +0100118 )
119
Paul Bakker27f1cae2014-04-30 16:31:54 +0200120 ADD_CUSTOM_TARGET(memcheck
Manuel Pégourié-Gonnardd68b6512015-01-07 14:55:38 +0100121 COMMAND sed -i.bak s+/usr/bin/valgrind+`which valgrind`+ DartConfiguration.tcl
Manuel Pégourié-Gonnarddeb79492013-09-13 13:43:43 +0200122 COMMAND ctest -O memcheck.log -D ExperimentalMemCheck
Manuel Pégourié-Gonnard7669f282013-09-07 16:52:42 +0200123 COMMAND tail -n1 memcheck.log | grep 'Memory checking results:' > /dev/null
124 COMMAND rm -f memcheck.log
Manuel Pégourié-Gonnard3da751e2014-12-15 10:41:53 +0100125 COMMAND mv DartConfiguration.tcl.bak DartConfiguration.tcl
Manuel Pégourié-Gonnard7669f282013-09-07 16:52:42 +0200126 )
Paul Bakker27f1cae2014-04-30 16:31:54 +0200127endif()