CTestCoverageCollectGCOV.cmake 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. CTestCoverageCollectGCOV
  5. ------------------------
  6. This module provides the ``ctest_coverage_collect_gcov`` function.
  7. This function runs gcov on all .gcda files found in the binary tree
  8. and packages the resulting .gcov files into a tar file.
  9. This tarball also contains the following:
  10. * *data.json* defines the source and build directories for use by CDash.
  11. * *Labels.json* indicates any :prop_sf:`LABELS` that have been set on the
  12. source files.
  13. * The *uncovered* directory holds any uncovered files found by
  14. :variable:`CTEST_EXTRA_COVERAGE_GLOB`.
  15. After generating this tar file, it can be sent to CDash for display with the
  16. :command:`ctest_submit(CDASH_UPLOAD)` command.
  17. .. command:: cdash_coverage_collect_gcov
  18. ::
  19. ctest_coverage_collect_gcov(TARBALL <tarfile>
  20. [SOURCE <source_dir>][BUILD <build_dir>]
  21. [GCOV_COMMAND <gcov_command>]
  22. [GCOV_OPTIONS <options>...]
  23. )
  24. Run gcov and package a tar file for CDash. The options are:
  25. ``TARBALL <tarfile>``
  26. Specify the location of the ``.tar`` file to be created for later
  27. upload to CDash. Relative paths will be interpreted with respect
  28. to the top-level build directory.
  29. ``SOURCE <source_dir>``
  30. Specify the top-level source directory for the build.
  31. Default is the value of :variable:`CTEST_SOURCE_DIRECTORY`.
  32. ``BUILD <build_dir>``
  33. Specify the top-level build directory for the build.
  34. Default is the value of :variable:`CTEST_BINARY_DIRECTORY`.
  35. ``GCOV_COMMAND <gcov_command>``
  36. Specify the full path to the ``gcov`` command on the machine.
  37. Default is the value of :variable:`CTEST_COVERAGE_COMMAND`.
  38. ``GCOV_OPTIONS <options>...``
  39. Specify options to be passed to gcov. The ``gcov`` command
  40. is run as ``gcov <options>... -o <gcov-dir> <file>.gcda``.
  41. If not specified, the default option is just ``-b``.
  42. ``GLOB``
  43. Recursively search for .gcda files in build_dir rather than
  44. determining search locations by reading TargetDirectories.txt.
  45. ``DELETE``
  46. Delete coverage files after they've been packaged into the .tar.
  47. ``QUIET``
  48. Suppress non-error messages that otherwise would have been
  49. printed out by this function.
  50. #]=======================================================================]
  51. function(ctest_coverage_collect_gcov)
  52. set(options QUIET GLOB DELETE)
  53. set(oneValueArgs TARBALL SOURCE BUILD GCOV_COMMAND)
  54. set(multiValueArgs GCOV_OPTIONS)
  55. cmake_parse_arguments(GCOV "${options}" "${oneValueArgs}"
  56. "${multiValueArgs}" "" ${ARGN} )
  57. if(NOT DEFINED GCOV_TARBALL)
  58. message(FATAL_ERROR
  59. "TARBALL must be specified. for ctest_coverage_collect_gcov")
  60. endif()
  61. if(NOT DEFINED GCOV_SOURCE)
  62. set(source_dir "${CTEST_SOURCE_DIRECTORY}")
  63. else()
  64. set(source_dir "${GCOV_SOURCE}")
  65. endif()
  66. if(NOT DEFINED GCOV_BUILD)
  67. set(binary_dir "${CTEST_BINARY_DIRECTORY}")
  68. else()
  69. set(binary_dir "${GCOV_BUILD}")
  70. endif()
  71. if(NOT DEFINED GCOV_GCOV_COMMAND)
  72. set(gcov_command "${CTEST_COVERAGE_COMMAND}")
  73. else()
  74. set(gcov_command "${GCOV_GCOV_COMMAND}")
  75. endif()
  76. # run gcov on each gcda file in the binary tree
  77. set(gcda_files)
  78. set(label_files)
  79. if (GCOV_GLOB)
  80. file(GLOB_RECURSE gfiles RELATIVE ${binary_dir} "${binary_dir}/*.gcda")
  81. list(LENGTH gfiles len)
  82. # if we have gcda files then also grab the labels file for that target
  83. if(${len} GREATER 0)
  84. file(GLOB_RECURSE lfiles RELATIVE ${binary_dir} "${binary_dir}/Labels.json")
  85. list(APPEND gcda_files ${gfiles})
  86. list(APPEND label_files ${lfiles})
  87. endif()
  88. else()
  89. # look for gcda files in the target directories
  90. # this will be faster and only look where the files will be
  91. file(STRINGS "${binary_dir}/CMakeFiles/TargetDirectories.txt" target_dirs
  92. ENCODING UTF-8)
  93. foreach(target_dir ${target_dirs})
  94. file(GLOB_RECURSE gfiles RELATIVE ${binary_dir} "${target_dir}/*.gcda")
  95. list(LENGTH gfiles len)
  96. # if we have gcda files then also grab the labels file for that target
  97. if(${len} GREATER 0)
  98. file(GLOB_RECURSE lfiles RELATIVE ${binary_dir}
  99. "${target_dir}/Labels.json")
  100. list(APPEND gcda_files ${gfiles})
  101. list(APPEND label_files ${lfiles})
  102. endif()
  103. endforeach()
  104. endif()
  105. # return early if no coverage files were found
  106. list(LENGTH gcda_files len)
  107. if(len EQUAL 0)
  108. if (NOT GCOV_QUIET)
  109. message("ctest_coverage_collect_gcov: No .gcda files found, "
  110. "ignoring coverage request.")
  111. endif()
  112. return()
  113. endif()
  114. # setup the dir for the coverage files
  115. set(coverage_dir "${binary_dir}/Testing/CoverageInfo")
  116. file(MAKE_DIRECTORY "${coverage_dir}")
  117. # call gcov on each .gcda file
  118. foreach (gcda_file ${gcda_files})
  119. # get the directory of the gcda file
  120. get_filename_component(gcda_file ${binary_dir}/${gcda_file} ABSOLUTE)
  121. get_filename_component(gcov_dir ${gcda_file} DIRECTORY)
  122. # run gcov, this will produce the .gcov file in the current
  123. # working directory
  124. if(NOT DEFINED GCOV_GCOV_OPTIONS)
  125. set(GCOV_GCOV_OPTIONS -b)
  126. endif()
  127. execute_process(COMMAND
  128. ${gcov_command} ${GCOV_GCOV_OPTIONS} -o ${gcov_dir} ${gcda_file}
  129. OUTPUT_VARIABLE out
  130. RESULT_VARIABLE res
  131. WORKING_DIRECTORY ${coverage_dir})
  132. if (GCOV_DELETE)
  133. file(REMOVE ${gcda_file})
  134. endif()
  135. endforeach()
  136. if(NOT "${res}" EQUAL 0)
  137. if (NOT GCOV_QUIET)
  138. message(STATUS "Error running gcov: ${res} ${out}")
  139. endif()
  140. endif()
  141. # create json file with project information
  142. file(WRITE ${coverage_dir}/data.json
  143. "{
  144. \"Source\": \"${source_dir}\",
  145. \"Binary\": \"${binary_dir}\"
  146. }")
  147. # collect the gcov files
  148. set(unfiltered_gcov_files)
  149. file(GLOB_RECURSE unfiltered_gcov_files RELATIVE ${binary_dir} "${coverage_dir}/*.gcov")
  150. # if CTEST_EXTRA_COVERAGE_GLOB was specified we search for files
  151. # that might be uncovered
  152. if (DEFINED CTEST_EXTRA_COVERAGE_GLOB)
  153. set(uncovered_files)
  154. foreach(search_entry IN LISTS CTEST_EXTRA_COVERAGE_GLOB)
  155. if(NOT GCOV_QUIET)
  156. message("Add coverage glob: ${search_entry}")
  157. endif()
  158. file(GLOB_RECURSE matching_files "${source_dir}/${search_entry}")
  159. if (matching_files)
  160. list(APPEND uncovered_files "${matching_files}")
  161. endif()
  162. endforeach()
  163. endif()
  164. set(gcov_files)
  165. foreach(gcov_file ${unfiltered_gcov_files})
  166. file(STRINGS ${binary_dir}/${gcov_file} first_line LIMIT_COUNT 1 ENCODING UTF-8)
  167. set(is_excluded false)
  168. if(first_line MATCHES "^ -: 0:Source:(.*)$")
  169. set(source_file ${CMAKE_MATCH_1})
  170. elseif(NOT GCOV_QUIET)
  171. message(STATUS "Could not determine source file corresponding to: ${gcov_file}")
  172. endif()
  173. foreach(exclude_entry IN LISTS CTEST_CUSTOM_COVERAGE_EXCLUDE)
  174. if(source_file MATCHES "${exclude_entry}")
  175. set(is_excluded true)
  176. if(NOT GCOV_QUIET)
  177. message("Excluding coverage for: ${source_file} which matches ${exclude_entry}")
  178. endif()
  179. break()
  180. endif()
  181. endforeach()
  182. get_filename_component(resolved_source_file "${source_file}" ABSOLUTE)
  183. foreach(uncovered_file IN LISTS uncovered_files)
  184. get_filename_component(resolved_uncovered_file "${uncovered_file}" ABSOLUTE)
  185. if (resolved_uncovered_file STREQUAL resolved_source_file)
  186. list(REMOVE_ITEM uncovered_files "${uncovered_file}")
  187. endif()
  188. endforeach()
  189. if(NOT is_excluded)
  190. list(APPEND gcov_files ${gcov_file})
  191. endif()
  192. endforeach()
  193. foreach (uncovered_file ${uncovered_files})
  194. # Check if this uncovered file should be excluded.
  195. set(is_excluded false)
  196. foreach(exclude_entry IN LISTS CTEST_CUSTOM_COVERAGE_EXCLUDE)
  197. if(uncovered_file MATCHES "${exclude_entry}")
  198. set(is_excluded true)
  199. if(NOT GCOV_QUIET)
  200. message("Excluding coverage for: ${uncovered_file} which matches ${exclude_entry}")
  201. endif()
  202. break()
  203. endif()
  204. endforeach()
  205. if(is_excluded)
  206. continue()
  207. endif()
  208. # Copy from source to binary dir, preserving any intermediate subdirectories.
  209. get_filename_component(filename "${uncovered_file}" NAME)
  210. get_filename_component(relative_path "${uncovered_file}" DIRECTORY)
  211. string(REPLACE "${source_dir}" "" relative_path "${relative_path}")
  212. if (relative_path)
  213. # Strip leading slash.
  214. string(SUBSTRING "${relative_path}" 1 -1 relative_path)
  215. endif()
  216. file(COPY ${uncovered_file} DESTINATION ${binary_dir}/uncovered/${relative_path})
  217. if(relative_path)
  218. list(APPEND uncovered_files_for_tar uncovered/${relative_path}/${filename})
  219. else()
  220. list(APPEND uncovered_files_for_tar uncovered/${filename})
  221. endif()
  222. endforeach()
  223. # tar up the coverage info with the same date so that the md5
  224. # sum will be the same for the tar file independent of file time
  225. # stamps
  226. string(REPLACE ";" "\n" gcov_files "${gcov_files}")
  227. string(REPLACE ";" "\n" label_files "${label_files}")
  228. string(REPLACE ";" "\n" uncovered_files_for_tar "${uncovered_files_for_tar}")
  229. file(WRITE "${coverage_dir}/coverage_file_list.txt"
  230. "${gcov_files}
  231. ${coverage_dir}/data.json
  232. ${label_files}
  233. ${uncovered_files_for_tar}
  234. ")
  235. if (GCOV_QUIET)
  236. set(tar_opts "cfj")
  237. else()
  238. set(tar_opts "cvfj")
  239. endif()
  240. execute_process(COMMAND
  241. ${CMAKE_COMMAND} -E tar ${tar_opts} ${GCOV_TARBALL}
  242. "--mtime=1970-01-01 0:0:0 UTC"
  243. "--format=gnutar"
  244. --files-from=${coverage_dir}/coverage_file_list.txt
  245. WORKING_DIRECTORY ${binary_dir})
  246. if (GCOV_DELETE)
  247. foreach(gcov_file ${unfiltered_gcov_files})
  248. file(REMOVE ${binary_dir}/${gcov_file})
  249. endforeach()
  250. file(REMOVE ${coverage_dir}/coverage_file_list.txt)
  251. file(REMOVE ${coverage_dir}/data.json)
  252. if (EXISTS ${binary_dir}/uncovered)
  253. file(REMOVE ${binary_dir}/uncovered)
  254. endif()
  255. endif()
  256. endfunction()