Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 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
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ option(XGRAMMAR_BUILD_CXX_TESTS "Build C++ tests" OFF)
option(XGRAMMAR_ENABLE_CPPTRACE
"Enable C++ trace (Now only support Linux, and RelWithDebugInfo or Debug build)" OFF
)
option(XGRAMMAR_ENABLE_COVERAGE "Enable code coverage with gcov" OFF)

set(XGRAMMAR_CUDA_ARCHITECTURES
native
Expand Down Expand Up @@ -93,3 +94,16 @@ if(XGRAMMAR_BUILD_CXX_TESTS)
include(GoogleTest)
gtest_discover_tests(xgrammar_test)
endif()

if(XGRAMMAR_ENABLE_COVERAGE)
target_link_libraries(xgrammar_bindings PRIVATE gcov)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --coverage")

if(XGRAMMAR_BUILD_PYTHON_BINDINGS)
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --coverage")
endif()
endif()
1 change: 1 addition & 0 deletions cmake/config.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set(CMAKE_BUILD_TYPE RelWithDebInfo)
set(XGRAMMAR_BUILD_PYTHON_BINDINGS ON)
set(XGRAMMAR_ENABLE_COVERAGE OFF)
set(XGRAMMAR_BUILD_CXX_TESTS OFF)
set(XGRAMMAR_ENABLE_CPPTRACE OFF)
11 changes: 11 additions & 0 deletions scripts/run_coverage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

# Usage: bash ./scripts/run_coverage.sh
lcov --directory . --zerocounters
ctest --test-dir build -V --timeout 60 --stop-on-failure
pytest

lcov --gcov-tool /usr/bin/gcov-13 --directory . --capture --output-file coverage.info --ignore-errors mismatch,gcov
genhtml coverage.info --output-directory coverage_report --ignore-errors version
rm coverage.info
echo "Coverage report generated at: $(pwd)/coverage_report/index.html"
Loading