From d85a7e9b09fe3998be72667e965b74d97d4b2df5 Mon Sep 17 00:00:00 2001 From: Ashley Duncan Date: Mon, 29 Apr 2019 20:35:06 +1200 Subject: [PATCH] Remove use of CMAKE_SOURCE_DIR Remove use of CMAKE_SOURCE_DIR in case mbedtls is built from within another CMake project. Define MBEDTLS_DIR to ${CMAKE_CURRENT_SOURCE_DIR} in the main CMakeLists.txt file and refer to that when defining target include paths to enable mbedtls to be built as a sub project. Fixes https://github.com/ARMmbed/mbedtls/issues/2609 Signed-off-by: Ashley Duncan Signed-off-by: Jaeden Amero --- CMakeLists.txt | 3 +++ library/CMakeLists.txt | 21 ++++++++++++++------- tests/CMakeLists.txt | 13 ++++++++++--- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d29839e0e..c512ad6282 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,9 @@ else() project("mbed TLS" C) endif() +# Set the project root directory. +set(MBEDTLS_DIR ${CMAKE_CURRENT_SOURCE_DIR}) + option(ENABLE_PROGRAMS "Build mbed TLS programs." ON) option(UNSAFE_BUILD "Allow unsafe builds. These builds ARE NOT SECURE." OFF) diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 61bc13d32d..6b2a8508a6 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -2,6 +2,13 @@ option(USE_STATIC_MBEDTLS_LIBRARY "Build mbed TLS static library." ON) option(USE_SHARED_MBEDTLS_LIBRARY "Build mbed TLS shared library." OFF) option(LINK_WITH_PTHREAD "Explicitly link mbed TLS library to pthread." OFF) +# Set the project root directory if it's not already defined, as may happen if +# the library folder is included directly by a parent project, without +# including the top level CMakeLists.txt. +if(NOT DEFINED MBEDTLS_DIR) + set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR}) +endif() + set(src_crypto aes.c aesni.c @@ -72,9 +79,9 @@ set(src_crypto if(USE_CRYPTO_SUBMODULE) set(src_crypto ${src_crypto} - ${CMAKE_SOURCE_DIR}/library/version.c - ${CMAKE_SOURCE_DIR}/library/version_features.c - ${CMAKE_SOURCE_DIR}/library/error.c + ${MBEDTLS_DIR}/library/version.c + ${MBEDTLS_DIR}/library/version_features.c + ${MBEDTLS_DIR}/library/error.c ) else() set(src_crypto @@ -133,8 +140,8 @@ if(USE_STATIC_MBEDTLS_LIBRARY) set_target_properties(${mbedcrypto_static_target} PROPERTIES OUTPUT_NAME mbedcrypto) target_link_libraries(${mbedcrypto_static_target} ${libs}) target_include_directories(${mbedcrypto_static_target} - PUBLIC ${CMAKE_SOURCE_DIR}/include/ - PUBLIC ${CMAKE_SOURCE_DIR}/crypto/include/) + PUBLIC ${MBEDTLS_DIR}/include/ + PUBLIC ${MBEDTLS_DIR}/crypto/include/) install(TARGETS ${mbedcrypto_static_target} DESTINATION ${LIB_INSTALL_DIR} @@ -146,8 +153,8 @@ if(USE_SHARED_MBEDTLS_LIBRARY) set_target_properties(mbedcrypto PROPERTIES VERSION 2.17.0 SOVERSION 3) target_link_libraries(mbedcrypto ${libs}) target_include_directories(mbedcrypto - PUBLIC ${CMAKE_SOURCE_DIR}/include/ - PUBLIC ${CMAKE_SOURCE_DIR}/crypto/include/) + PUBLIC ${MBEDTLS_DIR}/include/ + PUBLIC ${MBEDTLS_DIR}/crypto/include/) install(TARGETS mbedcrypto DESTINATION ${LIB_INSTALL_DIR} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 42d99d6235..9dc190816d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -2,6 +2,13 @@ set(libs mbedcrypto ) +# Set the project root directory if it's not already defined, as may happen if +# the tests folder is included directly by a parent project, without including +# the top level CMakeLists.txt. +if(NOT DEFINED MBEDTLS_DIR) + set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR}) +endif() + find_package(Perl) if(NOT PERL_FOUND) message(FATAL_ERROR "Cannot build test suites without Perl") @@ -43,9 +50,9 @@ function(add_test_suite suite_name) add_executable(${exe_name} test_suite_${data_name}.c) target_link_libraries(${exe_name} ${libs}) target_include_directories(${exe_name} - PUBLIC ${CMAKE_SOURCE_DIR}/include/ - PUBLIC ${CMAKE_SOURCE_DIR}/crypto/include/ - PRIVATE ${CMAKE_SOURCE_DIR}/crypto/library/) + PUBLIC ${MBEDTLS_DIR}/include/ + PUBLIC ${MBEDTLS_DIR}/crypto/include/ + PRIVATE ${MBEDTLS_DIR}/crypto/library/) if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX}) message(STATUS "The test suite ${data_name} will not be executed.")