@@ -124,6 +124,35 @@ else()
124124 option (ENABLE_BUILD_TESTS "Build tests" OFF )
125125 option (ENABLE_VALGRIND_TESTS "Build tests with valgrind" OFF )
126126endif ()
127+ option (ENABLE_COVERAGE "Build code coverage report from tests" OFF )
128+
129+ if (ENABLE_COVERAGE)
130+ find_program (PATH_GCOV NAMES gcov )
131+ if (NOT PATH_GCOV)
132+ message (WARNING "'gcov' executable not found! Disabling building code coverage report." )
133+ set (ENABLE_COVERAGE OFF )
134+ endif ()
135+
136+ find_program (PATH_LCOV NAMES lcov )
137+ if (NOT PATH_LCOV)
138+ message (WARNING "'lcov' executable not found! Disabling building code coverage report." )
139+ set (ENABLE_COVERAGE OFF )
140+ endif ()
141+
142+ find_program (PATH_GENHTML NAMES genhtml )
143+ if (NOT PATH_GENHTML)
144+ message (WARNING "'genhtml' executable not found! Disabling building code coverage report." )
145+ set (ENABLE_COVERAGE OFF )
146+ endif ()
147+
148+ if (NOT CMAKE_COMPILER_IS_GNUCC)
149+ message (WARNING "Compiler is not gcc! Coverage may break the tests!" )
150+ endif ()
151+
152+ if (ENABLE_COVERAGE)
153+ set (CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS} --coverage -fprofile-arcs -ftest-coverage" )
154+ endif ()
155+ endif ()
127156
128157# dependencies - pthread
129158set (CMAKE_THREAD_PREFER_PTHREAD TRUE )
@@ -141,7 +170,7 @@ if(ENABLE_TLS OR ENABLE_DNSSEC OR ENABLE_SSH)
141170 find_package (OpenSSL REQUIRED )
142171 if (ENABLE_TLS)
143172 message (STATUS "OPENSSL found, required for TLS" )
144- set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNC_ENABLED_TLS" )
173+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_COVERAGE} -DNC_ENABLED_TLS" )
145174 endif ()
146175
147176 target_link_libraries (netconf2 ${OPENSSL_LIBRARIES} )
@@ -158,7 +187,7 @@ if(ENABLE_SSH)
158187 target_link_libraries (netconf2 ${LIBSSH_LIBRARIES} )
159188 list (APPEND CMAKE_REQUIRED_LIBRARIES ${LIBSSH_LIBRARIES} )
160189 include_directories (${LIBSSH_INCLUDE_DIRS} )
161- set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNC_ENABLED_SSH" )
190+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_COVERAGE} -DNC_ENABLED_SSH" )
162191
163192 # crypt
164193 if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "QNX" )
@@ -173,7 +202,7 @@ endif()
173202# dependencies - libval
174203if (ENABLE_DNSSEC)
175204 find_package (LibVAL REQUIRED )
176- set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DENABLE_DNSSEC" )
205+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_COVERAGE} -DENABLE_DNSSEC" )
177206 target_link_libraries (netconf2 ${LIBVAL_LIBRARIES} )
178207 include_directories (${LIBVAL_INCLUDE_DIRS} )
179208endif ()
0 commit comments