diff --git a/CMakeLists.txt b/CMakeLists.txt index cfd7255..0a7231b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,10 +9,11 @@ if (NOT DEFINED PARSI_MAIN_PROJECT) endif() project(parsi - DESCRIPTION "lightweight simple parser combinator" + DESCRIPTION "lightweight parser combinator" LANGUAGES CXX) option(PARSI_TESTS "build tests" ${PARSI_MAIN_PROJECT}) +option(PARSI_ENABLE_COVERAGE "enable code coverage" ${PARSI_MAIN_PROJECT}) option(PARSI_DOCS "build docs" ${PARSI_MAIN_PROJECT}) option(PARSI_EXAMPLES "build examples" ${PARSI_MAIN_PROJECT}) option(PARSI_BENCHMARK "build benchmarks" OFF) @@ -25,7 +26,7 @@ set(CMAKE_VISIBILITY_INLINES_HIDDEN ON) set(CMAKE_BUILD_WITH_INSTALL_RPATH ON) if (NOT DEFINED CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) - set(CMAKE_BUILD_TYPE Release) + set(CMAKE_BUILD_TYPE Debug) endif() if (NOT DEFINED CMAKE_ARCHIVE_OUTPUT_DIRECTORY) @@ -45,9 +46,22 @@ target_compile_features(${PROJECT_NAME}-options INTERFACE cxx_std_20) target_include_directories(${PROJECT_NAME}-options INTERFACE $ - $) + $ +) + +if (PARSI_ENABLE_COVERAGE) + if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug") + message(WARNING "code-coverage with non-Debug build is inaccurate") + endif() + if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + target_compile_options(${PROJECT_NAME}-options INTERFACE --coverage -O0 -g) + target_link_libraries(${PROJECT_NAME}-options INTERFACE --coverage) + message(STATUS "added compiler flags to generate coverage report") + else() + message(FATAL_ERROR "no known rule to add coverage to compiler: ${CMAKE_CXX_COMPILER_ID}") + endif() +endif() -# TODO decide whether to go header-only or not. add_library(${PROJECT_NAME} INTERFACE) add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME}) target_link_libraries(${PROJECT_NAME} INTERFACE ${PROJECT_NAME}-options) diff --git a/CMakePresets.json b/CMakePresets.json index 2b9bf3a..e0aa04c 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -162,6 +162,16 @@ "displayName": "Linux x64 Release", "configurePreset": "linux-x64-release" }, + { + "name": "linux-x64-debug", + "displayName": "Linux x64 Debug", + "configurePreset": "linux-x64-debug" + }, + { + "name": "Linux-x64-release", + "displayName": "Linux x64 Release", + "configurePreset": "linux-x64-release" + }, { "name": "android-arm-debug", "displayName": "Android ARM32 Debug", diff --git a/include/parsi/parsi.hpp b/include/parsi/parsi.hpp index 82d5bc2..b792054 100644 --- a/include/parsi/parsi.hpp +++ b/include/parsi/parsi.hpp @@ -3,6 +3,10 @@ namespace parsi { +inline bool sample() { + return true; +} + } // namespace parsi #endif // PARSI_PARSI_HPP diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9e4a386..86be8e2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,7 +9,8 @@ target_link_libraries(${PROJECT_NAME}-tests ${PROJECT_NAME} ${PROJECT_NAME}-options Catch2::Catch2 - Catch2::Catch2WithMain) + Catch2::Catch2WithMain +) list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras) @@ -19,3 +20,27 @@ include(Catch) if (NOT ANDROID) catch_discover_tests(${PROJECT_NAME}-tests) endif() + +if (PARSI_ENABLE_COVERAGE) + find_program(GCOVR_PROGRAM gcovr REQUIRED) + + set(COVERAGE_DIR ${PROJECT_BINARY_DIR}/coverage) + + add_custom_target(coverage + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} + COMMAND ${CMAKE_COMMAND} -E make_directory ${COVERAGE_DIR} + COMMAND ctest --output-on-failure + COMMAND ${GCOVR_PROGRAM} + -r "${PROJECT_SOURCE_DIR}/include" + --print-summary + --fail-under-line 70 + --fail-under-branch 70 + --html "${COVERAGE_DIR}/index.html" --html-details + --xml "${COVERAGE_DIR}/coverage.xml" + --json "${COVERAGE_DIR}/coverage.json" + --object-directory=${PROJECT_BINARY_DIR} + DEPENDS ${PROJECT_NAME}-tests + BYPRODUCTS ${COVERAGE_DIR}/coverage.xml + VERBATIM + ) +endif() diff --git a/tests/sample.cpp b/tests/sample.cpp index a914a7d..566ee58 100644 --- a/tests/sample.cpp +++ b/tests/sample.cpp @@ -1,6 +1,7 @@ #include +#include "parsi/parsi.hpp" + TEST_CASE("sample") { - CHECK(true); + CHECK(parsi::sample()); } -