Skip to content

Commit

Permalink
Add the updates to Cmake files to build unit tests on Mac (#306)
Browse files Browse the repository at this point in the history
<!--- Title -->

Description
-----------
<!--- Describe your changes in detail. -->
Updated the coverage.cmake and create_test.cmake files to build the unit
tests on Mac
Updated the README file to include the correct Cmake command for the
users to use to build the unit tests

Checklist:
----------
<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're
here to help! -->
- [x] I have tested my changes. No regression in existing tests.
- [x] I have modified and/or added unit-tests to cover the code changes
in this Pull Request.

Related Issue
-----------
<!-- If any, please provide issue ID. -->
By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.

---------

Signed-off-by: Gaurav Aggarwal <[email protected]>
Co-authored-by: Dakshit Babbar <[email protected]>
Co-authored-by: Gaurav Aggarwal <[email protected]>
  • Loading branch information
3 people authored Sep 10, 2024
1 parent e7322e8 commit c0c05f9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .github/.cSpellWords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ coverity
Coverity
CSDK
ctest
DCMAKE
DCMOCK
decihours
Decihours
DECIHOURS
DLIBRARY
DNDEBUG
DUNITTEST
DUNITY
getpacketid
isystem
Expand Down Expand Up @@ -44,4 +47,6 @@ vect
Vect
VECT
Werror
Wextra
Wsign
Wunused
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +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`

1. 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
Expand Down
6 changes: 3 additions & 3 deletions tools/cmock/coverage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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/*")
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down
32 changes: 17 additions & 15 deletions tools/cmock/create_test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
)
Expand Down Expand Up @@ -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}
)
Expand All @@ -151,18 +157,14 @@ function(create_real_library target
${real_include_list}
)
set_target_properties(${target} PROPERTIES
COMPILE_FLAGS "-Wextra -Wpedantic \
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
)
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()

0 comments on commit c0c05f9

Please sign in to comment.