rakefile_helper.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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'
  13. def load_configuration(config_file)
  14. unless ($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. end
  21. def configure_clean
  22. CLEAN.include($cfg['compiler']['build_path'] + '*.*') unless $cfg['compiler']['build_path'].nil?
  23. end
  24. def configure_toolchain(config_file=DEFAULT_CONFIG_FILE)
  25. config_file += '.yml' unless config_file =~ /\.yml$/
  26. config_file = config_file unless config_file =~ /[\\|\/]/
  27. load_configuration(config_file)
  28. configure_clean
  29. end
  30. def get_unit_test_files
  31. path = $cfg['compiler']['unit_tests_path'] + 'test*' + C_EXTENSION
  32. path.gsub!(/\\/, '/')
  33. FileList.new(path)
  34. end
  35. def get_local_include_dirs
  36. include_dirs = $cfg['compiler']['includes']['items'].dup
  37. include_dirs.delete_if {|dir| dir.is_a?(Array)}
  38. return include_dirs
  39. end
  40. def extract_headers(filename)
  41. includes = []
  42. lines = File.readlines(filename)
  43. lines.each do |line|
  44. m = line.match(/^\s*#include\s+\"\s*(.+\.[hH])\s*\"/)
  45. if not m.nil?
  46. includes << m[1]
  47. end
  48. end
  49. return includes
  50. end
  51. def find_source_file(header, paths)
  52. paths.each do |dir|
  53. src_file = dir + header.ext(C_EXTENSION)
  54. if (File.exists?(src_file))
  55. return src_file
  56. end
  57. end
  58. return nil
  59. end
  60. def tackit(strings)
  61. if strings.is_a?(Array)
  62. result = "\"#{strings.join}\""
  63. else
  64. result = strings
  65. end
  66. return result
  67. end
  68. def squash(prefix, items)
  69. result = ''
  70. items.each { |item| result += " #{prefix}#{tackit(item)}" }
  71. return result
  72. end
  73. def should(behave, &block)
  74. if block
  75. puts "Should " + behave
  76. yield block
  77. else
  78. puts "UNIMPLEMENTED CASE: Should #{behave}"
  79. end
  80. end
  81. def build_compiler_fields(inject_defines)
  82. command = tackit($cfg['compiler']['path'])
  83. if $cfg['compiler']['defines']['items'].nil?
  84. defines = ''
  85. else
  86. defines = squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items'] + ['UNITY_OUTPUT_CHAR=putcharSpy'] + inject_defines)
  87. end
  88. options = squash('', $cfg['compiler']['options'])
  89. includes = squash($cfg['compiler']['includes']['prefix'], $cfg['compiler']['includes']['items'])
  90. includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
  91. return {:command => command, :defines => defines, :options => options, :includes => includes}
  92. end
  93. def compile(file, defines=[])
  94. compiler = build_compiler_fields(defines)
  95. defines =
  96. cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{compiler[:options]}#{compiler[:includes]} #{file} " +
  97. "#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}"
  98. obj_file = "#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}"
  99. execute(cmd_str + obj_file)
  100. return obj_file
  101. end
  102. def build_linker_fields
  103. command = tackit($cfg['linker']['path'])
  104. if $cfg['linker']['options'].nil?
  105. options = ''
  106. else
  107. options = squash('', $cfg['linker']['options'])
  108. end
  109. if ($cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil?)
  110. includes = ''
  111. else
  112. includes = squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items'])
  113. end
  114. includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
  115. return {:command => command, :options => options, :includes => includes}
  116. end
  117. def link_it(exe_name, obj_list)
  118. linker = build_linker_fields
  119. cmd_str = "#{linker[:command]}#{linker[:options]}#{linker[:includes]} " +
  120. (obj_list.map{|obj|"#{$cfg['linker']['object_files']['path']}#{obj} "}).join +
  121. $cfg['linker']['bin_files']['prefix'] + ' ' +
  122. $cfg['linker']['bin_files']['destination'] +
  123. exe_name + $cfg['linker']['bin_files']['extension']
  124. execute(cmd_str)
  125. end
  126. def build_simulator_fields
  127. return nil if $cfg['simulator'].nil?
  128. if $cfg['simulator']['path'].nil?
  129. command = ''
  130. else
  131. command = (tackit($cfg['simulator']['path']) + ' ')
  132. end
  133. if $cfg['simulator']['pre_support'].nil?
  134. pre_support = ''
  135. else
  136. pre_support = squash('', $cfg['simulator']['pre_support'])
  137. end
  138. if $cfg['simulator']['post_support'].nil?
  139. post_support = ''
  140. else
  141. post_support = squash('', $cfg['simulator']['post_support'])
  142. end
  143. return {:command => command, :pre_support => pre_support, :post_support => post_support}
  144. end
  145. def execute(command_string, ok_to_fail=false)
  146. report command_string if $verbose
  147. output = `#{command_string}`.chomp
  148. report(output) if ($verbose && !output.nil? && (output.length > 0))
  149. if (($?.exitstatus != 0) && !ok_to_fail)
  150. raise "Command failed. (Returned #{$?.exitstatus})"
  151. end
  152. return output
  153. end
  154. def report_summary
  155. summary = UnityTestSummary.new
  156. summary.set_root_path(UNITY_ROOT)
  157. results_glob = "#{$cfg['compiler']['build_path']}*.test*"
  158. results_glob.gsub!(/\\/, '/')
  159. results = Dir[results_glob]
  160. summary.set_targets(results)
  161. report summary.run
  162. end
  163. def run_tests(test_files)
  164. report 'Running Unity system tests...'
  165. # Tack on TEST define for compiling unit tests
  166. load_configuration($cfg_file)
  167. test_defines = ['TEST']
  168. $cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil?
  169. $cfg['compiler']['defines']['items'] << 'TEST'
  170. include_dirs = get_local_include_dirs
  171. # Build and execute each unit test
  172. test_files.each do |test|
  173. obj_list = []
  174. if !$cfg['compiler']['aux_sources'].nil?
  175. $cfg['compiler']['aux_sources'].each do |aux|
  176. obj_list << compile(aux, test_defines)
  177. end
  178. end
  179. # Detect dependencies and build required modules
  180. extract_headers(test).each do |header|
  181. # Compile corresponding source file if it exists
  182. src_file = find_source_file(header, include_dirs)
  183. if !src_file.nil?
  184. obj_list << compile(src_file, test_defines)
  185. end
  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 = ''
  191. if $cfg['compiler']['runner_path'].nil?
  192. runner_path = $cfg['compiler']['build_path'] + runner_name
  193. else
  194. runner_path = $cfg['compiler']['runner_path'] + runner_name
  195. end
  196. options = $cfg[:unity]
  197. options[:use_param_tests] = (test =~ /parameterized/) ? true : false
  198. UnityTestRunnerGenerator.new(options).run(test, runner_path)
  199. obj_list << compile(runner_path, test_defines)
  200. # Build the test module
  201. obj_list << compile(test, test_defines)
  202. # Link the test executable
  203. link_it(test_base, obj_list)
  204. # Execute unit test and generate results file
  205. simulator = build_simulator_fields
  206. executable = $cfg['linker']['bin_files']['destination'] + test_base + $cfg['linker']['bin_files']['extension']
  207. if simulator.nil?
  208. cmd_str = executable
  209. else
  210. cmd_str = "#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}"
  211. end
  212. output = execute(cmd_str)
  213. test_results = $cfg['compiler']['build_path'] + test_base
  214. if output.match(/OK$/m).nil?
  215. test_results += '.testfail'
  216. else
  217. report output if (!$verbose) #verbose already prints this line, as does a failure
  218. test_results += '.testpass'
  219. end
  220. File.open(test_results, 'w') { |f| f.print output }
  221. end
  222. end
  223. end