From da406cb11287e2961ad5aaa6ca857efd57fb9d03 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 23 Apr 2016 09:57:38 -0700 Subject: [PATCH] build: add test coverage target --- CMakeLists.txt | 25 ++++++++++++++++++++++++- README.md | 14 ++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d9cc6abd..e32e99b1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,8 @@ set(ZIG_LIBC_INCLUDE_DIR "/usr/include" CACHE STRING "Default native target libc set(ZIG_LD_PATH "ld" CACHE STRING "Path to ld for the native target") set(ZIG_DYNAMIC_LINKER "" CACHE STRING "Override dynamic linker for native target") +option(ZIG_TEST_COVERAGE "Build Zig with test coverage instrumentation" OFF) + find_package(llvm) @@ -175,10 +177,17 @@ if(MINGW) endif() set(EXE_CFLAGS "-std=c++11 -fno-exceptions -fno-rtti -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__USE_MINGW_ANSI_STDIO -Werror=strict-prototypes -Werror=old-style-definition -Werror=missing-prototypes") +set(EXE_LDFLAGS " ") +if(ZIG_TEST_COVERAGE) + set(EXE_CFLAGS "${EXE_CFLAGS} -fprofile-arcs -ftest-coverage") + set(EXE_LDFLAGS "-fprofile-arcs -ftest-coverage") +endif() add_executable(zig ${ZIG_SOURCES}) set_target_properties(zig PROPERTIES - COMPILE_FLAGS ${EXE_CFLAGS}) + COMPILE_FLAGS ${EXE_CFLAGS} + LINK_FLAGS ${EXE_LDFLAGS} +) target_link_libraries(zig LINK_PUBLIC ${CLANG_LIBRARIES} ${LLVM_LIBRARIES} @@ -208,4 +217,18 @@ add_executable(run_tests ${TEST_SOURCES}) target_link_libraries(run_tests) set_target_properties(run_tests PROPERTIES COMPILE_FLAGS ${EXE_CFLAGS} + LINK_FLAGS ${EXE_LDFLAGS} ) + +if (ZIG_TEST_COVERAGE) + add_custom_target(coverage + DEPENDS run_tests + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + COMMAND lcov --directory . --zerocounters --rc lcov_branch_coverage=1 + COMMAND ./run_tests + COMMAND lcov --directory . --capture --output-file coverage.info --rc lcov_branch_coverage=1 + COMMAND lcov --remove coverage.info '/usr/*' --output-file coverage.info.cleaned --rc lcov_branch_coverage=1 + COMMAND genhtml -o coverage coverage.info.cleaned --rc lcov_branch_coverage=1 + COMMAND rm coverage.info coverage.info.cleaned + ) +endif() diff --git a/README.md b/README.md index 4d84c40bf..cef0d874a 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,20 @@ make sudo make install ``` +### Test Coverage + +To see test coverage in Zig, configure with `-DZIG_TEST_COVERAGE=ON` as an +additional parameter to the Debug build. + +You must have `lcov` installed and available. + +Then `make coverage`. + +With GCC you will get a nice HTML view of the coverage data. With clang, +the last step will fail, but you can execute +`llvm-cov gcov $(find CMakeFiles/ -name "*.gcda")` and then inspect the +produced .gcov files. + ### Troubleshooting If you get one of these: