| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 1 | #!/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 |  | 
|  | 15 | use strict; | 
|  | 16 | use warnings; | 
|  | 17 | use File::Temp qw/ :mktemp /; | 
|  | 18 |  | 
|  | 19 | my $Verbose = 0;       # Verbose output from this script. | 
|  | 20 | my $Prog = "scan-build"; | 
|  | 21 |  | 
|  | 22 | ##----------------------------------------------------------------------------## | 
|  | 23 | # GetHTMLRunDir - Construct an HTML directory name for the current run. | 
|  | 24 | ##----------------------------------------------------------------------------## | 
|  | 25 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 26 | sub GetHTMLRunDir { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 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) { | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 49 | die "error: '$Dir' exists but is not readable.\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 50 | } | 
|  | 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 Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 79 | die "error: '$Dir' exists but is not a directory.\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 80 | } | 
|  | 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 Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 95 | sub SetHtmlEnv { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 96 |  | 
|  | 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 Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 121 | sub Postprocess { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 122 |  | 
|  | 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 Kremenek | 0249378 | 2008-04-02 07:05:07 +0000 | [diff] [blame] | 136 | print "$Prog: Removing directory '$Dir' because it contains no reports.\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 137 | `rm -fR $Dir`; | 
|  | 138 | return; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 |  | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 145 | # RunBuildCommand - Run the build command. | 
|  | 146 | ##----------------------------------------------------------------------------## | 
|  | 147 |  | 
|  | 148 | sub RunBuildCommand { | 
|  | 149 |  | 
|  | 150 | my $Args = shift; | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame^] | 151 | my $IgnoreErrors = shift; | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 152 | my $Cmd = $Args->[0]; | 
|  | 153 |  | 
| Ted Kremenek | 6a43ba9 | 2008-04-02 15:34:12 +0000 | [diff] [blame] | 154 | if ($Cmd eq "gcc" or $Cmd eq "cc" or $Cmd eq "llvm-gcc") { | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 155 | shift @$Args; | 
|  | 156 | unshift @$Args, "ccc-analyzer" | 
|  | 157 | } | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame^] | 158 | 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 Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 166 |  | 
|  | 167 | system(@$Args); | 
|  | 168 | } | 
|  | 169 |  | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 170 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 171 | # DisplayHelp - Utility function to display all help options. | 
|  | 172 | ##----------------------------------------------------------------------------## | 
|  | 173 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 174 | sub DisplayHelp { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 175 |  | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 176 | print <<ENDTEXT | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 177 | USAGE: $Prog [options] <build command> [build options] | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 178 |  | 
|  | 179 | OPTIONS: | 
|  | 180 |  | 
|  | 181 | -o            - Target directory for HTML report files.  Subdirectories | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 182 | will be created as needed to represent separate "runs" of | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 183 | the analyzer.  If this option is not specified, a directory | 
|  | 184 | is created in /tmp to store the reports. | 
|  | 185 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 186 | -?, -h        - Display this message. | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 187 | --help | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 188 |  | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 189 | -k            - Add "keep on going option" to the specified build command. | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 190 | --keep-going    This command currently supports make and xcodebuild. | 
|  | 191 | This is a helper option; one can specify the arguments | 
|  | 192 | directly as build options. | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 193 |  | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 194 | -v            - Verbose output from $Prog and the analyzer. | 
|  | 195 | A second "-v" increases verbosity. | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 196 |  | 
|  | 197 | BUILD OPTIONS | 
|  | 198 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 199 | You can specify any build option acceptable to the build command.  For | 
|  | 200 | example: | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 201 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 202 | $Prog -o /tmp/myhtmldir make -j4 | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 203 |  | 
|  | 204 | The above causes analysis reports to be deposited in /tmp/myhtmldir (or | 
|  | 205 | rather a subdirectory corresponding to this particular running of the | 
|  | 206 | analyzer), and causes "make" to be run with the "-j4" option, allowing | 
|  | 207 | parallel builds (and parallel invocations of the analyzer). | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 208 |  | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 209 | NOTE: The analyzer will work for most parallel builds, but not distributed | 
|  | 210 | builds (such as using distcc). | 
|  | 211 |  | 
|  | 212 | ENDTEXT | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 213 | } | 
|  | 214 |  | 
|  | 215 | ##----------------------------------------------------------------------------## | 
|  | 216 | # Process command-line arguments. | 
|  | 217 | ##----------------------------------------------------------------------------## | 
|  | 218 |  | 
|  | 219 | my $HtmlDir;           # Parent directory to store HTML files. | 
|  | 220 | my $IgnoreErrors = 0;  # Ignore build errors. | 
|  | 221 |  | 
|  | 222 | if (!@ARGV) { | 
|  | 223 | DisplayHelp(); | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 224 | exit 1; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 225 | } | 
|  | 226 |  | 
|  | 227 | while (@ARGV) { | 
|  | 228 |  | 
|  | 229 | # Scan for options we recognize. | 
|  | 230 |  | 
|  | 231 | my $arg = $ARGV[0]; | 
|  | 232 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 233 | if ($arg eq "-?" or $arg eq "-h" or $arg eq "--help") { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 234 | DisplayHelp(); | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 235 | exit 0; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 236 | } | 
|  | 237 |  | 
|  | 238 | if ($arg eq "-o") { | 
|  | 239 | shift @ARGV; | 
|  | 240 |  | 
|  | 241 | if (!@ARGV) { | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 242 | die "'-o' option requires a target directory name."; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 243 | } | 
|  | 244 |  | 
|  | 245 | $HtmlDir = shift @ARGV; | 
|  | 246 | next; | 
|  | 247 | } | 
|  | 248 |  | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 249 | if ($arg eq "-k" or $arg eq "--keep-going") { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 250 | shift @ARGV; | 
|  | 251 | $IgnoreErrors = 1; | 
|  | 252 | next; | 
|  | 253 | } | 
|  | 254 |  | 
|  | 255 | if ($arg eq "-v") { | 
|  | 256 | shift @ARGV; | 
|  | 257 | $Verbose++; | 
|  | 258 | next; | 
|  | 259 | } | 
|  | 260 |  | 
|  | 261 | last; | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | if (!@ARGV) { | 
|  | 265 | print STDERR "$Prog: No build command specified.\n\n"; | 
|  | 266 | DisplayHelp(); | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 267 | exit 1; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 268 | } | 
|  | 269 |  | 
|  | 270 | # Determine the output directory for the HTML reports. | 
|  | 271 |  | 
|  | 272 | if (!defined($HtmlDir)) { | 
|  | 273 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 274 | $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX"); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 275 |  | 
|  | 276 | if (!defined($HtmlDir)) { | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 277 | die "error: Cannot create HTML directory in /tmp.\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 278 | } | 
|  | 279 |  | 
|  | 280 | if (!$Verbose) { | 
|  | 281 | print "$Prog: Using '$HtmlDir' as base HTML report directory.\n"; | 
|  | 282 | } | 
|  | 283 | } | 
|  | 284 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 285 | $HtmlDir = GetHTMLRunDir($HtmlDir); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 286 |  | 
|  | 287 | # Set the appropriate environment variables. | 
|  | 288 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 289 | SetHtmlEnv(\@ARGV, $HtmlDir); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 290 |  | 
|  | 291 | $ENV{'CC'} = "ccc-analyzer"; | 
|  | 292 |  | 
|  | 293 | if ($Verbose >= 2) { | 
|  | 294 | $ENV{'CCC_ANALYZER_VERBOSE'} = 1; | 
|  | 295 | } | 
|  | 296 |  | 
|  | 297 | # Run the build. | 
|  | 298 |  | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame^] | 299 | RunBuildCommand(\@ARGV, $IgnoreErrors); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 300 |  | 
|  | 301 | # Postprocess the HTML directory. | 
|  | 302 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 303 | Postprocess($HtmlDir); |