From 19ead5e5e65d8dcee9d15adec3e126da6ff4209a Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Thu, 27 Apr 2023 11:35:56 +0100 Subject: [PATCH 1/2] cmake: Use "coverage" preset instead of "Coverage" build type --- CMakeLists.txt | 8 +++----- CMakePresets.json | 13 ++++++++++++- src/CMakeLists.txt | 10 +++++----- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fe716c8bde..dd8d76851d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -134,6 +134,8 @@ endif() option(SECP256K1_BUILD_BENCHMARK "Build benchmarks." ON) option(SECP256K1_BUILD_TESTS "Build tests." ON) +option(SECP256K1_BUILD_VERIFY_TESTS "Build tests with -DVERIFY." ${SECP256K1_BUILD_TESTS}) +mark_as_advanced(SECP256K1_BUILD_VERIFY_TESTS) option(SECP256K1_BUILD_EXHAUSTIVE_TESTS "Build exhaustive tests." ON) option(SECP256K1_BUILD_CTIME_TESTS "Build constant-time tests." ${SECP256K1_VALGRIND}) option(SECP256K1_BUILD_EXAMPLES "Build examples." OFF) @@ -258,11 +260,7 @@ endif() message("Optional binaries:") message(" benchmark ........................... ${SECP256K1_BUILD_BENCHMARK}") message(" noverify_tests ...................... ${SECP256K1_BUILD_TESTS}") -set(tests_status "${SECP256K1_BUILD_TESTS}") -if(CMAKE_BUILD_TYPE STREQUAL "Coverage") - set(tests_status OFF) -endif() -message(" tests ............................... ${tests_status}") +message(" tests ............................... ${SECP256K1_BUILD_VERIFY_TESTS}") message(" exhaustive tests .................... ${SECP256K1_BUILD_EXHAUSTIVE_TESTS}") message(" ctime_tests ......................... ${SECP256K1_BUILD_CTIME_TESTS}") message(" examples ............................ ${SECP256K1_BUILD_EXAMPLES}") diff --git a/CMakePresets.json b/CMakePresets.json index b35cd80579..b551e222e2 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -1,5 +1,5 @@ { - "cmakeMinimumRequired": {"major": 3, "minor": 21, "patch": 0}, + "cmakeMinimumRequired": {"major": 3, "minor": 21, "patch": 0}, "version": 3, "configurePresets": [ { @@ -14,6 +14,17 @@ "dev": true, "uninitialized": true } + }, + { + "name": "coverage", + "displayName": "Build for coverage analysis", + "generator": "Unix Makefiles", + "cacheVariables": { + "CMAKE_C_COMPILER": "gcc", + "CMAKE_C_FLAGS": "-DCOVERAGE=1 -g -O0 --coverage", + "CMAKE_BUILD_TYPE": "None", + "SECP256K1_BUILD_VERIFY_TESTS": "OFF" + } } ] } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 24eb0d3011..4da37a8505 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -52,8 +52,8 @@ if(SECP256K1_BUILD_TESTS) add_executable(noverify_tests tests.c) target_link_libraries(noverify_tests secp256k1_precomputed secp256k1_asm) add_test(noverify_tests noverify_tests) - if(NOT CMAKE_BUILD_TYPE STREQUAL "Coverage") - add_executable(tests tests.c) + if(SECP256K1_BUILD_VERIFY_TESTS) + add_executable(tests tests.c ${internal_obj}) target_compile_definitions(tests PRIVATE VERIFY) target_link_libraries(tests secp256k1_precomputed secp256k1_asm) add_test(tests tests) @@ -62,9 +62,9 @@ endif() if(SECP256K1_BUILD_EXHAUSTIVE_TESTS) # Note: do not include secp256k1_precomputed in exhaustive_tests (it uses runtime-generated tables). - add_executable(exhaustive_tests tests_exhaustive.c) - target_link_libraries(exhaustive_tests secp256k1_asm) - target_compile_definitions(exhaustive_tests PRIVATE $<$>:VERIFY>) + add_executable(exhaustive_tests tests_exhaustive.c ${common_obj}) + target_compile_definitions(exhaustive_tests PRIVATE $<$>:VERIFY>) + target_link_libraries(exhaustive_tests binary_interface) add_test(exhaustive_tests exhaustive_tests) endif() From 98bed068c6abba5b19c89dbd34ab987e4fd7ff98 Mon Sep 17 00:00:00 2001 From: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com> Date: Tue, 28 Mar 2023 14:30:04 +0100 Subject: [PATCH 2/2] cmake, doc: Document test coverage using CMake-based build system --- README.md | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 19dabe8505..c2dc9fa9f8 100644 --- a/README.md +++ b/README.md @@ -122,22 +122,31 @@ Test coverage This library aims to have full coverage of the reachable lines and branches. -To create a test coverage report, configure with `--enable-coverage` (use of GCC is necessary): +To create a test coverage report, configure for coverage analysis (use of GCC is necessary), build and run the tests: - $ ./configure --enable-coverage +* using Autotools-based build system: -Run the tests: +``` +$ ./configure --enable-coverage +$ make check +``` - $ make check +* using CMake-based build system: + +``` +$ cmake --preset coverage -S . -B build +$ cmake --build build +$ ctest --test-dir build +``` To create a report, `gcovr` is recommended, as it includes branch coverage reporting: - $ gcovr --exclude 'src/bench*' --print-summary + $ gcovr --exclude 'build/*' --exclude 'src/bench*' --print-summary To create a HTML report with coloured and annotated source code: $ mkdir -p coverage - $ gcovr --exclude 'src/bench*' --html --html-details -o coverage/coverage.html + $ gcovr --exclude 'build/*' --exclude 'src/bench*' --html --html-details -o coverage/coverage.html Benchmark ------------