Index: CMakeLists.txt =================================================================== --- CMakeLists.txt +++ CMakeLists.txt @@ -64,6 +64,9 @@ 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 @@ 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 @@ 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}") Index: cmake/Modules/CodeCoverage.cmake =================================================================== --- /dev/null +++ cmake/Modules/CodeCoverage.cmake @@ -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() Index: lib/CMakeLists.txt =================================================================== --- lib/CMakeLists.txt +++ lib/CMakeLists.txt @@ -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_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) Index: test/CMakeLists.txt =================================================================== --- test/CMakeLists.txt +++ test/CMakeLists.txt @@ -47,6 +47,8 @@ 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 @@ 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!") Index: test/libcxx/test/config.py =================================================================== --- test/libcxx/test/config.py +++ test/libcxx/test/config.py @@ -98,6 +98,7 @@ self.configure_debug_mode() self.configure_warnings() self.configure_sanitizer() + self.configure_coverage() self.configure_substitutions() self.configure_features() @@ -594,6 +595,12 @@ 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 Index: test/lit.site.cfg.in =================================================================== --- test/lit.site.cfg.in +++ test/lit.site.cfg.in @@ -20,6 +20,7 @@ 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@"