From 0d040e28c3f6276bab1595378d69aa33c9315243 Mon Sep 17 00:00:00 2001 From: Daniel Carroll <6995669+dijidiji@users.noreply.github.com> Date: Fri, 4 Jul 2025 14:22:45 +1000 Subject: [PATCH 1/2] Coverage integration (#110) * Add LCOV custom targets * Add coverage preset for macOS * Add gcovr target for coverage on Mac (and possibly MSYS) --------- Co-authored-by: omckeon --- projects/cmake/CMakeLists.txt | 63 +++++++++++++++++++++++++++++++- projects/cmake/CMakePresets.json | 24 +++++++++++- 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/projects/cmake/CMakeLists.txt b/projects/cmake/CMakeLists.txt index 3a27846c..e416ae4e 100644 --- a/projects/cmake/CMakeLists.txt +++ b/projects/cmake/CMakeLists.txt @@ -17,6 +17,13 @@ set(SK_BIN "${CMAKE_CURRENT_SOURCE_DIR}/../../bin") # FLAGS set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20") +# Option to use GCOV for coverage when compiling and linking +option(USE_COVERAGE "Enable coverage during the build" OFF) +if(USE_COVERAGE) + add_compile_options(--coverage) + add_link_options(--coverage) +endif() + # Includes for CTest/Catch2 integration include(CTest) include("${CMAKE_CURRENT_SOURCE_DIR}/catch/Catch.cmake") @@ -324,4 +331,58 @@ catch_discover_tests(skunit_tests) #### END sktest EXECUTABLE #### install(TARGETS SplashKitBackend DESTINATION lib) -install(FILES ${INCLUDE_FILES} DESTINATION include/SplashKitBackend) \ No newline at end of file +install(FILES ${INCLUDE_FILES} DESTINATION include/SplashKitBackend) + +# Coverage collection with LCOV +# Get absolute path to SK_SRC so we can pass it to LCOV +# and collect coverage only for src files +get_filename_component(SRC_ABS ${SK_SRC} ABSOLUTE) +message(SRC_ABS="${SRC_ABS}") + +if(USE_COVERAGE) + if(APPLE OR MSYS) + find_program(GCOVR gcovr REQUIRED) + else() + find_program(LCOV lcov REQUIRED) + set(LCOV_BASE lcov-base.info) + set(LCOV_TEST lcov-test.info) + set(LCOV_LOG lcov.log) + set(LCOV_ERR lcov.err) + endif() + set(LCOV_TOTAL lcov.info) +endif() + +# lcov coverage targets +# Collect baseline coverage before running tests +add_custom_target(lcov-init-coverage + COMMENT "Collecting initial coverage with lcov" + COMMAND lcov --capture --initial --directory ${CMAKE_CURRENT_BINARY_DIR} --include "'${SRC_ABS}*'" + --output-file ${LCOV_BASE} 2>${LCOV_ERR} >${LCOV_LOG} +) +add_dependencies(lcov-init-coverage lcov-reset-coverage) + +add_custom_target(lcov-reset-coverage + COMMENT "Reset all coverage counters to zero with lcov" + COMMAND lcov --quiet --zerocounters --directory ${CMAKE_CURRENT_BINARY_DIR} + --output-file ${LCOV_BASE} + COMMAND lcov --quiet --zerocounters --directory ${CMAKE_CURRENT_BINARY_DIR} + --output-file ${LCOV_TEST} + COMMAND lcov --quiet --zerocounters --directory ${CMAKE_CURRENT_BINARY_DIR} + --output-file ${LCOV_TOTAL} +) + +# Collect post-test coverage data +add_custom_target(lcov-capture-coverage + COMMENT "Capture coverage data with lcov" + DEPENDS ${LCOV_BASE} + COMMAND lcov --capture --directory ${CMAKE_CURRENT_BINARY_DIR} --output-file ${LCOV_TEST} --include "'${SRC_ABS}*'" + 2>${LCOV_ERR} >${LCOV_LOG} + COMMAND lcov --add-tracefile ${LCOV_BASE} --add-tracefile ${LCOV_TEST} --output-file ${LCOV_TOTAL} + >>${LCOV_LOG} +) + +# gcovr post-test coverage target +add_custom_target(gcovr-capture-coverage + COMMENT "Capture coverage data with gcovr" + COMMAND gcovr --root ../../.. --lcov ${LCOV_TOTAL} --filter ".*/coresdk/src/.*" +) \ No newline at end of file diff --git a/projects/cmake/CMakePresets.json b/projects/cmake/CMakePresets.json index ba4509f3..9fc6234c 100644 --- a/projects/cmake/CMakePresets.json +++ b/projects/cmake/CMakePresets.json @@ -9,7 +9,17 @@ "cacheVariables": { "CMAKE_C_COMPILER": "gcc", "CMAKE_CXX_COMPILER": "g++", - "CMAKE_BUILD_TYPE": "Debug" + "CMAKE_BUILD_TYPE": "Debug", + "USE_COVERAGE": "OFF" + } + }, + { + "name": "LinuxCoverage", + "displayName": "Linux with coverage", + "description": "Linux gcc g++", + "inherits": "Linux", + "cacheVariables": { + "USE_COVERAGE": "ON" } }, { @@ -33,7 +43,17 @@ "cacheVariables": { "CMAKE_C_COMPILER": "clang", "CMAKE_CXX_COMPILER": "clang++", - "CMAKE_BUILD_TYPE": "Debug" + "CMAKE_BUILD_TYPE": "Debug", + "USE_COVERAGE": "OFF" + } + }, + { + "name": "macOSCoverage", + "displayName": "macOS with coverage", + "description": "macOS clang", + "inherits": "macOS", + "cacheVariables": { + "USE_COVERAGE": "ON" } } ] From 04f8e5aa796e5b7c6e31851f32dff9f60cf8feba Mon Sep 17 00:00:00 2001 From: Trung Pham <99781714+peterpham0801@users.noreply.github.com> Date: Fri, 4 Jul 2025 14:25:14 +1000 Subject: [PATCH 2/2] Coverage integration mac (#114) * Add LCOV custom targets * Add coverage preset for macOS * modify contributing and cmakepresets * deleted CMakeLists file * second commit * just add build presets * fix contributing.md * fix contributing.md * delete files * delete * add missed file * Minor updates --------- Co-authored-by: dijidiji Co-authored-by: Olivia McKeon Co-authored-by: omckeon --- CONTRIBUTING.md | 61 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 6 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9f09e132..5fb72dd2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,3 +1,4 @@ + # SplashKit Development ## macOS @@ -20,10 +21,15 @@ git clone --recursive -j2 https://github.com//splashkit-core.git ``` -3. Build the test project: +3. Initialise submodules if they weren't included during clone: - - In a terminal application, go into the cloned directory of your SplashKit - fork, and type: + ```sh + git submodule update --init --recursive + ``` + +4. Build the test project: + + - In a terminal application, go into the cloned directory of your SplashKit fork, and type: ```sh cd projects/cmake @@ -31,20 +37,63 @@ make ``` -4. Run the test program by executing + - For Apple Silicon (arm64) Macs, you may need to specify the architecture: + + ```sh + cd projects/cmake + # Clean any previous build files if you encounter errors + ./clean.sh + cmake . -DCMAKE_OSX_ARCHITECTURES=arm64 + make + ``` + +5. Run the test program by executing: ```sh cd ../../bin ./sktest ``` -5. Add features to code in +6. Generate Code Coverage Report (requires lcov) + + SplashKit includes a CMake preset for generating code coverage reports using lcov. To use it: + + ```sh + # Install lcov if you don't have it + brew install lcov + + # Navigate to the cmake directory from the repository root + cd projects/cmake + + # Configure the project with coverage flags + cmake --preset macOSCoverage -S . + + # Build the project + cmake --build --preset macOSCoverage + + # Run the tests to generate coverage data + cd ../../bin && ./skunit_tests + + # Generate the coverage report + cd .. + lcov --capture --directory projects/cmake/build/macOSCoverage --output-file coverage.info --ignore-errors inconsistent,unsupported,format,count --include "/coresdk/src/coresdk/" --include "/coresdk/src/backend/" + + # Create an HTML report + genhtml coverage.info --output-directory coverage_report --ignore-errors inconsistent,corrupt,unsupported,category + + # Open the report + open coverage_report/index.html + ``` + + The report will show which parts of your code are covered by tests and which parts are not covered. This helps identify areas that need additional testing. + +7. Add features to code in: ```sh ./coresdk ``` -6. Add test code into `coresdk/src/test`. Now you should be good to go. +8. Add test code into `coresdk/src/test`. Now you should be good to go. ## Linux