[libcxx] r297553 - Change test coverage generation to use llvm-cov instead of gcov.

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 10 19:24:19 PST 2017


Author: ericwf
Date: Fri Mar 10 21:24:18 2017
New Revision: 297553

URL: http://llvm.org/viewvc/llvm-project?rev=297553&view=rev
Log:
Change test coverage generation to use llvm-cov instead of gcov.

Clang doesn't produce gcov compatible coverage files. This
causes lcov to break because it uses gcov by default. This
patch switches lcov to use llvm-cov as the gcov-tool.

Unfortunatly llvm-cov doesn't provide a gcov like interface by
default so it won't work with lcov. However `llvm-cov gcov` does.
For this reason we generate 'llvm-cov-wrapper' script that always
passes the gcov flag.

Added:
    libcxx/trunk/cmake/Modules/HandleCompilerRT.cmake
Modified:
    libcxx/trunk/CMakeLists.txt
    libcxx/trunk/cmake/Modules/CodeCoverage.cmake
    libcxx/trunk/lib/CMakeLists.txt

Modified: libcxx/trunk/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/CMakeLists.txt?rev=297553&r1=297552&r2=297553&view=diff
==============================================================================
--- libcxx/trunk/CMakeLists.txt (original)
+++ libcxx/trunk/CMakeLists.txt Fri Mar 10 21:24:18 2017
@@ -57,6 +57,7 @@ endif()
 # Setup CMake Options
 #===============================================================================
 include(CMakeDependentOption)
+include(HandleCompilerRT)
 
 # Basic options ---------------------------------------------------------------
 option(LIBCXX_ENABLE_ASSERTIONS "Enable assertions independent of build mode." OFF)

Modified: libcxx/trunk/cmake/Modules/CodeCoverage.cmake
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/cmake/Modules/CodeCoverage.cmake?rev=297553&r1=297552&r2=297553&view=diff
==============================================================================
--- libcxx/trunk/cmake/Modules/CodeCoverage.cmake (original)
+++ libcxx/trunk/cmake/Modules/CodeCoverage.cmake Fri Mar 10 21:24:18 2017
@@ -3,6 +3,11 @@ if (NOT CODE_COVERAGE_LCOV)
   message(FATAL_ERROR "Cannot find lcov...")
 endif()
 
+find_program(CODE_COVERAGE_LLVM_COV llvm-cov)
+if (NOT CODE_COVERAGE_LLVM_COV)
+  message(FATAL_ERROR "Cannot find llvm-cov...")
+endif()
+
 find_program(CODE_COVERAGE_GENHTML genhtml)
 if (NOT CODE_COVERAGE_GENHTML)
   message(FATAL_ERROR "Cannot find genhtml...")
@@ -11,6 +16,14 @@ endif()
 set(CMAKE_CXX_FLAGS_COVERAGE "-g -O0 --coverage")
 
 function(setup_lcov_test_target_coverage target_name output_dir capture_dirs source_dirs)
+  if (NOT DEFINED LIBCXX_BINARY_DIR)
+    message(FATAL_ERROR "Variable must be set")
+  endif()
+
+  set(GCOV_TOOL "${LIBCXX_BINARY_DIR}/llvm-cov-wrapper")
+  file(GENERATE OUTPUT ${GCOV_TOOL}
+    CONTENT "#!/usr/bin/env bash\n${CODE_COVERAGE_LLVM_COV} gcov \"$@\"\n")
+
   file(MAKE_DIRECTORY ${output_dir})
 
   set(CAPTURE_DIRS "")
@@ -27,8 +40,9 @@ function(setup_lcov_test_target_coverage
   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 chmod +x ${GCOV_TOOL}
+        COMMAND ${CODE_COVERAGE_LCOV} --gcov-tool ${GCOV_TOOL} --capture ${CAPTURE_DIRS} -o test_coverage.info
+        COMMAND ${CODE_COVERAGE_LCOV} --gcov-tool ${GCOV_TOOL} --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}

Added: libcxx/trunk/cmake/Modules/HandleCompilerRT.cmake
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/cmake/Modules/HandleCompilerRT.cmake?rev=297553&view=auto
==============================================================================
--- libcxx/trunk/cmake/Modules/HandleCompilerRT.cmake (added)
+++ libcxx/trunk/cmake/Modules/HandleCompilerRT.cmake Fri Mar 10 21:24:18 2017
@@ -0,0 +1,43 @@
+function(find_compiler_rt_library name dest)
+  if (NOT DEFINED LIBCXX_COMPILE_FLAGS)
+    message(FATAL_ERROR "LIBCXX_COMPILE_FLAGS must be defined when using this function")
+  endif()
+  set(dest "" PARENT_SCOPE)
+  set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${LIBCXX_COMPILE_FLAGS}
+      "--rtlib=compiler-rt" "--print-libgcc-file-name")
+  execute_process(
+      COMMAND ${CLANG_COMMAND}
+      RESULT_VARIABLE HAD_ERROR
+      OUTPUT_VARIABLE LIBRARY_FILE
+  )
+  string(STRIP "${LIBRARY_FILE}" LIBRARY_FILE)
+  string(REPLACE "builtins" "${name}" LIBRARY_FILE "${LIBRARY_FILE}")
+  if (NOT HAD_ERROR AND EXISTS "${LIBRARY_FILE}")
+    message(STATUS "Found compiler-rt library: ${LIBRARY_FILE}")
+    set(${dest} "${LIBRARY_FILE}" PARENT_SCOPE)
+  else()
+    message(STATUS "Failed to find compiler-rt library")
+  endif()
+endfunction()
+
+function(find_compiler_rt_dir dest)
+  if (NOT DEFINED LIBCXX_COMPILE_FLAGS)
+    message(FATAL_ERROR "LIBCXX_COMPILE_FLAGS must be defined when using this function")
+  endif()
+  set(dest "" PARENT_SCOPE)
+  set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${LIBCXX_COMPILE_FLAGS}
+      "--rtlib=compiler-rt" "--print-libgcc-file-name")
+  execute_process(
+      COMMAND ${CLANG_COMMAND}
+      RESULT_VARIABLE HAD_ERROR
+      OUTPUT_VARIABLE LIBRARY_FILE
+  )
+  string(STRIP "${LIBRARY_FILE}" LIBRARY_FILE)
+  get_filename_component(LIBRARY_DIR "${LIBRARY_FILE}" DIRECTORY)
+  if (NOT HAD_ERROR AND EXISTS "${LIBRARY_DIR}")
+    message(STATUS "Found compiler-rt directory: ${LIBRARY_DIR}")
+    set(${dest} "${LIBRARY_DIR}" PARENT_SCOPE)
+  else()
+    message(STATUS "Failed to find compiler-rt directory")
+  endif()
+endfunction()

Modified: libcxx/trunk/lib/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/lib/CMakeLists.txt?rev=297553&r1=297552&r2=297553&view=diff
==============================================================================
--- libcxx/trunk/lib/CMakeLists.txt (original)
+++ libcxx/trunk/lib/CMakeLists.txt Fri Mar 10 21:24:18 2017
@@ -32,6 +32,10 @@ endif()
 add_link_flags_if(LIBCXX_CXX_ABI_LIBRARY_PATH
                   "${CMAKE_LIBRARY_PATH_FLAG}${LIBCXX_CXX_ABI_LIBRARY_PATH}")
 
+
+if (LIBCXX_GENERATE_COVERAGE AND NOT LIBCXX_COVERAGE_LIBRARY)
+  find_compiler_rt_library(profile LIBCXX_COVERAGE_LIBRARY)
+endif()
 add_library_flags_if(LIBCXX_COVERAGE_LIBRARY "${LIBCXX_COVERAGE_LIBRARY}")
 
 if (APPLE AND (LIBCXX_CXX_ABI_LIBNAME STREQUAL "libcxxabi" OR
@@ -62,12 +66,7 @@ if (APPLE AND LLVM_USE_SANITIZER)
     message(WARNING "LLVM_USE_SANITIZER=${LLVM_USE_SANITIZER} is not supported on OS X")
   endif()
   if (LIBFILE)
-    execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=lib OUTPUT_VARIABLE LIBDIR RESULT_VARIABLE Result)
-    if (NOT ${Result} EQUAL "0")
-      message(FATAL "Failed to find library resource directory")
-    endif()
-    string(STRIP "${LIBDIR}" LIBDIR)
-    set(LIBDIR "${LIBDIR}/darwin/")
+    find_compiler_rt_dir(LIBDIR)
     if (NOT IS_DIRECTORY "${LIBDIR}")
       message(FATAL_ERROR "Cannot find compiler-rt directory on OS X required for LLVM_USE_SANITIZER")
     endif()




More information about the cfe-commits mailing list