From 1b3df3a63fb8748ed4c818d724bf7146c529d587 Mon Sep 17 00:00:00 2001
From: Niels Lohmann <mail@nlohmann.me>
Date: Wed, 4 Oct 2017 22:18:21 +0200
Subject: [PATCH] :hammer: trying to use Coveralls with CMake #698

---
 .travis.yml         | 16 +++-------------
 test/CMakeLists.txt | 29 ++++++++++++++++++++++++-----
 2 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index 43323c34ba..f0df62b5a2 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -81,24 +81,14 @@ matrix:
     addons:
       apt:
         sources: ['ubuntu-toolchain-r-test']
-        packages: ['g++-4.9', 'ruby', 'ninja-build']
+        packages: ['g++-4.9', 'ninja-build']
     before_script:
-      - wget http://ftp.de.debian.org/debian/pool/main/l/lcov/lcov_1.11.orig.tar.gz
-      - tar xf lcov_1.11.orig.tar.gz
-      - sudo make -C lcov-1.11/ install
-      - gem install coveralls-lcov
       - pip install --user cpp-coveralls
     after_success:
-      - make clean
-      - CXXFLAGS="--coverage -g -O0" CPPFLAGS="-DNDEBUG" make json_unit
-      - test/json_unit "*"
-      - coveralls --build-root test --exclude src/catch.hpp --exclude src/unit-algorithms.cpp --exclude src/unit-allocator.cpp --exclude src/unit-capacity.cpp --exclude src/unit-class_const_iterator.cpp --exclude src/unit-class_iterator.cpp --exclude src/unit-class_lexer.cpp --exclude src/unit-class_parser.cpp --exclude src/unit-comparison.cpp --exclude src/unit-concepts.cpp --exclude src/unit-constructor1.cpp --exclude src/unit-constructor2.cpp --exclude src/unit-convenience.cpp --exclude src/unit-conversions.cpp --exclude src/unit-deserialization.cpp --exclude src/unit-element_access1.cpp --exclude src/unit-element_access2.cpp --exclude src/unit-inspection.cpp --exclude src/unit-iterator_wrapper.cpp --exclude src/unit-iterators1.cpp --exclude src/unit-iterators2.cpp --exclude src/unit-json_patch.cpp --exclude src/unit-json_pointer.cpp --exclude src/unit-modifiers.cpp --exclude src/unit-pointer_access.cpp --exclude src/unit-readme.cpp --exclude src/unit-reference_access.cpp --exclude src/unit-regression.cpp --exclude src/unit-serialization.cpp --exclude src/unit-testsuites.cpp --exclude src/unit-unicode.cpp --include ../src/json.hpp --gcov-options '\-lp' --gcov 'gcov-4.9'
-      - lcov --directory src --directory test/src --capture --output-file coverage.info --rc lcov_branch_coverage=1 --no-external
-      - lcov --remove coverage.info 'test/src/*' --output-file coverage.info --rc lcov_branch_coverage=1
-      - lcov --list coverage.info --rc lcov_branch_coverage=1
-      - coveralls-lcov --repo-token F9bs4Nop10JRgqPQXRcifyQKYhb3FczkS coverage.info
+      - coveralls --build-root test --include src --gcov 'gcov-4.9' --gcov-options '\-lp'
     env:
       - COMPILER=g++-4.9
+      - CMAKE_OPTIONS=-DJSON_Coverage=ON
       - SPECIAL=coveralls
 
   # Coverity (only for branch coverity_scan)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 6404df33ef..bb2dfb1854 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,6 +1,7 @@
 option(JSON_Sanitizer "Build test suite with Clang sanitizer" OFF)
 option(JSON_Valgrind "Execute test suite with Valgrind" OFF)
 option(JSON_NoExceptions "Build test suite without exceptions" OFF)
+option(JSON_Coverage "Build test suite with coverage information" OFF)
 
 if(JSON_Sanitizer)
     message(STATUS "Building test suite with Clang sanitizer")
@@ -25,6 +26,29 @@ if(JSON_NoExceptions)
     set(CATCH_TEST_FILTER -e)
 endif()
 
+if(JSON_Coverage)
+    message(STATUS "Building test suite with coverage information")
+    if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
+        message(FATAL_ERROR "JSON_Coverage requires GCC.")
+    endif()
+
+    # enable profiling
+    set(CMAKE_CXX_FLAGS "--coverage -g -O0 -fprofile-arcs -ftest-coverage")
+
+    # from https://github.com/RWTH-HPC/CMake-codecov/blob/master/cmake/FindGcov.cmake
+    get_filename_component(COMPILER_PATH "${CMAKE_CXX_COMPILER}" PATH)
+    string(REGEX MATCH "^[0-9]+" GCC_VERSION "${CMAKE_CXX_COMPILER_VERSION}")
+    find_program(GCOV_BIN NAMES gcov-${GCC_VERSION} gcov HINTS ${COMPILER_PATH})
+
+    # add target to collect coverage information and generate HTML file
+    add_custom_target(lcov_html
+        COMMAND lcov --directory . --capture --output-file json.info --gcov-tool ${GCOV_BIN} --rc lcov_branch_coverage=1
+        COMMAND lcov -e json.info ${CMAKE_SOURCE_DIR}/src/json.hpp --output-file json.info.filtered --rc lcov_branch_coverage=1
+        COMMAND genhtml --title "JSON for Modern C++" --legend --demangle-cpp --output-directory html --show-details --branch-coverage json.info.filtered
+        COMMENT "Generating HTML report test/html/index.html"
+    )
+endif()
+
 #############################################################################
 # Catch library with the main function to speed up build
 #############################################################################
@@ -65,11 +89,6 @@ foreach(file ${files})
         set_target_properties(${testcase} PROPERTIES COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated -Wno-float-equal")
     endif()
 
-    include(cotire OPTIONAL)
-    if(COMMAND cotire)
-        cotire(${testcase})
-    endif()
-
     add_test(NAME "${testcase}_default"
       COMMAND ${testcase} ${CATCH_TEST_FILTER}
       WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}