From f78d2741ba41ddaeee9f26e1972364e4230cebf1 Mon Sep 17 00:00:00 2001
From: Stephen Roderick <kiwi.net@mac.com>
Date: Sat, 23 Apr 2011 08:20:39 -0400
Subject: [PATCH 3/5] cmake: Add code coverage

---
 CMakeLists.txt            |    8 ++++++++
 config/FindLcov.cmake     |   23 +++++++++++++++++++++++
 config/check_depend.cmake |   35 +++++++++++++++++++++++++++++++++++
 src/CMakeLists.txt        |    4 ++++
 4 files changed, 70 insertions(+), 0 deletions(-)
 create mode 100644 config/FindLcov.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 70b3ce7..64b23bc 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -72,6 +72,14 @@ IF(BUILD_TESTING)
   MARK_AS_ADVANCED(BUILDNAME)
 ENDIF(BUILD_TESTING)
 
+# turn on code coverage of tests
+include (CMakeDependentOption)
+CMAKE_DEPENDENT_OPTION(BUILD_ENABLE_COVERAGE "Turn on code coverage of all tests." OFF "ENABLE_TESTS" OFF)
+IF (BUILD_ENABLE_COVERAGE)
+  # coverage requires test
+  SET(ENABLE_TESTS ON CACHE BOOL "" FORCE)
+ENDIF (BUILD_ENABLE_COVERAGE)
+
 # MESSAGE("Looking for compiler:")
 INCLUDE (${CMAKE_ROOT}/Modules/CMakeDetermineCXXCompiler.cmake)
 
diff --git a/config/FindLcov.cmake b/config/FindLcov.cmake
new file mode 100644
index 0000000..8191c7a
--- /dev/null
+++ b/config/FindLcov.cmake
@@ -0,0 +1,23 @@
+################################################################################
+#
+# CMake script for finding lcov.
+# The default CMake search process is used to locate files.
+#
+# This script creates the following variables:
+#
+#  LCOV_FOUND               Boolean that indicates if the package was found
+#  LCOV_LCOV_EXECUTABLE     Where to find the 'lcov' tool
+#  LCOV_GENHTML_EXECUTABLE  Where to find the 'genhtml' tool
+#
+################################################################################
+
+include(FindPackageHandleStandardArgs)
+
+find_program(LCOV_LCOV_EXECUTABLE lcov)
+find_program(LCOV_GENHTML_EXECUTABLE genhtml)
+
+# Set LCOV_FOUND honoring the QUIET and REQUIRED arguments
+find_package_handle_standard_args(LCOV DEFAULT_MSG LCOV_LCOV_EXECUTABLE LCOV_GENHTML_EXECUTABLE)
+
+# Advanced options for not cluttering the cmake UIs
+mark_as_advanced(LCOV_LCOV_EXECUTABLE LCOV_GENHTML_EXECUTABLE)
diff --git a/config/check_depend.cmake b/config/check_depend.cmake
index 1aa03a5..7569d7a 100644
--- a/config/check_depend.cmake
+++ b/config/check_depend.cmake
@@ -267,6 +267,41 @@ IF (CMAKE_COMPILER_IS_GNUCXX)
 ENDIF()
 
 #
+# Set flags for code coverage, and setup coverage target
+#
+IF (BUILD_ENABLE_COVERAGE)
+
+  FIND_PACKAGE(Lcov REQUIRED)
+
+  # for required flags see
+  # http://git.benboeckel.net/?p=chasmd.git;a=blob;f=CMakeLists.txt
+  # http://www.cmake.org/Wiki/CTest:Coverage
+  # man gcov
+
+  SET(CMAKE_CXX_FLAGS_ADD "${CMAKE_CXX_FLAGS_ADD} -g -O0 -fprofile-arcs -ftest-coverage")
+  SET(CMAKE_C_FLAGS_ADD "${CMAKE_C_FLAGS_ADD} -g -O0 -fprofile-arcs -ftest-coverage")
+  SET(CMAKE_LD_FLAGS_ADD "${CMAKE_LD_FLAGS_ADD} -fprofile-arcs -ftest-coverage")
+
+  # coverage
+  ADD_CUSTOM_TARGET(coverage)
+  ADD_CUSTOM_COMMAND(TARGET coverage
+    COMMAND mkdir -p coverage
+    COMMAND ${LCOV_LCOV_EXECUTABLE} --directory . --zerocounters
+    COMMAND make check
+	# all coverage data
+    COMMAND ${LCOV_LCOV_EXECUTABLE} --directory . --capture --output-file ./coverage/all.info
+	# RTT-only coverage data
+    COMMAND ${LCOV_LCOV_EXECUTABLE} --output-file ./coverage/rtt.info -e ./coverage/all.info '${CMAKE_SOURCE_DIR}/*'
+	# generate based on RTT-only data
+    COMMAND ${LCOV_GENHTML_EXECUTABLE} -t "Orocos RTT coverage" -p "${CMAKE_SOURCE_DIR}"  -o ./coverage ./coverage/rtt.info
+    COMMAND echo "Open ${CMAKE_BINARY_DIR}/coverage/index.html to view the coverage analysis results."
+    WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
+    )
+  # todo dependancy of coverage on test?
+
+ENDIF (BUILD_ENABLE_COVERAGE)
+
+#
 # Check for Doxygen and enable documentation building
 #
 find_package( Doxygen )
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 2ebbe46..d85d3bb 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -203,6 +203,10 @@ ENDIF ( BUILD_STATIC )
     target_link_libraries(orocos-rtt-dynamic_${OROCOS_TARGET} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY})
   endif()
 
+IF (BUILD_ENABLE_COVERAGE)
+  target_link_libraries(orocos-rtt-dynamic_${OROCOS_TARGET} gcov)
+ENDIF (BUILD_ENABLE_COVERAGE)
+ 
 create_pc_flags( "${OROCOS-RTT_DEFINITIONS}" "${OROCOS-RTT_INCLUDE_DIRS}" "${OROCOS-RTT_LIBRARIES}" RTT_DEFINES RTT_CFLAGS RTT_LINKFLAGS)
 create_pc_flags( "${RTT_DUMMY}" "${RTT_DUMMY}" "${OROCOS-RTT_USER_LINK_LIBS}" RTT_DUMMY RTT_DUMMY RTT_USER_LINK_LIBS)
 
-- 
1.7.4.4

