blob: da7a07f9170d15200b2f6ee484e031f07c9739e2 [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 /;
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
26sub GetHTMLRunDir() {
27
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) {
49 print STDERR "error: '$Dir' exists but is not readable.\n";
50 exit 0;
51 }
52
53 # Iterate over all files in the specified directory.
54
55 my $max = 0;
56
57 opendir(DIR, $Dir);
58 my @FILES= readdir(DIR);
59 closedir(DIR);
60
61 foreach my $f (@FILES) {
62
63 my @x = split/-/, $f;
64
65 next if (scalar(@x) != 4);
66 next if ($x[0] != $year);
67 next if ($x[1] != $month);
68 next if ($x[2] != $day);
69
70 if ($x[3] > $max) {
71 $max = $x[3];
72 }
73 }
74
75 $RunNumber = $max + 1;
76 }
77 else {
78
79 if (-x $Dir) {
80 print STDERR "error: '$Dir' exists but is not a directory.\n";
81 exit 0;
82 }
83
84 # $Dir does not exist. It will be automatically created by the
85 # clang driver. Set the run number to 1.
86
87 $RunNumber = 1;
88 }
89
90 die "RunNumber must be defined!" if (!defined($RunNumber));
91
92 # Append the run number.
93
94 return "$Dir/$DateString-$RunNumber";
95}
96
97sub SetHtmlEnv() {
98
99 die "Wrong number of arguments." if (scalar(@_) != 2);
100
101 my $Args = shift;
102 my $Dir = shift;
103
104 die "No build command." if (scalar(@$Args) == 0);
105
106 my $Cmd = $$Args[0];
107
108 if ($Cmd =~ /configure/) {
109 return;
110 }
111
112 if ($Verbose) {
113 print "$Prog: Emitting reports for this run to '$Dir'.\n";
114 }
115
116 $ENV{'CCC_ANALYZER_HTML'} = $Dir;
117}
118
119##----------------------------------------------------------------------------##
120# Postprocess - Postprocess the results of an analysis scan.
121##----------------------------------------------------------------------------##
122
123sub Postprocess() {
124
125 my $Dir = shift;
126
127 die "No directory specified." if (!defined($Dir));
128
129 if (! -d $Dir) {
130 return;
131 }
132
133 opendir(DIR, $Dir);
134 my @files = grep(/^report-.*\.html$/,readdir(DIR));
135 closedir(DIR);
136
137 if (scalar(@files) == 0) {
138 print "$Prog: Remove directory '$Dir' because it contains no reports.\n";
139 `rm -fR $Dir`;
140 return;
141 }
142
143
144}
145
146##----------------------------------------------------------------------------##
147# DisplayHelp - Utility function to display all help options.
148##----------------------------------------------------------------------------##
149
150sub DisplayHelp() {
151
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000152print <<ENDTEXT
153USAGE: scan-build [options] <build command> [build options]
154
155OPTIONS:
156
157 -o - Target directory for HTML report files. Subdirectories
158 Will be created as needed to represent separate "runs" of
159 the analyzer. If this option is not specified, a directory
160 is created in /tmp to store the reports.
161
162 -? - Display this message.
163 --help
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000164
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000165 -k - Add "keep on going option" to the specified build command.
166 --keep-going This command currently supports "make" and "xcodebuild." You
Ted Kremenekc0520462008-04-01 21:36:28 +0000167 can also directly specify the corresponding option to the build command.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000168
169 -v - Verbose output from scan-build and the analyzer. A second
170 "-v" increases verbosity.
171
172
173BUILD OPTIONS
174
175 You can specify any build option acceptable to the build command. For example:
176
177 scan-build -o /tmp/myhtmldir make -j4
178
179 The above causes analysis reports to be deposited in /tmp/myhtmldir (or
180 rather a subdirectory corresponding to this particular running of the
181 analyzer), and causes "make" to be run with the "-j4" option, allowing
182 parallel builds (and parallel invocations of the analyzer).
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000183
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000184 NOTE: The analyzer will work for most parallel builds, but not distributed
185 builds (such as using distcc).
186
187ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000188}
189
190##----------------------------------------------------------------------------##
191# Process command-line arguments.
192##----------------------------------------------------------------------------##
193
194my $HtmlDir; # Parent directory to store HTML files.
195my $IgnoreErrors = 0; # Ignore build errors.
196
197if (!@ARGV) {
198 DisplayHelp();
199 exit 1
200}
201
202while (@ARGV) {
203
204 # Scan for options we recognize.
205
206 my $arg = $ARGV[0];
207
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000208 if ($arg eq "-?" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000209 DisplayHelp();
210 exit 1;
211 }
212
213 if ($arg eq "-o") {
214 shift @ARGV;
215
216 if (!@ARGV) {
217 print STDERR "'-o' option requires a target directory name.";
218 exit 0;
219 }
220
221 $HtmlDir = shift @ARGV;
222 next;
223 }
224
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000225 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000226 shift @ARGV;
227 $IgnoreErrors = 1;
228 next;
229 }
230
231 if ($arg eq "-v") {
232 shift @ARGV;
233 $Verbose++;
234 next;
235 }
236
237 last;
238}
239
240if (!@ARGV) {
241 print STDERR "$Prog: No build command specified.\n\n";
242 DisplayHelp();
243 exit 0;
244}
245
246# Determine the output directory for the HTML reports.
247
248if (!defined($HtmlDir)) {
249
250 $HtmlDir = mkdtemp("/tmp/scan-build-XXXXXX");
251
252 if (!defined($HtmlDir)) {
253 print STDERR "error: Cannot create HTML directory in /tmp.\n";
254 exit 0;
255 }
256
257 if (!$Verbose) {
258 print "$Prog: Using '$HtmlDir' as base HTML report directory.\n";
259 }
260}
261
262$HtmlDir = &GetHTMLRunDir($HtmlDir);
263
264# Set the appropriate environment variables.
265
266&SetHtmlEnv(\@ARGV, $HtmlDir);
267
268$ENV{'CC'} = "ccc-analyzer";
269
270if ($Verbose >= 2) {
271 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
272}
273
274# Run the build.
275
276system(@ARGV);
277
278# Postprocess the HTML directory.
279
280&Postprocess($HtmlDir);