Skip to content

Commit e8451f2

Browse files
committed
CMake: Add a subdirectory build regression test
If we have a regression with the "build Mbed Crypto as a subdirectory with CMake" feature and fail to build, fail the test.
1 parent 77dd25d commit e8451f2

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
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/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
@@ -1046,6 +1051,19 @@ component_test_cmake_out_of_source () {
10461051
unset MBEDTLS_ROOT_DIR
10471052
}
10481053

1054+
component_test_cmake_as_subdirectory () {
1055+
msg "build: cmake 'as-subdirectory' build"
1056+
MBEDTLS_ROOT_DIR="$PWD"
1057+
1058+
cd programs/test/cmake_subproject
1059+
cmake .
1060+
make
1061+
if_build_succeeded ./cmake_subproject
1062+
1063+
cd "$MBEDTLS_ROOT_DIR"
1064+
unset MBEDTLS_ROOT_DIR
1065+
}
1066+
10491067
component_test_zeroize () {
10501068
# Test that the function mbedtls_platform_zeroize() is not optimized away by
10511069
# different combinations of compilers and optimization flags by using an

0 commit comments

Comments
 (0)