1a8e1175bSopenharmony_ci# 2a8e1175bSopenharmony_ci# CMake build system design considerations: 3a8e1175bSopenharmony_ci# 4a8e1175bSopenharmony_ci# - Include directories: 5a8e1175bSopenharmony_ci# + Do not define include directories globally using the include_directories 6a8e1175bSopenharmony_ci# command but rather at the target level using the 7a8e1175bSopenharmony_ci# target_include_directories command. That way, it is easier to guarantee 8a8e1175bSopenharmony_ci# that targets are built using the proper list of include directories. 9a8e1175bSopenharmony_ci# + Use the PUBLIC and PRIVATE keywords to specify the scope of include 10a8e1175bSopenharmony_ci# directories. That way, a target linking to a library (using the 11a8e1175bSopenharmony_ci# target_link_libraries command) inherits from the library PUBLIC include 12a8e1175bSopenharmony_ci# directories and not from the PRIVATE ones. 13a8e1175bSopenharmony_ci# - MBEDTLS_TARGET_PREFIX: CMake targets are designed to be alterable by calling 14a8e1175bSopenharmony_ci# CMake in order to avoid target name clashes, via the use of 15a8e1175bSopenharmony_ci# MBEDTLS_TARGET_PREFIX. The value of this variable is prefixed to the 16a8e1175bSopenharmony_ci# mbedtls, mbedx509, mbedcrypto and apidoc targets. 17a8e1175bSopenharmony_ci# 18a8e1175bSopenharmony_ci 19a8e1175bSopenharmony_ci# We specify a minimum requirement of 3.10.2, but for now use 3.5.1 here 20a8e1175bSopenharmony_ci# until our infrastructure catches up. 21a8e1175bSopenharmony_cicmake_minimum_required(VERSION 3.5.1) 22a8e1175bSopenharmony_ci 23a8e1175bSopenharmony_ciinclude(CMakePackageConfigHelpers) 24a8e1175bSopenharmony_ci 25a8e1175bSopenharmony_ci# https://cmake.org/cmake/help/latest/policy/CMP0011.html 26a8e1175bSopenharmony_ci# Setting this policy is required in CMake >= 3.18.0, otherwise a warning is generated. The OLD 27a8e1175bSopenharmony_ci# policy setting is deprecated, and will be removed in future versions. 28a8e1175bSopenharmony_cicmake_policy(SET CMP0011 NEW) 29a8e1175bSopenharmony_ci# https://cmake.org/cmake/help/latest/policy/CMP0012.html 30a8e1175bSopenharmony_ci# Setting the CMP0012 policy to NEW is required for FindPython3 to work with CMake 3.18.2 31a8e1175bSopenharmony_ci# (there is a bug in this particular version), otherwise, setting the CMP0012 policy is required 32a8e1175bSopenharmony_ci# for CMake versions >= 3.18.3 otherwise a deprecated warning is generated. The OLD policy setting 33a8e1175bSopenharmony_ci# is deprecated and will be removed in future versions. 34a8e1175bSopenharmony_cicmake_policy(SET CMP0012 NEW) 35a8e1175bSopenharmony_ci 36a8e1175bSopenharmony_ciif(TEST_CPP) 37a8e1175bSopenharmony_ci project("Mbed TLS" 38a8e1175bSopenharmony_ci LANGUAGES C CXX 39a8e1175bSopenharmony_ci VERSION 3.6.0 40a8e1175bSopenharmony_ci ) 41a8e1175bSopenharmony_cielse() 42a8e1175bSopenharmony_ci project("Mbed TLS" 43a8e1175bSopenharmony_ci LANGUAGES C 44a8e1175bSopenharmony_ci VERSION 3.6.0 45a8e1175bSopenharmony_ci ) 46a8e1175bSopenharmony_ciendif() 47a8e1175bSopenharmony_ci 48a8e1175bSopenharmony_ciinclude(GNUInstallDirs) 49a8e1175bSopenharmony_ci 50a8e1175bSopenharmony_ci# Determine if Mbed TLS is being built as a subproject using add_subdirectory() 51a8e1175bSopenharmony_ciif(NOT DEFINED MBEDTLS_AS_SUBPROJECT) 52a8e1175bSopenharmony_ci set(MBEDTLS_AS_SUBPROJECT ON) 53a8e1175bSopenharmony_ci if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) 54a8e1175bSopenharmony_ci set(MBEDTLS_AS_SUBPROJECT OFF) 55a8e1175bSopenharmony_ci endif() 56a8e1175bSopenharmony_ciendif() 57a8e1175bSopenharmony_ci 58a8e1175bSopenharmony_ci# Set the project root directory. 59a8e1175bSopenharmony_ciset(MBEDTLS_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 60a8e1175bSopenharmony_ci 61a8e1175bSopenharmony_cioption(ENABLE_PROGRAMS "Build Mbed TLS programs." ON) 62a8e1175bSopenharmony_ci 63a8e1175bSopenharmony_cioption(UNSAFE_BUILD "Allow unsafe builds. These builds ARE NOT SECURE." OFF) 64a8e1175bSopenharmony_cioption(MBEDTLS_FATAL_WARNINGS "Compiler warnings treated as errors" ON) 65a8e1175bSopenharmony_ciif(CMAKE_HOST_WIN32) 66a8e1175bSopenharmony_ci # N.B. The comment on the next line is significant! If you change it, 67a8e1175bSopenharmony_ci # edit the sed command in prepare_release.sh that modifies 68a8e1175bSopenharmony_ci # CMakeLists.txt. 69a8e1175bSopenharmony_ci option(GEN_FILES "Generate the auto-generated files as needed" OFF) # off in development 70a8e1175bSopenharmony_cielse() 71a8e1175bSopenharmony_ci option(GEN_FILES "Generate the auto-generated files as needed" OFF) 72a8e1175bSopenharmony_ciendif() 73a8e1175bSopenharmony_ci 74a8e1175bSopenharmony_cioption(DISABLE_PACKAGE_CONFIG_AND_INSTALL "Disable package configuration, target export and installation" ${MBEDTLS_AS_SUBPROJECT}) 75a8e1175bSopenharmony_ci 76a8e1175bSopenharmony_cistring(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}") 77a8e1175bSopenharmony_cistring(REGEX MATCH "GNU" CMAKE_COMPILER_IS_GNU "${CMAKE_C_COMPILER_ID}") 78a8e1175bSopenharmony_cistring(REGEX MATCH "IAR" CMAKE_COMPILER_IS_IAR "${CMAKE_C_COMPILER_ID}") 79a8e1175bSopenharmony_cistring(REGEX MATCH "MSVC" CMAKE_COMPILER_IS_MSVC "${CMAKE_C_COMPILER_ID}") 80a8e1175bSopenharmony_ci 81a8e1175bSopenharmony_ci# the test suites currently have compile errors with MSVC 82a8e1175bSopenharmony_ciif(CMAKE_COMPILER_IS_MSVC) 83a8e1175bSopenharmony_ci option(ENABLE_TESTING "Build Mbed TLS tests." OFF) 84a8e1175bSopenharmony_cielse() 85a8e1175bSopenharmony_ci option(ENABLE_TESTING "Build Mbed TLS tests." ON) 86a8e1175bSopenharmony_ciendif() 87a8e1175bSopenharmony_ci 88a8e1175bSopenharmony_ci# Warning string - created as a list for compatibility with CMake 2.8 89a8e1175bSopenharmony_ciset(CTR_DRBG_128_BIT_KEY_WARN_L1 "**** WARNING! MBEDTLS_CTR_DRBG_USE_128_BIT_KEY defined!\n") 90a8e1175bSopenharmony_ciset(CTR_DRBG_128_BIT_KEY_WARN_L2 "**** Using 128-bit keys for CTR_DRBG limits the security of generated\n") 91a8e1175bSopenharmony_ciset(CTR_DRBG_128_BIT_KEY_WARN_L3 "**** keys and operations that use random values generated to 128-bit security\n") 92a8e1175bSopenharmony_ci 93a8e1175bSopenharmony_ciset(CTR_DRBG_128_BIT_KEY_WARNING "${WARNING_BORDER}" 94a8e1175bSopenharmony_ci "${CTR_DRBG_128_BIT_KEY_WARN_L1}" 95a8e1175bSopenharmony_ci "${CTR_DRBG_128_BIT_KEY_WARN_L2}" 96a8e1175bSopenharmony_ci "${CTR_DRBG_128_BIT_KEY_WARN_L3}" 97a8e1175bSopenharmony_ci "${WARNING_BORDER}") 98a8e1175bSopenharmony_ci 99a8e1175bSopenharmony_ci# Python 3 is only needed here to check for configuration warnings. 100a8e1175bSopenharmony_ciif(NOT CMAKE_VERSION VERSION_LESS 3.15.0) 101a8e1175bSopenharmony_ci set(Python3_FIND_STRATEGY LOCATION) 102a8e1175bSopenharmony_ci find_package(Python3 COMPONENTS Interpreter) 103a8e1175bSopenharmony_ci if(Python3_Interpreter_FOUND) 104a8e1175bSopenharmony_ci set(MBEDTLS_PYTHON_EXECUTABLE ${Python3_EXECUTABLE}) 105a8e1175bSopenharmony_ci endif() 106a8e1175bSopenharmony_cielse() 107a8e1175bSopenharmony_ci find_package(PythonInterp 3) 108a8e1175bSopenharmony_ci if(PYTHONINTERP_FOUND) 109a8e1175bSopenharmony_ci set(MBEDTLS_PYTHON_EXECUTABLE ${PYTHON_EXECUTABLE}) 110a8e1175bSopenharmony_ci endif() 111a8e1175bSopenharmony_ciendif() 112a8e1175bSopenharmony_ciif(MBEDTLS_PYTHON_EXECUTABLE) 113a8e1175bSopenharmony_ci 114a8e1175bSopenharmony_ci # If 128-bit keys are configured for CTR_DRBG, display an appropriate warning 115a8e1175bSopenharmony_ci execute_process(COMMAND ${MBEDTLS_PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/config.py -f ${CMAKE_CURRENT_SOURCE_DIR}/include/mbedtls/mbedtls_config.h get MBEDTLS_CTR_DRBG_USE_128_BIT_KEY 116a8e1175bSopenharmony_ci RESULT_VARIABLE result) 117a8e1175bSopenharmony_ci if(${result} EQUAL 0) 118a8e1175bSopenharmony_ci message(WARNING ${CTR_DRBG_128_BIT_KEY_WARNING}) 119a8e1175bSopenharmony_ci endif() 120a8e1175bSopenharmony_ci 121a8e1175bSopenharmony_ciendif() 122a8e1175bSopenharmony_ci 123a8e1175bSopenharmony_ci# We now potentially need to link all executables against PThreads, if available 124a8e1175bSopenharmony_ciset(CMAKE_THREAD_PREFER_PTHREAD TRUE) 125a8e1175bSopenharmony_ciset(THREADS_PREFER_PTHREAD_FLAG TRUE) 126a8e1175bSopenharmony_cifind_package(Threads) 127a8e1175bSopenharmony_ci 128a8e1175bSopenharmony_ci# If this is the root project add longer list of available CMAKE_BUILD_TYPE values 129a8e1175bSopenharmony_ciif(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) 130a8e1175bSopenharmony_ci set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} 131a8e1175bSopenharmony_ci CACHE STRING "Choose the type of build: None Debug Release Coverage ASan ASanDbg MemSan MemSanDbg Check CheckFull TSan TSanDbg" 132a8e1175bSopenharmony_ci FORCE) 133a8e1175bSopenharmony_ciendif() 134a8e1175bSopenharmony_ci 135a8e1175bSopenharmony_ci# Make MBEDTLS_CONFIG_FILE and MBEDTLS_USER_CONFIG_FILE into PATHs 136a8e1175bSopenharmony_ciset(MBEDTLS_CONFIG_FILE "" CACHE FILEPATH "Mbed TLS config file (overrides default).") 137a8e1175bSopenharmony_ciset(MBEDTLS_USER_CONFIG_FILE "" CACHE FILEPATH "Mbed TLS user config file (appended to default).") 138a8e1175bSopenharmony_ci 139a8e1175bSopenharmony_ci# Create a symbolic link from ${base_name} in the binary directory 140a8e1175bSopenharmony_ci# to the corresponding path in the source directory. 141a8e1175bSopenharmony_ci# Note: Copies the file(s) on Windows. 142a8e1175bSopenharmony_cifunction(link_to_source base_name) 143a8e1175bSopenharmony_ci set(link "${CMAKE_CURRENT_BINARY_DIR}/${base_name}") 144a8e1175bSopenharmony_ci set(target "${CMAKE_CURRENT_SOURCE_DIR}/${base_name}") 145a8e1175bSopenharmony_ci 146a8e1175bSopenharmony_ci # Linking to non-existent file is not desirable. At best you will have a 147a8e1175bSopenharmony_ci # dangling link, but when building in tree, this can create a symbolic link 148a8e1175bSopenharmony_ci # to itself. 149a8e1175bSopenharmony_ci if (EXISTS ${target} AND NOT EXISTS ${link}) 150a8e1175bSopenharmony_ci if (CMAKE_HOST_UNIX) 151a8e1175bSopenharmony_ci execute_process(COMMAND ln -s ${target} ${link} 152a8e1175bSopenharmony_ci RESULT_VARIABLE result 153a8e1175bSopenharmony_ci ERROR_VARIABLE output) 154a8e1175bSopenharmony_ci 155a8e1175bSopenharmony_ci if (NOT ${result} EQUAL 0) 156a8e1175bSopenharmony_ci message(FATAL_ERROR "Could not create symbolic link for: ${target} --> ${output}") 157a8e1175bSopenharmony_ci endif() 158a8e1175bSopenharmony_ci else() 159a8e1175bSopenharmony_ci if (IS_DIRECTORY ${target}) 160a8e1175bSopenharmony_ci file(GLOB_RECURSE files FOLLOW_SYMLINKS LIST_DIRECTORIES false RELATIVE ${target} "${target}/*") 161a8e1175bSopenharmony_ci foreach(file IN LISTS files) 162a8e1175bSopenharmony_ci configure_file("${target}/${file}" "${link}/${file}" COPYONLY) 163a8e1175bSopenharmony_ci endforeach(file) 164a8e1175bSopenharmony_ci else() 165a8e1175bSopenharmony_ci configure_file(${target} ${link} COPYONLY) 166a8e1175bSopenharmony_ci endif() 167a8e1175bSopenharmony_ci endif() 168a8e1175bSopenharmony_ci endif() 169a8e1175bSopenharmony_ciendfunction(link_to_source) 170a8e1175bSopenharmony_ci 171a8e1175bSopenharmony_ci# Get the filename without the final extension (i.e. convert "a.b.c" to "a.b") 172a8e1175bSopenharmony_cifunction(get_name_without_last_ext dest_var full_name) 173a8e1175bSopenharmony_ci # Split into a list on '.' (but a cmake list is just a ';'-separated string) 174a8e1175bSopenharmony_ci string(REPLACE "." ";" ext_parts "${full_name}") 175a8e1175bSopenharmony_ci # Remove the last item if there are more than one 176a8e1175bSopenharmony_ci list(LENGTH ext_parts ext_parts_len) 177a8e1175bSopenharmony_ci if (${ext_parts_len} GREATER "1") 178a8e1175bSopenharmony_ci math(EXPR ext_parts_last_item "${ext_parts_len} - 1") 179a8e1175bSopenharmony_ci list(REMOVE_AT ext_parts ${ext_parts_last_item}) 180a8e1175bSopenharmony_ci endif() 181a8e1175bSopenharmony_ci # Convert back to a string by replacing separators with '.' 182a8e1175bSopenharmony_ci string(REPLACE ";" "." no_ext_name "${ext_parts}") 183a8e1175bSopenharmony_ci # Copy into the desired variable 184a8e1175bSopenharmony_ci set(${dest_var} ${no_ext_name} PARENT_SCOPE) 185a8e1175bSopenharmony_ciendfunction(get_name_without_last_ext) 186a8e1175bSopenharmony_ci 187a8e1175bSopenharmony_cistring(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}") 188a8e1175bSopenharmony_ci 189a8e1175bSopenharmony_ciinclude(CheckCCompilerFlag) 190a8e1175bSopenharmony_ci 191a8e1175bSopenharmony_ciset(CMAKE_C_EXTENSIONS OFF) 192a8e1175bSopenharmony_ciset(CMAKE_C_STANDARD 99) 193a8e1175bSopenharmony_ci 194a8e1175bSopenharmony_ciif(CMAKE_COMPILER_IS_GNU) 195a8e1175bSopenharmony_ci # some warnings we want are not available with old GCC versions 196a8e1175bSopenharmony_ci # note: starting with CMake 2.8 we could use CMAKE_C_COMPILER_VERSION 197a8e1175bSopenharmony_ci execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion 198a8e1175bSopenharmony_ci OUTPUT_VARIABLE GCC_VERSION) 199a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings") 200a8e1175bSopenharmony_ci if (GCC_VERSION VERSION_GREATER 3.0 OR GCC_VERSION VERSION_EQUAL 3.0) 201a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat=2 -Wno-format-nonliteral") 202a8e1175bSopenharmony_ci endif() 203a8e1175bSopenharmony_ci if (GCC_VERSION VERSION_GREATER 4.3 OR GCC_VERSION VERSION_EQUAL 4.3) 204a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wvla") 205a8e1175bSopenharmony_ci endif() 206a8e1175bSopenharmony_ci if (GCC_VERSION VERSION_GREATER 4.5 OR GCC_VERSION VERSION_EQUAL 4.5) 207a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wlogical-op") 208a8e1175bSopenharmony_ci endif() 209a8e1175bSopenharmony_ci if (GCC_VERSION VERSION_GREATER 4.8 OR GCC_VERSION VERSION_EQUAL 4.8) 210a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wshadow") 211a8e1175bSopenharmony_ci endif() 212a8e1175bSopenharmony_ci if (GCC_VERSION VERSION_GREATER 5.0) 213a8e1175bSopenharmony_ci CHECK_C_COMPILER_FLAG("-Wformat-signedness" C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS) 214a8e1175bSopenharmony_ci if(C_COMPILER_SUPPORTS_WFORMAT_SIGNEDNESS) 215a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat-signedness") 216a8e1175bSopenharmony_ci endif() 217a8e1175bSopenharmony_ci endif() 218a8e1175bSopenharmony_ci if (GCC_VERSION VERSION_GREATER 7.0 OR GCC_VERSION VERSION_EQUAL 7.0) 219a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wformat-overflow=2 -Wformat-truncation") 220a8e1175bSopenharmony_ci endif() 221a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_RELEASE "-O2") 222a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_DEBUG "-O0 -g3") 223a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage") 224a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3") 225a8e1175bSopenharmony_ci 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") 226a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_TSAN "-fsanitize=thread -O3") 227a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_TSANDBG "-fsanitize=thread -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls") 228a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_CHECK "-Os") 229a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_CHECKFULL "${CMAKE_C_FLAGS_CHECK} -Wcast-qual") 230a8e1175bSopenharmony_ciendif(CMAKE_COMPILER_IS_GNU) 231a8e1175bSopenharmony_ci 232a8e1175bSopenharmony_ciif(CMAKE_COMPILER_IS_CLANG) 233a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wwrite-strings -Wpointer-arith -Wimplicit-fallthrough -Wshadow -Wvla -Wformat=2 -Wno-format-nonliteral") 234a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_RELEASE "-O2") 235a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_DEBUG "-O0 -g3") 236a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_COVERAGE "-O0 -g3 --coverage") 237a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_ASAN "-fsanitize=address -fno-common -fsanitize=undefined -fno-sanitize-recover=all -O3") 238a8e1175bSopenharmony_ci 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") 239a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_MEMSAN "-fsanitize=memory -O3") 240a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_MEMSANDBG "-fsanitize=memory -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize-memory-track-origins=2") 241a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_TSAN "-fsanitize=thread -O3") 242a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_TSANDBG "-fsanitize=thread -O1 -g3 -fno-omit-frame-pointer -fno-optimize-sibling-calls") 243a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_CHECK "-Os") 244a8e1175bSopenharmony_ciendif(CMAKE_COMPILER_IS_CLANG) 245a8e1175bSopenharmony_ci 246a8e1175bSopenharmony_ciif(CMAKE_COMPILER_IS_IAR) 247a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warn_about_c_style_casts") 248a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_RELEASE "-Ohz") 249a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_DEBUG "--debug -On") 250a8e1175bSopenharmony_ciendif(CMAKE_COMPILER_IS_IAR) 251a8e1175bSopenharmony_ci 252a8e1175bSopenharmony_ciif(CMAKE_COMPILER_IS_MSVC) 253a8e1175bSopenharmony_ci # Strictest warnings, UTF-8 source and execution charset 254a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /W3 /utf-8") 255a8e1175bSopenharmony_ciendif(CMAKE_COMPILER_IS_MSVC) 256a8e1175bSopenharmony_ci 257a8e1175bSopenharmony_ciif(MBEDTLS_FATAL_WARNINGS) 258a8e1175bSopenharmony_ci if(CMAKE_COMPILER_IS_MSVC) 259a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /WX") 260a8e1175bSopenharmony_ci endif(CMAKE_COMPILER_IS_MSVC) 261a8e1175bSopenharmony_ci 262a8e1175bSopenharmony_ci if(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU) 263a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror") 264a8e1175bSopenharmony_ci if(UNSAFE_BUILD) 265a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=cpp") 266a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_ASAN "${CMAKE_C_FLAGS_ASAN} -Wno-error=cpp") 267a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS_ASANDBG "${CMAKE_C_FLAGS_ASANDBG} -Wno-error=cpp") 268a8e1175bSopenharmony_ci endif(UNSAFE_BUILD) 269a8e1175bSopenharmony_ci endif(CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNU) 270a8e1175bSopenharmony_ci 271a8e1175bSopenharmony_ci if (CMAKE_COMPILER_IS_IAR) 272a8e1175bSopenharmony_ci set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --warnings_are_errors") 273a8e1175bSopenharmony_ci endif(CMAKE_COMPILER_IS_IAR) 274a8e1175bSopenharmony_ciendif(MBEDTLS_FATAL_WARNINGS) 275a8e1175bSopenharmony_ci 276a8e1175bSopenharmony_ciif(CMAKE_BUILD_TYPE STREQUAL "Coverage") 277a8e1175bSopenharmony_ci if(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG) 278a8e1175bSopenharmony_ci set(CMAKE_SHARED_LINKER_FLAGS "--coverage") 279a8e1175bSopenharmony_ci endif(CMAKE_COMPILER_IS_GNU OR CMAKE_COMPILER_IS_CLANG) 280a8e1175bSopenharmony_ciendif(CMAKE_BUILD_TYPE STREQUAL "Coverage") 281a8e1175bSopenharmony_ci 282a8e1175bSopenharmony_ciif(LIB_INSTALL_DIR) 283a8e1175bSopenharmony_ci set(CMAKE_INSTALL_LIBDIR "${LIB_INSTALL_DIR}") 284a8e1175bSopenharmony_ciendif() 285a8e1175bSopenharmony_ci 286a8e1175bSopenharmony_ciif (NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/framework/CMakeLists.txt") 287a8e1175bSopenharmony_ci message(FATAL_ERROR "${CMAKE_CURRENT_SOURCE_DIR}/framework/CMakeLists.txt not found. Run `git submodule update --init` from the source tree to fetch the submodule contents.") 288a8e1175bSopenharmony_ciendif() 289a8e1175bSopenharmony_ciadd_subdirectory(framework) 290a8e1175bSopenharmony_ci 291a8e1175bSopenharmony_ciadd_subdirectory(include) 292a8e1175bSopenharmony_ci 293a8e1175bSopenharmony_ciadd_subdirectory(3rdparty) 294a8e1175bSopenharmony_ci 295a8e1175bSopenharmony_ciadd_subdirectory(library) 296a8e1175bSopenharmony_ci 297a8e1175bSopenharmony_ciadd_subdirectory(pkgconfig) 298a8e1175bSopenharmony_ci 299a8e1175bSopenharmony_ci# 300a8e1175bSopenharmony_ci# The C files in tests/src directory contain test code shared among test suites 301a8e1175bSopenharmony_ci# and programs. This shared test code is compiled and linked to test suites and 302a8e1175bSopenharmony_ci# programs objects as a set of compiled objects. The compiled objects are NOT 303a8e1175bSopenharmony_ci# built into a library that the test suite and program objects would link 304a8e1175bSopenharmony_ci# against as they link against the mbedcrypto, mbedx509 and mbedtls libraries. 305a8e1175bSopenharmony_ci# The reason is that such library is expected to have mutual dependencies with 306a8e1175bSopenharmony_ci# the aforementioned libraries and that there is as of today no portable way of 307a8e1175bSopenharmony_ci# handling such dependencies (only toolchain specific solutions). 308a8e1175bSopenharmony_ci# 309a8e1175bSopenharmony_ci# Thus the below definition of the `mbedtls_test` CMake library of objects 310a8e1175bSopenharmony_ci# target. This library of objects is used by tests and programs CMake files 311a8e1175bSopenharmony_ci# to define the test executables. 312a8e1175bSopenharmony_ci# 313a8e1175bSopenharmony_ciif(ENABLE_TESTING OR ENABLE_PROGRAMS) 314a8e1175bSopenharmony_ci file(GLOB MBEDTLS_TEST_FILES 315a8e1175bSopenharmony_ci ${CMAKE_CURRENT_SOURCE_DIR}/tests/src/*.c 316a8e1175bSopenharmony_ci ${CMAKE_CURRENT_SOURCE_DIR}/tests/src/drivers/*.c) 317a8e1175bSopenharmony_ci add_library(mbedtls_test OBJECT ${MBEDTLS_TEST_FILES}) 318a8e1175bSopenharmony_ci target_include_directories(mbedtls_test 319a8e1175bSopenharmony_ci PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tests/include 320a8e1175bSopenharmony_ci PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include 321a8e1175bSopenharmony_ci PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/library) 322a8e1175bSopenharmony_ci # Request C11, needed for memory poisoning tests 323a8e1175bSopenharmony_ci set_target_properties(mbedtls_test PROPERTIES C_STANDARD 11) 324a8e1175bSopenharmony_ci 325a8e1175bSopenharmony_ci file(GLOB MBEDTLS_TEST_HELPER_FILES 326a8e1175bSopenharmony_ci ${CMAKE_CURRENT_SOURCE_DIR}/tests/src/test_helpers/*.c) 327a8e1175bSopenharmony_ci add_library(mbedtls_test_helpers OBJECT ${MBEDTLS_TEST_HELPER_FILES}) 328a8e1175bSopenharmony_ci target_include_directories(mbedtls_test_helpers 329a8e1175bSopenharmony_ci PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tests/include 330a8e1175bSopenharmony_ci PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include 331a8e1175bSopenharmony_ci PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/library 332a8e1175bSopenharmony_ci PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/everest/include) 333a8e1175bSopenharmony_ci 334a8e1175bSopenharmony_ci # Pass-through MBEDTLS_CONFIG_FILE and MBEDTLS_USER_CONFIG_FILE 335a8e1175bSopenharmony_ci if(MBEDTLS_CONFIG_FILE) 336a8e1175bSopenharmony_ci target_compile_definitions(mbedtls_test 337a8e1175bSopenharmony_ci PUBLIC MBEDTLS_CONFIG_FILE="${MBEDTLS_CONFIG_FILE}") 338a8e1175bSopenharmony_ci target_compile_definitions(mbedtls_test_helpers 339a8e1175bSopenharmony_ci PUBLIC MBEDTLS_CONFIG_FILE="${MBEDTLS_CONFIG_FILE}") 340a8e1175bSopenharmony_ci endif() 341a8e1175bSopenharmony_ci if(MBEDTLS_USER_CONFIG_FILE) 342a8e1175bSopenharmony_ci target_compile_definitions(mbedtls_test 343a8e1175bSopenharmony_ci PUBLIC MBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}") 344a8e1175bSopenharmony_ci target_compile_definitions(mbedtls_test_helpers 345a8e1175bSopenharmony_ci PUBLIC MBEDTLS_USER_CONFIG_FILE="${MBEDTLS_USER_CONFIG_FILE}") 346a8e1175bSopenharmony_ci endif() 347a8e1175bSopenharmony_ciendif() 348a8e1175bSopenharmony_ci 349a8e1175bSopenharmony_ciif(ENABLE_PROGRAMS) 350a8e1175bSopenharmony_ci add_subdirectory(programs) 351a8e1175bSopenharmony_ciendif() 352a8e1175bSopenharmony_ci 353a8e1175bSopenharmony_ciADD_CUSTOM_TARGET(${MBEDTLS_TARGET_PREFIX}apidoc 354a8e1175bSopenharmony_ci COMMAND doxygen mbedtls.doxyfile 355a8e1175bSopenharmony_ci WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doxygen) 356a8e1175bSopenharmony_ci 357a8e1175bSopenharmony_ciif(ENABLE_TESTING) 358a8e1175bSopenharmony_ci enable_testing() 359a8e1175bSopenharmony_ci 360a8e1175bSopenharmony_ci add_subdirectory(tests) 361a8e1175bSopenharmony_ci 362a8e1175bSopenharmony_ci # additional convenience targets for Unix only 363a8e1175bSopenharmony_ci if(UNIX) 364a8e1175bSopenharmony_ci 365a8e1175bSopenharmony_ci # For coverage testing: 366a8e1175bSopenharmony_ci # 1. Build with: 367a8e1175bSopenharmony_ci # cmake -D CMAKE_BUILD_TYPE=Coverage /path/to/source && make 368a8e1175bSopenharmony_ci # 2. Run the relevant tests for the part of the code you're interested in. 369a8e1175bSopenharmony_ci # For the reference coverage measurement, see 370a8e1175bSopenharmony_ci # tests/scripts/basic-build-test.sh 371a8e1175bSopenharmony_ci # 3. Run scripts/lcov.sh to generate an HTML report. 372a8e1175bSopenharmony_ci ADD_CUSTOM_TARGET(lcov 373a8e1175bSopenharmony_ci COMMAND scripts/lcov.sh 374a8e1175bSopenharmony_ci ) 375a8e1175bSopenharmony_ci 376a8e1175bSopenharmony_ci ADD_CUSTOM_TARGET(memcheck 377a8e1175bSopenharmony_ci COMMAND sed -i.bak s+/usr/bin/valgrind+`which valgrind`+ DartConfiguration.tcl 378a8e1175bSopenharmony_ci COMMAND ctest -O memcheck.log -D ExperimentalMemCheck 379a8e1175bSopenharmony_ci COMMAND tail -n1 memcheck.log | grep 'Memory checking results:' > /dev/null 380a8e1175bSopenharmony_ci COMMAND rm -f memcheck.log 381a8e1175bSopenharmony_ci COMMAND mv DartConfiguration.tcl.bak DartConfiguration.tcl 382a8e1175bSopenharmony_ci ) 383a8e1175bSopenharmony_ci endif(UNIX) 384a8e1175bSopenharmony_ci 385a8e1175bSopenharmony_ci # Make scripts needed for testing available in an out-of-source build. 386a8e1175bSopenharmony_ci if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) 387a8e1175bSopenharmony_ci link_to_source(scripts) 388a8e1175bSopenharmony_ci # Copy (don't link) DartConfiguration.tcl, needed for memcheck, to 389a8e1175bSopenharmony_ci # keep things simple with the sed commands in the memcheck target. 390a8e1175bSopenharmony_ci configure_file(${CMAKE_CURRENT_SOURCE_DIR}/DartConfiguration.tcl 391a8e1175bSopenharmony_ci ${CMAKE_CURRENT_BINARY_DIR}/DartConfiguration.tcl COPYONLY) 392a8e1175bSopenharmony_ci endif() 393a8e1175bSopenharmony_ciendif() 394a8e1175bSopenharmony_ci 395a8e1175bSopenharmony_ciif(NOT DISABLE_PACKAGE_CONFIG_AND_INSTALL) 396a8e1175bSopenharmony_ci configure_package_config_file( 397a8e1175bSopenharmony_ci "cmake/MbedTLSConfig.cmake.in" 398a8e1175bSopenharmony_ci "cmake/MbedTLSConfig.cmake" 399a8e1175bSopenharmony_ci INSTALL_DESTINATION "cmake") 400a8e1175bSopenharmony_ci 401a8e1175bSopenharmony_ci write_basic_package_version_file( 402a8e1175bSopenharmony_ci "cmake/MbedTLSConfigVersion.cmake" 403a8e1175bSopenharmony_ci COMPATIBILITY SameMajorVersion 404a8e1175bSopenharmony_ci VERSION 3.6.0) 405a8e1175bSopenharmony_ci 406a8e1175bSopenharmony_ci install( 407a8e1175bSopenharmony_ci FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake/MbedTLSConfig.cmake" 408a8e1175bSopenharmony_ci "${CMAKE_CURRENT_BINARY_DIR}/cmake/MbedTLSConfigVersion.cmake" 409a8e1175bSopenharmony_ci DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/MbedTLS") 410a8e1175bSopenharmony_ci 411a8e1175bSopenharmony_ci export( 412a8e1175bSopenharmony_ci EXPORT MbedTLSTargets 413a8e1175bSopenharmony_ci NAMESPACE MbedTLS:: 414a8e1175bSopenharmony_ci FILE "cmake/MbedTLSTargets.cmake") 415a8e1175bSopenharmony_ci 416a8e1175bSopenharmony_ci install( 417a8e1175bSopenharmony_ci EXPORT MbedTLSTargets 418a8e1175bSopenharmony_ci NAMESPACE MbedTLS:: 419a8e1175bSopenharmony_ci DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/MbedTLS" 420a8e1175bSopenharmony_ci FILE "MbedTLSTargets.cmake") 421a8e1175bSopenharmony_ci 422a8e1175bSopenharmony_ci if(CMAKE_VERSION VERSION_GREATER 3.15 OR CMAKE_VERSION VERSION_EQUAL 3.15) 423a8e1175bSopenharmony_ci # Do not export the package by default 424a8e1175bSopenharmony_ci cmake_policy(SET CMP0090 NEW) 425a8e1175bSopenharmony_ci 426a8e1175bSopenharmony_ci # Make this package visible to the system 427a8e1175bSopenharmony_ci export(PACKAGE MbedTLS) 428a8e1175bSopenharmony_ci endif() 429a8e1175bSopenharmony_ciendif() 430