Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the updates to Cmake files to build unit tests on Mac #306

Merged
merged 4 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
aggarg marked this conversation as resolved.
Show resolved Hide resolved
```
Note: For Mac users, additionally add the `-DCMAKE_C_STANDARD=99` flag to the
aggarg marked this conversation as resolved.
Show resolved Hide resolved
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
34 changes: 18 additions & 16 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 @@ -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
aggarg marked this conversation as resolved.
Show resolved Hide resolved
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()
Loading