Skip to content

Commit ee6f9b2

Browse files
authored
Merge pull request #152 from Patater/cmake-subproject-fix
Enable use of Mbed TLS and Mbed Crypto as a CMake subproject
2 parents 8646a92 + e8451f2 commit ee6f9b2

File tree

7 files changed

+121
-10
lines changed

7 files changed

+121
-10
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ else()
55
project("mbed TLS" C)
66
endif()
77

8+
# Set the project root directory.
9+
set(MBEDTLS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
10+
811
option(ENABLE_PROGRAMS "Build mbed TLS programs." ON)
912

1013
option(UNSAFE_BUILD "Allow unsafe builds. These builds ARE NOT SECURE." OFF)

library/CMakeLists.txt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ option(USE_STATIC_MBEDTLS_LIBRARY "Build mbed TLS static library." ON)
22
option(USE_SHARED_MBEDTLS_LIBRARY "Build mbed TLS shared library." OFF)
33
option(LINK_WITH_PTHREAD "Explicitly link mbed TLS library to pthread." OFF)
44

5+
# Set the project root directory if it's not already defined, as may happen if
6+
# the library folder is included directly by a parent project, without
7+
# including the top level CMakeLists.txt.
8+
if(NOT DEFINED MBEDTLS_DIR)
9+
set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR})
10+
endif()
11+
512
set(src_crypto
613
aes.c
714
aesni.c
@@ -72,9 +79,9 @@ set(src_crypto
7279
if(USE_CRYPTO_SUBMODULE)
7380
set(src_crypto
7481
${src_crypto}
75-
${CMAKE_SOURCE_DIR}/library/version.c
76-
${CMAKE_SOURCE_DIR}/library/version_features.c
77-
${CMAKE_SOURCE_DIR}/library/error.c
82+
${MBEDTLS_DIR}/library/version.c
83+
${MBEDTLS_DIR}/library/version_features.c
84+
${MBEDTLS_DIR}/library/error.c
7885
)
7986
else()
8087
set(src_crypto
@@ -133,8 +140,8 @@ if(USE_STATIC_MBEDTLS_LIBRARY)
133140
set_target_properties(${mbedcrypto_static_target} PROPERTIES OUTPUT_NAME mbedcrypto)
134141
target_link_libraries(${mbedcrypto_static_target} ${libs})
135142
target_include_directories(${mbedcrypto_static_target}
136-
PUBLIC ${CMAKE_SOURCE_DIR}/include/
137-
PUBLIC ${CMAKE_SOURCE_DIR}/crypto/include/)
143+
PUBLIC ${MBEDTLS_DIR}/include/
144+
PUBLIC ${MBEDTLS_DIR}/crypto/include/)
138145

139146
install(TARGETS ${mbedcrypto_static_target}
140147
DESTINATION ${LIB_INSTALL_DIR}
@@ -146,8 +153,8 @@ if(USE_SHARED_MBEDTLS_LIBRARY)
146153
set_target_properties(mbedcrypto PROPERTIES VERSION 2.17.0 SOVERSION 3)
147154
target_link_libraries(mbedcrypto ${libs})
148155
target_include_directories(mbedcrypto
149-
PUBLIC ${CMAKE_SOURCE_DIR}/include/
150-
PUBLIC ${CMAKE_SOURCE_DIR}/crypto/include/)
156+
PUBLIC ${MBEDTLS_DIR}/include/
157+
PUBLIC ${MBEDTLS_DIR}/crypto/include/)
151158

152159
install(TARGETS mbedcrypto
153160
DESTINATION ${LIB_INSTALL_DIR}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
Makefile
3+
cmake_subproject
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
cmake_minimum_required(VERSION 2.6)
2+
3+
# We use the parent Mbed TLS directory as the MBEDTLS_DIR for this test. Other
4+
# projects that use Mbed TLS as a subproject are likely to add by their own
5+
# relative paths.
6+
set(MBEDTLS_DIR ../../../)
7+
8+
# Add Mbed TLS as a subdirectory.
9+
add_subdirectory(${MBEDTLS_DIR} build)
10+
11+
# Link against the Mbed Crypto library.
12+
set(libs
13+
mbedcrypto
14+
)
15+
16+
add_executable(cmake_subproject cmake_subproject.c)
17+
target_link_libraries(cmake_subproject ${libs})
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Simple program to test that CMake builds with Mbed TLS as a subdirectory
3+
* work correctly.
4+
*
5+
* Copyright (C) 2006-2019, ARM Limited, All Rights Reserved
6+
* SPDX-License-Identifier: Apache-2.0
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
9+
* not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*
20+
* This file is part of mbed TLS (https://tls.mbed.org)
21+
*/
22+
23+
#if !defined(MBEDTLS_CONFIG_FILE)
24+
#include "mbedtls/config.h"
25+
#else
26+
#include MBEDTLS_CONFIG_FILE
27+
#endif
28+
29+
#if defined(MBEDTLS_PLATFORM_C)
30+
#include "mbedtls/platform.h"
31+
#else
32+
#include <stdio.h>
33+
#include <stdlib.h>
34+
#define mbedtls_fprintf fprintf
35+
#define mbedtls_printf printf
36+
#define mbedtls_exit exit
37+
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
38+
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
39+
#endif /* MBEDTLS_PLATFORM_C */
40+
41+
#include "mbedtls/version.h"
42+
43+
/* The main reason to build this is for testing the CMake build, so the program
44+
* doesn't need to do very much. It calls a single library function to ensure
45+
* linkage works, but that is all. */
46+
int main()
47+
{
48+
/* This version string is 18 bytes long, as advised by version.h. */
49+
char version[18];
50+
51+
mbedtls_version_get_string_full( version );
52+
53+
mbedtls_printf( "Built against %s\n", version );
54+
55+
return( 0 );
56+
}

tests/CMakeLists.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ set(libs
22
mbedcrypto
33
)
44

5+
# Set the project root directory if it's not already defined, as may happen if
6+
# the tests folder is included directly by a parent project, without including
7+
# the top level CMakeLists.txt.
8+
if(NOT DEFINED MBEDTLS_DIR)
9+
set(MBEDTLS_DIR ${CMAKE_SOURCE_DIR})
10+
endif()
11+
512
find_package(Perl)
613
if(NOT PERL_FOUND)
714
message(FATAL_ERROR "Cannot build test suites without Perl")
@@ -43,9 +50,9 @@ function(add_test_suite suite_name)
4350
add_executable(${exe_name} test_suite_${data_name}.c)
4451
target_link_libraries(${exe_name} ${libs})
4552
target_include_directories(${exe_name}
46-
PUBLIC ${CMAKE_SOURCE_DIR}/include/
47-
PUBLIC ${CMAKE_SOURCE_DIR}/crypto/include/
48-
PRIVATE ${CMAKE_SOURCE_DIR}/crypto/library/)
53+
PUBLIC ${MBEDTLS_DIR}/include/
54+
PUBLIC ${MBEDTLS_DIR}/crypto/include/
55+
PUBLIC ${MBEDTLS_DIR}/crypto/library/)
4956

5057
if(${data_name} MATCHES ${SKIP_TEST_SUITES_REGEX})
5158
message(STATUS "The test suite ${data_name} will not be executed.")

tests/scripts/all.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ cleanup()
218218
git update-index --no-skip-worktree Makefile library/Makefile programs/Makefile tests/Makefile
219219
git checkout -- Makefile library/Makefile programs/Makefile tests/Makefile
220220

221+
# Remove any artifacts from the component_test_cmake_as_subdirectory test.
222+
rm -rf programs/test/cmake_subproject/build
223+
rm -f programs/test/cmake_subproject/Makefile
224+
rm -f programs/test/cmake_subproject/cmake_subproject
225+
221226
if [ -f "$CONFIG_BAK" ]; then
222227
mv "$CONFIG_BAK" "$CONFIG_H"
223228
fi
@@ -1056,6 +1061,19 @@ component_test_cmake_out_of_source () {
10561061
unset MBEDTLS_ROOT_DIR
10571062
}
10581063

1064+
component_test_cmake_as_subdirectory () {
1065+
msg "build: cmake 'as-subdirectory' build"
1066+
MBEDTLS_ROOT_DIR="$PWD"
1067+
1068+
cd programs/test/cmake_subproject
1069+
cmake .
1070+
make
1071+
if_build_succeeded ./cmake_subproject
1072+
1073+
cd "$MBEDTLS_ROOT_DIR"
1074+
unset MBEDTLS_ROOT_DIR
1075+
}
1076+
10591077
component_test_zeroize () {
10601078
# Test that the function mbedtls_platform_zeroize() is not optimized away by
10611079
# different combinations of compilers and optimization flags by using an

0 commit comments

Comments
 (0)