blob: 64419a3c4c2fa9a3f04c14557618d7956a499f58 [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 Kremenek9cc8c2c2008-04-01 20:47:38 +000021
22my $Verbose = 0; # Verbose output from this script.
23my $Prog = "scan-build";
Ted Kremenekf4cdf412008-05-23 18:17:05 +000024my $BuildName;
25my $BuildDate;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000026
27##----------------------------------------------------------------------------##
28# GetHTMLRunDir - Construct an HTML directory name for the current run.
29##----------------------------------------------------------------------------##
30
Sam Bishopa0e22662008-04-02 03:35:43 +000031sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000032
33 die "Not enough arguments." if (@_ == 0);
34
35 my $Dir = shift @_;
36
37 # Get current date and time.
38
39 my @CurrentTime = localtime();
40
41 my $year = $CurrentTime[5] + 1900;
42 my $day = $CurrentTime[3];
43 my $month = $CurrentTime[4] + 1;
44
Ted Kremenek9d7405f2008-05-14 17:23:56 +000045 my $DateString = sprintf("%d-%02d-%02d", $year, $month, $day);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000046
47 # Determine the run number.
48
49 my $RunNumber;
50
51 if (-d $Dir) {
52
53 if (! -r $Dir) {
Sam Bishopa0e22662008-04-02 03:35:43 +000054 die "error: '$Dir' exists but is not readable.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000055 }
56
57 # Iterate over all files in the specified directory.
58
59 my $max = 0;
60
61 opendir(DIR, $Dir);
62 my @FILES= readdir(DIR);
63 closedir(DIR);
64
65 foreach my $f (@FILES) {
66
67 my @x = split/-/, $f;
68
69 next if (scalar(@x) != 4);
70 next if ($x[0] != $year);
71 next if ($x[1] != $month);
72 next if ($x[2] != $day);
73
74 if ($x[3] > $max) {
75 $max = $x[3];
76 }
77 }
78
79 $RunNumber = $max + 1;
80 }
81 else {
82
83 if (-x $Dir) {
Sam Bishopa0e22662008-04-02 03:35:43 +000084 die "error: '$Dir' exists but is not a directory.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +000085 }
86
87 # $Dir does not exist. It will be automatically created by the
88 # clang driver. Set the run number to 1.
89
90 $RunNumber = 1;
91 }
92
93 die "RunNumber must be defined!" if (!defined($RunNumber));
94
95 # Append the run number.
96
97 return "$Dir/$DateString-$RunNumber";
98}
99
Sam Bishopa0e22662008-04-02 03:35:43 +0000100sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000101
102 die "Wrong number of arguments." if (scalar(@_) != 2);
103
104 my $Args = shift;
105 my $Dir = shift;
106
107 die "No build command." if (scalar(@$Args) == 0);
108
109 my $Cmd = $$Args[0];
110
111 if ($Cmd =~ /configure/) {
112 return;
113 }
114
115 if ($Verbose) {
116 print "$Prog: Emitting reports for this run to '$Dir'.\n";
117 }
118
119 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
120}
121
122##----------------------------------------------------------------------------##
Ted Kremenek57cf4462008-04-18 15:09:30 +0000123# ComputeDigest - Compute a digest of the specified file.
124##----------------------------------------------------------------------------##
125
126sub ComputeDigest {
127 my $FName = shift;
128 die "Cannot read $FName" if (! -r $FName);
Ted Kremeneka6e24812008-04-19 18:05:48 +0000129
130 # Use Digest::MD5. We don't have to be cryptographically secure. We're
Ted Kremenek7ea02e62008-04-19 18:07:44 +0000131 # just looking for duplicate files that come from a non-malicious source.
132 # We use Digest::MD5 because it is a standard Perl module that should
Ted Kremeneka6e24812008-04-19 18:05:48 +0000133 # come bundled on most systems.
134
135 open(FILE, $FName) or die "Cannot open $FName.";
136 binmode FILE;
137 my $Result = Digest::MD5->new->addfile(*FILE)->hexdigest;
138 close(FILE);
139
140 # Return the digest.
141
142 return $Result;
Ted Kremenek57cf4462008-04-18 15:09:30 +0000143}
144
145##----------------------------------------------------------------------------##
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000146# UpdatePrefix - Compute the common prefix of files.
147##----------------------------------------------------------------------------##
148
149my $Prefix;
150
151sub UpdatePrefix {
152
153 my $x = shift;
154 my $y = basename($x);
155 $x =~ s/\Q$y\E$//;
156
157 # Ignore /usr, /Library, /System, /Developer
158
159 return if ( $x =~ /^\/usr/ or $x =~ /^\/Library/
160 or $x =~ /^\/System/ or $x =~ /^\/Developer/);
161
162
163 if (!defined $Prefix) {
164 $Prefix = $x;
165 return;
166 }
167
168 chop $Prefix while (!($x =~ /^$Prefix/));
169}
170
171sub GetPrefix {
172 return $Prefix;
173}
174
175##----------------------------------------------------------------------------##
176# UpdateInFilePath - Update the path in the report file.
177##----------------------------------------------------------------------------##
178
179sub UpdateInFilePath {
180 my $fname = shift;
181 my $regex = shift;
182 my $newtext = shift;
183
184 open (RIN, $fname) or die "cannot open $fname";
185 open (ROUT, ">$fname.tmp") or die "cannot open $fname.tmp";
186
187 while (<RIN>) {
188 s/$regex/$newtext/;
189 print ROUT $_;
190 }
191
192 close (ROUT);
193 close (RIN);
194 `mv $fname.tmp $fname`;
195}
196
197##----------------------------------------------------------------------------##
Ted Kremenek5744dc22008-04-02 18:03:36 +0000198# ScanFile - Scan a report file for various identifying attributes.
199##----------------------------------------------------------------------------##
200
Ted Kremenek57cf4462008-04-18 15:09:30 +0000201# Sometimes a source file is scanned more than once, and thus produces
202# multiple error reports. We use a cache to solve this problem.
203
204my %AlreadyScanned;
205
Ted Kremenek5744dc22008-04-02 18:03:36 +0000206sub ScanFile {
207
208 my $Index = shift;
209 my $Dir = shift;
210 my $FName = shift;
211
Ted Kremenek57cf4462008-04-18 15:09:30 +0000212 # Compute a digest for the report file. Determine if we have already
213 # scanned a file that looks just like it.
214
215 my $digest = ComputeDigest("$Dir/$FName");
216
217 if (defined($AlreadyScanned{$digest})) {
218 # Redundant file. Remove it.
219 `rm -f $Dir/$FName`;
220 return;
221 }
222
223 $AlreadyScanned{$digest} = 1;
224
Ted Kremenek809709f2008-04-18 16:58:34 +0000225 # At this point the report file is not world readable. Make it happen.
Ted Kremenek684bb092008-04-18 15:18:20 +0000226 `chmod 644 $Dir/$FName`;
227
228 # Scan the report file for tags.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000229 open(IN, "$Dir/$FName") or die "$Prog: Cannot open '$Dir/$FName'\n";
230
231 my $BugDesc = "";
Ted Kremenek22d6a632008-04-02 20:43:36 +0000232 my $BugFile = "";
233 my $BugPathLength = 1;
234 my $BugLine = 0;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000235
236 while (<IN>) {
237
238 if (/<!-- BUGDESC (.*) -->$/) {
239 $BugDesc = $1;
Ted Kremenek5744dc22008-04-02 18:03:36 +0000240 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000241 elsif (/<!-- BUGFILE (.*) -->$/) {
242 $BugFile = $1;
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000243 UpdatePrefix($BugFile);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000244 }
245 elsif (/<!-- BUGPATHLENGTH (.*) -->$/) {
246 $BugPathLength = $1;
247 }
248 elsif (/<!-- BUGLINE (.*) -->$/) {
249 $BugLine = $1;
250 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000251 }
252
253 close(IN);
254
Ted Kremenek22d6a632008-04-02 20:43:36 +0000255 push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ];
256}
257
258##----------------------------------------------------------------------------##
259# CopyJS - Copy JavaScript code to target directory.
260##----------------------------------------------------------------------------##
261
262sub CopyJS {
263
264 my $Dir = shift;
265
266 die "$Prog: Cannot find 'sorttable.js'.\n"
267 if (! -r "$RealBin/sorttable.js");
268
269 `cp $RealBin/sorttable.js $Dir`;
270
271 die "$Prog: Could not copy 'sorttable.js' to '$Dir'."
272 if (! -r "$Dir/sorttable.js");
Ted Kremenek5744dc22008-04-02 18:03:36 +0000273}
274
275##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000276# Postprocess - Postprocess the results of an analysis scan.
277##----------------------------------------------------------------------------##
278
Sam Bishopa0e22662008-04-02 03:35:43 +0000279sub Postprocess {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000280
281 my $Dir = shift;
Ted Kremenek684bb092008-04-18 15:18:20 +0000282 my $BaseDir = shift;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000283
284 die "No directory specified." if (!defined($Dir));
Ted Kremenek684bb092008-04-18 15:18:20 +0000285 die "No base directory specified." if (!defined($BaseDir));
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000286
287 if (! -d $Dir) {
288 return;
289 }
290
291 opendir(DIR, $Dir);
292 my @files = grep(/^report-.*\.html$/,readdir(DIR));
293 closedir(DIR);
294
295 if (scalar(@files) == 0) {
Ted Kremenek02493782008-04-02 07:05:07 +0000296 print "$Prog: Removing directory '$Dir' because it contains no reports.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000297 `rm -fR $Dir`;
298 return;
299 }
Ted Kremenek5744dc22008-04-02 18:03:36 +0000300
301 # Scan each report file and build an index.
302
303 my @Index;
304
305 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
306
307 # Generate an index.html file.
308
309 my $FName = "$Dir/index.html";
310
311 open(OUT, ">$FName") or die "$Prog: Cannot create file '$FName'\n";
312
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000313 # Print out the header.
314
Ted Kremenek5744dc22008-04-02 18:03:36 +0000315print OUT <<ENDTEXT;
316<html>
317<head>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000318<style type="text/css">
319 body { color:#000000; background-color:#ffffff }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000320 body { font-family: Helvetica, sans-serif; font-size:9pt }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000321 h1 { font-size:12pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000322 table.sortable thead {
323 background-color:#eee; color:#666666;
324 font-weight: bold; cursor: default;
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000325 text-align:center;
326 border-top: 2px solid #000000;
327 border-bottom: 2px solid #000000;
328 font-weight: bold; font-family: Verdana
329 }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000330 table.sortable { border: 1px #000000 solid }
331 table.sortable { border-collapse: collapse; border-spacing: 0px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000332 td { border-bottom: 1px #000000 dotted }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000333 td { padding:5px; padding-left:8px; padding-right:8px }
Ted Kremenekd8c6d0c2008-04-07 23:50:07 +0000334 td { text-align:left; font-size:9pt }
Ted Kremenek22d6a632008-04-02 20:43:36 +0000335 td.View { padding-left: 10px }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000336</style>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000337<script src="sorttable.js"></script>
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000338<script language='javascript' type="text/javascript">
339function SetDisplay(RowClass, DisplayVal)
340{
341 var Rows = document.getElementsByTagName("tr");
342 for ( var i = 0 ; i < Rows.length; ++i ) {
343 if (Rows[i].className == RowClass) {
344 Rows[i].style.display = DisplayVal;
345 }
346 }
347}
348
349function ToggleDisplay(CheckButton, ClassName) {
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000350 if (CheckButton.checked) {
351 SetDisplay(ClassName, "");
352 }
353 else {
354 SetDisplay(ClassName, "none");
355 }
356}
357</script>
358</head>
359<body>
360ENDTEXT
361
362 # Print out the summary table.
363
364 my %Totals;
365
366 for my $row ( @Index ) {
367
Ted Kremenek432af592008-05-06 18:11:36 +0000368 #my $bug_type = lc($row->[1]);
369 my $bug_type = ($row->[1]);
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000370
371 if (!defined($Totals{$bug_type})) {
372 $Totals{$bug_type} = 1;
373 }
374 else {
375 $Totals{$bug_type}++;
376 }
377 }
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000378
379 print OUT "<h3>Summary</h3>";
380
381 if (defined($BuildName)) {
382 print OUT "\n<p>Results in this analysis run are based on analyzer build <b>$BuildName</b>.</p>\n"
383 }
384
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000385print OUT <<ENDTEXT;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000386<table class="sortable">
387<tr>
388 <td>Bug Type</td>
389 <td>Quantity</td>
390 <td "sorttable_nosort">Display?</td>
391</tr>
392ENDTEXT
393
394 for my $key ( sort { $a cmp $b } keys %Totals ) {
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000395 my $x = lc($key);
396 $x =~ s/\s[,]/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000397 print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n";
398 }
399
400 # Print out the table of errors.
401
402print OUT <<ENDTEXT;
403</table>
404<h3>Reports</h3>
Ted Kremenek22d6a632008-04-02 20:43:36 +0000405<table class="sortable">
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000406<tr>
Ted Kremenekbba1cf52008-04-03 05:50:51 +0000407 <td>Bug Type</td>
408 <td>File</td>
409 <td>Line</td>
410 <td>Path Length</td>
411 <td "sorttable_nosort"></td>
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000412</tr>
Ted Kremenek5744dc22008-04-02 18:03:36 +0000413ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000414
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000415 my $prefix = GetPrefix();
416 my $regex;
417 my $InFileRegex;
418 my $InFilePrefix = "File:</td><td>";
419
420 if (defined($prefix)) {
421 $regex = qr/^\Q$prefix\E/is;
422 $InFileRegex = qr/\Q$InFilePrefix$prefix\E/is;
423 }
424
Ted Kremenek5744dc22008-04-02 18:03:36 +0000425 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
426
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000427 my $x = lc($row->[1]);
Ted Kremenekbdf66c72008-05-06 23:51:45 +0000428 $x =~ s/\s[,]/_/g;
Ted Kremenek6e6eff72008-04-15 20:47:02 +0000429
430 print OUT "<tr class=\"bt_$x\">\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000431
Ted Kremenek5744dc22008-04-02 18:03:36 +0000432 my $ReportFile = $row->[0];
433
Ted Kremenek22d6a632008-04-02 20:43:36 +0000434 print OUT " <td class=\"DESC\">";
Ted Kremenek432af592008-05-06 18:11:36 +0000435 #print OUT lc($row->[1]);
436 print OUT $row->[1];
Ted Kremenek22d6a632008-04-02 20:43:36 +0000437 print OUT "</td>\n";
Ted Kremenek5744dc22008-04-02 18:03:36 +0000438
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000439 # Update the file prefix.
440
441 my $fname = $row->[2];
442 if (defined($regex)) {
443 $fname =~ s/$regex//;
444 UpdateInFilePath("$Dir/$ReportFile", $InFileRegex, $InFilePrefix)
445 }
Ted Kremenek3e56e0b2008-05-02 23:40:49 +0000446
Ted Kremenek7a4648d2008-05-02 22:04:53 +0000447 print OUT "<td>$fname</td>\n";
448
449 # Print the rest of the columns.
450
451 for my $j ( 3 .. $#{$row} ) {
Ted Kremenek5744dc22008-04-02 18:03:36 +0000452 print OUT "<td>$row->[$j]</td>\n"
453 }
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000454
455 # Emit the "View" link.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000456
Ted Kremenek22d6a632008-04-02 20:43:36 +0000457 print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n";
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000458
459 # End the row.
Ted Kremenek5744dc22008-04-02 18:03:36 +0000460 print OUT "</tr>\n";
461 }
462
463 print OUT "</table>\n</body></html>\n";
464 close(OUT);
Ted Kremenek22d6a632008-04-02 20:43:36 +0000465
Ted Kremenek22d6a632008-04-02 20:43:36 +0000466 CopyJS($Dir);
Ted Kremenek684bb092008-04-18 15:18:20 +0000467
468 # Make sure $Dir and $BaseDir is world readable/executable.
469 `chmod 755 $Dir`;
470 `chmod 755 $BaseDir`;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000471}
472
473##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000474# RunBuildCommand - Run the build command.
475##----------------------------------------------------------------------------##
476
Ted Kremenek6b628982008-04-30 23:47:12 +0000477sub AddIfNotPresent {
478 my $Args = shift;
479 my $Arg = shift;
480 my $found = 0;
481
482 foreach my $k (@$Args) {
483 if ($k eq $Arg) {
484 $found = 1;
485 last;
486 }
487 }
488
489 if ($found == 0) {
490 push @$Args, $Arg;
491 }
492}
493
Ted Kremenekdab11102008-04-02 04:43:42 +0000494sub RunBuildCommand {
495
496 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000497 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000498 my $Cmd = $Args->[0];
499
Ted Kremenek6a43ba92008-04-02 15:34:12 +0000500 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000501 shift @$Args;
502 unshift @$Args, "ccc-analyzer"
503 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000504 elsif ($IgnoreErrors) {
505 if ($Cmd eq "make" or $Cmd eq "gmake") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000506 AddIfNotPresent($Args,"-k");
Ted Kremenek8912b542008-05-13 21:28:02 +0000507 AddIfNotPresent($Args,"-i");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000508 }
509 elsif ($Cmd eq "xcodebuild") {
Ted Kremenek6b628982008-04-30 23:47:12 +0000510 AddIfNotPresent($Args,"-PBXBuildsContinueAfterErrors=YES");
Ted Kremenek7442ca62008-04-02 16:04:51 +0000511 }
Ted Kremenek6b628982008-04-30 23:47:12 +0000512 }
513
Ted Kremenek6b628982008-04-30 23:47:12 +0000514 if ($Cmd eq "xcodebuild") {
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000515 # Disable distributed builds for xcodebuild.
Ted Kremenek6b628982008-04-30 23:47:12 +0000516 AddIfNotPresent($Args,"-nodistribute");
Ted Kremenekcfd4c7b2008-05-23 22:18:16 +0000517
518 # Disable PCH files until clang supports them.
519 AddIfNotPresent($Args,"GCC_PRECOMPILE_PREFIX_HEADER=NO");
Ted Kremenek915e9722008-05-27 23:18:07 +0000520
521 # When 'CC' is set, xcodebuild uses it to do all linking, even if we are
522 # linking C++ object files. Set 'LDPLUSPLUS' so that xcodebuild uses 'g++'
523 # when linking such files.
524 my $LDPLUSPLUS = `which g++`;
525 $LDPLUSPLUS =~ s/\015?\012//; # strip newlines
526 $ENV{'LDPLUSPLUS'} = $LDPLUSPLUS;
Ted Kremenek6b628982008-04-30 23:47:12 +0000527 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000528
529 system(@$Args);
530}
531
Ted Kremenekdab11102008-04-02 04:43:42 +0000532##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000533# DisplayHelp - Utility function to display all help options.
534##----------------------------------------------------------------------------##
535
Sam Bishopa0e22662008-04-02 03:35:43 +0000536sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000537
Ted Kremenek5744dc22008-04-02 18:03:36 +0000538print <<ENDTEXT;
Sam Bishopa0e22662008-04-02 03:35:43 +0000539USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000540
Ted Kremenekf4cdf412008-05-23 18:17:05 +0000541ENDTEXT
542
543 if (defined($BuildName)) {
544 print "ANALYZER BUILD: $BuildName ($BuildDate)\n\n";
545 }
546
547print <<ENDTEXT;
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000548OPTIONS:
549
Ted Kremenek1262fc42008-05-14 20:10:33 +0000550 -a - The analysis to run. The default is 'checker-cfref'.
551 Valid options are: 'checker-cfref', 'fsyntax-only'
552
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000553 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000554 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000555 the analyzer. If this option is not specified, a directory
556 is created in /tmp to store the reports.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000557
Ted Kremenek10f883f2008-04-03 07:11:44 +0000558 -h - Display this message.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000559 --help
Ted Kremenek1262fc42008-05-14 20:10:33 +0000560
Ted Kremenekaf08f642008-04-02 16:31:58 +0000561 -k - Add a "keep on going" option to the specified build command.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000562 --keep-going This option currently supports make and xcodebuild.
563 This is a convenience option; one can specify this
564 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000565
Ted Kremenekdab11102008-04-02 04:43:42 +0000566 -v - Verbose output from $Prog and the analyzer.
567 A second "-v" increases verbosity.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000568
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000569 -V - View analysis results in a web browser when the build
570 --view completes.
571
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000572BUILD OPTIONS
573
Ted Kremenek39eefde2008-04-02 16:47:27 +0000574 You can specify any build option acceptable to the build command.
575
Ted Kremenek5744dc22008-04-02 18:03:36 +0000576EXAMPLE
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000577
Ted Kremenek5744dc22008-04-02 18:03:36 +0000578 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000579
Ted Kremenek39eefde2008-04-02 16:47:27 +0000580 The above example causes analysis reports to be deposited into
581 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
582 A different subdirectory is created each time $Prog analyzes a project.
583 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000584
585ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000586}
587
588##----------------------------------------------------------------------------##
589# Process command-line arguments.
590##----------------------------------------------------------------------------##
591
592my $HtmlDir; # Parent directory to store HTML files.
593my $IgnoreErrors = 0; # Ignore build errors.
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000594my $ViewResults = 0; # View results when the build terminates.
Ted Kremenek1262fc42008-05-14 20:10:33 +0000595my $Analysis = "checker-cfref";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000596
597if (!@ARGV) {
598 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000599 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000600}
601
602while (@ARGV) {
603
604 # Scan for options we recognize.
605
606 my $arg = $ARGV[0];
607
Sam Bishop2f2418e2008-04-03 14:29:47 +0000608 if ($arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000609 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000610 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000611 }
612
Ted Kremenek1262fc42008-05-14 20:10:33 +0000613 if ($arg eq "-a") {
614 shift @ARGV;
615
616 if (!@ARGV) {
617 die "$Prog: '-a' option requires an analysis type.\n";
618 }
619
620 $Analysis = shift @ARGV;
621
622 if (!($Analysis eq "checker-cfref" or $Analysis eq "fsyntax-only")) {
623 die "$Prog: Invalid argument '$Analysis' to -a.\n";
624 }
625
626 next;
627 }
628
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000629 if ($arg eq "-o") {
630 shift @ARGV;
631
632 if (!@ARGV) {
Ted Kremenek0062ad42008-04-02 16:35:01 +0000633 die "$Prog: '-o' option requires a target directory name.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000634 }
635
636 $HtmlDir = shift @ARGV;
637 next;
638 }
639
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000640 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000641 shift @ARGV;
642 $IgnoreErrors = 1;
643 next;
644 }
645
646 if ($arg eq "-v") {
647 shift @ARGV;
648 $Verbose++;
649 next;
650 }
651
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000652 if ($arg eq "-V" or $arg eq "--view") {
653 shift @ARGV;
654 $ViewResults = 1;
655 next;
656 }
657
Ted Kremenek0062ad42008-04-02 16:35:01 +0000658 die "$Prog: unrecognized option '$arg'\n" if ($arg =~ /^-/);
659
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000660 last;
661}
662
663if (!@ARGV) {
664 print STDERR "$Prog: No build command specified.\n\n";
665 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000666 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000667}
668
669# Determine the output directory for the HTML reports.
670
671if (!defined($HtmlDir)) {
672
Sam Bishopa0e22662008-04-02 03:35:43 +0000673 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000674
675 if (!defined($HtmlDir)) {
Sam Bishopa0e22662008-04-02 03:35:43 +0000676 die "error: Cannot create HTML directory in /tmp.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000677 }
678
679 if (!$Verbose) {
680 print "$Prog: Using '$HtmlDir' as base HTML report directory.\n";
681 }
682}
683
Ted Kremenek684bb092008-04-18 15:18:20 +0000684my $BaseDir = $HtmlDir;
Sam Bishopa0e22662008-04-02 03:35:43 +0000685$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000686
687# Set the appropriate environment variables.
688
Sam Bishopa0e22662008-04-02 03:35:43 +0000689SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000690
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000691my $Cmd = "$RealBin/ccc-analyzer";
692
693die "$Prog: Executable 'ccc-analyzer' does not exist at '$Cmd'\n"
694 if (! -x $Cmd);
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000695
696my $Clang = "$RealBin/clang";
697
698if (! -x $Clang) {
699 print "$Prog: 'clang' executable not found in '$RealBin'. Using 'clang' from path.\n";
700 $Clang = "clang";
701}
Ted Kremenek0b6c1532008-04-08 20:22:12 +0000702
Ted Kremenek4f4b17d2008-04-03 20:08:18 +0000703$ENV{'CC'} = $Cmd;
Ted Kremenekf22eacb2008-04-18 22:00:56 +0000704$ENV{'CLANG'} = $Clang;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000705
706if ($Verbose >= 2) {
707 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
708}
709
Ted Kremeneka9525c92008-05-12 22:07:14 +0000710if ($Verbose >= 3) {
711 $ENV{'CCC_ANALYZER_LOG'} = 1;
712}
713
Ted Kremenek1262fc42008-05-14 20:10:33 +0000714$ENV{'CCC_ANALYZER_ANALYSIS'} = $Analysis;
715
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000716# Run the build.
717
Ted Kremenek7442ca62008-04-02 16:04:51 +0000718RunBuildCommand(\@ARGV, $IgnoreErrors);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000719
720# Postprocess the HTML directory.
721
Ted Kremenek684bb092008-04-18 15:18:20 +0000722Postprocess($HtmlDir, $BaseDir);
Ted Kremenek7f8a3252008-04-02 18:42:49 +0000723
724if ($ViewResults and -r "$HtmlDir/index.html") {
725 # Only works on Mac OS X (for now).
726 print "Viewing analysis results: '$HtmlDir/index.html'\n";
727 `open $HtmlDir/index.html`
728}