blob: 1f8aefbd88d1a943dae291327ba3fa66825788ba [file] [log] [blame]
Ted Kremenekc9d8fde2008-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 /;
18
19my $Verbose = 0; # Verbose output from this script.
20my $Prog = "scan-build";
21
22##----------------------------------------------------------------------------##
23# GetHTMLRunDir - Construct an HTML directory name for the current run.
24##----------------------------------------------------------------------------##
25
Sam Bishop479982e2008-04-02 03:35:43 +000026sub GetHTMLRunDir {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +000027
28 die "Not enough arguments." if (@_ == 0);
29
30 my $Dir = shift @_;
31
32 # Get current date and time.
33
34 my @CurrentTime = localtime();
35
36 my $year = $CurrentTime[5] + 1900;
37 my $day = $CurrentTime[3];
38 my $month = $CurrentTime[4] + 1;
39
40 my $DateString = "$year-$month-$day";
41
42 # Determine the run number.
43
44 my $RunNumber;
45
46 if (-d $Dir) {
47
48 if (! -r $Dir) {
Sam Bishop479982e2008-04-02 03:35:43 +000049 die "error: '$Dir' exists but is not readable.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +000050 }
51
52 # Iterate over all files in the specified directory.
53
54 my $max = 0;
55
56 opendir(DIR, $Dir);
57 my @FILES= readdir(DIR);
58 closedir(DIR);
59
60 foreach my $f (@FILES) {
61
62 my @x = split/-/, $f;
63
64 next if (scalar(@x) != 4);
65 next if ($x[0] != $year);
66 next if ($x[1] != $month);
67 next if ($x[2] != $day);
68
69 if ($x[3] > $max) {
70 $max = $x[3];
71 }
72 }
73
74 $RunNumber = $max + 1;
75 }
76 else {
77
78 if (-x $Dir) {
Sam Bishop479982e2008-04-02 03:35:43 +000079 die "error: '$Dir' exists but is not a directory.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +000080 }
81
82 # $Dir does not exist. It will be automatically created by the
83 # clang driver. Set the run number to 1.
84
85 $RunNumber = 1;
86 }
87
88 die "RunNumber must be defined!" if (!defined($RunNumber));
89
90 # Append the run number.
91
92 return "$Dir/$DateString-$RunNumber";
93}
94
Sam Bishop479982e2008-04-02 03:35:43 +000095sub SetHtmlEnv {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +000096
97 die "Wrong number of arguments." if (scalar(@_) != 2);
98
99 my $Args = shift;
100 my $Dir = shift;
101
102 die "No build command." if (scalar(@$Args) == 0);
103
104 my $Cmd = $$Args[0];
105
106 if ($Cmd =~ /configure/) {
107 return;
108 }
109
110 if ($Verbose) {
111 print "$Prog: Emitting reports for this run to '$Dir'.\n";
112 }
113
114 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
115}
116
117##----------------------------------------------------------------------------##
Ted Kremeneked757e02008-04-02 18:03:36 +0000118# ScanFile - Scan a report file for various identifying attributes.
119##----------------------------------------------------------------------------##
120
121sub ScanFile {
122
123 my $Index = shift;
124 my $Dir = shift;
125 my $FName = shift;
126
127 open(IN, "$Dir/$FName") or die "$Prog: Cannot open '$Dir/$FName'\n";
128
129 my $BugDesc = "";
130
131 while (<IN>) {
132
133 if (/<!-- BUGDESC (.*) -->$/) {
134 $BugDesc = $1;
135 last;
136 }
137
138 }
139
140 close(IN);
141
142 push @$Index,[ $FName, $BugDesc ];
143}
144
145##----------------------------------------------------------------------------##
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000146# Postprocess - Postprocess the results of an analysis scan.
147##----------------------------------------------------------------------------##
148
Sam Bishop479982e2008-04-02 03:35:43 +0000149sub Postprocess {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000150
151 my $Dir = shift;
152
153 die "No directory specified." if (!defined($Dir));
154
155 if (! -d $Dir) {
156 return;
157 }
158
159 opendir(DIR, $Dir);
160 my @files = grep(/^report-.*\.html$/,readdir(DIR));
161 closedir(DIR);
162
163 if (scalar(@files) == 0) {
Ted Kremenek4efca192008-04-02 07:05:07 +0000164 print "$Prog: Removing directory '$Dir' because it contains no reports.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000165 `rm -fR $Dir`;
166 return;
167 }
Ted Kremeneked757e02008-04-02 18:03:36 +0000168
169 # Scan each report file and build an index.
170
171 my @Index;
172
173 foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); }
174
175 # Generate an index.html file.
176
177 my $FName = "$Dir/index.html";
178
179 open(OUT, ">$FName") or die "$Prog: Cannot create file '$FName'\n";
180
181print OUT <<ENDTEXT;
182<html>
183<head>
184</head>\n<body>
185<table class="reports">
186ENDTEXT
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000187
Ted Kremeneked757e02008-04-02 18:03:36 +0000188 for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) {
189
190 print OUT "<tr>\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000191
Ted Kremeneked757e02008-04-02 18:03:36 +0000192 my $ReportFile = $row->[0];
193
194 print OUT " <td class=\"DESC\">$row->[1]</td>\n";
195
196 for my $j ( 2 .. $#{$row} ) {
197 print OUT "<td>$row->[$j]</td>\n"
198 }
199
200# print OUT "<td><input type=\"button\" value=\"View\"></td>\n";
201 print OUT "<td><a href=\"$ReportFile#EndPath\">View</a></td>\n";
202 print OUT "</tr>\n";
203 }
204
205 print OUT "</table>\n</body></html>\n";
206 close(OUT);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000207}
208
209##----------------------------------------------------------------------------##
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000210# RunBuildCommand - Run the build command.
211##----------------------------------------------------------------------------##
212
213sub RunBuildCommand {
214
215 my $Args = shift;
Ted Kremenek091f9372008-04-02 16:04:51 +0000216 my $IgnoreErrors = shift;
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000217 my $Cmd = $Args->[0];
218
Ted Kremenek404cb262008-04-02 15:34:12 +0000219 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000220 shift @$Args;
221 unshift @$Args, "ccc-analyzer"
222 }
Ted Kremenek091f9372008-04-02 16:04:51 +0000223 elsif ($IgnoreErrors) {
224 if ($Cmd eq "make" or $Cmd eq "gmake") {
225 push @$Args, "-k";
226 }
227 elsif ($Cmd eq "xcodebuild") {
228 push @$Args, "-PBXBuildsContinueAfterErrors=YES";
229 }
230 }
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000231
232 system(@$Args);
233}
234
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000235##----------------------------------------------------------------------------##
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000236# DisplayHelp - Utility function to display all help options.
237##----------------------------------------------------------------------------##
238
Sam Bishop479982e2008-04-02 03:35:43 +0000239sub DisplayHelp {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000240
Ted Kremeneked757e02008-04-02 18:03:36 +0000241print <<ENDTEXT;
Sam Bishop479982e2008-04-02 03:35:43 +0000242USAGE: $Prog [options] <build command> [build options]
Ted Kremenekb9177df2008-04-01 21:22:03 +0000243
244OPTIONS:
245
246 -o - Target directory for HTML report files. Subdirectories
Sam Bishop479982e2008-04-02 03:35:43 +0000247 will be created as needed to represent separate "runs" of
Ted Kremenekb9177df2008-04-01 21:22:03 +0000248 the analyzer. If this option is not specified, a directory
249 is created in /tmp to store the reports.
250
Sam Bishop479982e2008-04-02 03:35:43 +0000251 -?, -h - Display this message.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000252 --help
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000253
Ted Kremenekd1843772008-04-02 16:31:58 +0000254 -k - Add a "keep on going" option to the specified build command.
Ted Kremenek707a9ad2008-04-02 16:41:25 +0000255 --keep-going This option currently supports make and xcodebuild.
256 This is a convenience option; one can specify this
257 behavior directly using build options.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000258
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000259 -v - Verbose output from $Prog and the analyzer.
260 A second "-v" increases verbosity.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000261
262BUILD OPTIONS
263
Ted Kremenekf76812d2008-04-02 16:47:27 +0000264 You can specify any build option acceptable to the build command.
265
Ted Kremeneked757e02008-04-02 18:03:36 +0000266EXAMPLE
Ted Kremenekb9177df2008-04-01 21:22:03 +0000267
Ted Kremeneked757e02008-04-02 18:03:36 +0000268 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenekb9177df2008-04-01 21:22:03 +0000269
Ted Kremenekf76812d2008-04-02 16:47:27 +0000270 The above example causes analysis reports to be deposited into
271 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
272 A different subdirectory is created each time $Prog analyzes a project.
273 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000274
275ENDTEXT
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000276}
277
278##----------------------------------------------------------------------------##
279# Process command-line arguments.
280##----------------------------------------------------------------------------##
281
282my $HtmlDir; # Parent directory to store HTML files.
283my $IgnoreErrors = 0; # Ignore build errors.
284
285if (!@ARGV) {
286 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000287 exit 1;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000288}
289
290while (@ARGV) {
291
292 # Scan for options we recognize.
293
294 my $arg = $ARGV[0];
295
Sam Bishop479982e2008-04-02 03:35:43 +0000296 if ($arg eq "-?" or $arg eq "-h" or $arg eq "--help") {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000297 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000298 exit 0;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000299 }
300
301 if ($arg eq "-o") {
302 shift @ARGV;
303
304 if (!@ARGV) {
Ted Kremenek78965de2008-04-02 16:35:01 +0000305 die "$Prog: '-o' option requires a target directory name.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000306 }
307
308 $HtmlDir = shift @ARGV;
309 next;
310 }
311
Ted Kremenekb9177df2008-04-01 21:22:03 +0000312 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000313 shift @ARGV;
314 $IgnoreErrors = 1;
315 next;
316 }
317
318 if ($arg eq "-v") {
319 shift @ARGV;
320 $Verbose++;
321 next;
322 }
323
Ted Kremenek78965de2008-04-02 16:35:01 +0000324 die "$Prog: unrecognized option '$arg'\n" if ($arg =~ /^-/);
325
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000326 last;
327}
328
329if (!@ARGV) {
330 print STDERR "$Prog: No build command specified.\n\n";
331 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000332 exit 1;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000333}
334
335# Determine the output directory for the HTML reports.
336
337if (!defined($HtmlDir)) {
338
Sam Bishop479982e2008-04-02 03:35:43 +0000339 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000340
341 if (!defined($HtmlDir)) {
Sam Bishop479982e2008-04-02 03:35:43 +0000342 die "error: Cannot create HTML directory in /tmp.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000343 }
344
345 if (!$Verbose) {
346 print "$Prog: Using '$HtmlDir' as base HTML report directory.\n";
347 }
348}
349
Sam Bishop479982e2008-04-02 03:35:43 +0000350$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000351
352# Set the appropriate environment variables.
353
Sam Bishop479982e2008-04-02 03:35:43 +0000354SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000355
356$ENV{'CC'} = "ccc-analyzer";
357
358if ($Verbose >= 2) {
359 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
360}
361
362# Run the build.
363
Ted Kremenek091f9372008-04-02 16:04:51 +0000364RunBuildCommand(\@ARGV, $IgnoreErrors);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000365
366# Postprocess the HTML directory.
367
Sam Bishop479982e2008-04-02 03:35:43 +0000368Postprocess($HtmlDir);