[libcxx] Add code coverage configuration to CMake and LIT.

Summary:
This patch adds configuration to CMake and LIT for running the libc++ test-suite to generate code coverage.

To use code coverage use following instructions.

* Find the clang resource dir using `$CXX -print-search-dirs`. Let <library-dir> be the first library search directory.
* `cmake <regular-options> -DLIBCXX_GENERATE_COVERAGE=ON -DLIBCXX_COVERAGE_LIBRARY=<library-dir>/lib/<platform>/libclang_rt.profile.a <source>`
* `make cxx`
* `make check-libcxx`
* `make generate-libcxx-coverage`


The reason I want this patch upstreamed is so I can setup a bot that generates code coverage and posts in online for every revision. 



Reviewers: mclow.lists, jroelofs, danalbert

Reviewed By: danalbert

Differential Revision: http://reviews.llvm.org/D8716

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@233669 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Fiselier 2015-03-31 04:15:45 +00:00
parent f68d637860
commit bf9653d85f
6 changed files with 73 additions and 0 deletions

View File

@ -64,6 +64,9 @@ option(LIBCXX_ENABLE_MONOTONIC_CLOCK
This option may only be used when LIBCXX_ENABLE_THREADS=OFF." ON)
option(LIBCXX_INSTALL_HEADERS "Install the libc++ headers." ON)
option(LIBCXX_INSTALL_SUPPORT_HEADERS "Install libc++ support headers." ON)
option(LIBCXX_GENERATE_COVERAGE "Enable generating code coverage." OFF)
set(LIBCXX_COVERAGE_LIBRARY "" CACHE STRING
"The Profile-rt library used to build with code coverage")
option(LIBCXX_ENABLE_STATIC_ABI_LIBRARY "Statically link the ABI library" OFF)
set(LIBCXX_SYSROOT "" CACHE STRING "Use alternate sysroot.")
set(LIBCXX_GCC_TOOLCHAIN "" CACHE STRING "Use alternate GCC toolchain.")
@ -140,6 +143,11 @@ set(LIBCXX_LINK_FLAGS "")
include(config-ix)
# Configure ABI library
include(HandleLibCXXABI)
# Configure coverage options.
if (LIBCXX_GENERATE_COVERAGE)
include(CodeCoverage)
set(CMAKE_BUILD_TYPE "COVERAGE" CACHE STRING "" FORCE)
endif()
#===============================================================================
# Setup Compiler Flags
@ -318,10 +326,15 @@ endif()
append_if(LIBCXX_CXX_FLAGS LIBCXX_TARGET_TRIPLE
"-target ${LIBCXX_TARGET_TRIPLE}")
append_if(LIBCXX_CXX_FLAGS LIBCXX_SYSROOT "--sysroot ${LIBCXX_SYSROOT}")
append_if(LIBCXX_CXX_FLAGS LIBCXX_GCC_TOOLCHAIN
"-gcc-toolchain ${LIBCXX_GCC_TOOLCHAIN}")
if (LLVM_USE_SANITIZER AND LIBCXX_GENERATE_COVERAGE)
message(FATAL_ERROR "LLVM_USE_SANITIZER cannot be used with LIBCXX_GENERATE_COVERAGE")
endif()
string(REPLACE ";" " " LIBCXX_CXX_FLAGS "${LIBCXX_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXX_CXX_FLAGS}")

View File

@ -0,0 +1,36 @@
find_program(CODE_COVERAGE_LCOV lcov)
if (NOT CODE_COVERAGE_LCOV)
message(FATAL_ERROR "Cannot find lcov...")
endif()
find_program(CODE_COVERAGE_GENHTML genhtml)
if (NOT CODE_COVERAGE_GENHTML)
message(FATAL_ERROR "Cannot find genhtml...")
endif()
set(CMAKE_CXX_FLAGS_COVERAGE "-g -O0 --coverage")
function(setup_lcov_test_target_coverage target_name output_dir capture_dirs source_dirs)
file(MAKE_DIRECTORY ${output_dir})
set(CAPTURE_DIRS "")
foreach(cdir ${capture_dirs})
list(APPEND CAPTURE_DIRS "-d;${cdir}")
endforeach()
set(EXTRACT_DIRS "")
foreach(sdir ${source_dirs})
list(APPEND EXTRACT_DIRS "'${sdir}/*'")
endforeach()
message(STATUS "Capture Directories: ${CAPTURE_DIRS}")
message(STATUS "Extract Directories: ${EXTRACT_DIRS}")
add_custom_target(generate-lib${target_name}-coverage
COMMAND ${CODE_COVERAGE_LCOV} --capture ${CAPTURE_DIRS} -o test_coverage.info
COMMAND ${CODE_COVERAGE_LCOV} --extract test_coverage.info ${EXTRACT_DIRS} -o test_coverage.info
COMMAND ${CODE_COVERAGE_GENHTML} --demangle-cpp test_coverage.info -o test_coverage
COMMAND ${CMAKE_COMMAND} -E remove test_coverage.info
WORKING_DIRECTORY ${output_dir}
COMMENT "Generating coverage results")
endfunction()

View File

@ -1,3 +1,5 @@
set(LIBCXX_LIB_CMAKEFILES_DIR "${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}" PARENT_SCOPE)
# Get sources
file(GLOB LIBCXX_SOURCES ../src/*.cpp)
if(WIN32)
@ -61,8 +63,12 @@ append_if(libraries LIBCXX_HAS_M_LIB m)
append_if(libraries LIBCXX_HAS_RT_LIB rt)
append_if(libraries LIBCXX_HAS_GCC_S_LIB gcc_s)
if (LIBCXX_COVERAGE_LIBRARY)
target_link_libraries(cxx ${LIBCXX_COVERAGE_LIBRARY})
endif()
target_link_libraries(cxx ${libraries})
# Setup flags.
append_if(LIBCXX_COMPILE_FLAGS LIBCXX_HAS_FPIC_FLAG -fPIC)
append_if(LIBCXX_LINK_FLAGS LIBCXX_HAS_NODEFAULTLIBS_FLAG -nodefaultlibs)

View File

@ -47,6 +47,8 @@ if (LIT_EXECUTABLE)
pythonize_bool(LIBCXX_ENABLE_STDOUT)
pythonize_bool(LIBCXX_ENABLE_THREADS)
pythonize_bool(LIBCXX_ENABLE_MONOTONIC_CLOCK)
pythonize_bool(LIBCXX_GENERATE_COVERAGE)
# The tests shouldn't link to any ABI library when it has been linked into
# libc++ statically.
if (LIBCXX_ENABLE_STATIC_ABI_LIBRARY)
@ -71,6 +73,14 @@ if (LIT_EXECUTABLE)
DEPENDS cxx
COMMENT "Running libcxx tests"
${cmake_3_2_USES_TERMINAL})
if (LIBCXX_GENERATE_COVERAGE)
include(CodeCoverage)
set(output_dir "${CMAKE_CURRENT_BINARY_DIR}/coverage")
set(capture_dirs "${LIBCXX_LIB_CMAKEFILES_DIR}/cxx.dir/;${CMAKE_CURRENT_BINARY_DIR}")
set(extract_dirs "${LIBCXX_SOURCE_DIR}/include;${LIBCXX_SOURCE_DIR}/src")
setup_lcov_test_target_coverage("cxx" "${output_dir}" "${capture_dirs}" "${extract_dirs}")
endif()
else()
message(WARNING
"LIT_EXECUTABLE not set, no check-libcxx target will be available!")

View File

@ -98,6 +98,7 @@ class Configuration(object):
self.configure_debug_mode()
self.configure_warnings()
self.configure_sanitizer()
self.configure_coverage()
self.configure_substitutions()
self.configure_features()
@ -594,6 +595,12 @@ class Configuration(object):
self.lit_config.fatal('unsupported value for '
'use_sanitizer: {0}'.format(san))
def configure_coverage(self):
self.generate_coverage = self.get_lit_bool('generate_coverage', False)
if self.generate_coverage:
self.cxx.flags += ['-g', '--coverage']
self.cxx.compile_flags += ['-O0']
def configure_substitutions(self):
sub = self.config.substitutions
# Configure compiler substitions

View File

@ -20,6 +20,7 @@ config.configuration_variant = "@LIBCXX_LIT_VARIANT@"
config.target_triple = "@LIBCXX_TARGET_TRIPLE@"
config.sysroot = "@LIBCXX_SYSROOT@"
config.gcc_toolchain = "@LIBCXX_GCC_TOOLCHAIN@"
config.generate_coverage = "@LIBCXX_GENERATE_COVERAGE@"
config.target_info = "@LIBCXX_TARGET_INFO@"
config.executor = "@LIBCXX_EXECUTOR@"