interplane.Get Omega
Read in your language

cJSON

[Ported from github.com/DaveGamble/cJSON] Fix: heap-use-after-free in merge_patch when patch is subtree of target (#1065)

Brought by drakestapleton

branch master Branches Tags History

cJSON / tests / unity / examples / example_3 / rakefile_helper.rb

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