rakefile_helper.rb 6.5 KB

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