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