blob: d9585f6a40861cb1ee8fda1a66320627101f9a0e [file] [log] [blame]
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +00001#!/usr/bin/env perl
2#
3# The LLVM Compiler Infrastructure
4#
5# This file is distributed under the University of Illinois Open Source
6# License. See LICENSE.TXT for details.
7#
8##===----------------------------------------------------------------------===##
9#
10# A script designed to wrap a build so that all calls to gcc are intercepted
11# and piped to the static analyzer.
12#
13##===----------------------------------------------------------------------===##
14
15use strict;
16use warnings;
17use File::Temp qw/ :mktemp /;
Ted Kremenek22d6a632008-04-02 20:43:36 +000018use FindBin qw($RealBin);
Ted Kremeneka6e24812008-04-19 18:05:48 +000019use Digest::MD5;
Ted Kremenek7a4648d2008-05-02 22:04:53 +000020use File::Basename;
Ted Kremenek23cfca32008-06-16 22:40:14 +000021use Term::ANSIColor;
22use Term::ANSIColor qw(:constants);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000023
24my $Verbose = 0; # Verbose output from this script.
25my $Prog = "scan-build";
Ted Kremenekf4cdf412008-05-23 18:17:05 +000026my $BuildName;
27my $BuildDate;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000028
Ted Kremenek23cfca32008-06-16 22:40:14 +000029my $UseColor = (($ENV{'TERM'} eq 'xterm-color') and -t STDOUT);
30
31sub Diag {
32 if ($UseColor) {
33 print BOLD, MAGENTA "$Prog: @_";
34 print RESET;
35 }
36 else {
37 print "$Prog: @_";
38 }
39}
40
41sub DieDiag {
42 if ($UseColor) {
43 print BOLD, RED "$Prog: ";
44 print RESET, RED @_;
45 print RESET;
46 }
47 else {
48 print "$Prog: ", @_;
49 }
50 exit(0);
51}
52
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000053##----------------------------------------------------------------------------##
54# GetHTMLRunDir - Construct an HTML directory name for the current run.
55##----------------------------------------------------------------------------##
56
Sam Bishopa0e22662008-04-02 03:35:43 +000057sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000058
59 die "Not enough arguments." if (@_ == 0);
60
61 my $Dir = shift @_;
62
63 # Get current date and time.
64
65 my @CurrentTime = localtime();
66
67 my $year = $CurrentTime[5] + 1900;
68 my $day = $CurrentTime[3];
69 my $month = $CurrentTime[4] + 1;
70
Ted Kremenek9d7405f2008-05-14 17:23:56 +000071 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000072
73 # Determine the run number.
74
75 my $RunNumber;
76
77 if (-d $Dir) {
78
79 if (! -r $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +000080 DieDiag("directory '$Dir' exists but is not readable.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000081 }
82
83 # Iterate over all files in the specified directory.
84
85 my $max = 0;
86
87 opendir(DIR, $Dir);
88 my @FILES= readdir(DIR);
89 closedir(DIR);
90
91 foreach my $f (@FILES) {
92
93 my @x = split/-/, $f;
94
95 next if (scalar(@x) != 4);
96 next if ($x[0] != $year);
97 next if ($x[1] != $month);
98 next if ($x[2] != $day);
99
100 if ($x[3] > $max) {
101 $max = $x[3];
102 }
103 }
104
105 $RunNumber = $max + 1;
106 }
107 else {
108
109 if (-x $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000110 DieDiag("'$Dir' exists but is not a directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000111 }
112
113 # $Dir does not exist. It will be automatically created by the
114 # clang driver. Set the run number to 1.
115
116 $RunNumber = 1;
117 }
118
119 die "RunNumber must be defined!" if (!defined($RunNumber));
120
121 # Append the run number.
122
123 return "$Dir/$DateString-$RunNumber";
124}
125
Sam Bishopa0e22662008-04-02 03:35:43 +0000126sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000127
128 die "Wrong number of arguments." if (scalar(@_) != 2);
129
130 my $Args = shift;
131 my $Dir = shift;
132
133 die "No build command." if (scalar(@$Args) == 0);
134
135 my $Cmd = $$Args[0];
136
137 if ($Cmd =~ /configure/) {
138 return;
139 }
140
141 if ($Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000142 Diag("Emitting reports for this run to '$Dir'.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000143 }
144
145 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
146}
147
148##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000149# ComputeDigest - Compute a digest of the specified file.
150##----------------------------------------------------------------------------##
151
152sub ComputeDigest {
153 my $FName = shift;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000154 DieDiag("Cannot read $FName to compute Digest.\n") if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000155
156 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000157 # just looking for duplicate files that come from a non-malicious source.
158 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremeneka6e24812008-04-19 18:05:48 +0000159 # come bundled on most systems.
160
Ted Kremenek23cfca32008-06-16 22:40:14 +0000161 open(FILE, $FName) or DieDiag("Cannot open $FName when computing Digest.\n");
Ted Kremeneka6e24812008-04-19 18:05:48 +0000162 binmode FILE;
163 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
164 close(FILE);
165
166 # Return the digest.
167
168 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000169}
170
171##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000172# UpdatePrefix - Compute the common prefix of files.
173##----------------------------------------------------------------------------##
174
175my $Prefix;
176
177sub UpdatePrefix {
178
179 my $x = shift;
180 my $y = basename($x);
181 $x =~ s/\Q$y\E$//;
182
183 # Ignore /usr, /Library, /System, /Developer
184
185 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
186 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
187
188
189 if (!defined $Prefix) {
190 $Prefix = $x;
191 return;
192 }
193
194 chop $Prefix while (!($x =~ /^$Prefix/));
195}
196
197sub GetPrefix {
198 return $Prefix;
199}
200
201##----------------------------------------------------------------------------##
202# UpdateInFilePath - Update the path in the report file.
203##----------------------------------------------------------------------------##
204
205sub UpdateInFilePath {
206 my $fname = shift;
207 my $regex = shift;
208 my $newtext = shift;
209
210 open (RIN, $fname) or die "cannot open $fname";
211 open (ROUT, ">$fname.tmp") or die "cannot open $fname.tmp";
212
213 while (<RIN>) {
214 s/$regex/$newtext/;
215 print ROUT $_;
216 }
217
218 close (ROUT);
219 close (RIN);
220 `mv $fname.tmp $fname`;
221}
222
223##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000224# ScanFile - Scan a report file for various identifying attributes.
225##----------------------------------------------------------------------------##
226
Ted Kremenek57cf4462008-04-18 15:09:30 +0000227# Sometimes a source file is scanned more than once, and thus produces
228# multiple error reports. We use a cache to solve this problem.
229
230my %AlreadyScanned;
231
Ted Kremenek5744dc22008-04-02 18:03:36 +0000232sub ScanFile {
233
234 my $Index = shift;
235 my $Dir = shift;
236 my $FName = shift;
237
Ted Kremenek57cf4462008-04-18 15:09:30 +0000238 # Compute a digest for the report file. Determine if we have already
239 # scanned a file that looks just like it.
240
241 my $digest = ComputeDigest("$Dir/$FName");
242
243 if (defined($AlreadyScanned{$digest})) {
244 # Redundant file. Remove it.
245 `rm -f $Dir/$FName`;
246 return;
247 }
248
249 $AlreadyScanned{$digest} = 1;
250
Ted Kremenek809709f2008-04-18 16:58:34 +0000251 # At this point the report file is not world readable. Make it happen.
Ted Kremenek684bb092008-04-18 15:18:20 +0000252 `chmod 644 $Dir/$FName`;
253
254 # Scan the report file for tags.
Ted Kremenek23cfca32008-06-16 22:40:14 +0000255 open(IN, "$Dir/$FName") or DieDiag("Cannot open '$Dir/$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000256
257 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000258 my $BugFile = "";
259 my $BugPathLength = 1;
260 my $BugLine = 0;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000261
262 while (<IN>) {
263
264 if (/<!-- BUGDESC (.*) -->$/) {
265 $BugDesc = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000266 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000267 elsif (/<!-- BUGFILE (.*) -->$/) {
268 $BugFile = $1;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000269 UpdatePrefix($BugFile);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000270 }
271 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
272 $BugPathLength = $1;
273 }
274 elsif (/<!-- BUGLINE (.*) -->$/) {
275 $BugLine = $1;
276 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000277 }
278
279 close(IN);
280
Ted Kremenek22d6a632008-04-02 20:43:36 +0000281 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
282}
283
284##----------------------------------------------------------------------------##
285# CopyJS - Copy JavaScript code to target directory.
286##----------------------------------------------------------------------------##
287
288sub CopyJS {
289
290 my $Dir = shift;
291
Ted Kremenek23cfca32008-06-16 22:40:14 +0000292 DieDiag("Cannot find 'sorttable.js'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000293 if (! -r "$RealBin/sorttable.js");
294
295 `cp $RealBin/sorttable.js $Dir`;
296
Ted Kremenek23cfca32008-06-16 22:40:14 +0000297 DieDiag("Could not copy 'sorttable.js' to '$Dir'.\n")
Ted Kremenek22d6a632008-04-02 20:43:36 +0000298 if (! -r "$Dir/sorttable.js");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000299}
300
301##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000302# Postprocess - Postprocess the results of an analysis scan.
303##----------------------------------------------------------------------------##
304
Sam Bishopa0e22662008-04-02 03:35:43 +0000305sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000306
307 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000308 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000309
310 die "No directory specified." if (!defined($Dir));
Ted Kremenek684bb092008-04-18 15:18:20 +0000311 die "No base directory specified." if (!defined($BaseDir));
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000312
313 if (! -d $Dir) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000314 Diag("No bugs found.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000315 return;
316 }
317
318 opendir(DIR, $Dir);
319 my @files = grep(/^report-.*\.html$/,readdir(DIR));
320 closedir(DIR);
321
322 if (scalar(@files) == 0) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000323 Diag("Removing directory '$Dir' because it contains no reports.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000324 `rm -fR $Dir`;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000325
326 # Remove the base directory if it contains no files (don't use '-R').
327 `rm -f $BaseDir`;
328
329 Diag("No bugs found.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000330 return;
331 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000332
333 # Scan each report file and build an index.
334
335 my @Index;
336
337 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
338
339 # Generate an index.html file.
340
341 my $FName = "$Dir/index.html";
342
Ted Kremenek23cfca32008-06-16 22:40:14 +0000343 open(OUT, ">$FName") or DieDiag("Cannot create file '$FName'\n");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000344
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000345 # Print out the header.
346
Ted Kremenek5744dc22008-04-02 18:03:36 +0000347print OUT <<ENDTEXT;
348<html>
349<head>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000350<style type="text/css">
351 body { color:#000000; background-color:#ffffff }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000352 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000353 h1 { font-size:12pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000354 table.sortable thead {
355 background-color:#eee; color:#666666;
356 font-weight: bold; cursor: default;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000357 text-align:center;
358 border-top: 2px solid #000000;
359 border-bottom: 2px solid #000000;
360 font-weight: bold; font-family: Verdana
361 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000362 table.sortable { border: 1px #000000 solid }
363 table.sortable { border-collapse: collapse; border-spacing: 0px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000364 td { border-bottom: 1px #000000 dotted }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000365 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenekd8c6d0c2008-04-07 23:50:07 +0000366 td { text-align:left; font-size:9pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000367 td.View { padding-left: 10px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000368</style>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000369<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000370<script language='javascript' type="text/javascript">
371function SetDisplay(RowClass, DisplayVal)
372{
373 var Rows = document.getElementsByTagName("tr");
374 for ( var i = 0 ; i < Rows.length; ++i ) {
375 if (Rows[i].className == RowClass) {
376 Rows[i].style.display = DisplayVal;
377 }
378 }
379}
380
381function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000382 if (CheckButton.checked) {
383 SetDisplay(ClassName, "");
384 }
385 else {
386 SetDisplay(ClassName, "none");
387 }
388}
389</script>
390</head>
391<body>
392ENDTEXT
393
394 # Print out the summary table.
395
396 my %Totals;
397
398 for my $row ( @Index ) {
399
Ted Kremenek432af592008-05-06 18:11:36 +0000400 #my $bug_type = lc($row->[1]);
401 my $bug_type = ($row->[1]);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000402
403 if (!defined($Totals{$bug_type})) {
404 $Totals{$bug_type} = 1;
405 }
406 else {
407 $Totals{$bug_type}++;
408 }
409 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000410
411 print OUT "<h3>Summary</h3>";
412
413 if (defined($BuildName)) {
414 print OUT "\n<p>Results in this analysis run are based on analyzer build <b>$BuildName</b>.</p>\n"
415 }
416
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000417print OUT <<ENDTEXT;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000418<table class="sortable">
419<tr>
420 <td>Bug Type</td>
421 <td>Quantity</td>
422 <td "sorttable_nosort">Display?</td>
423</tr>
424ENDTEXT
425
426 for my $key ( sort { $a cmp $b } keys %Totals ) {
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000427 my $x = lc($key);
428 $x =~ s/\s[,]/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000429 print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n";
430 }
431
432 # Print out the table of errors.
433
434print OUT <<ENDTEXT;
435</table>
436<h3>Reports</h3>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000437<table class="sortable">
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000438<tr>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000439 <td>Bug Type</td>
440 <td>File</td>
441 <td>Line</td>
442 <td>Path Length</td>
443 <td "sorttable_nosort"></td>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000444</tr>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000445ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000446
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000447 my $prefix = GetPrefix();
448 my $regex;
449 my $InFileRegex;
450 my $InFilePrefix = "File:</td><td>";
451
452 if (defined($prefix)) {
453 $regex = qr/^\Q$prefix\E/is;
454 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
455 }
456
Ted Kremenek5744dc22008-04-02 18:03:36 +0000457 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
458
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000459 my $x = lc($row->[1]);
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000460 $x =~ s/\s[,]/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000461
462 print OUT "<tr class=\"bt_$x\">\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000463
Ted Kremenek5744dc22008-04-02 18:03:36 +0000464 my $ReportFile = $row->[0];
465
Ted Kremenek22d6a632008-04-02 20:43:36 +0000466 print OUT " <td class=\"DESC\">";
Ted Kremenek432af592008-05-06 18:11:36 +0000467 #print OUT lc($row->[1]);
468 print OUT $row->[1];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000469 print OUT "</td>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000470
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000471 # Update the file prefix.
472
473 my $fname = $row->[2];
474 if (defined($regex)) {
475 $fname =~ s/$regex//;
476 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
477 }
Ted Kremenek3e56e0b2008-05-02 23:40:49 +0000478
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000479 print OUT "<td>$fname</td>\n";
480
481 # Print the rest of the columns.
482
483 for my $j ( 3 .. $#{$row} ) {
Ted Kremenek5744dc22008-04-02 18:03:36 +0000484 print OUT "<td>$row->[$j]</td>\n"
485 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000486
487 # Emit the "View" link.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000488
Ted Kremenek22d6a632008-04-02 20:43:36 +0000489 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000490
491 # End the row.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000492 print OUT "</tr>\n";
493 }
494
495 print OUT "</table>\n</body></html>\n";
496 close(OUT);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000497
Ted Kremenek22d6a632008-04-02 20:43:36 +0000498 CopyJS($Dir);
Ted Kremenek684bb092008-04-18 15:18:20 +0000499
500 # Make sure $Dir and $BaseDir is world readable/executable.
501 `chmod 755 $Dir`;
502 `chmod 755 $BaseDir`;
Ted Kremenek23cfca32008-06-16 22:40:14 +0000503
504 my $Num = scalar(@Index);
505 Diag("$Num bugs found.\n")
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000506}
507
508##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000509# RunBuildCommand - Run the build command.
510##----------------------------------------------------------------------------##
511
Ted Kremenek6b628982008-04-30 23:47:12 +0000512sub AddIfNotPresent {
513 my $Args = shift;
514 my $Arg = shift;
515 my $found = 0;
516
517 foreach my $k (@$Args) {
518 if ($k eq $Arg) {
519 $found = 1;
520 last;
521 }
522 }
523
524 if ($found == 0) {
525 push @$Args, $Arg;
526 }
527}
528
Ted Kremenekdab11102008-04-02 04:43:42 +0000529sub RunBuildCommand {
530
531 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000532 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000533 my $Cmd = $Args->[0];
Ted Kremenek6195c372008-06-02 21:52:47 +0000534 my $CCAnalyzer = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000535
Ted Kremenek6a43ba92008-04-02 15:34:12 +0000536 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000537 shift @$Args;
Ted Kremenek6195c372008-06-02 21:52:47 +0000538 unshift @$Args, $CCAnalyzer;
Ted Kremenekdab11102008-04-02 04:43:42 +0000539 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000540 elsif ($IgnoreErrors) {
541 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000542 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000543 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000544 }
545 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000546 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000547 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000548 }
549
Ted Kremenek6b628982008-04-30 23:47:12 +0000550 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000551 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000552 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000553
554 # Disable PCH files until clang supports them.
555 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000556
557 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
558 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
559 # when linking such files.
560 my $LDPLUSPLUS = `which g++`;
561 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
562 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000563 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000564
565 system(@$Args);
566}
567
Ted Kremenekdab11102008-04-02 04:43:42 +0000568##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000569# DisplayHelp - Utility function to display all help options.
570##----------------------------------------------------------------------------##
571
Sam Bishopa0e22662008-04-02 03:35:43 +0000572sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000573
Ted Kremenek5744dc22008-04-02 18:03:36 +0000574print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000575USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000576
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000577ENDTEXT
578
579 if (defined($BuildName)) {
580 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
581 }
582
583print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000584OPTIONS:
585
Ted Kremenek1262fc42008-05-14 20:10:33 +0000586 -a - The analysis to run. The default is 'checker-cfref'.
587 Valid options are: 'checker-cfref', 'fsyntax-only'
588
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000589 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000590 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000591 the analyzer. If this option is not specified, a directory
592 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000593
Ted Kremenek10f883f2008-04-03 07:11:44 +0000594 -h - Display this message.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000595 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000596
Ted Kremenekaf08f642008-04-02 16:31:58 +0000597 -k - Add a "keep on going" option to the specified build command.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000598 --keep-going This option currently supports make and xcodebuild.
599 This is a convenience option; one can specify this
600 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000601
Ted Kremenekdab11102008-04-02 04:43:42 +0000602 -v - Verbose output from $Prog and the analyzer.
603 A second "-v" increases verbosity.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000604
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000605 -V - View analysis results in a web browser when the build
606 --view completes.
607
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000608BUILD OPTIONS
609
Ted Kremenek39eefde2008-04-02 16:47:27 +0000610 You can specify any build option acceptable to the build command.
611
Ted Kremenek5744dc22008-04-02 18:03:36 +0000612EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000613
Ted Kremenek5744dc22008-04-02 18:03:36 +0000614 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000615
Ted Kremenek39eefde2008-04-02 16:47:27 +0000616 The above example causes analysis reports to be deposited into
617 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
618 A different subdirectory is created each time $Prog analyzes a project.
619 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000620
621ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000622}
623
624##----------------------------------------------------------------------------##
625# Process command-line arguments.
626##----------------------------------------------------------------------------##
627
628my $HtmlDir; # Parent directory to store HTML files.
629my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000630my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000631my $Analysis = "checker-cfref";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000632
633if (!@ARGV) {
634 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000635 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000636}
637
638while (@ARGV) {
639
640 # Scan for options we recognize.
641
642 my $arg = $ARGV[0];
643
Sam Bishop2f2418e2008-04-03 14:29:47 +0000644 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000645 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000646 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000647 }
648
Ted Kremenek1262fc42008-05-14 20:10:33 +0000649 if ($arg eq "-a") {
650 shift @ARGV;
651
652 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000653 DieDiag("'-a' option requires an analysis type.\n");
Ted Kremenek1262fc42008-05-14 20:10:33 +0000654 }
655
656 $Analysis = shift @ARGV;
657
658 if (!($Analysis eq "checker-cfref" or $Analysis eq "fsyntax-only")) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000659 DieDiag("Invalid argument '$Analysis' to -a.\n");
Ted Kremenek1262fc42008-05-14 20:10:33 +0000660 }
661
662 next;
663 }
664
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000665 if ($arg eq "-o") {
666 shift @ARGV;
667
668 if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000669 DieDiag("'-o' option requires a target directory name.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000670 }
671
672 $HtmlDir = shift @ARGV;
673 next;
674 }
675
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000676 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000677 shift @ARGV;
678 $IgnoreErrors = 1;
679 next;
680 }
681
682 if ($arg eq "-v") {
683 shift @ARGV;
684 $Verbose++;
685 next;
686 }
687
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000688 if ($arg eq "-V" or $arg eq "--view") {
689 shift @ARGV;
690 $ViewResults = 1;
691 next;
692 }
693
Ted Kremenek23cfca32008-06-16 22:40:14 +0000694 DieDiag("unrecognized option '$arg'\n") if ($arg =~ /^-/);
Ted Kremenek0062ad42008-04-02 16:35:01 +0000695
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000696 last;
697}
698
699if (!@ARGV) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000700 Diag("No build command specified.\n\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000701 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000702 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000703}
704
705# Determine the output directory for the HTML reports.
706
707if (!defined($HtmlDir)) {
708
Sam Bishopa0e22662008-04-02 03:35:43 +0000709 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000710
711 if (!defined($HtmlDir)) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000712 DieDiag("Cannot create HTML directory in /tmp.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000713 }
714
715 if (!$Verbose) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000716 Diag("Using '$HtmlDir' as base HTML report directory.\n");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000717 }
718}
719
Ted Kremenek684bb092008-04-18 15:18:20 +0000720my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +0000721$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000722
723# Set the appropriate environment variables.
724
Sam Bishopa0e22662008-04-02 03:35:43 +0000725SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000726
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000727my $Cmd = "$RealBin/ccc-analyzer";
728
Ted Kremenek23cfca32008-06-16 22:40:14 +0000729DieDiag("Executable 'ccc-analyzer' does not exist at '$Cmd'\n")
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000730 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000731
732my $Clang = "$RealBin/clang";
733
734if (! -x $Clang) {
Ted Kremenek23cfca32008-06-16 22:40:14 +0000735 Diag("'clang' executable not found in '$RealBin'. Using 'clang' from path.\n");
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000736 $Clang = "clang";
737}
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000738
Ted Kremenek4f4b17d2008-04-03 20:08:18 +0000739$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000740$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000741
742if ($Verbose >= 2) {
743 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
744}
745
Ted Kremeneka9525c92008-05-12 22:07:14 +0000746if ($Verbose >= 3) {
747 $ENV{'CCC_ANALYZER_LOG'} = 1;
748}
749
Ted Kremenek1262fc42008-05-14 20:10:33 +0000750$ENV{'CCC_ANALYZER_ANALYSIS'} = $Analysis;
751
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000752# Run the build.
753
Ted Kremenek6195c372008-06-02 21:52:47 +0000754RunBuildCommand(\@ARGV, $IgnoreErrors, $Cmd);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000755
756# Postprocess the HTML directory.
757
Ted Kremenek684bb092008-04-18 15:18:20 +0000758Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000759
760if ($ViewResults and -r "$HtmlDir/index.html") {
761 # Only works on Mac OS X (for now).
762 print "Viewing analysis results: '$HtmlDir/index.html'\n";
763 `open $HtmlDir/index.html`
764}