|
1 #!/usr/bin/perl |
|
2 |
|
3 # Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 # All rights reserved. |
|
5 # This component and the accompanying materials are made available |
|
6 # under the terms of the License "Symbian Foundation License v1.0" |
|
7 # which accompanies this distribution, and is available |
|
8 # at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html". |
|
9 # |
|
10 # Initial Contributors: |
|
11 # Nokia Corporation - initial contribution. |
|
12 # |
|
13 # Contributors: |
|
14 # |
|
15 # Description: |
|
16 # |
|
17 |
|
18 use strict; |
|
19 use Test::Harness; |
|
20 use Getopt::Long qw(:config auto_version auto_help); |
|
21 use File::Spec::Functions; |
|
22 use Cwd; |
|
23 |
|
24 $Test::Harness::Verbose = 1; |
|
25 |
|
26 # Version of the script - just use the date |
|
27 $main::VERSION = '17-Oct-08'; |
|
28 |
|
29 # a list of all the tests. |
|
30 my @all = ( |
|
31 '../bldtest/test.pl', |
|
32 '../symuser/test.pl', |
|
33 '../symfile/test.pl' |
|
34 ); |
|
35 |
|
36 my ( $verbose, $ignore, $coverage ); |
|
37 GetOptions("verbose" => \$verbose, "ignore" => \$ignore, "coverage" => \$coverage) or pod2usage(2); |
|
38 |
|
39 # Get OS version |
|
40 my $win32 = 1 if $^O =~ /MSWin32/; |
|
41 print "Running on Win32\n" if $win32 && $verbose; |
|
42 print "Running on Linux\n" if !$win32 && $verbose; |
|
43 |
|
44 # Clear coverage stats and rebuild the code with coverage enabled |
|
45 if ($coverage) |
|
46 { |
|
47 clearCoverageStats(); |
|
48 setCoverage(1); |
|
49 } |
|
50 |
|
51 # Run all the tests |
|
52 my $fail; |
|
53 eval { runtests(@all); }; |
|
54 $fail = $@ if $@; |
|
55 |
|
56 # Show coverage stats and rebuild the code with coverage disabled |
|
57 if ($coverage) |
|
58 { |
|
59 showCoverageStats(); |
|
60 setCoverage(0); |
|
61 clearCoverageStats(); |
|
62 } |
|
63 die $fail if $fail; |
|
64 |
|
65 # Show coverage stats |
|
66 sub showCoverageStats |
|
67 { |
|
68 my %hits; |
|
69 |
|
70 my $loc = cwd(); |
|
71 print "Calculating converage stats...\n"; |
|
72 |
|
73 # Find all the GCDA files |
|
74 my @dirs = "$ENV{EPOCROOT}epoc32/build"; |
|
75 foreach my $dir ( @dirs ) |
|
76 { |
|
77 opendir DIR, $dir or die "Failed on $dir: $!"; |
|
78 foreach my $child ( readdir DIR ) |
|
79 { |
|
80 next if $child =~ /^\./; |
|
81 |
|
82 push @{ $hits{$dir} }, "$child" if $child =~ /\.gcda$/; |
|
83 push @dirs, "$dir/$child" if -d "$dir/$child"; |
|
84 } |
|
85 closedir DIR; |
|
86 } |
|
87 |
|
88 # Collect the stats |
|
89 my %result; |
|
90 foreach my $dir ( keys %hits ) |
|
91 { |
|
92 chdir($dir) or die "Failed: $!"; |
|
93 my $cmd = $win32 ? "$ENV{EPOCROOT}epoc32/gcc_mingw/bin/gcov.exe @{ $hits{$dir} }" : "gcov @{ $hits{$dir} }"; |
|
94 print "Executing: $cmd\n"; |
|
95 open CMD, "$cmd|" or die "Failed: $!"; |
|
96 while(my $line = <CMD>) |
|
97 { |
|
98 if ($line =~ /Lines executed:(\d+.\d+)% of (\d+)/) |
|
99 { |
|
100 my ($total, $coverage) = ( $2, int(($2 / 100) * $1)); |
|
101 my $oldline = $line; |
|
102 $line = <CMD>; |
|
103 |
|
104 # Filter by filename symport/<component> |
|
105 if ($line =~ m[symport/([^/]+)/]) |
|
106 { |
|
107 print "\t$oldline"; |
|
108 my $comp = $1; |
|
109 |
|
110 $result{$comp}{total} = $result{$comp}{total} ? $result{$comp}{total} + $total : $total; |
|
111 $result{$comp}{coverage} = $result{$comp}{coverage} ? $result{$comp}{coverage} + $coverage : $coverage; |
|
112 } |
|
113 else |
|
114 { |
|
115 print STDERR "Unknown: Line $. - $line" if $verbose; |
|
116 } |
|
117 } |
|
118 } |
|
119 close CMD; |
|
120 } |
|
121 |
|
122 # Print a summary of the stats |
|
123 print "\nTEST COVERAGE RESULTS\n"; |
|
124 foreach ( keys %result ) |
|
125 { |
|
126 my $percent = ($result{$_}{coverage}/$result{$_}{total})*100; |
|
127 print "$_: $result{$_}{coverage}/$result{$_}{total} (${percent}%)\n"; |
|
128 } |
|
129 chdir($loc); |
|
130 } |
|
131 |
|
132 # Make sure all GCDA files are deleted |
|
133 sub clearCoverageStats |
|
134 { |
|
135 print "Clearing converage stats...\n" if $verbose; |
|
136 |
|
137 my @dirs = "$ENV{EPOCROOT}epoc32/build"; |
|
138 foreach my $dir ( @dirs ) |
|
139 { |
|
140 opendir DIR, $dir or die "Failed on $dir: $!"; |
|
141 foreach my $child ( readdir DIR ) |
|
142 { |
|
143 next if $child =~ /^\./; |
|
144 |
|
145 if ($child =~ /\.gcda$/) |
|
146 { |
|
147 print "Deleting: $dir/$child\n" if $verbose; |
|
148 unlink "$dir/$child"; |
|
149 } |
|
150 push @dirs, "$dir/$child" if -d "$dir/$child"; |
|
151 } |
|
152 closedir DIR; |
|
153 } |
|
154 } |
|
155 |
|
156 # Enables/Disables code coverage |
|
157 sub setCoverage |
|
158 { |
|
159 my $enable = shift; |
|
160 |
|
161 if ($verbose) { print $enable ? "Enabling code coverage...\n" : "Disabling code coverage...\n"; } |
|
162 my $makefile = catfile($ENV{EPOCROOT}, 'epoc32', 'tools', 'makefile_templates', 'tools', 'x86tool.mk'); |
|
163 die "Can't find makefile $makefile" if !-e $makefile; |
|
164 |
|
165 # Read the makefile |
|
166 my @lines; |
|
167 open IN, $makefile or die "Failed to open $makefile: $!"; |
|
168 while(<IN>) |
|
169 { |
|
170 if (/CODE_COVERAGE:=/) |
|
171 { |
|
172 push @lines, $enable ? "CODE_COVERAGE:=1\n" : "CODE_COVERAGE:=\n"; |
|
173 next; |
|
174 } |
|
175 push @lines, $_; |
|
176 } |
|
177 close IN; |
|
178 |
|
179 # Write out the makefile with the change |
|
180 open OUT, ">$makefile" or die "Failed to open $makefile for writing: $!"; |
|
181 print OUT @lines; |
|
182 close OUT; |
|
183 |
|
184 # Now do the same for the new build system |
|
185 my $xml = catfile($ENV{SBS_HOME}, 'lib', 'flm', 'x86tool.xml'); |
|
186 die "Can't find xml $xml" if !-e $xml; |
|
187 |
|
188 # Read the makefile |
|
189 @lines = (); |
|
190 open IN, $xml or die "Failed to open $xml: $!"; |
|
191 while(<IN>) |
|
192 { |
|
193 if (/(\s*<param name=\"CODE_COVERAGE\" default=\")\d?(\" \/>)/) |
|
194 { |
|
195 push @lines, sprintf("$1%s$2\n", $enable ? '1' : ''); |
|
196 next; |
|
197 } |
|
198 push @lines, $_; |
|
199 } |
|
200 close IN; |
|
201 |
|
202 # Write out the makefile with the change |
|
203 open OUT, ">$xml" or die "Failed to open $xml for writing: $!"; |
|
204 print OUT @lines; |
|
205 close OUT; |
|
206 |
|
207 doClean(); |
|
208 doBuild(); |
|
209 } |
|
210 |
|
211 # *** |
|
212 # Get the version of the build system |
|
213 # * |
|
214 sub getBuildVersion |
|
215 { |
|
216 # First of all see if SBSv2 is installed |
|
217 my $sbs_ver = 1; |
|
218 open SBS, 'sbs -v 2>&1|' or die "Failed to execute command: $!"; |
|
219 while(<SBS>) |
|
220 { |
|
221 if (/^sbs version/) |
|
222 { |
|
223 $sbs_ver = 2; |
|
224 last; |
|
225 } |
|
226 } |
|
227 close SBS; |
|
228 |
|
229 # Override the result using environment variable |
|
230 $sbs_ver = 1 if $ENV{SBS_VERSION} && $ENV{SBS_VERSION} == 1; |
|
231 return $sbs_ver; |
|
232 } |
|
233 |
|
234 # *** |
|
235 # Cleans the code |
|
236 # * |
|
237 sub doClean |
|
238 { |
|
239 print "Cleaning code...\n" if $verbose; |
|
240 my $sbs_ver = getBuildVersion(); |
|
241 my $redir = $verbose?'':' >nul 2>&1'; |
|
242 |
|
243 if ($sbs_ver == 1) |
|
244 { |
|
245 # Use the old build system |
|
246 system("bldmake bldfiles$redir"); |
|
247 system("abld makefile$redir"); |
|
248 system("abld clean$redir"); |
|
249 } |
|
250 else |
|
251 { |
|
252 # Use the new build system |
|
253 system("sbs -c tools2 CLEAN$redir"); |
|
254 system("sbs -c winscw CLEAN$redir"); |
|
255 } |
|
256 } |
|
257 |
|
258 # *** |
|
259 # Builds the code |
|
260 # * |
|
261 sub doBuild |
|
262 { |
|
263 print "Building code...\n" if $verbose; |
|
264 my $sbs_ver = getBuildVersion(); |
|
265 my $redir = $verbose?'':' >nul 2>&1'; |
|
266 |
|
267 # Now build the test code - assumes current working directory is correct! |
|
268 print "Using SBSv$sbs_ver\n" if $verbose; |
|
269 if ($sbs_ver == 1) |
|
270 { |
|
271 |
|
272 # Use the old build system |
|
273 system("bldmake bldfiles$redir"); |
|
274 system("abld build$redir"); |
|
275 } |
|
276 else |
|
277 { |
|
278 # Use the new build system |
|
279 system("sbs -c tools2$redir"); |
|
280 system("sbs -c winscw$redir"); |
|
281 } |
|
282 } |
|
283 |
|
284 =head1 NAME |
|
285 |
|
286 test.pl - A script for running other tests |
|
287 |
|
288 =head1 SYNOPSIS |
|
289 |
|
290 test.pl [-help] [-version] [-verbose] [-coverage] |
|
291 |
|
292 Options: |
|
293 -help brief help message |
|
294 -version version of the script |
|
295 -verbose print what the scripts does |
|
296 -coverage gather code coverage stats (rebuilds everything twice) |
|
297 |
|
298 =cut |