From 7e0d201e818a0eb9eca738feebd36ee1f340534e Mon Sep 17 00:00:00 2001 From: Dakshit Babbar Date: Tue, 10 Sep 2024 09:54:52 +0530 Subject: [PATCH 1/4] Add the updates to Cmake files to build unit tests on Mac --- README.md | 13 +++++++++++-- tools/cmock/coverage.cmake | 6 +++--- tools/cmock/create_test.cmake | 32 +++++++++++++++++--------------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index c59471abd..067b0f3ec 100644 --- a/README.md +++ b/README.md @@ -176,9 +176,18 @@ or the following: 1. Go to the root directory of this repository. (Make sure that the **CMock** submodule is cloned as described [above](#checkout-cmock-submodule)) -1. Run the _cmake_ command: `cmake -S test -B build` +1. Run the _cmake_ command: +``` +cmake -S test -B build/ \ + -G "Unix Makefiles" \ + -DCMAKE_BUILD_TYPE=Debug \ + -DBUILD_CLONE_SUBMODULES=ON \ + -DUNITTEST=1 \ + -DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DNDEBUG -DLIBRARY_LOG_LEVEL=LOG_DEBUG' \ +``` +Note: For Mac users, additionally add the `-DCMAKE_C_STANDARD=99` flag to the above command -1. Run this command to build the library and unit tests: `make -C build all` +4. Run this command to build the library and unit tests: `make -C build all` 1. The generated test executables will be present in `build/bin/tests` folder. diff --git a/tools/cmock/coverage.cmake b/tools/cmock/coverage.cmake index e035ea4c7..570f92758 100644 --- a/tools/cmock/coverage.cmake +++ b/tools/cmock/coverage.cmake @@ -15,7 +15,7 @@ execute_process( COMMAND lcov --directory ${CMAKE_BINARY_DIR} --initial --capture --rc lcov_branch_coverage=1 - --rc genhtml_branch_coverage=1 + --include "*source*" --output-file=${CMAKE_BINARY_DIR}/base_coverage.info ) file(GLOB files "${CMAKE_BINARY_DIR}/bin/tests/*") @@ -46,10 +46,10 @@ execute_process(COMMAND ruby execute_process( COMMAND lcov --capture --rc lcov_branch_coverage=1 - --rc genhtml_branch_coverage=1 --base-directory ${CMAKE_BINARY_DIR} --directory ${CMAKE_BINARY_DIR} --output-file ${CMAKE_BINARY_DIR}/second_coverage.info + --include "*source*" ) # combile baseline results (zeros) with the one after running the tests @@ -59,7 +59,7 @@ execute_process( --add-tracefile ${CMAKE_BINARY_DIR}/base_coverage.info --add-tracefile ${CMAKE_BINARY_DIR}/second_coverage.info --output-file ${CMAKE_BINARY_DIR}/coverage.info - --no-external + --include "*source*" --rc lcov_branch_coverage=1 ) execute_process( diff --git a/tools/cmock/create_test.cmake b/tools/cmock/create_test.cmake index 24b7e56fc..eede94183 100644 --- a/tools/cmock/create_test.cmake +++ b/tools/cmock/create_test.cmake @@ -22,9 +22,6 @@ function(create_test test_name COMPILE_FLAG "-O0 -ggdb" RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/tests" INSTALL_RPATH_USE_LINK_PATH TRUE - LINK_FLAGS " \ - -Wl,-rpath,${CMAKE_BINARY_DIR}/lib \ - -Wl,-rpath,${CMAKE_CURRENT_BINARY_DIR}/lib" ) target_include_directories(${test_name} PUBLIC ${mocks_dir} @@ -45,7 +42,7 @@ function(create_test test_name add_dependencies(${test_name} ${dependency}) target_link_libraries(${test_name} ${dependency}) endforeach() - target_link_libraries(${test_name} -lgcov unity) + target_link_libraries(${test_name} unity) target_link_directories(${test_name} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/lib ) @@ -129,10 +126,19 @@ function(create_mock_list mock_name ${mocks_dir} ${mock_include_list} ) - set_target_properties(${mock_name} PROPERTIES - LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib - POSITION_INDEPENDENT_CODE ON + if (APPLE) + set_target_properties(${mock_name} PROPERTIES + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib + POSITION_INDEPENDENT_CODE ON + LINK_FLAGS "-Wl,-undefined,dynamic_lookup" ) + else() + set_target_properties(${mock_name} PROPERTIES + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib + POSITION_INDEPENDENT_CODE ON + ) + endif() + target_compile_definitions(${mock_name} PUBLIC ${mock_define_list} ) @@ -150,19 +156,15 @@ function(create_real_library target target_include_directories(${target} PUBLIC ${real_include_list} ) - set_target_properties(${target} PROPERTIES - COMPILE_FLAGS "-Wextra -Wpedantic \ + set_target_properties(${target} PROPERTIES + COMPILE_FLAGS "-Wextra -Wpedantic \ -fprofile-arcs -ftest-coverage -fprofile-generate \ -Wno-unused-but-set-variable" LINK_FLAGS "-fprofile-arcs -ftest-coverage \ -fprofile-generate " - ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib - ) + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib + ) if(NOT(mock_name STREQUAL "")) add_dependencies(${target} ${mock_name}) - target_link_libraries(${target} - -l${mock_name} - -lgcov - ) endif() endfunction() From bbe3037dc5a955e1427d2e78eb2e082ac7cfcc7c Mon Sep 17 00:00:00 2001 From: Gaurav Aggarwal Date: Tue, 10 Sep 2024 04:46:44 +0000 Subject: [PATCH 2/4] Code review suggestions Signed-off-by: Gaurav Aggarwal --- README.md | 28 ++++++++++++++++------------ tools/cmock/create_test.cmake | 4 ++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 067b0f3ec..b75aed2d9 100644 --- a/README.md +++ b/README.md @@ -176,23 +176,27 @@ or the following: 1. Go to the root directory of this repository. (Make sure that the **CMock** submodule is cloned as described [above](#checkout-cmock-submodule)) -1. Run the _cmake_ command: -``` -cmake -S test -B build/ \ - -G "Unix Makefiles" \ - -DCMAKE_BUILD_TYPE=Debug \ - -DBUILD_CLONE_SUBMODULES=ON \ - -DUNITTEST=1 \ - -DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DNDEBUG -DLIBRARY_LOG_LEVEL=LOG_DEBUG' \ -``` -Note: For Mac users, additionally add the `-DCMAKE_C_STANDARD=99` flag to the above command - -4. Run this command to build the library and unit tests: `make -C build all` +1. Run the _cmake_ command: + ``` + cmake -S test -B build/ \ + -G "Unix Makefiles" \ + -DCMAKE_BUILD_TYPE=Debug \ + -DBUILD_CLONE_SUBMODULES=ON \ + -DUNITTEST=1 \ + -DCMAKE_C_FLAGS='--coverage -Wall -Wextra -Wsign-compare -Werror -DNDEBUG -DLIBRARY_LOG_LEVEL=LOG_DEBUG' + ``` + Note: For Mac users, additionally add the `-DCMAKE_C_STANDARD=99` flag to the + above command. + +1. Run this command to build the library and unit tests: `make -C build all`. 1. The generated test executables will be present in `build/bin/tests` folder. 1. Run `cd build && ctest` to execute all tests and view the test run summary. +1. Run `make coverage` to generate coverage report in the `build/coverage` + folder. + ## CBMC To learn more about CBMC and proofs specifically, review the training material diff --git a/tools/cmock/create_test.cmake b/tools/cmock/create_test.cmake index eede94183..55560c176 100644 --- a/tools/cmock/create_test.cmake +++ b/tools/cmock/create_test.cmake @@ -138,7 +138,7 @@ function(create_mock_list mock_name POSITION_INDEPENDENT_CODE ON ) endif() - + target_compile_definitions(${mock_name} PUBLIC ${mock_define_list} ) @@ -160,7 +160,7 @@ function(create_real_library target COMPILE_FLAGS "-Wextra -Wpedantic \ -fprofile-arcs -ftest-coverage -fprofile-generate \ -Wno-unused-but-set-variable" - LINK_FLAGS "-fprofile-arcs -ftest-coverage \ + LINK_FLAGS "-fprofile-arcs -ftest-coverage \ -fprofile-generate " ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib ) From ba6b6ed004e6d14173b00f0336520c3b0edb99b8 Mon Sep 17 00:00:00 2001 From: Gaurav Aggarwal Date: Tue, 10 Sep 2024 05:02:16 +0000 Subject: [PATCH 3/4] Fix spell check Signed-off-by: Gaurav Aggarwal --- .github/.cSpellWords.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/.cSpellWords.txt b/.github/.cSpellWords.txt index 526fa1220..d2ba4f174 100644 --- a/.github/.cSpellWords.txt +++ b/.github/.cSpellWords.txt @@ -11,11 +11,14 @@ coverity Coverity CSDK ctest +DCMAKE DCMOCK decihours Decihours DECIHOURS +DLIBRARY DNDEBUG +DUNITTEST DUNITY getpacketid isystem @@ -44,4 +47,6 @@ vect Vect VECT Werror +Wextra +Wsign Wunused From 3bf588146f4173a001e52f86ad6906de7a31ccef Mon Sep 17 00:00:00 2001 From: Gaurav Aggarwal Date: Tue, 10 Sep 2024 07:59:45 +0000 Subject: [PATCH 4/4] Fix indentation Signed-off-by: Gaurav Aggarwal --- tools/cmock/create_test.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/cmock/create_test.cmake b/tools/cmock/create_test.cmake index 55560c176..78b08b495 100644 --- a/tools/cmock/create_test.cmake +++ b/tools/cmock/create_test.cmake @@ -156,7 +156,7 @@ function(create_real_library target target_include_directories(${target} PUBLIC ${real_include_list} ) - set_target_properties(${target} PROPERTIES + set_target_properties(${target} PROPERTIES COMPILE_FLAGS "-Wextra -Wpedantic \ -fprofile-arcs -ftest-coverage -fprofile-generate \ -Wno-unused-but-set-variable"