blob: bd0367d5a894f22f147fa41dbe23e8cdfda01169 [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é-Gonnard705b70f2014-11-13 13:35:50 +010015 set(CMAKE_C_FLAGS_RELEASE "-O2")
16 set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
17 set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
18 set(CMAKE_C_FLAGS_ASAN "-Werror -fsanitize=address -fno-common -O3")
19 set(CMAKE_C_FLAGS_ASANDBG "-Werror -fsanitize=address -fno-common -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls ")
20 set(CMAKE_C_FLAGS_CHECK "-Werror -O1")
21 set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual")
Paul Bakker2015eac2011-07-27 16:52:28 +000022endif(CMAKE_COMPILER_IS_GNUCC)
Paul Bakker76f03112013-11-28 17:20:04 +010023
Paul Bakker92bc8752013-12-30 17:56:23 +010024if(CMAKE_COMPILER_IS_CLANG)
Manuel Pégourié-Gonnard98aa1912014-11-14 16:34:36 +010025 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -W -Wdeclaration-after-statement -Wwrite-strings -Wpointer-arith")
Manuel Pégourié-Gonnard705b70f2014-11-13 13:35:50 +010026 set(CMAKE_C_FLAGS_RELEASE "-O2")
27 set(CMAKE_C_FLAGS_DEBUG "-O0 -g3")
28 set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage")
29 set(CMAKE_C_FLAGS_ASAN "-Werror -fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover -O3")
30 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 ")
31 set(CMAKE_C_FLAGS_MEMSAN "-Werror -fsanitize=memory -O3")
32 set(CMAKE_C_FLAGS_MEMSANDBG "-Werror -fsanitize=memory -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2")
33 set(CMAKE_C_FLAGS_CHECK "-Werror -O1")
Paul Bakker92bc8752013-12-30 17:56:23 +010034endif(CMAKE_COMPILER_IS_CLANG)
35
Paul Bakkerad0db972013-12-30 14:09:27 +010036set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}
Manuel Pégourié-Gonnard705b70f2014-11-13 13:35:50 +010037 CACHE STRING "Choose the type of build: None Debug Release Coverage ASan ASanDbg MemSan MemSanDbg Check CheckFull"
Paul Bakkerad0db972013-12-30 14:09:27 +010038 FORCE)
Manuel Pégourié-Gonnard0933d1f2014-01-31 13:16:30 +010039
Paul Bakker396c52f2009-07-11 19:54:40 +000040if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
Paul Bakker2015eac2011-07-27 16:52:28 +000041 if(CMAKE_COMPILER_IS_GNUCC)
Manuel Pégourié-Gonnard0933d1f2014-01-31 13:16:30 +010042 set(CMAKE_SHARED_LINKER_FLAGS "--coverage")
Paul Bakker2015eac2011-07-27 16:52:28 +000043 endif(CMAKE_COMPILER_IS_GNUCC)
Manuel Pégourié-Gonnard0933d1f2014-01-31 13:16:30 +010044 if(CMAKE_COMPILER_IS_CLANG)
45 set(CMAKE_SHARED_LINKER_FLAGS "--coverage")
46 endif(CMAKE_COMPILER_IS_CLANG)
Paul Bakker396c52f2009-07-11 19:54:40 +000047endif(CMAKE_BUILD_TYPE STREQUAL "Coverage")
Paul Bakker367dae42009-06-28 21:50:27 +000048
Manuel Pégourié-Gonnardcfa9a452015-01-23 13:33:31 +000049option(USE_PKCS11_HELPER_LIBRARY "Build mbed TLS with the pkcs11-helper library." OFF)
Paul Bakkerb06819b2011-01-18 16:18:38 +000050
Manuel Pégourié-Gonnardcfa9a452015-01-23 13:33:31 +000051option(ENABLE_ZLIB_SUPPORT "Build mbed TLS with zlib library." OFF)
52option(ENABLE_PROGRAMS "Build mbed TLS programs." ON)
53option(ENABLE_TESTING "Build mbed TLS tests." ON)
Paul Bakker27f1cae2014-04-30 16:31:54 +020054
55if(ENABLE_TESTING)
56 enable_testing()
57endif()
Paul Bakker92eeea42012-07-03 15:10:33 +000058
Paul Bakker091e2872011-07-13 11:45:58 +000059if(LIB_INSTALL_DIR)
60else()
Paul Bakkerb3b49012011-12-11 11:28:52 +000061set(LIB_INSTALL_DIR lib)
Paul Bakker091e2872011-07-13 11:45:58 +000062endif()
63
Paul Bakker367dae42009-06-28 21:50:27 +000064include_directories(include/)
65
Paul Bakker92eeea42012-07-03 15:10:33 +000066if(ENABLE_ZLIB_SUPPORT)
67 find_package(ZLIB)
68
69 if(ZLIB_FOUND)
hasufell7c4a5532014-03-06 15:46:06 +010070 include_directories(${ZLIB_INCLUDE_DIR})
Paul Bakker92eeea42012-07-03 15:10:33 +000071 endif(ZLIB_FOUND)
72endif(ENABLE_ZLIB_SUPPORT)
73
Paul Bakker367dae42009-06-28 21:50:27 +000074add_subdirectory(library)
Paul Bakker547f73d2011-01-05 15:07:54 +000075add_subdirectory(include)
Paul Bakker2015eac2011-07-27 16:52:28 +000076
Paul Bakker27f1cae2014-04-30 16:31:54 +020077if(ENABLE_TESTING)
78 if(CMAKE_COMPILER_IS_GNUCC)
79 add_subdirectory(tests)
80 endif(CMAKE_COMPILER_IS_GNUCC)
81 if(CMAKE_COMPILER_IS_CLANG)
82 add_subdirectory(tests)
83 endif(CMAKE_COMPILER_IS_CLANG)
84endif()
Paul Bakker2015eac2011-07-27 16:52:28 +000085
Paul Bakkerdf5024c2014-03-26 13:27:51 +010086if(ENABLE_PROGRAMS)
87 add_subdirectory(programs)
88endif()
Paul Bakkerccba9bc2011-01-05 15:30:32 +000089
90ADD_CUSTOM_TARGET(apidoc
Manuel Pégourié-Gonnardf234ff82015-01-22 17:01:27 +000091 COMMAND doxygen doxygen/mbedtls.doxyfile
Paul Bakkerccba9bc2011-01-05 15:30:32 +000092 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
Manuel Pégourié-Gonnard7669f282013-09-07 16:52:42 +020093
Paul Bakker27f1cae2014-04-30 16:31:54 +020094if(ENABLE_TESTING)
95 ADD_CUSTOM_TARGET(test-ref-config
Manuel Pégourié-Gonnard6529ff02014-01-31 13:41:07 +010096 COMMAND tests/scripts/test-ref-configs.pl
97 )
98
Paul Bakker27f1cae2014-04-30 16:31:54 +020099 ADD_CUSTOM_TARGET(covtest
Manuel Pégourié-Gonnard61137df2014-02-24 11:57:36 +0100100 COMMAND make test
101 COMMAND programs/test/selftest
102 COMMAND cd tests && ./compat.sh
103 COMMAND cd tests && ./ssl-opt.sh
104 )
105
Paul Bakker27f1cae2014-04-30 16:31:54 +0200106 ADD_CUSTOM_TARGET(lcov
Manuel Pégourié-Gonnard720375e2014-02-24 12:39:18 +0100107 COMMAND rm -rf Coverage
Manuel Pégourié-Gonnard9014b6f2015-01-27 15:44:46 +0000108 COMMAND lcov --capture --initial --directory library/CMakeFiles/mbedtls.dir -o files.info
109 COMMAND lcov --capture --directory library/CMakeFiles/mbedtls.dir -o tests.info
Manuel Pégourié-Gonnarde41072e2014-06-16 16:24:24 +0200110 COMMAND lcov --add-tracefile files.info --add-tracefile tests.info -o all.info
111 COMMAND lcov --remove all.info -o final.info '*.h'
Manuel Pégourié-Gonnard720375e2014-02-24 12:39:18 +0100112 COMMAND gendesc tests/Descriptions.txt -o descriptions
Manuel Pégourié-Gonnardafe8f532015-01-23 17:40:33 +0000113 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 +0200114 COMMAND rm -f files.info tests.info all.info final.info descriptions
Manuel Pégourié-Gonnard546d86c2014-01-31 16:19:43 +0100115 )
116
Paul Bakker27f1cae2014-04-30 16:31:54 +0200117 ADD_CUSTOM_TARGET(memcheck
Manuel Pégourié-Gonnardd68b6512015-01-07 14:55:38 +0100118 COMMAND sed -i.bak s+/usr/bin/valgrind+`which valgrind`+ DartConfiguration.tcl
Manuel Pégourié-Gonnarddeb79492013-09-13 13:43:43 +0200119 COMMAND ctest -O memcheck.log -D ExperimentalMemCheck
Manuel Pégourié-Gonnard7669f282013-09-07 16:52:42 +0200120 COMMAND tail -n1 memcheck.log | grep 'Memory checking results:' > /dev/null
121 COMMAND rm -f memcheck.log
Manuel Pégourié-Gonnard3da751e2014-12-15 10:41:53 +0100122 COMMAND mv DartConfiguration.tcl.bak DartConfiguration.tcl
Manuel Pégourié-Gonnard7669f282013-09-07 16:52:42 +0200123 )
Paul Bakker27f1cae2014-04-30 16:31:54 +0200124endif()