From 84052570355bb5608248f162b622068b7b9eaa7c Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 21 Mar 2018 12:12:47 +0100 Subject: [PATCH 1/7] Support out-of-tree testing with CMake Create extra symbolic links with CMake so that SSL testing (ssl-opt.sh and compat.sh) works in out-of-tree builds. --- CMakeLists.txt | 30 ++++++++++++++++++++++++++++++ include/CMakeLists.txt | 6 ++++++ tests/CMakeLists.txt | 27 ++++++--------------------- 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ca4cba2165..df03dd6073 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,30 @@ set(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE} CACHE STRING "Choose the type of build: None Debug Release Coverage ASan ASanDbg MemSan MemSanDbg Check CheckFull" FORCE) +# Create a symbolic link from ${base_name} in the binary directory +# to the corresponding path in the source directory. +function(link_to_source base_name) + # Get OS dependent path to use in `execute_process` + file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}/${base_name}" link) + file(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/${base_name}" target) + + if (NOT EXISTS ${link}) + if (CMAKE_HOST_UNIX) + set(command ln -s ${target} ${link}) + else() + set(command cmd.exe /c mklink /j ${link} ${target}) + endif() + + execute_process(COMMAND ${command} + RESULT_VARIABLE result + ERROR_VARIABLE output) + + if (NOT ${result} EQUAL 0) + message(FATAL_ERROR "Could not create symbolic link for: ${target} --> ${output}") + endif() + endif() +endfunction(link_to_source) + string(REGEX MATCH "Clang" CMAKE_COMPILER_IS_CLANG "${CMAKE_C_COMPILER_ID}") if(CMAKE_COMPILER_IS_GNUCC) @@ -164,3 +188,9 @@ if(ENABLE_TESTING) ) endif(UNIX) endif() + +# Make scripts and data files needed for testing available in an +# out-of-source build. +if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) + link_to_source(scripts) +endif() diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 985a3530b9..3081b2678b 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -9,3 +9,9 @@ if(INSTALL_MBEDTLS_HEADERS) PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ) endif(INSTALL_MBEDTLS_HEADERS) + +# Make scripts and data files needed for testing available in an +# out-of-source build. +if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) + link_to_source(mbedtls) +endif() diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 16e19a9275..9fd4916bbd 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -105,26 +105,11 @@ add_test_suite(xtea) add_test_suite(x509parse) add_test_suite(x509write) -# Make data_files available in an out-of-source build +# Make scripts and data files needed for testing available in an +# out-of-source build. if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) - # Get OS dependent path to use in `execute_process` - file(TO_NATIVE_PATH "${CMAKE_CURRENT_BINARY_DIR}/data_files" link) - file(TO_NATIVE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/data_files" target) - - if (NOT EXISTS ${link}) - if (CMAKE_HOST_UNIX) - set(command ln -s ${target} ${link}) - else() - set(command cmd.exe /c mklink /j ${link} ${target}) - endif() - - execute_process(COMMAND ${command} - RESULT_VARIABLE result - ERROR_VARIABLE output) - - if (NOT ${result} EQUAL 0) - message(FATAL_ERROR "Could not create symbolic link for: ${target} --> ${output}") - endif() - endif() + link_to_source(compat.sh) + link_to_source(data_files) + link_to_source(scripts) + link_to_source(ssl-opt.sh) endif() - From 31b07e283321b1470c31466d10ca8a8099b73bba Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 21 Mar 2018 12:15:06 +0100 Subject: [PATCH 2/7] all.sh: be more conservative when cleaning up CMake artefacts Only delete things that we expect to find, to avoid deleting other things that people might have lying around in their build tree. Explicitly skip .git to avoid e.g. accidentally matching a branch name. --- tests/scripts/all.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index b559af8e18..7caebd5dc1 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -149,7 +149,13 @@ cleanup() { command make clean - find . -name yotta -prune -o -iname '*cmake*' -not -name CMakeLists.txt -exec rm -rf {} \+ + # Remove CMake artefacts + find . -name .git -prune -o -name yotta -prune -o \ + -iname CMakeFiles -exec rm -rf {} \+ -o \ + \( -iname cmake_install.cmake -o \ + -iname CTestTestfile.cmake -o \ + -iname CMakeCache.txt \) -exec rm {} \+ + # Recover files overwritten by in-tree CMake builds rm -f include/Makefile include/mbedtls/Makefile programs/*/Makefile git update-index --no-skip-worktree Makefile library/Makefile programs/Makefile tests/Makefile git checkout -- Makefile library/Makefile programs/Makefile tests/Makefile From a71d64c74fccffb1affe07cda551f6d073c88a88 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 21 Mar 2018 12:16:57 +0100 Subject: [PATCH 3/7] all.sh: fix cleanup happening during an out-of-tree build --- tests/scripts/all.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 7caebd5dc1..00dc9ca2ee 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -147,6 +147,10 @@ EOF # remove built files as well as the cmake cache/config cleanup() { + if [ -n "${MBEDTLS_ROOT_DIR+set}" ]; then + cd "$MBEDTLS_ROOT_DIR" + fi + command make clean # Remove CMake artefacts @@ -857,6 +861,7 @@ msg "test: cmake 'out-of-source' build" make test cd "$MBEDTLS_ROOT_DIR" rm -rf "$OUT_OF_SOURCE_DIR" +unset MBEDTLS_ROOT_DIR From 0114ffc76bc4ff85b665e27cd180bd2e80968a77 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 21 Mar 2018 12:17:20 +0100 Subject: [PATCH 4/7] all.sh: Verify out-of-tree testing with CMake Run a test case in ssl-opt.sh to validate that testing works in an out-of-tree CMake build. --- tests/scripts/all.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/scripts/all.sh b/tests/scripts/all.sh index 00dc9ca2ee..cbf4837e6d 100755 --- a/tests/scripts/all.sh +++ b/tests/scripts/all.sh @@ -859,6 +859,17 @@ make msg "test: cmake 'out-of-source' build" make test +# Test an SSL option that requires an auxiliary script in test/scripts/. +# Also ensure that there are no error messages such as +# "No such file or directory", which would indicate that some required +# file is missing (ssl-opt.sh tolerates the absence of some files so +# may exit with status 0 but emit errors). +if_build_succeeded ./tests/ssl-opt.sh -f 'Fallback SCSV: beginning of list' 2>ssl-opt.err +if [ -s ssl-opt.err ]; then + cat ssl-opt.err >&2 + record_status [ ! -s ssl-opt.err ] + rm ssl-opt.err +fi cd "$MBEDTLS_ROOT_DIR" rm -rf "$OUT_OF_SOURCE_DIR" unset MBEDTLS_ROOT_DIR From b2f09c32658668d0d79469ed32c86b6836a4beb1 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Wed, 21 Mar 2018 12:38:00 +0100 Subject: [PATCH 5/7] Support out-of-tree testing with CMake: add ChangeLog entry. Fixes #1193 --- ChangeLog | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ChangeLog b/ChangeLog index 8db0215914..5d8673373a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ mbed TLS ChangeLog (Sorted per branch, date) += mbed TLS 2.x.x branch released xxxx-xx-xx + +Changes + * Support TLS testing in out-of-source builds using cmake. Fixes #1193. + = mbed TLS 2.7.0 branch released 2018-02-03 Security From be038366eab275e7157f979b2d57fc0da00eab3e Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 22 Mar 2018 17:12:46 +0100 Subject: [PATCH 6/7] Fix some comments regarding what files are symlinked --- CMakeLists.txt | 3 +-- include/CMakeLists.txt | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index df03dd6073..8b26e6e7c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -189,8 +189,7 @@ if(ENABLE_TESTING) endif(UNIX) endif() -# Make scripts and data files needed for testing available in an -# out-of-source build. +# Make scripts needed for testing available in an out-of-source build. if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) link_to_source(scripts) endif() diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt index 3081b2678b..1b581a54dd 100644 --- a/include/CMakeLists.txt +++ b/include/CMakeLists.txt @@ -10,8 +10,7 @@ if(INSTALL_MBEDTLS_HEADERS) endif(INSTALL_MBEDTLS_HEADERS) -# Make scripts and data files needed for testing available in an -# out-of-source build. +# Make config.h available in an out-of-source build. ssl-opt.sh requires it. if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) link_to_source(mbedtls) endif() From c33c7c8363e998a0d7b54f1f310372757c67ada4 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 5 Apr 2018 15:57:56 +0200 Subject: [PATCH 7/7] Copy DartConfiguration.tcl, needed for make memcheck --- CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b26e6e7c7..e9a632e9bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -192,4 +192,8 @@ endif() # Make scripts needed for testing available in an out-of-source build. if (NOT ${CMAKE_CURRENT_BINARY_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) link_to_source(scripts) + # Copy (don't link) DartConfiguration.tcl, needed for memcheck, to + # keep things simple with the sed commands in the memcheck target. + configure_file(${CMAKE_CURRENT_SOURCE_DIR}/DartConfiguration.tcl + ${CMAKE_CURRENT_BINARY_DIR}/DartConfiguration.tcl COPYONLY) endif()