aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAiden Woodruff <aiden@aidenw.net>2024-01-21 12:36:51 -0500
committerAiden Woodruff <aiden@aidenw.net>2024-01-21 12:36:51 -0500
commitf722c69e101c6febb2b27d7cc4b7a652a6e7a150 (patch)
treed6abfd8602964fabc3f43647df069f78f7db2210
parent5ef7ef5742799c59d182946119df348822a1df8c (diff)
parentc353940ad9ad109b10730732abebf6e7f4ea6a1c (diff)
downloadstars-f722c69e101c6febb2b27d7cc4b7a652a6e7a150.tar.gz
stars-f722c69e101c6febb2b27d7cc4b7a652a6e7a150.tar.bz2
stars-f722c69e101c6febb2b27d7cc4b7a652a6e7a150.zip
Merge branch 'add-coverage'
-rw-r--r--CMakeLists.txt15
-rw-r--r--README.md8
-rw-r--r--cmake/Coverage.cmake66
-rw-r--r--src/CMakeLists.txt2
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
10find_package(SFML 2 REQUIRED COMPONENTS graphics) 10find_package(SFML 2 REQUIRED COMPONENTS graphics)
11 11
12configure_file(config.h.in config.h ESCAPE_QUOTES @ONLY)
13include_directories("${CMAKE_CURRENT_BINARY_DIR}"
14 "${CMAKE_CURRENT_SOURCE_DIR}/include")
15
16# Add debugging warnings. 12# Add debugging warnings.
17if(MSVC) 13if(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.")
23endif() 19endif()
24 20
21set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
22 "Choose the type of build: None, Debug, Release, RelWithDebInfo, \
23MinSizeRel, or Coverage")
24list(APPEND CMAKE_CONFIGURATION_TYPES Coverage)
25
26include(cmake/Coverage.cmake)
27
28configure_file(config.h.in config.h ESCAPE_QUOTES @ONLY)
29include_directories("${CMAKE_CURRENT_BINARY_DIR}"
30 "${CMAKE_CURRENT_SOURCE_DIR}/include")
31
25add_subdirectory(src) 32add_subdirectory(src)
26 33
27if("${BUILD_TESTING}" OR "${STARS_BUILD_TESTING}") 34if("${BUILD_TESTING}" OR "${STARS_BUILD_TESTING}")
diff --git a/README.md b/README.md
index ba4588a..6b0f350 100644
--- a/README.md
+++ b/README.md
@@ -15,6 +15,14 @@ sh config.sh
15cmake --build build 15cmake --build build
16``` 16```
17 17
18## Developer instructions
19
20### Coverage
21To 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
24reporting 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 @@
1string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type)
2
3if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
4set(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)
8set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "-fprofile-instr-generate" CACHE STRING
9 "Flags used by the linker during COVERAGE builds." FORCE)
10find_program(LLVM-PROFDATA llvm-profdata)
11find_program(LLVM-COV llvm-cov)
12elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
13find_program(LCOV lcov)
14find_program(GENHTML genhtml)
15set(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)
18set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "--coverage" CACHE STRING
19 "Flags used by the linker during COVERAGE builds." FORCE)
20endif()
21mark_as_advanced(CMAKE_CXX_FLAGS_COVERAGE CMAKE_EXE_LINKER_FLAGS_COVERAGE)
22
23if(build_type STREQUAL "coverage")
24set(STARS_COVERAGE_DIR "${CMAKE_CURRENT_BINARY_DIR}/coverage" CACHE STRING
25 "Coverage HTML output directory for stars.")
26
27# Meta-target for subdirectory coverage.
28add_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}")
32endif()
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.
36function(cover_subdirectory_targets SUBDIR OBJ1)
37if(build_type STREQUAL "coverage")
38if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
39set(LLVM_COV_OBJECTS -object "$<TARGET_FILE:${OBJ1}>")
40foreach(tgt ${ARGN})
41string(APPEND LLVM_COV_OBJECTS -object "$<TARGET_FILE:${tgt}>")
42endforeach()
43
44add_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}")
52add_dependencies(coverage coverage-${SUBDIR})
53elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
54add_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}")
63add_dependencies(coverage coverage-${SUBDIR})
64endif()
65endif()
66endfunction()
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.
25install(TARGETS main DESTINATION bin) 25install(TARGETS main DESTINATION bin)
26
27cover_subdirectory_targets(src main)