rakefile_helper.rb 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. # ==========================================
  2. # Unity Project - A Test Framework for C
  3. # Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
  4. # [Released under MIT License. Please refer to license.txt for details]
  5. # ==========================================
  6. require 'yaml'
  7. require 'fileutils'
  8. require UNITY_ROOT + '../auto/unity_test_summary'
  9. require UNITY_ROOT + '../auto/generate_test_runner'
  10. require UNITY_ROOT + '../auto/colour_reporter'
  11. module RakefileHelpers
  12. C_EXTENSION = '.c'.freeze
  13. def load_configuration(config_file)
  14. return if $configured
  15. $cfg_file = "targets/#{config_file}" unless config_file =~ /[\\|\/]/
  16. $cfg = YAML.load(File.read($cfg_file))
  17. $colour_output = false unless $cfg['colour']
  18. $configured = true if config_file != DEFAULT_CONFIG_FILE
  19. end
  20. def configure_clean
  21. CLEAN.include($cfg['compiler']['build_path'] + '*.*') unless $cfg['compiler']['build_path'].nil?
  22. end
  23. def configure_toolchain(config_file = DEFAULT_CONFIG_FILE)
  24. config_file += '.yml' unless config_file =~ /\.yml$/
  25. config_file = config_file unless config_file =~ /[\\|\/]/
  26. load_configuration(config_file)
  27. configure_clean
  28. end
  29. def unit_test_files
  30. path = $cfg['compiler']['unit_tests_path'] + 'test*' + C_EXTENSION
  31. path.tr!('\\', '/')
  32. FileList.new(path)
  33. end
  34. def local_include_dirs
  35. include_dirs = $cfg['compiler']['includes']['items'].dup
  36. include_dirs.delete_if { |dir| dir.is_a?(Array) }
  37. include_dirs
  38. end
  39. def extract_headers(filename)
  40. includes = []
  41. lines = File.readlines(filename)
  42. lines.each do |line|
  43. m = line.match(/^\s*#include\s+\"\s*(.+\.[hH])\s*\"/)
  44. includes << m[1] unless m.nil?
  45. end
  46. includes
  47. end
  48. def find_source_file(header, paths)
  49. paths.each do |dir|
  50. src_file = dir + header.ext(C_EXTENSION)
  51. return src_file if File.exist?(src_file)
  52. end
  53. nil
  54. end
  55. def tackit(strings)
  56. result = if strings.is_a?(Array)
  57. "\"#{strings.join}\""
  58. else
  59. strings
  60. end
  61. result
  62. end
  63. def squash(prefix, items)
  64. result = ''
  65. items.each { |item| result += " #{prefix}#{tackit(item)}" }
  66. result
  67. end
  68. def should(behave, &block)
  69. if block
  70. puts 'Should ' + behave
  71. yield block
  72. else
  73. puts "UNIMPLEMENTED CASE: Should #{behave}"
  74. end
  75. end
  76. def build_compiler_fields(inject_defines)
  77. command = tackit($cfg['compiler']['path'])
  78. defines = if $cfg['compiler']['defines']['items'].nil?
  79. ''
  80. else
  81. squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items'] + ['UNITY_OUTPUT_CHAR=putcharSpy'] + inject_defines)
  82. end
  83. options = squash('', $cfg['compiler']['options'])
  84. includes = squash($cfg['compiler']['includes']['prefix'], $cfg['compiler']['includes']['items'])
  85. includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
  86. { :command => command, :defines => defines, :options => options, :includes => includes }
  87. end
  88. def compile(file, defines = [])
  89. compiler = build_compiler_fields(defines)
  90. cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{compiler[:options]}#{compiler[:includes]} #{file} " \
  91. "#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}"
  92. obj_file = "#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}"
  93. execute(cmd_str + obj_file)
  94. obj_file
  95. end
  96. def build_linker_fields
  97. command = tackit($cfg['linker']['path'])
  98. options = if $cfg['linker']['options'].nil?
  99. ''
  100. else
  101. squash('', $cfg['linker']['options'])
  102. end
  103. includes = if $cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil?
  104. ''
  105. else
  106. squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items'])
  107. end.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
  108. { :command => command, :options => options, :includes => includes }
  109. end
  110. def link_it(exe_name, obj_list)
  111. linker = build_linker_fields
  112. cmd_str = "#{linker[:command]}#{linker[:options]}#{linker[:includes]} " +
  113. (obj_list.map { |obj| "#{$cfg['linker']['object_files']['path']}#{obj} " }).join +
  114. $cfg['linker']['bin_files']['prefix'] + ' ' +
  115. $cfg['linker']['bin_files']['destination'] +
  116. exe_name + $cfg['linker']['bin_files']['extension']
  117. execute(cmd_str)
  118. end
  119. def build_simulator_fields
  120. return nil if $cfg['simulator'].nil?
  121. command = if $cfg['simulator']['path'].nil?
  122. ''
  123. else
  124. (tackit($cfg['simulator']['path']) + ' ')
  125. end
  126. pre_support = if $cfg['simulator']['pre_support'].nil?
  127. ''
  128. else
  129. squash('', $cfg['simulator']['pre_support'])
  130. end
  131. post_support = if $cfg['simulator']['post_support'].nil?
  132. ''
  133. else
  134. squash('', $cfg['simulator']['post_support'])
  135. end
  136. { :command => command, :pre_support => pre_support, :post_support => post_support }
  137. end
  138. def run_astyle(style_what)
  139. report "Styling C Code..."
  140. command = "AStyle " \
  141. "--style=allman --indent=spaces=4 --indent-switches --indent-preproc-define --indent-preproc-block " \
  142. "--pad-oper --pad-comma --unpad-paren --pad-header " \
  143. "--align-pointer=type --align-reference=name " \
  144. "--add-brackets --mode=c --suffix=none " \
  145. "#{style_what}"
  146. execute(command, false)
  147. report "Styling C:PASS"
  148. end
  149. def execute(command_string, ok_to_fail = false)
  150. report command_string if $verbose
  151. output = `#{command_string}`.chomp
  152. report(output) if $verbose && !output.nil? && !output.empty?
  153. raise "Command failed. (Returned #{$?.exitstatus})" if !$?.exitstatus.zero? && !ok_to_fail
  154. output
  155. end
  156. def report_summary
  157. summary = UnityTestSummary.new
  158. summary.root = UNITY_ROOT
  159. results_glob = "#{$cfg['compiler']['build_path']}*.test*"
  160. results_glob.tr!('\\', '/')
  161. results = Dir[results_glob]
  162. summary.targets = results
  163. report summary.run
  164. end
  165. def run_tests(test_files)
  166. report 'Running Unity system tests...'
  167. # Tack on TEST define for compiling unit tests
  168. load_configuration($cfg_file)
  169. test_defines = ['TEST']
  170. $cfg['compiler']['defines']['items'] ||= []
  171. $cfg['compiler']['defines']['items'] << 'TEST'
  172. include_dirs = local_include_dirs
  173. # Build and execute each unit test
  174. test_files.each do |test|
  175. obj_list = []
  176. unless $cfg['compiler']['aux_sources'].nil?
  177. $cfg['compiler']['aux_sources'].each do |aux|
  178. obj_list << compile(aux, test_defines)
  179. end
  180. end
  181. # Detect dependencies and build required modules
  182. extract_headers(test).each do |header|
  183. # Compile corresponding source file if it exists
  184. src_file = find_source_file(header, include_dirs)
  185. obj_list << compile(src_file, test_defines) unless src_file.nil?
  186. end
  187. # Build the test runner (generate if configured to do so)
  188. test_base = File.basename(test, C_EXTENSION)
  189. runner_name = test_base + '_Runner.c'
  190. runner_path = if $cfg['compiler']['runner_path'].nil?
  191. $cfg['compiler']['build_path'] + runner_name
  192. else
  193. $cfg['compiler']['runner_path'] + runner_name
  194. end
  195. options = $cfg[:unity]
  196. options[:use_param_tests] = test =~ /parameterized/ ? true : false
  197. UnityTestRunnerGenerator.new(options).run(test, runner_path)
  198. obj_list << compile(runner_path, test_defines)
  199. # Build the test module
  200. obj_list << compile(test, test_defines)
  201. # Link the test executable
  202. link_it(test_base, obj_list)
  203. # Execute unit test and generate results file
  204. simulator = build_simulator_fields
  205. executable = $cfg['linker']['bin_files']['destination'] + test_base + $cfg['linker']['bin_files']['extension']
  206. cmd_str = if simulator.nil?
  207. executable
  208. else
  209. "#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}"
  210. end
  211. output = execute(cmd_str)
  212. test_results = $cfg['compiler']['build_path'] + test_base
  213. if output.match(/OK$/m).nil?
  214. test_results += '.testfail'
  215. else
  216. report output unless $verbose # Verbose already prints this line, as does a failure
  217. test_results += '.testpass'
  218. end
  219. File.open(test_results, 'w') { |f| f.print output }
  220. end
  221. end
  222. end