Make CMakeLists.txt discover if mbed TLS is being built as subproject

The main CMakeLists.txt is capable to detect if it's being built as
a subproject (i.e. through add_subdirectory()) hence allowing to
disable the package configuration, target export and installation
that generally are not required when mbed TLS is being built as
part of another project.

Signed-off-by: Antonio de Angelis <antonio.deangelis@arm.com>
This commit is contained in:
Antonio de Angelis 2021-12-07 20:45:55 +00:00
parent 8c8cea25c7
commit 2869c67d63

View file

@ -39,6 +39,14 @@ else()
project("mbed TLS" C)
endif()
# Determine if mbed TLS is being built as a subproject using add_subdirectory()
if(NOT DEFINED MBEDTLS_AS_SUBPROJECT)
set(MBEDTLS_AS_SUBPROJECT ON)
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(MBEDTLS_AS_SUBPROJECT OFF)
endif()
endif()
# Set the project root directory.
set(MBEDTLS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
@ -52,6 +60,8 @@ else()
option(GEN_FILES "Generate the auto-generated files as needed" ON)
endif()
option(DISABLE_PACKAGE_CONFIG_AND_INSTALL "Disable package configuration, target export and installation" ${MBEDTLS_AS_SUBPROJECT})
string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}")
string(REGEX MATCH "GNU" CMAKE_COMPILER_IS_GNU "${CMAKE_C_COMPILER_ID}")
string(REGEX MATCH "IAR" CMAKE_COMPILER_IS_IAR "${CMAKE_C_COMPILER_ID}")
@ -325,6 +335,7 @@ if(ENABLE_TESTING)
endif()
endif()
if(NOT DISABLE_PACKAGE_CONFIG_AND_INSTALL)
configure_package_config_file(
"cmake/MbedTLSConfig.cmake.in"
"cmake/MbedTLSConfig.cmake"
@ -358,3 +369,4 @@ if(CMAKE_VERSION VERSION_GREATER 3.15 OR CMAKE_VERSION VERSION_EQUAL 3.15)
# Make this package visible to the system
export(PACKAGE MbedTLS)
endif()
endif()