rakefile_helper.rb 8.2 KB
Newer Older
1 2 3 4
# ==========================================
#   Unity Project - A Test Framework for C
#   Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams
#   [Released under MIT License. Please refer to license.txt for details]
5
# ==========================================
6

7 8
require 'yaml'
require 'fileutils'
9 10 11
require UNITY_ROOT + '../auto/unity_test_summary'
require UNITY_ROOT + '../auto/generate_test_runner'
require UNITY_ROOT + '../auto/colour_reporter'
12 13 14 15

module RakefileHelpers

  C_EXTENSION = '.c'
16

17
  def load_configuration(config_file)
18 19 20 21 22 23
    unless ($configured)
      $cfg_file = "targets/#{config_file}" unless (config_file =~ /[\\|\/]/)
      $cfg = YAML.load(File.read($cfg_file))
      $colour_output = false unless $cfg['colour']
      $configured = true if (config_file != DEFAULT_CONFIG_FILE)
    end
24
  end
25

26 27 28
  def configure_clean
    CLEAN.include($cfg['compiler']['build_path'] + '*.*') unless $cfg['compiler']['build_path'].nil?
  end
29

30 31
  def configure_toolchain(config_file=DEFAULT_CONFIG_FILE)
    config_file += '.yml' unless config_file =~ /\.yml$/
32
    config_file = config_file unless config_file =~ /[\\|\/]/
33 34 35
    load_configuration(config_file)
    configure_clean
  end
36

37
  def get_unit_test_files
38
    path = $cfg['compiler']['unit_tests_path'] + 'test*' + C_EXTENSION
39 40 41
    path.gsub!(/\\/, '/')
    FileList.new(path)
  end
42

43 44 45 46 47 48 49 50 51 52
  def get_local_include_dirs
    include_dirs = $cfg['compiler']['includes']['items'].dup
    include_dirs.delete_if {|dir| dir.is_a?(Array)}
    return include_dirs
  end

  def extract_headers(filename)
    includes = []
    lines = File.readlines(filename)
    lines.each do |line|
53
      m = line.match(/^\s*#include\s+\"\s*(.+\.[hH])\s*\"/)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
      if not m.nil?
        includes << m[1]
      end
    end
    return includes
  end

  def find_source_file(header, paths)
    paths.each do |dir|
      src_file = dir + header.ext(C_EXTENSION)
      if (File.exists?(src_file))
        return src_file
      end
    end
    return nil
  end
70

71 72 73 74 75 76 77 78
  def tackit(strings)
    if strings.is_a?(Array)
      result = "\"#{strings.join}\""
    else
      result = strings
    end
    return result
  end
79

80 81 82 83 84 85
  def squash(prefix, items)
    result = ''
    items.each { |item| result += " #{prefix}#{tackit(item)}" }
    return result
  end

86 87 88 89 90 91 92 93 94
  def should(behave, &block)
    if block
      puts "Should " + behave
      yield block
    else
      puts "UNIMPLEMENTED CASE: Should #{behave}"
    end
  end

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  def build_compiler_fields
    command  = tackit($cfg['compiler']['path'])
    if $cfg['compiler']['defines']['items'].nil?
      defines  = ''
    else
      defines  = squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items'])
    end
    options  = squash('', $cfg['compiler']['options'])
    includes = squash($cfg['compiler']['includes']['prefix'], $cfg['compiler']['includes']['items'])
    includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
    return {:command => command, :defines => defines, :options => options, :includes => includes}
  end

  def compile(file, defines=[])
    compiler = build_compiler_fields
110 111 112 113 114
    cmd_str  = "#{compiler[:command]}#{compiler[:defines]}#{compiler[:options]}#{compiler[:includes]} #{file} " +
               "#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}"
    obj_file = "#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}"
    execute(cmd_str + obj_file)
    return obj_file
115
  end
116

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  def build_linker_fields
    command  = tackit($cfg['linker']['path'])
    if $cfg['linker']['options'].nil?
      options  = ''
    else
      options  = squash('', $cfg['linker']['options'])
    end
    if ($cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil?)
      includes = ''
    else
      includes = squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items'])
    end
    includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
    return {:command => command, :options => options, :includes => includes}
  end
132

133
  def link_it(exe_name, obj_list)
134 135 136 137 138 139 140 141
    linker = build_linker_fields
    cmd_str = "#{linker[:command]}#{linker[:options]}#{linker[:includes]} " +
      (obj_list.map{|obj|"#{$cfg['linker']['object_files']['path']}#{obj} "}).join +
      $cfg['linker']['bin_files']['prefix'] + ' ' +
      $cfg['linker']['bin_files']['destination'] +
      exe_name + $cfg['linker']['bin_files']['extension']
    execute(cmd_str)
  end
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
  def build_simulator_fields
    return nil if $cfg['simulator'].nil?
    if $cfg['simulator']['path'].nil?
      command = ''
    else
      command = (tackit($cfg['simulator']['path']) + ' ')
    end
    if $cfg['simulator']['pre_support'].nil?
      pre_support = ''
    else
      pre_support = squash('', $cfg['simulator']['pre_support'])
    end
    if $cfg['simulator']['post_support'].nil?
      post_support = ''
    else
      post_support = squash('', $cfg['simulator']['post_support'])
    end
    return {:command => command, :pre_support => pre_support, :post_support => post_support}
  end
162

163 164 165 166 167 168 169 170 171
  def execute(command_string, verbose=true)
    report command_string
    output = `#{command_string}`.chomp
    report(output) if (verbose && !output.nil? && (output.length > 0))
    if $?.exitstatus != 0
      raise "Command failed. (Returned #{$?.exitstatus})"
    end
    return output
  end
172

173 174
  def report_summary
    summary = UnityTestSummary.new
M
Mark VanderVoord 已提交
175
    summary.set_root_path(UNITY_ROOT)
176 177 178 179
    results_glob = "#{$cfg['compiler']['build_path']}*.test*"
    results_glob.gsub!(/\\/, '/')
    results = Dir[results_glob]
    summary.set_targets(results)
M
Mark VanderVoord 已提交
180
    report summary.run
181
  end
182

183
  def run_tests(test_files)
184
    report 'Running Unity system tests...'
185

186 187 188 189 190
    # Tack on TEST define for compiling unit tests
    load_configuration($cfg_file)
    test_defines = ['TEST']
    $cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil?
    $cfg['compiler']['defines']['items'] << 'TEST'
191

192
    include_dirs = get_local_include_dirs
193

194 195 196
    # Build and execute each unit test
    test_files.each do |test|
      obj_list = []
197

198 199 200 201 202 203
      if !$cfg['compiler']['aux_sources'].nil?
        $cfg['compiler']['aux_sources'].each do |aux|
          obj_list << compile(aux, test_defines)
        end
      end

204
      # Detect dependencies and build required modules
205 206 207 208
      extract_headers(test).each do |header|
        # Compile corresponding source file if it exists
        src_file = find_source_file(header, include_dirs)
        if !src_file.nil?
209
          obj_list << compile(src_file, test_defines)
210 211
        end
      end
212

213 214
      # Build the test runner (generate if configured to do so)
      test_base = File.basename(test, C_EXTENSION)
215

216
      runner_name = test_base + '_Runner.c'
217
      runner_path = ''
218

219 220 221 222 223
      if $cfg['compiler']['runner_path'].nil?
        runner_path = $cfg['compiler']['build_path'] + runner_name
      else
        runner_path = $cfg['compiler']['runner_path'] + runner_name
      end
224

225 226 227
      options = $cfg[:unity]
      options[:use_param_tests] = (test =~ /parameterized/) ? true : false
      UnityTestRunnerGenerator.new(options).run(test, runner_path)
228
      obj_list << compile(runner_path, test_defines)
229

230
      # Build the test module
231
      obj_list << compile(test, test_defines)
232

233
      # Link the test executable
234
      link_it(test_base, obj_list)
235

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
      # Execute unit test and generate results file
      simulator = build_simulator_fields
      executable = $cfg['linker']['bin_files']['destination'] + test_base + $cfg['linker']['bin_files']['extension']
      if simulator.nil?
        cmd_str = executable
      else
        cmd_str = "#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}"
      end
      output = execute(cmd_str)
      test_results = $cfg['compiler']['build_path'] + test_base
      if output.match(/OK$/m).nil?
        test_results += '.testfail'
      else
        test_results += '.testpass'
      end
      File.open(test_results, 'w') { |f| f.print output }
252

253 254 255
    end
  end
end