From 929f5d398c03abbdfb8dc648ef56516aba63c168 Mon Sep 17 00:00:00 2001
From: Niels Lohmann <mail@nlohmann.me>
Date: Sat, 2 May 2020 23:24:21 +0200
Subject: [PATCH] :hammer: fix coverage tests

---
 Makefile            |  2 +-
 test/CMakeLists.txt | 17 +++++++----------
 2 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/Makefile b/Makefile
index da3cc2f2..dbcf6111 100644
--- a/Makefile
+++ b/Makefile
@@ -77,7 +77,7 @@ check-fast:
 coverage:
 	rm -fr build_coverage
 	mkdir build_coverage
-	cd build_coverage ; CXX=g++-8 cmake .. -GNinja -DJSON_Coverage=ON -DJSON_MultipleHeaders=ON
+	cd build_coverage ; cmake .. -GNinja -DCMAKE_BUILD_TYPE=Debug -DJSON_Coverage=ON -DJSON_MultipleHeaders=ON
 	cd build_coverage ; ninja
 	cd build_coverage ; ctest -j10
 	cd build_coverage ; ninja lcov_html
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 28ad7f71..61ee0662 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -34,12 +34,6 @@ 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)
@@ -71,9 +65,7 @@ endif()
 # doctest library with the main function to speed up build
 #############################################################################
 
-add_library(doctest_main OBJECT
-    "src/unit.cpp"
-)
+add_library(doctest_main OBJECT src/unit.cpp)
 set_target_properties(doctest_main PROPERTIES
     COMPILE_DEFINITIONS "$<$<CXX_COMPILER_ID:MSVC>:_SCL_SECURE_NO_WARNINGS>"
     COMPILE_OPTIONS "$<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>"
@@ -162,7 +154,12 @@ foreach(file ${files})
         $<$<CXX_COMPILER_ID:GNU>:-Wno-deprecated-declarations>
     )
     target_include_directories(${testcase} PRIVATE ${CMAKE_BINARY_DIR}/include thirdparty/doctest thirdparty/fifo_map)
-    target_link_libraries(${testcase} ${NLOHMANN_JSON_TARGET_NAME})
+    target_link_libraries(${testcase} PRIVATE ${NLOHMANN_JSON_TARGET_NAME})
+
+    if (JSON_Coverage)
+        target_compile_options(${testcase} PRIVATE --coverage)
+        target_link_libraries(${testcase} PRIVATE --coverage)
+    endif()
 
     add_test(NAME "${testcase}"
         COMMAND ${testcase} ${DOCTEST_TEST_FILTER} --no-skip