Build: Add MBEDTLS_TARGET_PREFIX

Allows required targets to have prefixes added to them, so that external
projects can avoid target names clashing.

Signed-off-by: Raef Coles <raef.coles@arm.com>
This commit is contained in:
Raef Coles 2020-10-13 16:30:41 +01:00
parent 8f24a8bb34
commit 995c66f702
15 changed files with 69 additions and 40 deletions

View File

@ -14,6 +14,10 @@
# CMake files. It is related to ZLIB support which is planned to be removed.
# When the support is removed, the associated include_directories command
# will be removed as well as this note.
# - MBEDTLS_TARGET_PREFIX: CMake targets are designed to be alterable by calling
# CMake in order to avoid target name clashes, via the use of
# MBEDTLS_TARGET_PREFIX. The value of this variable is prefixed to the
# mbedtls, mbedx509, mbedcrypto and apidoc targets.
#
cmake_minimum_required(VERSION 2.6)
@ -273,7 +277,7 @@ if(ENABLE_PROGRAMS)
add_subdirectory(programs)
endif()
ADD_CUSTOM_TARGET(apidoc
ADD_CUSTOM_TARGET(${MBEDTLS_TARGET_PREFIX}apidoc
COMMAND doxygen mbedtls.doxyfile
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doxygen)

View File

@ -0,0 +1,6 @@
Features
* Add MBEDTLS_TARGET_PREFIX CMake variable, which is prefixed to the mbedtls,
mbedcrypto, mbedx509 and apidoc CMake target names. This can be used by
external CMake projects that include this one to avoid CMake target name
clashes. The default value of this variable is "", so default target names
are unchanged.

View File

@ -150,18 +150,31 @@ if (NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
message(FATAL_ERROR "Need to choose static or shared mbedtls build!")
endif(NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY)
set(target_libraries "mbedcrypto" "mbedx509" "mbedtls")
set(mbedtls_target "${MBEDTLS_TARGET_PREFIX}mbedtls")
set(mbedx509_target "${MBEDTLS_TARGET_PREFIX}mbedx509")
set(mbedcrypto_target "${MBEDTLS_TARGET_PREFIX}mbedcrypto")
set(mbedtls_target ${mbedtls_target} PARENT_SCOPE)
set(mbedx509_target ${mbedx509_target} PARENT_SCOPE)
set(mbedcrypto_target ${mbedcrypto_target} PARENT_SCOPE)
if (USE_STATIC_MBEDTLS_LIBRARY)
set(mbedtls_static_target ${mbedtls_target})
set(mbedx509_static_target ${mbedx509_target})
set(mbedcrypto_static_target ${mbedcrypto_target})
endif()
set(target_libraries ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target})
if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
set(mbedtls_static_target "mbedtls_static")
set(mbedx509_static_target "mbedx509_static")
set(mbedcrypto_static_target "mbedcrypto_static")
string(APPEND mbedtls_static_target "_static")
string(APPEND mbedx509_static_target "_static")
string(APPEND mbedcrypto_static_target "_static")
list(APPEND target_libraries
"mbedcrypto_static" "mbedx509_static" "mbedtls_static")
elseif(USE_STATIC_MBEDTLS_LIBRARY)
set(mbedtls_static_target "mbedtls")
set(mbedx509_static_target "mbedx509")
set(mbedcrypto_static_target "mbedcrypto")
${mbedcrypto_static_target}
${mbedx509_static_target}
${mbedtls_static_target})
endif()
if(USE_STATIC_MBEDTLS_LIBRARY)
@ -179,17 +192,17 @@ if(USE_STATIC_MBEDTLS_LIBRARY)
endif(USE_STATIC_MBEDTLS_LIBRARY)
if(USE_SHARED_MBEDTLS_LIBRARY)
add_library(mbedcrypto SHARED ${src_crypto})
set_target_properties(mbedcrypto PROPERTIES VERSION 2.24.0 SOVERSION 5)
target_link_libraries(mbedcrypto ${libs})
add_library(${mbedcrypto_target} SHARED ${src_crypto})
set_target_properties(${mbedcrypto_target} PROPERTIES VERSION 2.24.0 SOVERSION 5)
target_link_libraries(${mbedcrypto_target} ${libs})
add_library(mbedx509 SHARED ${src_x509})
set_target_properties(mbedx509 PROPERTIES VERSION 2.24.0 SOVERSION 1)
target_link_libraries(mbedx509 ${libs} mbedcrypto)
add_library(${mbedx509_target} SHARED ${src_x509})
set_target_properties(${mbedx509_target} PROPERTIES VERSION 2.24.0 SOVERSION 1)
target_link_libraries(${mbedx509_target} ${libs} ${mbedcrypto_target})
add_library(mbedtls SHARED ${src_tls})
set_target_properties(mbedtls PROPERTIES VERSION 2.24.0 SOVERSION 13)
target_link_libraries(mbedtls ${libs} mbedx509)
add_library(${mbedtls_target} SHARED ${src_tls})
set_target_properties(${mbedtls_target} PROPERTIES VERSION 2.24.0 SOVERSION 13)
target_link_libraries(${mbedtls_target} ${libs} ${mbedx509_target})
endif(USE_SHARED_MBEDTLS_LIBRARY)
foreach(target IN LISTS target_libraries)
@ -210,7 +223,9 @@ foreach(target IN LISTS target_libraries)
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
endforeach(target)
add_custom_target(lib DEPENDS mbedcrypto mbedx509 mbedtls)
set(lib_target "${MBEDTLS_TARGET_PREFIX}lib")
add_custom_target(${lib_target} DEPENDS ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target})
if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY)
add_dependencies(lib mbedcrypto_static mbedx509_static mbedtls_static)
add_dependencies(${lib_target} ${mbedcrypto_static_target} ${mbedx509_static_target} ${mbedtls_static_target})
endif()

View File

@ -5,7 +5,7 @@ set(executables
foreach(exe IN LISTS executables)
add_executable(${exe} ${exe}.c $<TARGET_OBJECTS:mbedtls_test>)
target_link_libraries(${exe} mbedcrypto)
target_link_libraries(${exe} ${mbedcrypto_target})
endforeach()
install(TARGETS ${executables}

View File

@ -1,5 +1,5 @@
set(libs
mbedtls
${mbedtls_target}
)
if(USE_PKCS11_HELPER_LIBRARY)

View File

@ -5,7 +5,7 @@ set(executables
foreach(exe IN LISTS executables)
add_executable(${exe} ${exe}.c $<TARGET_OBJECTS:mbedtls_test>)
target_link_libraries(${exe} mbedcrypto)
target_link_libraries(${exe} ${mbedcrypto_target})
endforeach()
install(TARGETS ${executables}

View File

@ -5,7 +5,7 @@ set(executables_mbedtls
foreach(exe IN LISTS executables_mbedtls)
add_executable(${exe} ${exe}.c $<TARGET_OBJECTS:mbedtls_test>)
target_link_libraries(${exe} mbedtls)
target_link_libraries(${exe} ${mbedtls_target})
endforeach()
set(executables_mbedcrypto
@ -31,7 +31,7 @@ set(executables_mbedcrypto
foreach(exe IN LISTS executables_mbedcrypto)
add_executable(${exe} ${exe}.c $<TARGET_OBJECTS:mbedtls_test>)
target_link_libraries(${exe} mbedcrypto)
target_link_libraries(${exe} ${mbedcrypto_target})
endforeach()
install(TARGETS ${executables_mbedtls} ${executables_mbedcrypto}

View File

@ -6,7 +6,7 @@ set(executables
foreach(exe IN LISTS executables)
add_executable(${exe} ${exe}.c $<TARGET_OBJECTS:mbedtls_test>)
target_link_libraries(${exe} mbedcrypto)
target_link_libraries(${exe} ${mbedcrypto_target})
target_include_directories(${exe} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../tests/include)
endforeach()

View File

@ -6,7 +6,7 @@ set(executables
foreach(exe IN LISTS executables)
add_executable(${exe} ${exe}.c $<TARGET_OBJECTS:mbedtls_test>)
target_link_libraries(${exe} mbedcrypto)
target_link_libraries(${exe} ${mbedcrypto_target})
endforeach()
install(TARGETS ${executables}

View File

@ -2,7 +2,7 @@ set(THREADS_USE_PTHREADS_WIN32 true)
find_package(Threads)
set(libs
mbedtls
${mbedtls_target}
)
if(USE_PKCS11_HELPER_LIBRARY)

View File

@ -1,5 +1,5 @@
set(libs
mbedtls
${mbedtls_target}
)
if(USE_PKCS11_HELPER_LIBRARY)
@ -33,7 +33,7 @@ foreach(exe IN LISTS executables_libs executables_mbedcrypto)
if (${exe_index} GREATER -1)
target_link_libraries(${exe} ${libs})
else()
target_link_libraries(${exe} mbedcrypto)
target_link_libraries(${exe} ${mbedcrypto_target})
endif()
endforeach()

View File

@ -1,5 +1,8 @@
cmake_minimum_required(VERSION 2.6)
# Test the target renaming support by adding a prefix to the targets built
set(MBEDTLS_TARGET_PREFIX subproject_test_)
# We use the parent Mbed TLS directory as the MBEDTLS_DIR for this test. Other
# projects that use Mbed TLS as a subproject are likely to add by their own
# relative paths.
@ -8,11 +11,12 @@ set(MBEDTLS_DIR ../../../)
# Add Mbed TLS as a subdirectory.
add_subdirectory(${MBEDTLS_DIR} build)
# Link against all the Mbed TLS libraries.
# Link against all the Mbed TLS libraries. Verifies that the targets have been
# created using the specified prefix
set(libs
mbedcrypto
mbedx509
mbedtls
subproject_test_mbedcrypto
subproject_test_mbedx509
subproject_test_mbedtls
)
add_executable(cmake_subproject cmake_subproject.c)

View File

@ -1,5 +1,5 @@
set(libs
mbedcrypto
${mbedcrypto_target}
)
set(executables

View File

@ -1,5 +1,5 @@
set(libs
mbedx509
${mbedx509_target}
)
if(USE_PKCS11_HELPER_LIBRARY)
@ -23,7 +23,7 @@ foreach(exe IN LISTS executables)
target_link_libraries(${exe} ${libs})
endforeach()
target_link_libraries(cert_app mbedtls)
target_link_libraries(cert_app ${mbedtls_target})
install(TARGETS ${executables}
DESTINATION "bin"

View File

@ -1,5 +1,5 @@
set(libs
mbedtls
${mbedtls_target}
)
# Set the project root directory if it's not already defined, as may happen if
@ -43,7 +43,7 @@ function(add_test_suite suite_name)
add_custom_command(
OUTPUT test_suite_${data_name}.c
COMMAND ${MBEDTLS_PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py -f ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function -d ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data -t ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function -p ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function -s ${CMAKE_CURRENT_SOURCE_DIR}/suites --helpers-file ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function -o .
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py mbedtls ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/scripts/generate_test_code.py ${mbedtls_target} ${CMAKE_CURRENT_SOURCE_DIR}/suites/helpers.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/main_test.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/host_test.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${suite_name}.function ${CMAKE_CURRENT_SOURCE_DIR}/suites/test_suite_${data_name}.data
)
add_executable(test_suite_${data_name} test_suite_${data_name}.c $<TARGET_OBJECTS:mbedtls_test>)