Summer Qin | e8412b4 | 2020-10-15 14:20:21 +0800 | [diff] [blame] | 1 | From 995c66f702db3a004be1e3d822ffad64b2ad125f Mon Sep 17 00:00:00 2001 |
| 2 | From: Raef Coles <raef.coles@arm.com> |
| 3 | Date: Tue, 13 Oct 2020 16:30:41 +0100 |
| 4 | Subject: [PATCH 1/2] Build: Add MBEDTLS_TARGET_PREFIX |
| 5 | |
| 6 | Allows required targets to have prefixes added to them, so that external |
| 7 | projects can avoid target names clashing. |
| 8 | |
| 9 | Signed-off-by: Raef Coles <raef.coles@arm.com> |
| 10 | --- |
| 11 | CMakeLists.txt | 6 +- |
| 12 | .../add_MBEDTLS_TARGET_PREFIX_to_cmake.txt | 6 ++ |
| 13 | library/CMakeLists.txt | 55 ++++++++++++------- |
| 14 | programs/aes/CMakeLists.txt | 2 +- |
| 15 | programs/fuzz/CMakeLists.txt | 2 +- |
| 16 | programs/hash/CMakeLists.txt | 2 +- |
| 17 | programs/pkey/CMakeLists.txt | 4 +- |
| 18 | programs/psa/CMakeLists.txt | 2 +- |
| 19 | programs/random/CMakeLists.txt | 2 +- |
| 20 | programs/ssl/CMakeLists.txt | 2 +- |
| 21 | programs/test/CMakeLists.txt | 4 +- |
| 22 | programs/test/cmake_subproject/CMakeLists.txt | 12 ++-- |
| 23 | programs/util/CMakeLists.txt | 2 +- |
| 24 | programs/x509/CMakeLists.txt | 4 +- |
| 25 | tests/CMakeLists.txt | 4 +- |
| 26 | 15 files changed, 69 insertions(+), 40 deletions(-) |
| 27 | create mode 100644 ChangeLog.d/add_MBEDTLS_TARGET_PREFIX_to_cmake.txt |
| 28 | |
| 29 | diff --git a/CMakeLists.txt b/CMakeLists.txt |
| 30 | index 5af4c8124..e4f318b3c 100644 |
| 31 | --- a/CMakeLists.txt |
| 32 | +++ b/CMakeLists.txt |
| 33 | @@ -14,6 +14,10 @@ |
| 34 | # CMake files. It is related to ZLIB support which is planned to be removed. |
| 35 | # When the support is removed, the associated include_directories command |
| 36 | # will be removed as well as this note. |
| 37 | +# - MBEDTLS_TARGET_PREFIX: CMake targets are designed to be alterable by calling |
| 38 | +# CMake in order to avoid target name clashes, via the use of |
| 39 | +# MBEDTLS_TARGET_PREFIX. The value of this variable is prefixed to the |
| 40 | +# mbedtls, mbedx509, mbedcrypto and apidoc targets. |
| 41 | # |
| 42 | |
| 43 | cmake_minimum_required(VERSION 2.6) |
| 44 | @@ -273,7 +277,7 @@ if(ENABLE_PROGRAMS) |
| 45 | add_subdirectory(programs) |
| 46 | endif() |
| 47 | |
| 48 | -ADD_CUSTOM_TARGET(apidoc |
| 49 | +ADD_CUSTOM_TARGET(${MBEDTLS_TARGET_PREFIX}apidoc |
| 50 | COMMAND doxygen mbedtls.doxyfile |
| 51 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doxygen) |
| 52 | |
| 53 | diff --git a/ChangeLog.d/add_MBEDTLS_TARGET_PREFIX_to_cmake.txt b/ChangeLog.d/add_MBEDTLS_TARGET_PREFIX_to_cmake.txt |
| 54 | new file mode 100644 |
| 55 | index 000000000..533f309ab |
| 56 | --- /dev/null |
| 57 | +++ b/ChangeLog.d/add_MBEDTLS_TARGET_PREFIX_to_cmake.txt |
| 58 | @@ -0,0 +1,6 @@ |
| 59 | +Features |
| 60 | + * Add MBEDTLS_TARGET_PREFIX CMake variable, which is prefixed to the mbedtls, |
| 61 | + mbedcrypto, mbedx509 and apidoc CMake target names. This can be used by |
| 62 | + external CMake projects that include this one to avoid CMake target name |
| 63 | + clashes. The default value of this variable is "", so default target names |
| 64 | + are unchanged. |
| 65 | diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt |
| 66 | index 4444b984e..f0315f721 100644 |
| 67 | --- a/library/CMakeLists.txt |
| 68 | +++ b/library/CMakeLists.txt |
| 69 | @@ -150,18 +150,31 @@ if (NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY) |
| 70 | message(FATAL_ERROR "Need to choose static or shared mbedtls build!") |
| 71 | endif(NOT USE_STATIC_MBEDTLS_LIBRARY AND NOT USE_SHARED_MBEDTLS_LIBRARY) |
| 72 | |
| 73 | -set(target_libraries "mbedcrypto" "mbedx509" "mbedtls") |
| 74 | +set(mbedtls_target "${MBEDTLS_TARGET_PREFIX}mbedtls") |
| 75 | +set(mbedx509_target "${MBEDTLS_TARGET_PREFIX}mbedx509") |
| 76 | +set(mbedcrypto_target "${MBEDTLS_TARGET_PREFIX}mbedcrypto") |
| 77 | + |
| 78 | +set(mbedtls_target ${mbedtls_target} PARENT_SCOPE) |
| 79 | +set(mbedx509_target ${mbedx509_target} PARENT_SCOPE) |
| 80 | +set(mbedcrypto_target ${mbedcrypto_target} PARENT_SCOPE) |
| 81 | + |
| 82 | +if (USE_STATIC_MBEDTLS_LIBRARY) |
| 83 | + set(mbedtls_static_target ${mbedtls_target}) |
| 84 | + set(mbedx509_static_target ${mbedx509_target}) |
| 85 | + set(mbedcrypto_static_target ${mbedcrypto_target}) |
| 86 | +endif() |
| 87 | + |
| 88 | +set(target_libraries ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target}) |
| 89 | |
| 90 | if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY) |
| 91 | - set(mbedtls_static_target "mbedtls_static") |
| 92 | - set(mbedx509_static_target "mbedx509_static") |
| 93 | - set(mbedcrypto_static_target "mbedcrypto_static") |
| 94 | + string(APPEND mbedtls_static_target "_static") |
| 95 | + string(APPEND mbedx509_static_target "_static") |
| 96 | + string(APPEND mbedcrypto_static_target "_static") |
| 97 | + |
| 98 | list(APPEND target_libraries |
| 99 | - "mbedcrypto_static" "mbedx509_static" "mbedtls_static") |
| 100 | -elseif(USE_STATIC_MBEDTLS_LIBRARY) |
| 101 | - set(mbedtls_static_target "mbedtls") |
| 102 | - set(mbedx509_static_target "mbedx509") |
| 103 | - set(mbedcrypto_static_target "mbedcrypto") |
| 104 | + ${mbedcrypto_static_target} |
| 105 | + ${mbedx509_static_target} |
| 106 | + ${mbedtls_static_target}) |
| 107 | endif() |
| 108 | |
| 109 | if(USE_STATIC_MBEDTLS_LIBRARY) |
| 110 | @@ -179,17 +192,17 @@ if(USE_STATIC_MBEDTLS_LIBRARY) |
| 111 | endif(USE_STATIC_MBEDTLS_LIBRARY) |
| 112 | |
| 113 | if(USE_SHARED_MBEDTLS_LIBRARY) |
| 114 | - add_library(mbedcrypto SHARED ${src_crypto}) |
| 115 | - set_target_properties(mbedcrypto PROPERTIES VERSION 2.24.0 SOVERSION 5) |
| 116 | - target_link_libraries(mbedcrypto ${libs}) |
| 117 | + add_library(${mbedcrypto_target} SHARED ${src_crypto}) |
| 118 | + set_target_properties(${mbedcrypto_target} PROPERTIES VERSION 2.24.0 SOVERSION 5) |
| 119 | + target_link_libraries(${mbedcrypto_target} ${libs}) |
| 120 | |
| 121 | - add_library(mbedx509 SHARED ${src_x509}) |
| 122 | - set_target_properties(mbedx509 PROPERTIES VERSION 2.24.0 SOVERSION 1) |
| 123 | - target_link_libraries(mbedx509 ${libs} mbedcrypto) |
| 124 | + add_library(${mbedx509_target} SHARED ${src_x509}) |
| 125 | + set_target_properties(${mbedx509_target} PROPERTIES VERSION 2.24.0 SOVERSION 1) |
| 126 | + target_link_libraries(${mbedx509_target} ${libs} ${mbedcrypto_target}) |
| 127 | |
| 128 | - add_library(mbedtls SHARED ${src_tls}) |
| 129 | - set_target_properties(mbedtls PROPERTIES VERSION 2.24.0 SOVERSION 13) |
| 130 | - target_link_libraries(mbedtls ${libs} mbedx509) |
| 131 | + add_library(${mbedtls_target} SHARED ${src_tls}) |
| 132 | + set_target_properties(${mbedtls_target} PROPERTIES VERSION 2.24.0 SOVERSION 13) |
| 133 | + target_link_libraries(${mbedtls_target} ${libs} ${mbedx509_target}) |
| 134 | endif(USE_SHARED_MBEDTLS_LIBRARY) |
| 135 | |
| 136 | foreach(target IN LISTS target_libraries) |
| 137 | @@ -210,7 +223,9 @@ foreach(target IN LISTS target_libraries) |
| 138 | PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) |
| 139 | endforeach(target) |
| 140 | |
| 141 | -add_custom_target(lib DEPENDS mbedcrypto mbedx509 mbedtls) |
| 142 | +set(lib_target "${MBEDTLS_TARGET_PREFIX}lib") |
| 143 | + |
| 144 | +add_custom_target(${lib_target} DEPENDS ${mbedcrypto_target} ${mbedx509_target} ${mbedtls_target}) |
| 145 | if(USE_STATIC_MBEDTLS_LIBRARY AND USE_SHARED_MBEDTLS_LIBRARY) |
| 146 | - add_dependencies(lib mbedcrypto_static mbedx509_static mbedtls_static) |
| 147 | + add_dependencies(${lib_target} ${mbedcrypto_static_target} ${mbedx509_static_target} ${mbedtls_static_target}) |
| 148 | endif() |
| 149 | diff --git a/programs/aes/CMakeLists.txt b/programs/aes/CMakeLists.txt |
| 150 | index 2309789a6..6b8ce2ab4 100644 |
| 151 | --- a/programs/aes/CMakeLists.txt |
| 152 | +++ b/programs/aes/CMakeLists.txt |
| 153 | @@ -5,7 +5,7 @@ set(executables |
| 154 | |
| 155 | foreach(exe IN LISTS executables) |
| 156 | add_executable(${exe} ${exe}.c $<TARGET_OBJECTS:mbedtls_test>) |
| 157 | - target_link_libraries(${exe} mbedcrypto) |
| 158 | + target_link_libraries(${exe} ${mbedcrypto_target}) |
| 159 | endforeach() |
| 160 | |
| 161 | install(TARGETS ${executables} |
| 162 | diff --git a/programs/fuzz/CMakeLists.txt b/programs/fuzz/CMakeLists.txt |
| 163 | index e2b0eace2..35512c79a 100644 |
| 164 | --- a/programs/fuzz/CMakeLists.txt |
| 165 | +++ b/programs/fuzz/CMakeLists.txt |
| 166 | @@ -1,5 +1,5 @@ |
| 167 | set(libs |
| 168 | - mbedtls |
| 169 | + ${mbedtls_target} |
| 170 | ) |
| 171 | |
| 172 | if(USE_PKCS11_HELPER_LIBRARY) |
| 173 | diff --git a/programs/hash/CMakeLists.txt b/programs/hash/CMakeLists.txt |
| 174 | index ae294798b..b2f2a1f5c 100644 |
| 175 | --- a/programs/hash/CMakeLists.txt |
| 176 | +++ b/programs/hash/CMakeLists.txt |
| 177 | @@ -5,7 +5,7 @@ set(executables |
| 178 | |
| 179 | foreach(exe IN LISTS executables) |
| 180 | add_executable(${exe} ${exe}.c $<TARGET_OBJECTS:mbedtls_test>) |
| 181 | - target_link_libraries(${exe} mbedcrypto) |
| 182 | + target_link_libraries(${exe} ${mbedcrypto_target}) |
| 183 | endforeach() |
| 184 | |
| 185 | install(TARGETS ${executables} |
| 186 | diff --git a/programs/pkey/CMakeLists.txt b/programs/pkey/CMakeLists.txt |
| 187 | index b4b3d3042..9c6fe7d49 100644 |
| 188 | --- a/programs/pkey/CMakeLists.txt |
| 189 | +++ b/programs/pkey/CMakeLists.txt |
| 190 | @@ -5,7 +5,7 @@ set(executables_mbedtls |
| 191 | |
| 192 | foreach(exe IN LISTS executables_mbedtls) |
| 193 | add_executable(${exe} ${exe}.c $<TARGET_OBJECTS:mbedtls_test>) |
| 194 | - target_link_libraries(${exe} mbedtls) |
| 195 | + target_link_libraries(${exe} ${mbedtls_target}) |
| 196 | endforeach() |
| 197 | |
| 198 | set(executables_mbedcrypto |
| 199 | @@ -31,7 +31,7 @@ set(executables_mbedcrypto |
| 200 | |
| 201 | foreach(exe IN LISTS executables_mbedcrypto) |
| 202 | add_executable(${exe} ${exe}.c $<TARGET_OBJECTS:mbedtls_test>) |
| 203 | - target_link_libraries(${exe} mbedcrypto) |
| 204 | + target_link_libraries(${exe} ${mbedcrypto_target}) |
| 205 | endforeach() |
| 206 | |
| 207 | install(TARGETS ${executables_mbedtls} ${executables_mbedcrypto} |
| 208 | diff --git a/programs/psa/CMakeLists.txt b/programs/psa/CMakeLists.txt |
| 209 | index 5cbcf7191..23e85fea7 100644 |
| 210 | --- a/programs/psa/CMakeLists.txt |
| 211 | +++ b/programs/psa/CMakeLists.txt |
| 212 | @@ -6,7 +6,7 @@ set(executables |
| 213 | |
| 214 | foreach(exe IN LISTS executables) |
| 215 | add_executable(${exe} ${exe}.c $<TARGET_OBJECTS:mbedtls_test>) |
| 216 | - target_link_libraries(${exe} mbedcrypto) |
| 217 | + target_link_libraries(${exe} ${mbedcrypto_target}) |
| 218 | target_include_directories(${exe} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../tests/include) |
| 219 | endforeach() |
| 220 | |
| 221 | diff --git a/programs/random/CMakeLists.txt b/programs/random/CMakeLists.txt |
| 222 | index 95acb7e10..8df836580 100644 |
| 223 | --- a/programs/random/CMakeLists.txt |
| 224 | +++ b/programs/random/CMakeLists.txt |
| 225 | @@ -6,7 +6,7 @@ set(executables |
| 226 | |
| 227 | foreach(exe IN LISTS executables) |
| 228 | add_executable(${exe} ${exe}.c $<TARGET_OBJECTS:mbedtls_test>) |
| 229 | - target_link_libraries(${exe} mbedcrypto) |
| 230 | + target_link_libraries(${exe} ${mbedcrypto_target}) |
| 231 | endforeach() |
| 232 | |
| 233 | install(TARGETS ${executables} |
| 234 | diff --git a/programs/ssl/CMakeLists.txt b/programs/ssl/CMakeLists.txt |
| 235 | index 28fbfc5a7..ca0a6a429 100644 |
| 236 | --- a/programs/ssl/CMakeLists.txt |
| 237 | +++ b/programs/ssl/CMakeLists.txt |
| 238 | @@ -2,7 +2,7 @@ set(THREADS_USE_PTHREADS_WIN32 true) |
| 239 | find_package(Threads) |
| 240 | |
| 241 | set(libs |
| 242 | - mbedtls |
| 243 | + ${mbedtls_target} |
| 244 | ) |
| 245 | |
| 246 | if(USE_PKCS11_HELPER_LIBRARY) |
| 247 | diff --git a/programs/test/CMakeLists.txt b/programs/test/CMakeLists.txt |
| 248 | index 0df0becd9..217741bf9 100644 |
| 249 | --- a/programs/test/CMakeLists.txt |
| 250 | +++ b/programs/test/CMakeLists.txt |
| 251 | @@ -1,5 +1,5 @@ |
| 252 | set(libs |
| 253 | - mbedtls |
| 254 | + ${mbedtls_target} |
| 255 | ) |
| 256 | |
| 257 | if(USE_PKCS11_HELPER_LIBRARY) |
| 258 | @@ -33,7 +33,7 @@ foreach(exe IN LISTS executables_libs executables_mbedcrypto) |
| 259 | if (${exe_index} GREATER -1) |
| 260 | target_link_libraries(${exe} ${libs}) |
| 261 | else() |
| 262 | - target_link_libraries(${exe} mbedcrypto) |
| 263 | + target_link_libraries(${exe} ${mbedcrypto_target}) |
| 264 | endif() |
| 265 | endforeach() |
| 266 | |
| 267 | diff --git a/programs/test/cmake_subproject/CMakeLists.txt b/programs/test/cmake_subproject/CMakeLists.txt |
| 268 | index 3e32c5fc3..a9fcfde29 100644 |
| 269 | --- a/programs/test/cmake_subproject/CMakeLists.txt |
| 270 | +++ b/programs/test/cmake_subproject/CMakeLists.txt |
| 271 | @@ -1,5 +1,8 @@ |
| 272 | cmake_minimum_required(VERSION 2.6) |
| 273 | |
| 274 | +# Test the target renaming support by adding a prefix to the targets built |
| 275 | +set(MBEDTLS_TARGET_PREFIX subproject_test_) |
| 276 | + |
| 277 | # We use the parent Mbed TLS directory as the MBEDTLS_DIR for this test. Other |
| 278 | # projects that use Mbed TLS as a subproject are likely to add by their own |
| 279 | # relative paths. |
| 280 | @@ -8,11 +11,12 @@ set(MBEDTLS_DIR ../../../) |
| 281 | # Add Mbed TLS as a subdirectory. |
| 282 | add_subdirectory(${MBEDTLS_DIR} build) |
| 283 | |
| 284 | -# Link against all the Mbed TLS libraries. |
| 285 | +# Link against all the Mbed TLS libraries. Verifies that the targets have been |
| 286 | +# created using the specified prefix |
| 287 | set(libs |
| 288 | - mbedcrypto |
| 289 | - mbedx509 |
| 290 | - mbedtls |
| 291 | + subproject_test_mbedcrypto |
| 292 | + subproject_test_mbedx509 |
| 293 | + subproject_test_mbedtls |
| 294 | ) |
| 295 | |
| 296 | add_executable(cmake_subproject cmake_subproject.c) |
| 297 | diff --git a/programs/util/CMakeLists.txt b/programs/util/CMakeLists.txt |
| 298 | index cb14a3ee6..2a11212ec 100644 |
| 299 | --- a/programs/util/CMakeLists.txt |
| 300 | +++ b/programs/util/CMakeLists.txt |
| 301 | @@ -1,5 +1,5 @@ |
| 302 | set(libs |
| 303 | - mbedcrypto |
| 304 | + ${mbedcrypto_target} |
| 305 | ) |
| 306 | |
| 307 | set(executables |
| 308 | diff --git a/programs/x509/CMakeLists.txt b/programs/x509/CMakeLists.txt |
| 309 | index f7b5fe1d9..29cbeb800 100644 |
| 310 | --- a/programs/x509/CMakeLists.txt |
| 311 | +++ b/programs/x509/CMakeLists.txt |
| 312 | @@ -1,5 +1,5 @@ |
| 313 | set(libs |
| 314 | - mbedx509 |
| 315 | + ${mbedx509_target} |
| 316 | ) |
| 317 | |
| 318 | if(USE_PKCS11_HELPER_LIBRARY) |
| 319 | @@ -23,7 +23,7 @@ foreach(exe IN LISTS executables) |
| 320 | target_link_libraries(${exe} ${libs}) |
| 321 | endforeach() |
| 322 | |
| 323 | -target_link_libraries(cert_app mbedtls) |
| 324 | +target_link_libraries(cert_app ${mbedtls_target}) |
| 325 | |
| 326 | install(TARGETS ${executables} |
| 327 | DESTINATION "bin" |
| 328 | diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt |
| 329 | index cc6866309..580d936c0 100644 |
| 330 | --- a/tests/CMakeLists.txt |
| 331 | +++ b/tests/CMakeLists.txt |
| 332 | @@ -1,5 +1,5 @@ |
| 333 | set(libs |
| 334 | - mbedtls |
| 335 | + ${mbedtls_target} |
| 336 | ) |
| 337 | |
| 338 | # Set the project root directory if it's not already defined, as may happen if |
| 339 | @@ -43,7 +43,7 @@ function(add_test_suite suite_name) |
| 340 | add_custom_command( |
| 341 | OUTPUT test_suite_${data_name}.c |
| 342 | 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 . |
| 343 | - 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 |
| 344 | + 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 |
| 345 | ) |
| 346 | |
| 347 | add_executable(test_suite_${data_name} test_suite_${data_name}.c $<TARGET_OBJECTS:mbedtls_test>) |
| 348 | -- |
| 349 | 2.20.1 |
| 350 | |