Debian Codegraph @

examples/temp_sensor/rakefile_helper.rb

raw symbols SCIP
1 require 'yaml'
2 require 'fileutils'
3 require '../../vendor/unity/auto/unity_test_summary'
4 require '../../vendor/unity/auto/generate_test_runner'
5 require '../../vendor/unity/auto/colour_reporter'
6
7 module RakefileHelpers
8 $return_error_on_failures = false
9
10 C_EXTENSION = '.c'.freeze
11
12 def load_configuration(config_file)
13 $cfg_file = config_file
14 $cfg = YAML.load(File.read($cfg_file))
15 $colour_output = false unless $cfg['colour']
16 end
17
18 def configure_clean
19 CLEAN.include($cfg['compiler']['build_path'] + '*.*') unless $cfg['compiler']['build_path'].nil?
20 end
21
22 def configure_toolchain(config_file = DEFAULT_CONFIG_FILE)
23 config_file += '.yml' unless config_file =~ /\.yml$/
24 load_configuration(config_file)
25 configure_clean
26 end
27
28 def unit_test_files
29 path = $cfg['compiler']['unit_tests_path'] + 'Test*' + C_EXTENSION
30 path.tr!('\\', '/')
31 FileList.new(path)
32 end
33
34 def local_include_dirs
35 include_dirs = $cfg['compiler']['includes']['items'].dup
36 include_dirs.delete_if { |dir| dir.is_a?(Array) }
37 include_dirs
38 end
39
40 def extract_headers(filename)
41 includes = []
42 lines = File.readlines(filename)
43 lines.each do |line|
44 m = line.match(/^\s*#include\s+\"\s*(.+\.[hH])\s*\"/)
45 unless m.nil?
46 includes << m[1]
47 end
48 end
49 includes
50 end
51
52 def find_source_file(header, paths)
53 paths.each do |dir|
54 src_file = dir + header.ext(C_EXTENSION)
55 if File.exist?(src_file)
56 return src_file
57 end
58 end
59 nil
60 end
61
62 def tackit(strings)
63 case strings
64 when Array
65 "\"#{strings.join}\""
66 when /^-/
67 strings
68 when /\s/
69 "\"#{strings}\""
70 else
71 strings
72 end
73 end
74
75 def squash(prefix, items)
76 result = ''
77 items.each { |item| result += " #{prefix}#{tackit(item)}" }
78 result
79 end
80
81 def build_compiler_fields
82 command = tackit($cfg['compiler']['path'])
83 defines = if $cfg['compiler']['defines']['items'].nil?
84 ''
85 else
86 squash($cfg['compiler']['defines']['prefix'], $cfg['compiler']['defines']['items'])
87 end
88 options = squash('', $cfg['compiler']['options'])
89 includes = squash($cfg['compiler']['includes']['prefix'], $cfg['compiler']['includes']['items'])
90 includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
91 { :command => command, :defines => defines, :options => options, :includes => includes }
92 end
93
94 def compile(file, _defines = [])
95 compiler = build_compiler_fields
96 cmd_str = "#{compiler[:command]}#{compiler[:defines]}#{compiler[:options]}#{compiler[:includes]} #{file} " \
97 "#{$cfg['compiler']['object_files']['prefix']}#{$cfg['compiler']['object_files']['destination']}"
98 obj_file = "#{File.basename(file, C_EXTENSION)}#{$cfg['compiler']['object_files']['extension']}"
99 execute(cmd_str + obj_file)
100 obj_file
101 end
102
103 def build_linker_fields
104 command = tackit($cfg['linker']['path'])
105 options = if $cfg['linker']['options'].nil?
106 ''
107 else
108 squash('', $cfg['linker']['options'])
109 end
110 includes = if $cfg['linker']['includes'].nil? || $cfg['linker']['includes']['items'].nil?
111 ''
112 else
113 squash($cfg['linker']['includes']['prefix'], $cfg['linker']['includes']['items'])
114 end
115 includes = includes.gsub(/\\ /, ' ').gsub(/\\\"/, '"').gsub(/\\$/, '') # Remove trailing slashes (for IAR)
116 { :command => command, :options => options, :includes => includes }
117 end
118
119 def link_it(exe_name, obj_list)
120 linker = build_linker_fields
121 cmd_str = "#{linker[:command]}#{linker[:includes]} " +
122 (obj_list.map { |obj| "#{$cfg['linker']['object_files']['path']}#{obj} " }).join +
123 $cfg['linker']['bin_files']['prefix'] + ' ' +
124 $cfg['linker']['bin_files']['destination'] +
125 exe_name + $cfg['linker']['bin_files']['extension'] + " #{linker[:options]}"
126 execute(cmd_str)
127 end
128
129 def build_simulator_fields
130 return nil if $cfg['simulator'].nil?
131
132 command = if $cfg['simulator']['path'].nil?
133 ''
134 else
135 (tackit($cfg['simulator']['path']) + ' ')
136 end
137 pre_support = if $cfg['simulator']['pre_support'].nil?
138 ''
139 else
140 squash('', $cfg['simulator']['pre_support'])
141 end
142 post_support = if $cfg['simulator']['post_support'].nil?
143 ''
144 else
145 squash('', $cfg['simulator']['post_support'])
146 end
147 { :command => command, :pre_support => pre_support, :post_support => post_support }
148 end
149
150 def execute(command_string, verbose = true, ok_to_fail = false)
151 report command_string
152 output = `#{command_string}`.chomp
153 report(output) if verbose && !output.nil? && !output.empty?
154 unless (!$?.nil? && $?.exitstatus.zero?) || ok_to_fail
155 raise "Command failed. (Returned #{$?.exitstatus})"
156 end
157
158 output
159 end
160
161 def report_summary
162 summary = UnityTestSummary.new
163 summary.root = HERE
164 results_glob = "#{$cfg['compiler']['build_path']}*.test*"
165 results_glob.tr!('\\', '/')
166 results = Dir[results_glob]
167 summary.targets = results
168 report summary.run
169 raise 'There were failures' if (summary.failures > 0) && $return_error_on_failures
170 end
171
172 def run_tests(test_files)
173 report 'Running system tests...'
174
175 # Tack on TEST define for compiling unit tests
176 load_configuration($cfg_file)
177 test_defines = ['TEST']
178 $cfg['compiler']['defines']['items'] = [] if $cfg['compiler']['defines']['items'].nil?
179 $cfg['compiler']['defines']['items'] << 'TEST'
180
181 include_dirs = local_include_dirs
182
183 # Build and execute each unit test
184 test_files.each do |test|
185 obj_list = []
186
187 # Detect dependencies and build required required modules
188 header_list = (extract_headers(test) + ['cmock.h'] + [$cfg[:cmock][:unity_helper_path]]).compact.uniq
189 header_list.each do |header|
190 # create mocks if needed
191 next unless header =~ /Mock/
192
193 require '../../lib/cmock.rb'
194 @cmock ||= CMock.new($cfg_file)
195 @cmock.setup_mocks([$cfg['compiler']['source_path'] + header.gsub('Mock', '')])
196 end
197
198 # compile all mocks
199 header_list.each do |header|
200 # compile source file header if it exists
201 src_file = find_source_file(header, include_dirs)
202 unless src_file.nil?
203 obj_list << compile(src_file, test_defines)
204 end
205 end
206
207 # Build the test runner (generate if configured to do so)
208 test_base = File.basename(test, C_EXTENSION)
209 runner_name = test_base + '_Runner.c'
210 if $cfg['compiler']['runner_path'].nil?
211 runner_path = $cfg['compiler']['build_path'] + runner_name
212 test_gen = UnityTestRunnerGenerator.new($cfg_file)
213 test_gen.run(test, runner_path)
214 else
215 runner_path = $cfg['compiler']['runner_path'] + runner_name
216 end
217
218 obj_list << compile(runner_path, test_defines)
219
220 # Build the test module
221 obj_list << compile(test, test_defines)
222
223 # Link the test executable
224 link_it(test_base, obj_list)
225
226 # Execute unit test and generate results file
227 simulator = build_simulator_fields
228 executable = $cfg['linker']['bin_files']['destination'] + test_base + $cfg['linker']['bin_files']['extension']
229 cmd_str = if simulator.nil?
230 executable
231 else
232 "#{simulator[:command]} #{simulator[:pre_support]} #{executable} #{simulator[:post_support]}"
233 end
234 output = execute(cmd_str, true, true)
235 test_results = $cfg['compiler']['build_path'] + test_base
236 test_results += if output.match(/OK$/m).nil?
237 '.testfail'
238 else
239 '.testpass'
240 end
241 File.open(test_results, 'w') { |f| f.print output }
242 end
243 end
244
245 def build_application(main)
246 report 'Building application...'
247
248 obj_list = []
249 load_configuration($cfg_file)
250 main_path = $cfg['compiler']['source_path'] + main + C_EXTENSION
251
252 # Detect dependencies and build required required modules
253 include_dirs = local_include_dirs
254 extract_headers(main_path).each do |header|
255 src_file = find_source_file(header, include_dirs)
256 unless src_file.nil?
257 obj_list << compile(src_file)
258 end
259 end
260
261 # Build the main source file
262 main_base = File.basename(main_path, C_EXTENSION)
263 obj_list << compile(main_path)
264
265 # Create the executable
266 link_it(main_base, obj_list)
267 end
268 end