quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

CMakeLists.txt (22343B)


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