##// END OF EJS Templates
Merge pull request 134 from SCIQLOP-Initialisation feature/Gcov...
perrinel -
r43:0126f33b354b merge
parent child
Show More
@@ -0,0 +1,191
1 # Copyright (c) 2012 - 2017, Lars Bilke
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without modification,
5 # are permitted provided that the following conditions are met:
6 #
7 # 1. Redistributions of source code must retain the above copyright notice, this
8 # list of conditions and the following disclaimer.
9 #
10 # 2. Redistributions in binary form must reproduce the above copyright notice,
11 # this list of conditions and the following disclaimer in the documentation
12 # and/or other materials provided with the distribution.
13 #
14 # 3. Neither the name of the copyright holder nor the names of its contributors
15 # may be used to endorse or promote products derived from this software without
16 # specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #
29 # CHANGES:
30 #
31 # 2012-01-31, Lars Bilke
32 # - Enable Code Coverage
33 #
34 # 2013-09-17, Joakim Söderberg
35 # - Added support for Clang.
36 # - Some additional usage instructions.
37 #
38 # 2016-02-03, Lars Bilke
39 # - Refactored functions to use named parameters
40 #
41 # 2017-06-02, Lars Bilke
42 # - Merged with modified version from github.com/ufz/ogs
43 #
44 #
45 # USAGE:
46 #
47 # 1. Copy this file into your cmake modules path.
48 #
49 # 2. Add the following line to your CMakeLists.txt:
50 # include(CodeCoverage)
51 #
52 # 3. Append necessary compiler flags:
53 # APPEND_COVERAGE_COMPILER_FLAGS()
54 #
55 # 4. If you need to exclude additional directories from the report, specify them
56 # using the COVERAGE_EXCLUDES variable before calling SETUP_TARGET_FOR_COVERAGE.
57 # Example:
58 # set(COVERAGE_EXCLUDES 'dir1/*' 'dir2/*')
59 #
60 # 5. Use the function SETUP_TARGET_FOR_COVERAGE to create a custom make target
61 # which runs your test executable and produces a lcov code coverage report:
62 # Example:
63 # SETUP_TARGET_FOR_COVERAGE(
64 # my_coverage_target # Name for custom target.
65 # test_driver # Name of the test driver executable that runs the tests.
66 # # NOTE! This should always have a ZERO as exit code
67 # # otherwise the coverage generation will not complete.
68 # coverage # Name of output directory.
69 # )
70 #
71 # 6. Build a Debug build:
72 # cmake -DCMAKE_BUILD_TYPE=Debug ..
73 # make
74 # make my_coverage_target
75 #
76
77 include(CMakeParseArguments)
78
79 # Check prereqs
80 find_program( GCOV_PATH gcov )
81 find_program( LCOV_PATH lcov )
82 find_program( GENHTML_PATH genhtml )
83 find_program( GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/scripts/test)
84 find_program( SIMPLE_PYTHON_EXECUTABLE python )
85
86 if(NOT GCOV_PATH)
87 message(FATAL_ERROR "gcov not found! Aborting...")
88 endif() # NOT GCOV_PATH
89
90 if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
91 if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS 3)
92 message(FATAL_ERROR "Clang version must be 3.0.0 or greater! Aborting...")
93 endif()
94 elseif(NOT CMAKE_COMPILER_IS_GNUCXX)
95 message(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
96 endif()
97
98 set(COVERAGE_COMPILER_FLAGS "-g -O0 --coverage -fprofile-arcs -ftest-coverage"
99 CACHE INTERNAL "")
100
101 set(CMAKE_CXX_FLAGS_COVERAGE
102 ${COVERAGE_COMPILER_FLAGS}
103 CACHE STRING "Flags used by the C++ compiler during coverage builds."
104 FORCE )
105 set(CMAKE_C_FLAGS_COVERAGE
106 ${COVERAGE_COMPILER_FLAGS}
107 CACHE STRING "Flags used by the C compiler during coverage builds."
108 FORCE )
109 set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
110 ""
111 CACHE STRING "Flags used for linking binaries during coverage builds."
112 FORCE )
113 set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
114 ""
115 CACHE STRING "Flags used by the shared libraries linker during coverage builds."
116 FORCE )
117 mark_as_advanced(
118 CMAKE_CXX_FLAGS_COVERAGE
119 CMAKE_C_FLAGS_COVERAGE
120 CMAKE_EXE_LINKER_FLAGS_COVERAGE
121 CMAKE_SHARED_LINKER_FLAGS_COVERAGE )
122
123 if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
124 message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading")
125 endif() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
126
127 if(CMAKE_COMPILER_IS_GNUCXX)
128 link_libraries(gcov)
129 else()
130 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
131 endif()
132
133 # Defines a target for running and collection code coverage information
134 # Builds dependencies, runs the given executable and outputs reports.
135 # NOTE! The executable should always have a ZERO as exit code otherwise
136 # the coverage generation will not complete.
137 #
138 # SETUP_TARGET_FOR_COVERAGE(
139 # NAME testrunner_coverage # New target name
140 # EXECUTABLE testrunner -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
141 # DEPENDENCIES testrunner # Dependencies to build first
142 # )
143 function(SETUP_TARGET_FOR_COVERAGE)
144
145 set(options NONE)
146 set(oneValueArgs NAME TARGET OUTPUT)
147 set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
148 cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
149
150 if(NOT LCOV_PATH)
151 message(FATAL_ERROR "lcov not found! Aborting...")
152 endif() # NOT LCOV_PATH
153
154 if(NOT GENHTML_PATH)
155 message(FATAL_ERROR "genhtml not found! Aborting...")
156 endif() # NOT GENHTML_PATH
157
158 # Setup target
159 add_custom_target(${Coverage_TARGET}
160
161 # Cleanup lcov
162 COMMAND ${LCOV_PATH} --directory . --zerocounters
163
164 # Run tests
165 COMMAND ${Coverage_EXECUTABLE}
166
167 # Capturing lcov counters and generating report
168 COMMAND ${LCOV_PATH} --directory . --capture --output-file ${Coverage_NAME}.info
169 COMMAND ${LCOV_PATH} --remove ${Coverage_NAME}.info ${COVERAGE_EXCLUDES} --output-file ${Coverage_NAME}.info.cleaned
170 COMMAND ${GENHTML_PATH} -o ${Coverage_OUTPUT} ${Coverage_NAME}.info.cleaned
171 COMMAND ${CMAKE_COMMAND} -E remove ${Coverage_NAME}.info ${Coverage_NAME}.info.cleaned
172
173 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
174 DEPENDS ${Coverage_DEPENDENCIES}
175 COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
176 )
177
178 # Show info where to find the report
179 # message("Nome de la target ${Coverage_TARGET}")
180 # add_custom_command(TARGET ${Coverage_TARGET} POST_BUILD
181 # COMMAND ;
182 # COMMENT "Open ./${Coverage_NAME}/index.html in your browser to view the coverage report."
183 # )
184
185 endfunction()
186
187 function(APPEND_COVERAGE_COMPILER_FLAGS)
188 set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
189 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
190 message(STATUS "Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}")
191 endfunction() # APPEND_COVERAGE_COMPILER_FLAGS
@@ -82,6 +82,7 SCIQLOP_SET_TO_PARENT_SCOPE(CHECKSTYLE_EXCLUSION_FILES)
82 # Compile the tests
82 # Compile the tests
83 #
83 #
84 IF(BUILD_TESTS)
84 IF(BUILD_TESTS)
85
85 INCLUDE_DIRECTORIES(${SOURCES_DIR})
86 INCLUDE_DIRECTORIES(${SOURCES_DIR})
86 FILE (GLOB_RECURSE TESTS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/Test*.cpp)
87 FILE (GLOB_RECURSE TESTS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/Test*.cpp)
87 FILE (GLOB_RECURSE TESTS_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/Test*.h)
88 FILE (GLOB_RECURSE TESTS_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/Test*.h)
@@ -101,7 +102,7 IF(BUILD_TESTS)
101 LIST (REMOVE_ITEM currentTestSources ${TESTS_HEADERS})
102 LIST (REMOVE_ITEM currentTestSources ${TESTS_HEADERS})
102
103
103 ADD_EXECUTABLE(${testName} ${testFile} ${currentTestSources})
104 ADD_EXECUTABLE(${testName} ${testFile} ${currentTestSources})
104 TARGET_LINK_LIBRARIES( ${testName} ${TEST_LIBRARIES} )
105 TARGET_LINK_LIBRARIES( ${testName} ${TEST_LIBRARIES} gcov)
105 qt5_use_modules(${testName} Test)
106 qt5_use_modules(${testName} Test)
106
107
107 ADD_TEST( NAME ${testName} COMMAND ${testName} )
108 ADD_TEST( NAME ${testName} COMMAND ${testName} )
@@ -112,6 +113,7 IF(BUILD_TESTS)
112 LIST(APPEND testFilesToFormat ${TESTS_SOURCES})
113 LIST(APPEND testFilesToFormat ${TESTS_SOURCES})
113 LIST(APPEND testFilesToFormat ${TESTS_HEADERS})
114 LIST(APPEND testFilesToFormat ${TESTS_HEADERS})
114 LIST(APPEND FORMATTING_INPUT_FILES ${testFilesToFormat})
115 LIST(APPEND FORMATTING_INPUT_FILES ${testFilesToFormat})
116
115 SCIQLOP_SET_TO_PARENT_SCOPE(FORMATTING_INPUT_FILES)
117 SCIQLOP_SET_TO_PARENT_SCOPE(FORMATTING_INPUT_FILES)
116 ENDIF(BUILD_TESTS)
118 ENDIF(BUILD_TESTS)
117
119
@@ -13,6 +13,12 ELSE()
13 SET (CONFIG_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/dist/${CMAKE_BUILD_TYPE})
13 SET (CONFIG_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/dist/${CMAKE_BUILD_TYPE})
14 ENDIF()
14 ENDIF()
15
15
16 INCLUDE ("cmake/sciqlop_code_coverage.cmake")
17
18 if(BUILD_TESTS)
19 APPEND_COVERAGE_COMPILER_FLAGS()
20 endif(BUILD_TESTS)
21
16 #
22 #
17 # Compile the diffents modules
23 # Compile the diffents modules
18 #
24 #
@@ -36,7 +42,7 FILE(COPY ${QTLOGGING_INI_FILE} DESTINATION ${CONFIG_OUTPUT_PATH})
36 #
42 #
37 # Vera++ exclusion files
43 # Vera++ exclusion files
38 LIST(APPEND CHECKSTYLE_EXCLUSION_FILES ${CMAKE_CURRENT_SOURCE_DIR}/formatting/vera-exclusions/exclusions.txt)
44 LIST(APPEND CHECKSTYLE_EXCLUSION_FILES ${CMAKE_CURRENT_SOURCE_DIR}/formatting/vera-exclusions/exclusions.txt)
39 SCIQLOP_SET_TO_PARENT_SCOPE(CHECKSTYLE_EXCLUSION_FILES)
45 #SCIQLOP_SET_TO_PARENT_SCOPE(CHECKSTYLE_EXCLUSION_FILES)
40 INCLUDE ("cmake/sciqlop_formatting.cmake")
46 INCLUDE ("cmake/sciqlop_formatting.cmake")
41
47
42 #
48 #
@@ -7,11 +7,9
7 #SET(SCIQLOP_BUILD_TYPE "Release" CACHE STRING "Choose to compile in Debug or Release mode")
7 #SET(SCIQLOP_BUILD_TYPE "Release" CACHE STRING "Choose to compile in Debug or Release mode")
8
8
9 IF(CMAKE_BUILD_TYPE MATCHES "Debug")
9 IF(CMAKE_BUILD_TYPE MATCHES "Debug")
10 SET (CMAKE_BUILD_TYPE "Debug")
11 SET (DEBUG_SUFFIX "d")
10 SET (DEBUG_SUFFIX "d")
12 ELSE()
11 ELSE()
13 MESSAGE (STATUS "Build in Release")
12 MESSAGE (STATUS "Build in Release")
14 SET (SCIQLOP_BUILD_TYPE "Release")
15 SET (DEBUG_SUFFIX "")
13 SET (DEBUG_SUFFIX "")
16 ENDIF()
14 ENDIF()
17
15
@@ -88,9 +88,11 IF(BUILD_TESTS)
88 TARGET_LINK_LIBRARIES( ${testName} ${TEST_LIBRARIES} )
88 TARGET_LINK_LIBRARIES( ${testName} ${TEST_LIBRARIES} )
89 qt5_use_modules(${testName} Test)
89 qt5_use_modules(${testName} Test)
90
90
91 ADD_TEST( NAME ${testName} COMMAND ${testName} )
91 ADD_TEST( NAME ${testName} COMMAND ${testDirectory} )
92
92
93 SCIQLOP_COPY_TO_TARGET(RUNTIME ${testName} ${EXTERN_SHARED_LIBRARIES})
93 SCIQLOP_COPY_TO_TARGET(RUNTIME ${testName} ${EXTERN_SHARED_LIBRARIES})
94 set(Coverage_NAME ${testName})
95 SETUP_TARGET_FOR_COVERAGE(TARGET ${testName}_coverage OUTPUT ${testFile}-path NAME ${testFile} EXECUTABLE ${testName})
94 ENDFOREACH( testFile )
96 ENDFOREACH( testFile )
95
97
96 LIST(APPEND testFilesToFormat ${TESTS_SOURCES})
98 LIST(APPEND testFilesToFormat ${TESTS_SOURCES})
General Comments 0
You need to be logged in to leave comments. Login now