Add CMake package config file

This change enables automatic detection and consumption of Mbed TLS
library targets from within other CMake projects. By generating an
`MbedTLSConfig.cmake` file, consuming projects receive a more complete
view of these targets, allowing them to be used as dependencies which
properly inherit the transitive dependencies of the libraries.

This is fairly fragile, as it seems Mbed TLS's libraries do not appear
to properly model their dependencies on other targets, including
third-party dependencies. It is, however, sufficient for building and
linking the compiled Mbed TLS libraries when there are no third-party
dependencies involved. Further work is needed for more complex
use-cases, but this will likely meet the needs of most projects.

Resolves #298. Probably useful for #2857.

Signed-off-by: Chris Kay <chris.kay@arm.com>
This commit is contained in:
Chris Kay 2021-03-25 16:03:25 +00:00
parent 0c1a42a147
commit d259e347e6
14 changed files with 324 additions and 41 deletions

View file

@ -87,8 +87,6 @@ set(src_crypto
xtea.c
)
list(APPEND src_crypto ${thirdparty_src})
set(src_x509
x509.c
x509_create.c
@ -180,6 +178,10 @@ if(USE_STATIC_MBEDTLS_LIBRARY)
set_target_properties(${mbedcrypto_static_target} PROPERTIES OUTPUT_NAME mbedcrypto)
target_link_libraries(${mbedcrypto_static_target} PUBLIC ${libs})
if(TARGET everest)
target_link_libraries(${mbedcrypto_static_target} PUBLIC everest)
endif()
add_library(${mbedx509_static_target} STATIC ${src_x509})
set_target_properties(${mbedx509_static_target} PROPERTIES OUTPUT_NAME mbedx509)
target_link_libraries(${mbedx509_static_target} PUBLIC ${libs} ${mbedcrypto_static_target})
@ -194,6 +196,10 @@ if(USE_SHARED_MBEDTLS_LIBRARY)
set_target_properties(${mbedcrypto_target} PROPERTIES VERSION 2.26.0 SOVERSION 6)
target_link_libraries(${mbedcrypto_target} PUBLIC ${libs})
if(TARGET everest)
target_link_libraries(${mbedcrypto_target} PUBLIC everest)
endif()
add_library(${mbedx509_target} SHARED ${src_x509})
set_target_properties(${mbedx509_target} PROPERTIES VERSION 2.26.0 SOVERSION 1)
target_link_libraries(${mbedx509_target} PUBLIC ${libs} ${mbedcrypto_target})
@ -210,15 +216,14 @@ foreach(target IN LISTS target_libraries)
# /library needs to be listed explicitly when building .c files outside
# of /library (which currently means: under /3rdparty).
target_include_directories(${target}
PUBLIC ${MBEDTLS_DIR}/include/
PUBLIC ${thirdparty_inc_public}
PRIVATE ${MBEDTLS_DIR}/library/
PRIVATE ${thirdparty_inc})
target_compile_definitions(${target}
PRIVATE ${thirdparty_def})
install(TARGETS ${target}
DESTINATION ${LIB_INSTALL_DIR}
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
PUBLIC $<BUILD_INTERFACE:${MBEDTLS_DIR}/include/>
$<INSTALL_INTERFACE:include/>
PRIVATE ${MBEDTLS_DIR}/library/)
install(
TARGETS ${target}
EXPORT MbedTLSTargets
DESTINATION ${LIB_INSTALL_DIR}
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
endforeach(target)
set(lib_target "${MBEDTLS_TARGET_PREFIX}lib")