blob: 59b0f5f236225f4fb7748b68597aced8cf55865d [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##----------------------------------------------------------------------------##
118# Postprocess - Postprocess the results of an analysis scan.
119##----------------------------------------------------------------------------##
120
Sam Bishop479982e2008-04-02 03:35:43 +0000121sub Postprocess {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000122
123 my $Dir = shift;
124
125 die "No directory specified." if (!defined($Dir));
126
127 if (! -d $Dir) {
128 return;
129 }
130
131 opendir(DIR, $Dir);
132 my @files = grep(/^report-.*\.html$/,readdir(DIR));
133 closedir(DIR);
134
135 if (scalar(@files) == 0) {
Ted Kremenek4efca192008-04-02 07:05:07 +0000136 print "$Prog: Removing directory '$Dir' because it contains no reports.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000137 `rm -fR $Dir`;
138 return;
139 }
140
141
142}
143
144##----------------------------------------------------------------------------##
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000145# RunBuildCommand - Run the build command.
146##----------------------------------------------------------------------------##
147
148sub RunBuildCommand {
149
150 my $Args = shift;
151 my $Cmd = $Args->[0];
152
153 if ($Cmd =~ /gcc/ or $Cmd eq "cc" or $Cmd =~ /"llvm-gcc"/) {
154 shift @$Args;
155 unshift @$Args, "ccc-analyzer"
156 }
157
158 system(@$Args);
159}
160
161
162##----------------------------------------------------------------------------##
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000163# DisplayHelp - Utility function to display all help options.
164##----------------------------------------------------------------------------##
165
Sam Bishop479982e2008-04-02 03:35:43 +0000166sub DisplayHelp {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000167
Ted Kremenekb9177df2008-04-01 21:22:03 +0000168print <<ENDTEXT
Sam Bishop479982e2008-04-02 03:35:43 +0000169USAGE: $Prog [options] <build command> [build options]
Ted Kremenekb9177df2008-04-01 21:22:03 +0000170
171OPTIONS:
172
173 -o - Target directory for HTML report files. Subdirectories
Sam Bishop479982e2008-04-02 03:35:43 +0000174 will be created as needed to represent separate "runs" of
Ted Kremenekb9177df2008-04-01 21:22:03 +0000175 the analyzer. If this option is not specified, a directory
176 is created in /tmp to store the reports.
177
Sam Bishop479982e2008-04-02 03:35:43 +0000178 -?, -h - Display this message.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000179 --help
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000180
Ted Kremenekb9177df2008-04-01 21:22:03 +0000181 -k - Add "keep on going option" to the specified build command.
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000182 --keep-going This command currently supports make and xcodebuild.
183 This is a helper option; one can specify the arguments
184 directly as build options.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000185
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000186 -v - Verbose output from $Prog and the analyzer.
187 A second "-v" increases verbosity.
Ted Kremenekb9177df2008-04-01 21:22:03 +0000188
189BUILD OPTIONS
190
Sam Bishop479982e2008-04-02 03:35:43 +0000191 You can specify any build option acceptable to the build command. For
192 example:
Ted Kremenekb9177df2008-04-01 21:22:03 +0000193
Sam Bishop479982e2008-04-02 03:35:43 +0000194 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenekb9177df2008-04-01 21:22:03 +0000195
196 The above causes analysis reports to be deposited in /tmp/myhtmldir (or
197 rather a subdirectory corresponding to this particular running of the
198 analyzer), and causes "make" to be run with the "-j4" option, allowing
199 parallel builds (and parallel invocations of the analyzer).
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000200
Ted Kremenekb9177df2008-04-01 21:22:03 +0000201 NOTE: The analyzer will work for most parallel builds, but not distributed
202 builds (such as using distcc).
203
204ENDTEXT
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000205}
206
207##----------------------------------------------------------------------------##
208# Process command-line arguments.
209##----------------------------------------------------------------------------##
210
211my $HtmlDir; # Parent directory to store HTML files.
212my $IgnoreErrors = 0; # Ignore build errors.
213
214if (!@ARGV) {
215 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000216 exit 1;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000217}
218
219while (@ARGV) {
220
221 # Scan for options we recognize.
222
223 my $arg = $ARGV[0];
224
Sam Bishop479982e2008-04-02 03:35:43 +0000225 if ($arg eq "-?" or $arg eq "-h" or $arg eq "--help") {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000226 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000227 exit 0;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000228 }
229
230 if ($arg eq "-o") {
231 shift @ARGV;
232
233 if (!@ARGV) {
Sam Bishop479982e2008-04-02 03:35:43 +0000234 die "'-o' option requires a target directory name.";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000235 }
236
237 $HtmlDir = shift @ARGV;
238 next;
239 }
240
Ted Kremenekb9177df2008-04-01 21:22:03 +0000241 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000242 shift @ARGV;
243 $IgnoreErrors = 1;
244 next;
245 }
246
247 if ($arg eq "-v") {
248 shift @ARGV;
249 $Verbose++;
250 next;
251 }
252
253 last;
254}
255
256if (!@ARGV) {
257 print STDERR "$Prog: No build command specified.\n\n";
258 DisplayHelp();
Sam Bishop479982e2008-04-02 03:35:43 +0000259 exit 1;
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000260}
261
262# Determine the output directory for the HTML reports.
263
264if (!defined($HtmlDir)) {
265
Sam Bishop479982e2008-04-02 03:35:43 +0000266 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000267
268 if (!defined($HtmlDir)) {
Sam Bishop479982e2008-04-02 03:35:43 +0000269 die "error: Cannot create HTML directory in /tmp.\n";
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000270 }
271
272 if (!$Verbose) {
273 print "$Prog: Using '$HtmlDir' as base HTML report directory.\n";
274 }
275}
276
Sam Bishop479982e2008-04-02 03:35:43 +0000277$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000278
279# Set the appropriate environment variables.
280
Sam Bishop479982e2008-04-02 03:35:43 +0000281SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000282
283$ENV{'CC'} = "ccc-analyzer";
284
285if ($Verbose >= 2) {
286 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
287}
288
289# Run the build.
290
Ted Kremenekd6c03a82008-04-02 04:43:42 +0000291RunBuildCommand(\@ARGV);
Ted Kremenekc9d8fde2008-04-01 20:47:38 +0000292
293# Postprocess the HTML directory.
294
Sam Bishop479982e2008-04-02 03:35:43 +0000295Postprocess($HtmlDir);