blob: 357cfccb69a4a5229eb6304fd8aff5f29f8ba48f [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
Sam Bishopa0e22662008-04-02 03:35:43 +000026sub GetHTMLRunDir {
Ted Kremenek9cc8c2c2008-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 Bishopa0e22662008-04-02 03:35:43 +000049 die "error: '$Dir' exists but is not readable.\n";
Ted Kremenek9cc8c2c2008-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 Bishopa0e22662008-04-02 03:35:43 +000079 die "error: '$Dir' exists but is not a directory.\n";
Ted Kremenek9cc8c2c2008-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 Bishopa0e22662008-04-02 03:35:43 +000095sub SetHtmlEnv {
Ted Kremenek9cc8c2c2008-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 Bishopa0e22662008-04-02 03:35:43 +0000121sub Postprocess {
Ted Kremenek9cc8c2c2008-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 Kremenek02493782008-04-02 07:05:07 +0000136 print "$Prog: Removing directory '$Dir' because it contains no reports.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000137 `rm -fR $Dir`;
138 return;
139 }
140
141
142}
143
144##----------------------------------------------------------------------------##
Ted Kremenekdab11102008-04-02 04:43:42 +0000145# RunBuildCommand - Run the build command.
146##----------------------------------------------------------------------------##
147
148sub RunBuildCommand {
149
150 my $Args = shift;
Ted Kremenek7442ca62008-04-02 16:04:51 +0000151 my $IgnoreErrors = shift;
Ted Kremenekdab11102008-04-02 04:43:42 +0000152 my $Cmd = $Args->[0];
153
Ted Kremenek6a43ba92008-04-02 15:34:12 +0000154 if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") {
Ted Kremenekdab11102008-04-02 04:43:42 +0000155 shift @$Args;
156 unshift @$Args, "ccc-analyzer"
157 }
Ted Kremenek7442ca62008-04-02 16:04:51 +0000158 elsif ($IgnoreErrors) {
159 if ($Cmd eq "make" or $Cmd eq "gmake") {
160 push @$Args, "-k";
161 }
162 elsif ($Cmd eq "xcodebuild") {
163 push @$Args, "-PBXBuildsContinueAfterErrors=YES";
164 }
165 }
Ted Kremenekdab11102008-04-02 04:43:42 +0000166
167 system(@$Args);
168}
169
Ted Kremenekdab11102008-04-02 04:43:42 +0000170##----------------------------------------------------------------------------##
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000171# DisplayHelp - Utility function to display all help options.
172##----------------------------------------------------------------------------##
173
Sam Bishopa0e22662008-04-02 03:35:43 +0000174sub DisplayHelp {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000175
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000176print <<ENDTEXT
Sam Bishopa0e22662008-04-02 03:35:43 +0000177USAGE: $Prog [options] <build command> [build options]
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000178
179OPTIONS:
180
181 -o - Target directory for HTML report files. Subdirectories
Sam Bishopa0e22662008-04-02 03:35:43 +0000182 will be created as needed to represent separate "runs" of
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000183 the analyzer. If this option is not specified, a directory
184 is created in /tmp to store the reports.
185
Sam Bishopa0e22662008-04-02 03:35:43 +0000186 -?, -h - Display this message.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000187 --help
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000188
Ted Kremenekaf08f642008-04-02 16:31:58 +0000189 -k - Add a "keep on going" option to the specified build command.
Ted Kremenekf02e8db2008-04-02 16:41:25 +0000190 --keep-going This option currently supports make and xcodebuild.
191 This is a convenience option; one can specify this
192 behavior directly using build options.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000193
Ted Kremenekdab11102008-04-02 04:43:42 +0000194 -v - Verbose output from $Prog and the analyzer.
195 A second "-v" increases verbosity.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000196
197BUILD OPTIONS
198
Ted Kremenek39eefde2008-04-02 16:47:27 +0000199 You can specify any build option acceptable to the build command.
200
201 For example:
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000202
Sam Bishopa0e22662008-04-02 03:35:43 +0000203 $Prog -o /tmp/myhtmldir make -j4
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000204
Ted Kremenek39eefde2008-04-02 16:47:27 +0000205 The above example causes analysis reports to be deposited into
206 a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option.
207 A different subdirectory is created each time $Prog analyzes a project.
208 The analyzer should support most parallel builds, but not distributed builds.
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000209
210ENDTEXT
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000211}
212
213##----------------------------------------------------------------------------##
214# Process command-line arguments.
215##----------------------------------------------------------------------------##
216
217my $HtmlDir; # Parent directory to store HTML files.
218my $IgnoreErrors = 0; # Ignore build errors.
219
220if (!@ARGV) {
221 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000222 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000223}
224
225while (@ARGV) {
226
227 # Scan for options we recognize.
228
229 my $arg = $ARGV[0];
230
Sam Bishopa0e22662008-04-02 03:35:43 +0000231 if ($arg eq "-?" or $arg eq "-h" or $arg eq "--help") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000232 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000233 exit 0;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000234 }
235
236 if ($arg eq "-o") {
237 shift @ARGV;
238
239 if (!@ARGV) {
Ted Kremenek0062ad42008-04-02 16:35:01 +0000240 die "$Prog: '-o' option requires a target directory name.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000241 }
242
243 $HtmlDir = shift @ARGV;
244 next;
245 }
246
Ted Kremenek2b74ab62008-04-01 21:22:03 +0000247 if ($arg eq "-k" or $arg eq "--keep-going") {
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000248 shift @ARGV;
249 $IgnoreErrors = 1;
250 next;
251 }
252
253 if ($arg eq "-v") {
254 shift @ARGV;
255 $Verbose++;
256 next;
257 }
258
Ted Kremenek0062ad42008-04-02 16:35:01 +0000259 die "$Prog: unrecognized option '$arg'\n" if ($arg =~ /^-/);
260
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000261 last;
262}
263
264if (!@ARGV) {
265 print STDERR "$Prog: No build command specified.\n\n";
266 DisplayHelp();
Sam Bishopa0e22662008-04-02 03:35:43 +0000267 exit 1;
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000268}
269
270# Determine the output directory for the HTML reports.
271
272if (!defined($HtmlDir)) {
273
Sam Bishopa0e22662008-04-02 03:35:43 +0000274 $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX");
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000275
276 if (!defined($HtmlDir)) {
Sam Bishopa0e22662008-04-02 03:35:43 +0000277 die "error: Cannot create HTML directory in /tmp.\n";
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000278 }
279
280 if (!$Verbose) {
281 print "$Prog: Using '$HtmlDir' as base HTML report directory.\n";
282 }
283}
284
Sam Bishopa0e22662008-04-02 03:35:43 +0000285$HtmlDir = GetHTMLRunDir($HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000286
287# Set the appropriate environment variables.
288
Sam Bishopa0e22662008-04-02 03:35:43 +0000289SetHtmlEnv(\@ARGV, $HtmlDir);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000290
291$ENV{'CC'} = "ccc-analyzer";
292
293if ($Verbose >= 2) {
294 $ENV{'CCC_ANALYZER_VERBOSE'} = 1;
295}
296
297# Run the build.
298
Ted Kremenek7442ca62008-04-02 16:04:51 +0000299RunBuildCommand(\@ARGV, $IgnoreErrors);
Ted Kremenek9cc8c2c2008-04-01 20:47:38 +0000300
301# Postprocess the HTML directory.
302
Sam Bishopa0e22662008-04-02 03:35:43 +0000303Postprocess($HtmlDir);