rakefile_helper.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 HERE+'../../auto/unity_test_summary'
  9. require HERE+'../../auto/generate_test_runner'
  10. require HERE+'../../auto/colour_reporter'
  11. module RakefileHelpers
  12. C_EXTENSION = '.c'
  13. def load_configuration(config_file)
  14. unless ($configured)
  15. $cfg_file = HERE+"../../test/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 tackit(strings)
  31. if strings.is_a?(Array)
  32. result = "\"#{strings.join}\""
  33. else
  34. result = strings
  35. end
  36. return result
  37. end
  38. def squash(prefix, items)
  39. result = ''
  40. items.each { |item| result += " #{prefix}#{tackit(item)}" }
  41. return result
  42. end
  43. def build_compiler_fields
  44. command = tackit($cfg['compiler']['path'])
  45. if $cfg['compiler']['defines']['items'].nil?
  46. defines = ''
  47. else
  48. defines = squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items'] + ['UNITY_OUTPUT_CHAR=UnityOutputCharSpy_OutputChar'])
  49. end
  50. options = squash('', $cfg['compiler']['options'])
  51. includes = squash($cfg['compiler']['includes']['prefix'], $cfg['compiler']['includes']['items'])
  52. includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
  53. return {:command => command, :defines => defines, :options => options, :includes => includes}
  54. end
  55. def compile(file, defines=[])
  56. compiler = build_compiler_fields
  57. unity_include = $cfg['compiler']['includes']['prefix']+'../../src'
  58. cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{compiler[:options]}#{compiler[:includes]} #{unity_include} #{file} " +
  59. "#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}" +
  60. "#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}"
  61. execute(cmd_str)
  62. end
  63. def build_linker_fields
  64. command = tackit($cfg['linker']['path'])
  65. if $cfg['linker']['options'].nil?
  66. options = ''
  67. else
  68. options = squash('', $cfg['linker']['options'])
  69. end
  70. if ($cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil?)
  71. includes = ''
  72. else
  73. includes = squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items'])
  74. end
  75. includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
  76. return {:command => command, :options => options, :includes => includes}
  77. end
  78. def link_it(exe_name, obj_list)
  79. linker = build_linker_fields
  80. cmd_str = "#{linker[:command]}#{linker[:options]}#{linker[:includes]} " +
  81. (obj_list.map{|obj|"#{$cfg['linker']['object_files']['path']}#{obj} "}).join +
  82. $cfg['linker']['bin_files']['prefix'] + ' ' +
  83. $cfg['linker']['bin_files']['destination'] +
  84. exe_name + $cfg['linker']['bin_files']['extension']
  85. execute(cmd_str)
  86. end
  87. def build_simulator_fields
  88. return nil if $cfg['simulator'].nil?
  89. if $cfg['simulator']['path'].nil?
  90. command = ''
  91. else
  92. command = (tackit($cfg['simulator']['path']) + ' ')
  93. end
  94. if $cfg['simulator']['pre_support'].nil?
  95. pre_support = ''
  96. else
  97. pre_support = squash('', $cfg['simulator']['pre_support'])
  98. end
  99. if $cfg['simulator']['post_support'].nil?
  100. post_support = ''
  101. else
  102. post_support = squash('', $cfg['simulator']['post_support'])
  103. end
  104. return {:command => command, :pre_support => pre_support, :post_support => post_support}
  105. end
  106. def execute(command_string, verbose=true)
  107. report command_string
  108. output = `#{command_string}`.chomp
  109. report(output) if (verbose && !output.nil? && (output.length > 0))
  110. if ($?.exitstatus != 0)
  111. raise "Command failed. (Returned #{$?.exitstatus})"
  112. end
  113. return output
  114. end
  115. def report_summary
  116. summary = UnityTestSummary.new
  117. summary.set_root_path(HERE)
  118. results_glob = "#{$cfg['compiler']['build_path']}*.test*"
  119. results_glob.gsub!(/\\/, '/')
  120. results = Dir[results_glob]
  121. summary.set_targets(results)
  122. summary.run
  123. end
  124. def run_tests
  125. report 'Running Unity system tests...'
  126. # Tack on TEST define for compiling unit tests
  127. load_configuration($cfg_file)
  128. test_defines = ['TEST']
  129. $cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil?
  130. # Get a list of all source files needed
  131. src_files = Dir[HERE+'src/*.c']
  132. src_files += Dir[HERE+'test/*.c']
  133. src_files += Dir[HERE+'test/main/*.c']
  134. src_files << '../../src/unity.c'
  135. # Build object files
  136. src_files.each { |f| compile(f, test_defines) }
  137. obj_list = src_files.map {|f| File.basename(f.ext($cfg['compiler']['object_files']['extension'])) }
  138. # Link the test executable
  139. test_base = "framework_test"
  140. link_it(test_base, obj_list)
  141. # Execute unit test and generate results file
  142. simulator = build_simulator_fields
  143. executable = $cfg['linker']['bin_files']['destination'] + test_base + $cfg['linker']['bin_files']['extension']
  144. if simulator.nil?
  145. cmd_str = executable + " -v -r"
  146. else
  147. cmd_str = "#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}"
  148. end
  149. output = execute(cmd_str)
  150. test_results = $cfg['compiler']['build_path'] + test_base
  151. if output.match(/OK$/m).nil?
  152. test_results += '.testfail'
  153. else
  154. test_results += '.testpass'
  155. end
  156. File.open(test_results, 'w') { |f| f.print output }
  157. end
  158. end