Skip to content

Commit d259e34

Browse files
committed
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>
1 parent 0c1a42a commit d259e34

File tree

14 files changed

+324
-41
lines changed

14 files changed

+324
-41
lines changed

3rdparty/CMakeLists.txt

-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
list (APPEND thirdparty_src)
2-
list (APPEND thirdparty_lib)
3-
list (APPEND thirdparty_inc_public)
4-
list (APPEND thirdparty_inc)
5-
list (APPEND thirdparty_def)
6-
71
execute_process(COMMAND ${MBEDTLS_PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/../scripts/config.py -f ${CMAKE_CURRENT_SOURCE_DIR}/../include/mbedtls/config.h get MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED RESULT_VARIABLE result)
82

93
if(${result} EQUAL 0)
104
add_subdirectory(everest)
115
endif()
12-
13-
set(thirdparty_src ${thirdparty_src} PARENT_SCOPE)
14-
set(thirdparty_lib ${thirdparty_lib} PARENT_SCOPE)
15-
set(thirdparty_inc_public ${thirdparty_inc_public} PARENT_SCOPE)
16-
set(thirdparty_inc ${thirdparty_inc} PARENT_SCOPE)
17-
set(thirdparty_def ${thirdparty_def} PARENT_SCOPE)

3rdparty/everest/CMakeLists.txt

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
list (APPEND everest_src)
2-
list (APPEND everest_inc_public)
3-
list (APPEND everest_inc)
4-
list (APPEND everest_def)
1+
add_library(everest
2+
library/everest.c
3+
library/x25519.c
4+
library/Hacl_Curve25519_joined.c)
55

6-
set(everest_src
7-
${CMAKE_CURRENT_SOURCE_DIR}/library/everest.c
8-
${CMAKE_CURRENT_SOURCE_DIR}/library/x25519.c
9-
${CMAKE_CURRENT_SOURCE_DIR}/library/Hacl_Curve25519_joined.c
10-
)
11-
12-
list(APPEND everest_inc_public ${CMAKE_CURRENT_SOURCE_DIR}/include)
13-
list(APPEND everest_inc ${CMAKE_CURRENT_SOURCE_DIR}/include/everest ${CMAKE_CURRENT_SOURCE_DIR}/include/everest/kremlib)
6+
target_include_directories(everest
7+
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
8+
$<BUILD_INTERFACE:${MBEDTLS_DIR}/include>
9+
$<INSTALL_INTERFACE:include>
10+
PRIVATE include/everest
11+
include/everest/kremlib
12+
${MBEDTLS_DIR}/library/)
1413

1514
if(INSTALL_MBEDTLS_HEADERS)
1615

@@ -22,7 +21,7 @@ if(INSTALL_MBEDTLS_HEADERS)
2221

2322
endif(INSTALL_MBEDTLS_HEADERS)
2423

25-
set(thirdparty_src ${thirdparty_src} ${everest_src} PARENT_SCOPE)
26-
set(thirdparty_inc_public ${thirdparty_inc_public} ${everest_inc_public} PARENT_SCOPE)
27-
set(thirdparty_inc ${thirdparty_inc} ${everest_inc} PARENT_SCOPE)
28-
set(thirdparty_def ${thirdparty_def} ${everest_def} PARENT_SCOPE)
24+
install(TARGETS everest
25+
EXPORT MbedTLSTargets
26+
DESTINATION ${LIB_INSTALL_DIR}
27+
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)

CMakeLists.txt

+36-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
cmake_minimum_required(VERSION 2.8.12)
2020

21+
include(CMakePackageConfigHelpers)
22+
2123
# https://cmake.org/cmake/help/latest/policy/CMP0011.html
2224
# Setting this policy is required in CMake >= 3.18.0, otherwise a warning is generated. The OLD
2325
# policy setting is deprecated, and will be removed in future versions.
@@ -221,7 +223,6 @@ endif()
221223
add_subdirectory(include)
222224

223225
add_subdirectory(3rdparty)
224-
list(APPEND libs ${thirdparty_lib})
225226

226227
add_subdirectory(library)
227228

@@ -300,3 +301,37 @@ if(ENABLE_TESTING)
300301
${CMAKE_CURRENT_BINARY_DIR}/DartConfiguration.tcl COPYONLY)
301302
endif()
302303
endif()
304+
305+
configure_package_config_file(
306+
"cmake/MbedTLSConfig.cmake.in"
307+
"cmake/MbedTLSConfig.cmake"
308+
INSTALL_DESTINATION "cmake")
309+
310+
write_basic_package_version_file(
311+
"cmake/MbedTLSConfigVersion.cmake"
312+
COMPATIBILITY SameMajorVersion
313+
VERSION 2.26.0)
314+
315+
install(
316+
FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake/MbedTLSConfig.cmake"
317+
"${CMAKE_CURRENT_BINARY_DIR}/cmake/MbedTLSConfigVersion.cmake"
318+
DESTINATION "cmake")
319+
320+
export(
321+
EXPORT MbedTLSTargets
322+
NAMESPACE MbedTLS::
323+
FILE "cmake/MbedTLSTargets.cmake")
324+
325+
install(
326+
EXPORT MbedTLSTargets
327+
NAMESPACE MbedTLS::
328+
DESTINATION "cmake"
329+
FILE "MbedTLSTargets.cmake")
330+
331+
if(CMAKE_VERSION VERSION_GREATER 3.14)
332+
# Do not export the package by default
333+
cmake_policy(SET CMP0090 NEW)
334+
335+
# Make this package visible to the system
336+
export(PACKAGE MbedTLS)
337+
endif()
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Changes
2+
* Add CMake package config generation for CMake projects consuming Mbed TLS.

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,33 @@ Regarding variables, also note that if you set CFLAGS when invoking cmake,
181181
your value of CFLAGS doesn't override the content provided by cmake (depending
182182
on the build mode as seen above), it's merely prepended to it.
183183

184+
#### Consuming Mbed TLS
185+
186+
Mbed TLS provides a package config file for consumption as a dependency in other
187+
CMake projects. You can include Mbed TLS's CMake targets yourself with:
188+
189+
find_package(MbedTLS)
190+
191+
If prompted, set `MbedTLS_DIR` to `${YOUR_MBEDTLS_INSTALL_DIR}/cmake`. This
192+
creates the following targets:
193+
194+
- `MbedTLS::mbedcrypto` (Crypto library)
195+
- `MbedTLS::mbedtls` (TLS library)
196+
- `MbedTLS::mbedx509` (X509 library)
197+
198+
You can then use these directly through `target_link_libraries()`:
199+
200+
add_executable(xyz)
201+
202+
target_link_libraries(xyz
203+
PUBLIC MbedTLS::mbedtls
204+
MbedTLS::mbedcrypto
205+
MbedTLS::mbedx509)
206+
207+
This will link the Mbed TLS libraries to your library or application, and add
208+
its include directories to your target (transitively, in the case of `PUBLIC` or
209+
`INTERFACE` link libraries).
210+
184211
#### Mbed TLS as a subproject
185212

186213
Mbed TLS supports being built as a CMake subproject. One can

cmake/MbedTLSConfig.cmake.in

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/MbedTLSTargets.cmake")

library/CMakeLists.txt

+16-11
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ set(src_crypto
8787
xtea.c
8888
)
8989

90-
list(APPEND src_crypto ${thirdparty_src})
91-
9290
set(src_x509
9391
x509.c
9492
x509_create.c
@@ -180,6 +178,10 @@ if(USE_STATIC_MBEDTLS_LIBRARY)
180178
set_target_properties(${mbedcrypto_static_target} PROPERTIES OUTPUT_NAME mbedcrypto)
181179
target_link_libraries(${mbedcrypto_static_target} PUBLIC ${libs})
182180

181+
if(TARGET everest)
182+
target_link_libraries(${mbedcrypto_static_target} PUBLIC everest)
183+
endif()
184+
183185
add_library(${mbedx509_static_target} STATIC ${src_x509})
184186
set_target_properties(${mbedx509_static_target} PROPERTIES OUTPUT_NAME mbedx509)
185187
target_link_libraries(${mbedx509_static_target} PUBLIC ${libs} ${mbedcrypto_static_target})
@@ -194,6 +196,10 @@ if(USE_SHARED_MBEDTLS_LIBRARY)
194196
set_target_properties(${mbedcrypto_target} PROPERTIES VERSION 2.26.0 SOVERSION 6)
195197
target_link_libraries(${mbedcrypto_target} PUBLIC ${libs})
196198

199+
if(TARGET everest)
200+
target_link_libraries(${mbedcrypto_target} PUBLIC everest)
201+
endif()
202+
197203
add_library(${mbedx509_target} SHARED ${src_x509})
198204
set_target_properties(${mbedx509_target} PROPERTIES VERSION 2.26.0 SOVERSION 1)
199205
target_link_libraries(${mbedx509_target} PUBLIC ${libs} ${mbedcrypto_target})
@@ -210,15 +216,14 @@ foreach(target IN LISTS target_libraries)
210216
# /library needs to be listed explicitly when building .c files outside
211217
# of /library (which currently means: under /3rdparty).
212218
target_include_directories(${target}
213-
PUBLIC ${MBEDTLS_DIR}/include/
214-
PUBLIC ${thirdparty_inc_public}
215-
PRIVATE ${MBEDTLS_DIR}/library/
216-
PRIVATE ${thirdparty_inc})
217-
target_compile_definitions(${target}
218-
PRIVATE ${thirdparty_def})
219-
install(TARGETS ${target}
220-
DESTINATION ${LIB_INSTALL_DIR}
221-
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
219+
PUBLIC $<BUILD_INTERFACE:${MBEDTLS_DIR}/include/>
220+
$<INSTALL_INTERFACE:include/>
221+
PRIVATE ${MBEDTLS_DIR}/library/)
222+
install(
223+
TARGETS ${target}
224+
EXPORT MbedTLSTargets
225+
DESTINATION ${LIB_INSTALL_DIR}
226+
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
222227
endforeach(target)
223228

224229
set(lib_target "${MBEDTLS_TARGET_PREFIX}lib")
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
Makefile
3+
cmake_package
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
cmake_minimum_required(VERSION 2.8.12)
2+
3+
#
4+
# Simulate configuring and building Mbed TLS as the user might do it. We'll
5+
# skip installing it, and use the build directory directly instead.
6+
#
7+
8+
set(MbedTLS_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../..")
9+
set(MbedTLS_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/mbedtls")
10+
11+
execute_process(
12+
COMMAND "${CMAKE_COMMAND}"
13+
"-H${MbedTLS_SOURCE_DIR}"
14+
"-B${MbedTLS_BINARY_DIR}"
15+
"-DENABLE_PROGRAMS=NO"
16+
"-DENABLE_TESTING=NO")
17+
18+
execute_process(
19+
COMMAND "${CMAKE_COMMAND}"
20+
--build "${MbedTLS_BINARY_DIR}")
21+
22+
#
23+
# Locate the package.
24+
#
25+
26+
set(MbedTLS_DIR "${MbedTLS_BINARY_DIR}/cmake")
27+
find_package(MbedTLS REQUIRED)
28+
29+
#
30+
# At this point, the Mbed TLS targets should have been imported, and we can now
31+
# link to them from our own program.
32+
#
33+
34+
add_executable(cmake_package cmake_package.c)
35+
target_link_libraries(cmake_package
36+
MbedTLS::mbedcrypto MbedTLS::mbedtls MbedTLS::mbedx509)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Simple program to test that Mbed TLS builds correctly as a CMake package.
3+
*
4+
* Copyright The Mbed TLS Contributors
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
8+
* not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#if !defined(MBEDTLS_CONFIG_FILE)
21+
#include "mbedtls/config.h"
22+
#else
23+
#include MBEDTLS_CONFIG_FILE
24+
#endif
25+
26+
#if defined(MBEDTLS_PLATFORM_C)
27+
#include "mbedtls/platform.h"
28+
#else
29+
#include <stdio.h>
30+
#include <stdlib.h>
31+
#define mbedtls_fprintf fprintf
32+
#define mbedtls_printf printf
33+
#define mbedtls_exit exit
34+
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
35+
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
36+
#endif /* MBEDTLS_PLATFORM_C */
37+
38+
#include "mbedtls/version.h"
39+
40+
/* The main reason to build this is for testing the CMake build, so the program
41+
* doesn't need to do very much. It calls a single library function to ensure
42+
* linkage works, but that is all. */
43+
int main()
44+
{
45+
/* This version string is 18 bytes long, as advised by version.h. */
46+
char version[18];
47+
48+
mbedtls_version_get_string_full( version );
49+
50+
mbedtls_printf( "Built against %s\n", version );
51+
52+
return( 0 );
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
Makefile
3+
cmake_package_install
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
cmake_minimum_required(VERSION 2.8.12)
2+
3+
#
4+
# Simulate configuring and building Mbed TLS as the user might do it. We'll
5+
# install into a directory inside our own build directory.
6+
#
7+
8+
set(MbedTLS_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../..")
9+
set(MbedTLS_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/mbedtls")
10+
set(MbedTLS_BINARY_DIR "${MbedTLS_INSTALL_DIR}${CMAKE_FILES_DIRECTORY}")
11+
12+
execute_process(
13+
COMMAND "${CMAKE_COMMAND}"
14+
"-H${MbedTLS_SOURCE_DIR}"
15+
"-B${MbedTLS_BINARY_DIR}"
16+
"-DENABLE_PROGRAMS=NO"
17+
"-DENABLE_TESTING=NO"
18+
"-DCMAKE_INSTALL_PREFIX=${MbedTLS_INSTALL_DIR}")
19+
20+
execute_process(
21+
COMMAND "${CMAKE_COMMAND}"
22+
--build "${MbedTLS_BINARY_DIR}"
23+
--target install)
24+
25+
#
26+
# Locate the package.
27+
#
28+
29+
set(MbedTLS_DIR "${MbedTLS_INSTALL_DIR}/cmake")
30+
find_package(MbedTLS REQUIRED)
31+
32+
#
33+
# At this point, the Mbed TLS targets should have been imported, and we can now
34+
# link to them from our own program.
35+
#
36+
37+
add_executable(cmake_package_install cmake_package_install.c)
38+
target_link_libraries(cmake_package_install
39+
MbedTLS::mbedcrypto MbedTLS::mbedtls MbedTLS::mbedx509)

0 commit comments

Comments
 (0)