gcov.rake 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. directory(GCOV_BUILD_OUTPUT_PATH)
  2. directory(GCOV_RESULTS_PATH)
  3. directory(GCOV_ARTIFACTS_PATH)
  4. directory(GCOV_DEPENDENCIES_PATH)
  5. CLEAN.include(File.join(GCOV_BUILD_OUTPUT_PATH, '*'))
  6. CLEAN.include(File.join(GCOV_RESULTS_PATH, '*'))
  7. CLEAN.include(File.join(GCOV_ARTIFACTS_PATH, '*'))
  8. CLEAN.include(File.join(GCOV_DEPENDENCIES_PATH, '*'))
  9. CLOBBER.include(File.join(GCOV_BUILD_PATH, '**/*'))
  10. rule(/#{GCOV_BUILD_OUTPUT_PATH}\/#{'.+\\' + EXTENSION_OBJECT}$/ => [
  11. proc do |task_name|
  12. @ceedling[:file_finder].find_compilation_input_file(task_name)
  13. end
  14. ]) do |object|
  15. if File.basename(object.source) =~ /^(#{PROJECT_TEST_FILE_PREFIX}|#{CMOCK_MOCK_PREFIX}|#{GCOV_IGNORE_SOURCES.join('|')})/i
  16. @ceedling[:generator].generate_object_file(
  17. TOOLS_GCOV_COMPILER,
  18. OPERATION_COMPILE_SYM,
  19. GCOV_SYM,
  20. object.source,
  21. object.name,
  22. @ceedling[:file_path_utils].form_test_build_list_filepath(object.name)
  23. )
  24. else
  25. @ceedling[GCOV_SYM].generate_coverage_object_file(object.source, object.name)
  26. end
  27. end
  28. rule(/#{GCOV_BUILD_OUTPUT_PATH}\/#{'.+\\' + EXTENSION_EXECUTABLE}$/) do |bin_file|
  29. lib_args = @ceedling[:test_invoker].convert_libraries_to_arguments()
  30. @ceedling[:generator].generate_executable_file(
  31. TOOLS_GCOV_LINKER,
  32. GCOV_SYM,
  33. bin_file.prerequisites,
  34. bin_file.name,
  35. lib_args,
  36. @ceedling[:file_path_utils].form_test_build_map_filepath(bin_file.name)
  37. )
  38. end
  39. rule(/#{GCOV_RESULTS_PATH}\/#{'.+\\' + EXTENSION_TESTPASS}$/ => [
  40. proc do |task_name|
  41. @ceedling[:file_path_utils].form_test_executable_filepath(task_name)
  42. end
  43. ]) do |test_result|
  44. @ceedling[:generator].generate_test_results(TOOLS_GCOV_FIXTURE, GCOV_SYM, test_result.source, test_result.name)
  45. end
  46. rule(/#{GCOV_DEPENDENCIES_PATH}\/#{'.+\\' + EXTENSION_DEPENDENCIES}$/ => [
  47. proc do |task_name|
  48. @ceedling[:file_finder].find_compilation_input_file(task_name)
  49. end
  50. ]) do |dep|
  51. @ceedling[:generator].generate_dependencies_file(
  52. TOOLS_TEST_DEPENDENCIES_GENERATOR,
  53. GCOV_SYM,
  54. dep.source,
  55. File.join(GCOV_BUILD_OUTPUT_PATH, File.basename(dep.source).ext(EXTENSION_OBJECT)),
  56. dep.name
  57. )
  58. end
  59. task directories: [GCOV_BUILD_OUTPUT_PATH, GCOV_RESULTS_PATH, GCOV_DEPENDENCIES_PATH, GCOV_ARTIFACTS_PATH]
  60. namespace GCOV_SYM do
  61. task source_coverage: COLLECTION_ALL_SOURCE.pathmap("#{GCOV_BUILD_OUTPUT_PATH}/%n#{@ceedling[:configurator].extension_object}")
  62. desc 'Run code coverage for all tests'
  63. task all: [:directories] do
  64. @ceedling[:configurator].replace_flattened_config(@ceedling[GCOV_SYM].config)
  65. @ceedling[:test_invoker].setup_and_invoke(COLLECTION_ALL_TESTS, GCOV_SYM)
  66. @ceedling[:configurator].restore_config
  67. end
  68. desc 'Run single test w/ coverage ([*] real test or source file name, no path).'
  69. task :* do
  70. message = "\nOops! '#{GCOV_ROOT_NAME}:*' isn't a real task. " \
  71. "Use a real test or source file name (no path) in place of the wildcard.\n" \
  72. "Example: rake #{GCOV_ROOT_NAME}:foo.c\n\n"
  73. @ceedling[:streaminator].stdout_puts(message)
  74. end
  75. desc 'Run tests by matching regular expression pattern.'
  76. task :pattern, [:regex] => [:directories] do |_t, args|
  77. matches = []
  78. COLLECTION_ALL_TESTS.each do |test|
  79. matches << test if test =~ /#{args.regex}/
  80. end
  81. if !matches.empty?
  82. @ceedling[:configurator].replace_flattened_config(@ceedling[GCOV_SYM].config)
  83. @ceedling[:test_invoker].setup_and_invoke(matches, GCOV_SYM, force_run: false)
  84. @ceedling[:configurator].restore_config
  85. else
  86. @ceedling[:streaminator].stdout_puts("\nFound no tests matching pattern /#{args.regex}/.")
  87. end
  88. end
  89. desc 'Run tests whose test path contains [dir] or [dir] substring.'
  90. task :path, [:dir] => [:directories] do |_t, args|
  91. matches = []
  92. COLLECTION_ALL_TESTS.each do |test|
  93. matches << test if File.dirname(test).include?(args.dir.tr('\\', '/'))
  94. end
  95. if !matches.empty?
  96. @ceedling[:configurator].replace_flattened_config(@ceedling[GCOV_SYM].config)
  97. @ceedling[:test_invoker].setup_and_invoke(matches, GCOV_SYM, force_run: false)
  98. @ceedling[:configurator].restore_config
  99. else
  100. @ceedling[:streaminator].stdout_puts("\nFound no tests including the given path or path component.")
  101. end
  102. end
  103. desc 'Run code coverage for changed files'
  104. task delta: [:directories] do
  105. @ceedling[:configurator].replace_flattened_config(@ceedling[GCOV_SYM].config)
  106. @ceedling[:test_invoker].setup_and_invoke(COLLECTION_ALL_TESTS, GCOV_SYM, force_run: false)
  107. @ceedling[:configurator].restore_config
  108. end
  109. # use a rule to increase efficiency for large projects
  110. # gcov test tasks by regex
  111. rule(/^#{GCOV_TASK_ROOT}\S+$/ => [
  112. proc do |task_name|
  113. test = task_name.sub(/#{GCOV_TASK_ROOT}/, '')
  114. test = "#{PROJECT_TEST_FILE_PREFIX}#{test}" unless test.start_with?(PROJECT_TEST_FILE_PREFIX)
  115. @ceedling[:file_finder].find_test_from_file_path(test)
  116. end
  117. ]) do |test|
  118. @ceedling[:rake_wrapper][:directories].invoke
  119. @ceedling[:configurator].replace_flattened_config(@ceedling[GCOV_SYM].config)
  120. @ceedling[:test_invoker].setup_and_invoke([test.source], GCOV_SYM)
  121. @ceedling[:configurator].restore_config
  122. end
  123. end
  124. if PROJECT_USE_DEEP_DEPENDENCIES
  125. namespace REFRESH_SYM do
  126. task GCOV_SYM do
  127. @ceedling[:configurator].replace_flattened_config(@ceedling[GCOV_SYM].config)
  128. @ceedling[:test_invoker].refresh_deep_dependencies
  129. @ceedling[:configurator].restore_config
  130. end
  131. end
  132. end
  133. namespace UTILS_SYM do
  134. def gcov_args_builder(opts)
  135. args = ""
  136. args += "-r \"#{opts[:gcov_report_root] || '.'}\" "
  137. args += "-f \"#{opts[:gcov_report_include]}\" " unless opts[:gcov_report_include].nil?
  138. args += "-e \"#{opts[:gcov_report_exclude] || GCOV_FILTER_EXCLUDE}\" "
  139. [ :gcov_fail_under_line, :gcov_fail_under_branch, :gcov_html_medium_threshold, :gcov_html_high_threshold].each do |opt|
  140. args += "--#{opt.to_s.gsub('_','-').sub(/:?gcov-/,'')} #{opts[opt]} " unless opts[opt].nil?
  141. end
  142. return args
  143. end
  144. desc 'Create gcov code coverage html report (must run ceedling gcov first)'
  145. task GCOV_SYM do
  146. if !File.directory? GCOV_ARTIFACTS_PATH
  147. FileUtils.mkdir_p GCOV_ARTIFACTS_PATH
  148. end
  149. args = gcov_args_builder(@ceedling[:configurator].project_config_hash)
  150. if @ceedling[:configurator].project_config_hash[:gcov_html_report].nil?
  151. puts "In your project.yml, define: \n\n:gcov:\n :html_report:\n\n to true or false to refine this feature."
  152. puts "For now, assumimg you want an html report generated."
  153. html_enabled = true
  154. else
  155. html_enabled = @ceedling[:configurator].project_config_hash[:gcov_html_report]
  156. end
  157. if @ceedling[:configurator].project_config_hash[:gcov_xml_report].nil?
  158. puts "In your project.yml, define: \n\n:gcov:\n :xml_report:\n\n to true or false to refine this feature."
  159. puts "For now, assumimg you do not want an xml report generated."
  160. xml_enabled = false
  161. else
  162. xml_enabled = @ceedling[:configurator].project_config_hash[:gcov_xml_report]
  163. end
  164. if html_enabled
  165. if @ceedling[:configurator].project_config_hash[:gcov_html_report_type] == 'basic'
  166. puts "Creating a basic html report of gcov results in #{GCOV_ARTIFACTS_FILE}..."
  167. command = @ceedling[:tool_executor].build_command_line(TOOLS_GCOV_POST_REPORT_BASIC, [], args)
  168. @ceedling[:tool_executor].exec(command[:line], command[:options])
  169. elsif @ceedling[:configurator].project_config_hash[:gcov_html_report_type] == 'detailed'
  170. puts "Creating a detailed html report of gcov results in #{GCOV_ARTIFACTS_FILE}..."
  171. command = @ceedling[:tool_executor].build_command_line(TOOLS_GCOV_POST_REPORT_ADVANCED, [], args)
  172. @ceedling[:tool_executor].exec(command[:line], command[:options])
  173. else
  174. puts "In your project.yml, define: \n\n:gcov:\n :html_report_type:\n\n to basic or detailed to refine this feature."
  175. puts "For now, just creating basic."
  176. puts "Creating a basic html report of gcov results in #{GCOV_ARTIFACTS_FILE}..."
  177. command = @ceedling[:tool_executor].build_command_line(TOOLS_GCOV_POST_REPORT_BASIC, [], args)
  178. @ceedling[:tool_executor].exec(command[:line], command[:options])
  179. end
  180. end
  181. if xml_enabled
  182. puts "Creating an xml report of gcov results in #{GCOV_ARTIFACTS_FILE_XML}..."
  183. command = @ceedling[:tool_executor].build_command_line(TOOLS_GCOV_POST_REPORT_XML, [], filter)
  184. @ceedling[:tool_executor].exec(command[:line], command[:options])
  185. end
  186. puts "Done."
  187. end
  188. end