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 7require 'yaml' 8require 'fileutils' 9require_relative '../auto/unity_test_summary' 10require_relative '../auto/generate_test_runner' 11require_relative '../auto/colour_reporter' 12 13module RakefileHelpers 14 C_EXTENSION = '.c'.freeze 15 def load_configuration(config_file) 16 return if $configured 17 18 $cfg_file = "targets/#{config_file}" unless config_file =~ /[\\|\/]/ 19 $cfg = YAML.load(File.read($cfg_file)) 20 $colour_output = false unless $cfg['colour'] 21 $configured = true if config_file != DEFAULT_CONFIG_FILE 22 end 23 24 def configure_clean 25 CLEAN.include($cfg['compiler']['build_path'] + '*.*') unless $cfg['compiler']['build_path'].nil? 26 end 27 28 def configure_toolchain(config_file = DEFAULT_CONFIG_FILE) 29 config_file += '.yml' unless config_file =~ /\.yml$/ 30 config_file = config_file unless config_file =~ /[\\|\/]/ 31 load_configuration(config_file) 32 configure_clean 33 end 34 35 def unit_test_files 36 path = $cfg['compiler']['unit_tests_path'] + 'test*' + C_EXTENSION 37 path.tr!('\\', '/') 38 FileList.new(path) 39 end 40 41 def local_include_dirs 42 include_dirs = $cfg['compiler']['includes']['items'].dup 43 include_dirs.delete_if { |dir| dir.is_a?(Array) } 44 include_dirs 45 end 46 47 def extract_headers(filename) 48 includes = [] 49 lines = File.readlines(filename) 50 lines.each do |line| 51 m = line.match(/^\s*#include\s+\"\s*(.+\.[hH])\s*\"/) 52 includes << m[1] unless m.nil? 53 end 54 includes 55 end 56 57 def find_source_file(header, paths) 58 paths.each do |dir| 59 src_file = dir + header.ext(C_EXTENSION) 60 return src_file if File.exist?(src_file) 61 end 62 nil 63 end 64 65 def tackit(strings) 66 result = if strings.is_a?(Array) 67 "\"#{strings.join}\"" 68 else 69 strings 70 end 71 result 72 end 73 74 def squash(prefix, items) 75 result = '' 76 items.each { |item| result += " #{prefix}#{tackit(item)}" } 77 result 78 end 79 80 def should(behave, &block) 81 if block 82 puts 'Should ' + behave 83 yield block 84 else 85 puts "UNIMPLEMENTED CASE: Should #{behave}" 86 end 87 end 88 89 def build_compiler_fields(inject_defines) 90 command = tackit($cfg['compiler']['path']) 91 defines = if $cfg['compiler']['defines']['items'].nil? 92 '' 93 else 94 squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items'] + ['UNITY_OUTPUT_CHAR=putcharSpy'] + ['UNITY_OUTPUT_CHAR_HEADER_DECLARATION="putcharSpy(int)"'] + inject_defines) 95 end 96 options = squash('', $cfg['compiler']['options']) 97 includes = squash($cfg['compiler']['includes']['prefix'], $cfg['compiler']['includes']['items']) 98 includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR) 99 100 { :command => command, :defines => defines, :options => options, :includes => includes } 101 end 102 103 def compile(file, defines = []) 104 compiler = build_compiler_fields(defines) 105 cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{compiler[:options]}#{compiler[:includes]} #{file} " \ 106 "#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}" 107 obj_file = "#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}" 108 execute(cmd_str + obj_file) 109 110 obj_file 111 end 112 113 def build_linker_fields 114 command = tackit($cfg['linker']['path']) 115 options = if $cfg['linker']['options'].nil? 116 '' 117 else 118 squash('', $cfg['linker']['options']) 119 end 120 includes = if $cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil? 121 '' 122 else 123 squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items']) 124 end.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR) 125 126 { :command => command, :options => options, :includes => includes } 127 end 128 129 def link_it(exe_name, obj_list) 130 linker = build_linker_fields 131 cmd_str = "#{linker[:command]}#{linker[:options]}#{linker[:includes]} " + 132 (obj_list.map { |obj| "#{$cfg['linker']['object_files']['path']}#{obj} " }).join + 133 $cfg['linker']['bin_files']['prefix'] + ' ' + 134 $cfg['linker']['bin_files']['destination'] + 135 exe_name + $cfg['linker']['bin_files']['extension'] 136 execute(cmd_str) 137 end 138 139 def build_simulator_fields 140 return nil if $cfg['simulator'].nil? 141 command = if $cfg['simulator']['path'].nil? 142 '' 143 else 144 (tackit($cfg['simulator']['path']) + ' ') 145 end 146 pre_support = if $cfg['simulator']['pre_support'].nil? 147 '' 148 else 149 squash('', $cfg['simulator']['pre_support']) 150 end 151 post_support = if $cfg['simulator']['post_support'].nil? 152 '' 153 else 154 squash('', $cfg['simulator']['post_support']) 155 end 156 157 { :command => command, :pre_support => pre_support, :post_support => post_support } 158 end 159 160 def run_astyle(style_what) 161 report "Styling C Code..." 162 command = "AStyle " \ 163 "--style=allman --indent=spaces=4 --indent-switches --indent-preproc-define --indent-preproc-block " \ 164 "--pad-oper --pad-comma --unpad-paren --pad-header " \ 165 "--align-pointer=type --align-reference=name " \ 166 "--add-brackets --mode=c --suffix=none " \ 167 "#{style_what}" 168 execute(command, false) 169 report "Styling C:PASS" 170 end 171 172 def execute(command_string, ok_to_fail = false) 173 report command_string if $verbose 174 output = `#{command_string}`.chomp 175 report(output) if $verbose && !output.nil? && !output.empty? 176 raise "Command failed. (Returned #{$?.exitstatus})" if !$?.nil? && !$?.exitstatus.zero? && !ok_to_fail 177 output 178 end 179 180 def report_summary 181 summary = UnityTestSummary.new 182 summary.root = __dir__ 183 results_glob = "#{$cfg['compiler']['build_path']}*.test*" 184 results_glob.tr!('\\', '/') 185 results = Dir[results_glob] 186 summary.targets = results 187 report summary.run 188 end 189 190 def run_tests(test_files) 191 report 'Running Unity system tests...' 192 193 # Tack on TEST define for compiling unit tests 194 load_configuration($cfg_file) 195 test_defines = ['TEST'] 196 $cfg['compiler']['defines']['items'] ||= [] 197 $cfg['compiler']['defines']['items'] << 'TEST' 198 199 include_dirs = local_include_dirs 200 201 # Build and execute each unit test 202 test_files.each do |test| 203 204 # Drop Out if we're skipping this type of test 205 if $cfg[:skip_tests] 206 if $cfg[:skip_tests].include?(:parameterized) && test.match(/parameterized/) 207 report("Skipping Parameterized Tests for this Target:IGNORE") 208 next 209 end 210 end 211 212 obj_list = [] 213 214 unless $cfg['compiler']['aux_sources'].nil? 215 $cfg['compiler']['aux_sources'].each do |aux| 216 obj_list << compile(aux, test_defines) 217 end 218 end 219 220 # Detect dependencies and build required modules 221 extract_headers(test).each do |header| 222 # Compile corresponding source file if it exists 223 src_file = find_source_file(header, include_dirs) 224 225 obj_list << compile(src_file, test_defines) unless src_file.nil? 226 end 227 228 # Build the test runner (generate if configured to do so) 229 test_base = File.basename(test, C_EXTENSION) 230 231 runner_name = test_base + '_Runner.c' 232 233 runner_path = if $cfg['compiler']['runner_path'].nil? 234 $cfg['compiler']['build_path'] + runner_name 235 else 236 $cfg['compiler']['runner_path'] + runner_name 237 end 238 239 options = $cfg[:unity] 240 options[:use_param_tests] = test =~ /parameterized/ ? true : false 241 UnityTestRunnerGenerator.new(options).run(test, runner_path) 242 obj_list << compile(runner_path, test_defines) 243 244 # Build the test module 245 obj_list << compile(test, test_defines) 246 247 # Link the test executable 248 link_it(test_base, obj_list) 249 250 # Execute unit test and generate results file 251 simulator = build_simulator_fields 252 executable = $cfg['linker']['bin_files']['destination'] + test_base + $cfg['linker']['bin_files']['extension'] 253 cmd_str = if simulator.nil? 254 executable 255 else 256 "#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}" 257 end 258 output = execute(cmd_str) 259 test_results = $cfg['compiler']['build_path'] + test_base 260 if output.match(/OK$/m).nil? 261 test_results += '.testfail' 262 else 263 report output unless $verbose # Verbose already prints this line, as does a failure 264 test_results += '.testpass' 265 end 266 File.open(test_results, 'w') { |f| f.print output } 267 end 268 end 269end 270