CTestCoverageCollectGCOV.cmake 9.8 KB

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