rakefile_helper.rb 6.3 KB
Newer Older
1 2 3 4 5 6 7 8
# ==========================================
#   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]
# ==========================================

require 'yaml'
require 'fileutils'
9 10 11
require HERE + '../../auto/unity_test_summary'
require HERE + '../../auto/generate_test_runner'
require HERE + '../../auto/colour_reporter'
12 13

module RakefileHelpers
14
  C_EXTENSION = '.c'.freeze
15 16

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

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

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

  def tackit(strings)
37 38 39 40 41 42
    result = if strings.is_a?(Array)
               "\"#{strings.join}\""
             else
               strings
             end
    result
43 44 45 46 47
  end

  def squash(prefix, items)
    result = ''
    items.each { |item| result += " #{prefix}#{tackit(item)}" }
48
    result
49 50 51
  end

  def build_compiler_fields
52
    command = tackit($cfg['compiler']['path'])
53 54 55 56 57 58 59 60
    if $cfg['compiler']['defines']['items'].nil?
      defines  = ''
    else
      defines  = squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items'] + ['UNITY_OUTPUT_CHAR=UnityOutputCharSpy_OutputChar'])
    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)
61
    { command: command, defines: defines, options: options, includes: includes }
62 63
  end

64
  def compile(file, _defines = [])
65
    compiler = build_compiler_fields
66 67 68 69
    unity_include = $cfg['compiler']['includes']['prefix'] + '../../src'
    cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{compiler[:options]}#{compiler[:includes]} #{unity_include} #{file} " \
              "#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}" \
              "#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}"
70 71 72 73
    execute(cmd_str)
  end

  def build_linker_fields
74 75 76 77 78 79 80
    command = tackit($cfg['linker']['path'])
    options = if $cfg['linker']['options'].nil?
                ''
              else
                squash('', $cfg['linker']['options'])
              end
    if $cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil?
81 82 83 84 85
      includes = ''
    else
      includes = squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items'])
    end
    includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
86
    { command: command, options: options, includes: includes }
87 88 89 90 91
  end

  def link_it(exe_name, obj_list)
    linker = build_linker_fields
    cmd_str = "#{linker[:command]}#{linker[:options]}#{linker[:includes]} " +
92 93 94 95
              (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']
96 97 98 99 100
    execute(cmd_str)
  end

  def build_simulator_fields
    return nil if $cfg['simulator'].nil?
101 102 103 104 105 106 107 108 109 110
    command = if $cfg['simulator']['path'].nil?
                ''
              else
                (tackit($cfg['simulator']['path']) + ' ')
              end
    pre_support = if $cfg['simulator']['pre_support'].nil?
                    ''
                  else
                    squash('', $cfg['simulator']['pre_support'])
                  end
111 112 113 114 115
    if $cfg['simulator']['post_support'].nil?
      post_support = ''
    else
      post_support = squash('', $cfg['simulator']['post_support'])
    end
116
    { command: command, pre_support: pre_support, post_support: post_support }
117 118
  end

119
  def execute(command_string, verbose = true)
120 121
    report command_string
    output = `#{command_string}`.chomp
122 123 124
    report(output) if verbose && !output.nil? && !output.empty?
    raise "Command failed. (Returned #{$CHILD_STATUS.exitstatus})" if $CHILD_STATUS.exitstatus != 0
    output
125 126 127 128 129 130
  end

  def report_summary
    summary = UnityTestSummary.new
    summary.set_root_path(HERE)
    results_glob = "#{$cfg['compiler']['build_path']}*.test*"
131
    results_glob.tr!('\\', '/')
132 133 134 135 136 137 138 139 140 141 142 143 144 145
    results = Dir[results_glob]
    summary.set_targets(results)
    summary.run
  end

  def run_tests
    report 'Running Unity system tests...'

    # 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?

    # Get a list of all source files needed
146 147 148
    src_files  = Dir[HERE + 'src/*.c']
    src_files += Dir[HERE + 'test/*.c']
    src_files += Dir[HERE + 'test/main/*.c']
149 150 151 152
    src_files << '../../src/unity.c'

    # Build object files
    src_files.each { |f| compile(f, test_defines) }
153
    obj_list = src_files.map { |f| File.basename(f.ext($cfg['compiler']['object_files']['extension'])) }
154 155

    # Link the test executable
156
    test_base = 'framework_test'
157 158 159 160 161 162
    link_it(test_base, obj_list)

    # 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?
163
      cmd_str = executable + ' -v -r'
164 165 166 167 168
    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
169 170 171 172 173
    test_results += if output.match(/OK$/m).nil?
                      '.testfail'
                    else
                      '.testpass'
                    end
174 175 176
    File.open(test_results, 'w') { |f| f.print output }
  end
end