diff options
| author | Aiden Woodruff <aiden@aidenw.net> | 2024-01-21 12:36:51 -0500 |
|---|---|---|
| committer | Aiden Woodruff <aiden@aidenw.net> | 2024-01-21 12:36:51 -0500 |
| commit | f722c69e101c6febb2b27d7cc4b7a652a6e7a150 (patch) | |
| tree | d6abfd8602964fabc3f43647df069f78f7db2210 | |
| parent | 5ef7ef5742799c59d182946119df348822a1df8c (diff) | |
| parent | c353940ad9ad109b10730732abebf6e7f4ea6a1c (diff) | |
| download | stars-f722c69e101c6febb2b27d7cc4b7a652a6e7a150.tar.gz stars-f722c69e101c6febb2b27d7cc4b7a652a6e7a150.tar.bz2 stars-f722c69e101c6febb2b27d7cc4b7a652a6e7a150.zip | |
Merge branch 'add-coverage'
| -rw-r--r-- | CMakeLists.txt | 15 | ||||
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | cmake/Coverage.cmake | 66 | ||||
| -rw-r--r-- | src/CMakeLists.txt | 2 |
4 files changed, 87 insertions, 4 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index a8240a2..621136c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
| @@ -9,10 +9,6 @@ endif() | |||
| 9 | 9 | ||
| 10 | find_package(SFML 2 REQUIRED COMPONENTS graphics) | 10 | find_package(SFML 2 REQUIRED COMPONENTS graphics) |
| 11 | 11 | ||
| 12 | configure_file(config.h.in config.h ESCAPE_QUOTES @ONLY) | ||
| 13 | include_directories("${CMAKE_CURRENT_BINARY_DIR}" | ||
| 14 | "${CMAKE_CURRENT_SOURCE_DIR}/include") | ||
| 15 | |||
| 16 | # Add debugging warnings. | 12 | # Add debugging warnings. |
| 17 | if(MSVC) | 13 | if(MSVC) |
| 18 | string(APPEND CMAKE_CXX_FLAGS_DEBUG " /W4") | 14 | string(APPEND CMAKE_CXX_FLAGS_DEBUG " /W4") |
| @@ -22,6 +18,17 @@ else() | |||
| 22 | CACHE STRING "Flags used by the CXX compiler during DEBUG builds.") | 18 | CACHE STRING "Flags used by the CXX compiler during DEBUG builds.") |
| 23 | endif() | 19 | endif() |
| 24 | 20 | ||
| 21 | set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING | ||
| 22 | "Choose the type of build: None, Debug, Release, RelWithDebInfo, \ | ||
| 23 | MinSizeRel, or Coverage") | ||
| 24 | list(APPEND CMAKE_CONFIGURATION_TYPES Coverage) | ||
| 25 | |||
| 26 | include(cmake/Coverage.cmake) | ||
| 27 | |||
| 28 | configure_file(config.h.in config.h ESCAPE_QUOTES @ONLY) | ||
| 29 | include_directories("${CMAKE_CURRENT_BINARY_DIR}" | ||
| 30 | "${CMAKE_CURRENT_SOURCE_DIR}/include") | ||
| 31 | |||
| 25 | add_subdirectory(src) | 32 | add_subdirectory(src) |
| 26 | 33 | ||
| 27 | if("${BUILD_TESTING}" OR "${STARS_BUILD_TESTING}") | 34 | if("${BUILD_TESTING}" OR "${STARS_BUILD_TESTING}") |
| @@ -15,6 +15,14 @@ sh config.sh | |||
| 15 | cmake --build build | 15 | cmake --build build |
| 16 | ``` | 16 | ``` |
| 17 | 17 | ||
| 18 | ## Developer instructions | ||
| 19 | |||
| 20 | ### Coverage | ||
| 21 | To generate coverage reports, build the project with | ||
| 22 | `-DCMAKE_BUILD_TYPE=Coverage`, then from the `build/` directory, execute | ||
| 23 | `src/main`. Perform actions, exit cleanly, then run `make coverage`. Coverage | ||
| 24 | reporting is supported on Apple (LLVM) and Linux (gcov/lcov). | ||
| 25 | |||
| 18 | ## Bug reports, questions, etc. | 26 | ## Bug reports, questions, etc. |
| 19 | 27 | ||
| 20 | * The stars homepage is hosted at <https://git.aidenw.net/stars/about/> | 28 | * The stars homepage is hosted at <https://git.aidenw.net/stars/about/> |
diff --git a/cmake/Coverage.cmake b/cmake/Coverage.cmake new file mode 100644 index 0000000..5d2e2e0 --- /dev/null +++ b/cmake/Coverage.cmake | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type) | ||
| 2 | |||
| 3 | if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") | ||
| 4 | set(CMAKE_CXX_FLAGS_COVERAGE | ||
| 5 | "-g -O0 -fprofile-instr-generate -fcoverage-mapping" CACHE STRING | ||
| 6 | "Flags used by the CXX compiler during COVERAGE builds." | ||
| 7 | FORCE) | ||
| 8 | set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "-fprofile-instr-generate" CACHE STRING | ||
| 9 | "Flags used by the linker during COVERAGE builds." FORCE) | ||
| 10 | find_program(LLVM-PROFDATA llvm-profdata) | ||
| 11 | find_program(LLVM-COV llvm-cov) | ||
| 12 | elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") | ||
| 13 | find_program(LCOV lcov) | ||
| 14 | find_program(GENHTML genhtml) | ||
| 15 | set(CMAKE_CXX_FLAGS_COVERAGE "-g -O0 --coverage -fkeep-inline-functions \ | ||
| 16 | -fkeep-static-functions" CACHE STRING | ||
| 17 | "Flags used by the CXX compiler during COVERAGE builds." FORCE) | ||
| 18 | set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "--coverage" CACHE STRING | ||
| 19 | "Flags used by the linker during COVERAGE builds." FORCE) | ||
| 20 | endif() | ||
| 21 | mark_as_advanced(CMAKE_CXX_FLAGS_COVERAGE CMAKE_EXE_LINKER_FLAGS_COVERAGE) | ||
| 22 | |||
| 23 | if(build_type STREQUAL "coverage") | ||
| 24 | set(STARS_COVERAGE_DIR "${CMAKE_CURRENT_BINARY_DIR}/coverage" CACHE STRING | ||
| 25 | "Coverage HTML output directory for stars.") | ||
| 26 | |||
| 27 | # Meta-target for subdirectory coverage. | ||
| 28 | add_custom_target(coverage | ||
| 29 | # build/coverage/ is usually created by dependents but needed for BYPRODUCTS. | ||
| 30 | "${CMAKE}" -E make_directory "${STARS_COVERAGE_DIR}" | ||
| 31 | BYPRODUCTS "${STARS_COVERAGE_DIR}") | ||
| 32 | endif() | ||
| 33 | |||
| 34 | # Make a coverage report in the subdirectory SUBDIR with coverage from targets | ||
| 35 | # OBJ1...ARGN. Does nothing if this is not a "Coverage" build. | ||
| 36 | function(cover_subdirectory_targets SUBDIR OBJ1) | ||
| 37 | if(build_type STREQUAL "coverage") | ||
| 38 | if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") | ||
| 39 | set(LLVM_COV_OBJECTS -object "$<TARGET_FILE:${OBJ1}>") | ||
| 40 | foreach(tgt ${ARGN}) | ||
| 41 | string(APPEND LLVM_COV_OBJECTS -object "$<TARGET_FILE:${tgt}>") | ||
| 42 | endforeach() | ||
| 43 | |||
| 44 | add_custom_target(coverage-${SUBDIR} | ||
| 45 | "${LLVM-PROFDATA}" merge -o "${CMAKE_CURRENT_BINARY_DIR}/${SUBDIR}.profdata" | ||
| 46 | -sparse "${PROJECT_BINARY_DIR}/default.profraw" | ||
| 47 | COMMAND "${LLVM-COV}" show ${LLVM_COV_OBJECTS} | ||
| 48 | -instr-profile="${CMAKE_CURRENT_BINARY_DIR}/${SUBDIR}.profdata" | ||
| 49 | -output-dir="${STARS_COVERAGE_DIR}/${SUBDIR}" -format=html | ||
| 50 | COMMENT "Coverage available at file://${STARS_COVERAGE_DIR}/${SUBDIR}" | ||
| 51 | BYPRODUCTS "${STARS_COVERAGE_DIR}/${SUBDIR}") | ||
| 52 | add_dependencies(coverage coverage-${SUBDIR}) | ||
| 53 | elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") | ||
| 54 | add_custom_target(coverage-${SUBDIR} | ||
| 55 | "${LCOV}" -q --demangle-cpp --capture --no-external | ||
| 56 | --base-directory "${PROJECT_SOURCE_DIR}" | ||
| 57 | --directory "${CMAKE_CURRENT_BINARY_DIR}" | ||
| 58 | -o "${CMAKE_CURRENT_BINARY_DIR}/coverage.info" | ||
| 59 | COMMAND "${GENHTML}" -q -o "${STARS_COVERAGE_DIR}/${SUBDIR}" | ||
| 60 | "${CMAKE_CURRENT_BINARY_DIR}/coverage.info" | ||
| 61 | COMMENT "Coverage available at file://${STARS_COVERAGE_DIR}/${SUBDIR}" | ||
| 62 | BYPRODUCTS "${STARS_COVERAGE_DIR}/${SUBDIR}") | ||
| 63 | add_dependencies(coverage coverage-${SUBDIR}) | ||
| 64 | endif() | ||
| 65 | endif() | ||
| 66 | endfunction() | ||
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 31a39ad..5590923 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt | |||
| @@ -23,3 +23,5 @@ target_link_libraries(main app) | |||
| 23 | 23 | ||
| 24 | # Only installation target is this exe. | 24 | # Only installation target is this exe. |
| 25 | install(TARGETS main DESTINATION bin) | 25 | install(TARGETS main DESTINATION bin) |
| 26 | |||
| 27 | cover_subdirectory_targets(src main) | ||
