| 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 /; | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 18 | use FindBin qw($RealBin); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 19 |  | 
|  | 20 | my $Verbose = 0;       # Verbose output from this script. | 
|  | 21 | my $Prog = "scan-build"; | 
|  | 22 |  | 
|  | 23 | ##----------------------------------------------------------------------------## | 
|  | 24 | # GetHTMLRunDir - Construct an HTML directory name for the current run. | 
|  | 25 | ##----------------------------------------------------------------------------## | 
|  | 26 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 27 | sub GetHTMLRunDir { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 28 |  | 
|  | 29 | die "Not enough arguments." if (@_ == 0); | 
|  | 30 |  | 
|  | 31 | my $Dir = shift @_; | 
|  | 32 |  | 
|  | 33 | # Get current date and time. | 
|  | 34 |  | 
|  | 35 | my @CurrentTime = localtime(); | 
|  | 36 |  | 
|  | 37 | my $year  = $CurrentTime[5] + 1900; | 
|  | 38 | my $day   = $CurrentTime[3]; | 
|  | 39 | my $month = $CurrentTime[4] + 1; | 
|  | 40 |  | 
|  | 41 | my $DateString = "$year-$month-$day"; | 
|  | 42 |  | 
|  | 43 | # Determine the run number. | 
|  | 44 |  | 
|  | 45 | my $RunNumber; | 
|  | 46 |  | 
|  | 47 | if (-d $Dir) { | 
|  | 48 |  | 
|  | 49 | if (! -r $Dir) { | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 50 | die "error: '$Dir' exists but is not readable.\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 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) { | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 80 | die "error: '$Dir' exists but is not a directory.\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
|  | 83 | # $Dir does not exist.  It will be automatically created by the | 
|  | 84 | # clang driver.  Set the run number to 1. | 
|  | 85 |  | 
|  | 86 | $RunNumber = 1; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | die "RunNumber must be defined!" if (!defined($RunNumber)); | 
|  | 90 |  | 
|  | 91 | # Append the run number. | 
|  | 92 |  | 
|  | 93 | return "$Dir/$DateString-$RunNumber"; | 
|  | 94 | } | 
|  | 95 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 96 | sub SetHtmlEnv { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 97 |  | 
|  | 98 | die "Wrong number of arguments." if (scalar(@_) != 2); | 
|  | 99 |  | 
|  | 100 | my $Args = shift; | 
|  | 101 | my $Dir = shift; | 
|  | 102 |  | 
|  | 103 | die "No build command." if (scalar(@$Args) == 0); | 
|  | 104 |  | 
|  | 105 | my $Cmd = $$Args[0]; | 
|  | 106 |  | 
|  | 107 | if ($Cmd =~ /configure/) { | 
|  | 108 | return; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | if ($Verbose) { | 
|  | 112 | print "$Prog: Emitting reports for this run to '$Dir'.\n"; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | $ENV{'CCC_ANALYZER_HTML'} = $Dir; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 119 | # ComputeDigest - Compute a digest of the specified file. | 
|  | 120 | ##----------------------------------------------------------------------------## | 
|  | 121 |  | 
|  | 122 | sub ComputeDigest { | 
|  | 123 | my $FName = shift; | 
|  | 124 | die "Cannot read $FName" if (! -r $FName); | 
|  | 125 | my $Result = `sha1sum -b $FName`; | 
|  | 126 | my @Output = split /\s+/,$Result; | 
|  | 127 | die "Bad output from sha1sum" if (scalar(@Output) != 2); | 
|  | 128 | return $Output[0]; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 132 | # ScanFile - Scan a report file for various identifying attributes. | 
|  | 133 | ##----------------------------------------------------------------------------## | 
|  | 134 |  | 
| Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 135 | # Sometimes a source file is scanned more than once, and thus produces | 
|  | 136 | # multiple error reports.  We use a cache to solve this problem. | 
|  | 137 |  | 
|  | 138 | my %AlreadyScanned; | 
|  | 139 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 140 | sub ScanFile { | 
|  | 141 |  | 
|  | 142 | my $Index = shift; | 
|  | 143 | my $Dir = shift; | 
|  | 144 | my $FName = shift; | 
|  | 145 |  | 
| Ted Kremenek | 57cf446 | 2008-04-18 15:09:30 +0000 | [diff] [blame] | 146 | # Compute a digest for the report file.  Determine if we have already | 
|  | 147 | # scanned a file that looks just like it. | 
|  | 148 |  | 
|  | 149 | my $digest = ComputeDigest("$Dir/$FName"); | 
|  | 150 |  | 
|  | 151 | if (defined($AlreadyScanned{$digest})) { | 
|  | 152 | # Redundant file.  Remove it. | 
|  | 153 | `rm -f $Dir/$FName`; | 
|  | 154 | return; | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | $AlreadyScanned{$digest} = 1; | 
|  | 158 |  | 
| Ted Kremenek | 809709f | 2008-04-18 16:58:34 +0000 | [diff] [blame^] | 159 | # At this point the report file is not world readable.  Make it happen. | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 160 | `chmod 644 $Dir/$FName`; | 
|  | 161 |  | 
|  | 162 | # Scan the report file for tags. | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 163 | open(IN, "$Dir/$FName") or die "$Prog: Cannot open '$Dir/$FName'\n"; | 
|  | 164 |  | 
|  | 165 | my $BugDesc = ""; | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 166 | my $BugFile = ""; | 
|  | 167 | my $BugPathLength = 1; | 
|  | 168 | my $BugLine = 0; | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 169 |  | 
|  | 170 | while (<IN>) { | 
|  | 171 |  | 
|  | 172 | if (/<!-- BUGDESC (.*) -->$/) { | 
|  | 173 | $BugDesc = $1; | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 174 | } | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 175 | elsif (/<!-- BUGFILE (.*) -->$/) { | 
|  | 176 | $BugFile = $1; | 
|  | 177 | } | 
|  | 178 | elsif (/<!-- BUGPATHLENGTH (.*) -->$/) { | 
|  | 179 | $BugPathLength = $1; | 
|  | 180 | } | 
|  | 181 | elsif (/<!-- BUGLINE (.*) -->$/) { | 
|  | 182 | $BugLine = $1; | 
|  | 183 | } | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 184 | } | 
|  | 185 |  | 
|  | 186 | close(IN); | 
|  | 187 |  | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 188 | push @$Index,[ $FName, $BugDesc, $BugFile, $BugLine, $BugPathLength ]; | 
|  | 189 | } | 
|  | 190 |  | 
|  | 191 | ##----------------------------------------------------------------------------## | 
|  | 192 | # CopyJS - Copy JavaScript code to target directory. | 
|  | 193 | ##----------------------------------------------------------------------------## | 
|  | 194 |  | 
|  | 195 | sub CopyJS { | 
|  | 196 |  | 
|  | 197 | my $Dir = shift; | 
|  | 198 |  | 
|  | 199 | die "$Prog: Cannot find 'sorttable.js'.\n" | 
|  | 200 | if (! -r "$RealBin/sorttable.js"); | 
|  | 201 |  | 
|  | 202 | `cp $RealBin/sorttable.js $Dir`; | 
|  | 203 |  | 
|  | 204 | die "$Prog: Could not copy 'sorttable.js' to '$Dir'." | 
|  | 205 | if (! -r "$Dir/sorttable.js"); | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 206 | } | 
|  | 207 |  | 
|  | 208 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 209 | # Postprocess - Postprocess the results of an analysis scan. | 
|  | 210 | ##----------------------------------------------------------------------------## | 
|  | 211 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 212 | sub Postprocess { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 213 |  | 
|  | 214 | my $Dir = shift; | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 215 | my $BaseDir = shift; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 216 |  | 
|  | 217 | die "No directory specified." if (!defined($Dir)); | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 218 | die "No base directory specified." if (!defined($BaseDir)); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 219 |  | 
|  | 220 | if (! -d $Dir) { | 
|  | 221 | return; | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | opendir(DIR, $Dir); | 
|  | 225 | my @files = grep(/^report-.*\.html$/,readdir(DIR)); | 
|  | 226 | closedir(DIR); | 
|  | 227 |  | 
|  | 228 | if (scalar(@files) == 0) { | 
| Ted Kremenek | 0249378 | 2008-04-02 07:05:07 +0000 | [diff] [blame] | 229 | print "$Prog: Removing directory '$Dir' because it contains no reports.\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 230 | `rm -fR $Dir`; | 
|  | 231 | return; | 
|  | 232 | } | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 233 |  | 
|  | 234 | # Scan each report file and build an index. | 
|  | 235 |  | 
|  | 236 | my @Index; | 
|  | 237 |  | 
|  | 238 | foreach my $file (@files) { ScanFile(\@Index, $Dir, $file); } | 
|  | 239 |  | 
|  | 240 | # Generate an index.html file. | 
|  | 241 |  | 
|  | 242 | my $FName = "$Dir/index.html"; | 
|  | 243 |  | 
|  | 244 | open(OUT, ">$FName") or die "$Prog: Cannot create file '$FName'\n"; | 
|  | 245 |  | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 246 | # Print out the header. | 
|  | 247 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 248 | print OUT <<ENDTEXT; | 
|  | 249 | <html> | 
|  | 250 | <head> | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 251 | <style type="text/css"> | 
|  | 252 | body { color:#000000; background-color:#ffffff } | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 253 | body { font-family: Helvetica, sans-serif; font-size:9pt } | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 254 | h1 { font-size:12pt } | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 255 | table.sortable thead { | 
|  | 256 | background-color:#eee; color:#666666; | 
|  | 257 | font-weight: bold; cursor: default; | 
| Ted Kremenek | bba1cf5 | 2008-04-03 05:50:51 +0000 | [diff] [blame] | 258 | text-align:center; | 
|  | 259 | border-top: 2px solid #000000; | 
|  | 260 | border-bottom: 2px solid #000000; | 
|  | 261 | font-weight: bold; font-family: Verdana | 
|  | 262 | } | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 263 | table.sortable { border: 1px #000000 solid } | 
|  | 264 | table.sortable { border-collapse: collapse; border-spacing: 0px } | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 265 | td { border-bottom: 1px #000000 dotted } | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 266 | td { padding:5px; padding-left:8px; padding-right:8px } | 
| Ted Kremenek | d8c6d0c | 2008-04-07 23:50:07 +0000 | [diff] [blame] | 267 | td { text-align:left; font-size:9pt } | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 268 | td.View   { padding-left: 10px } | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 269 | </style> | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 270 | <script src="sorttable.js"></script> | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 271 | <script language='javascript' type="text/javascript"> | 
|  | 272 | function SetDisplay(RowClass, DisplayVal) | 
|  | 273 | { | 
|  | 274 | var Rows = document.getElementsByTagName("tr"); | 
|  | 275 | for ( var i = 0 ; i < Rows.length; ++i ) { | 
|  | 276 | if (Rows[i].className == RowClass) { | 
|  | 277 | Rows[i].style.display = DisplayVal; | 
|  | 278 | } | 
|  | 279 | } | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | function ToggleDisplay(CheckButton, ClassName) { | 
|  | 283 | window.console.log("writing"); | 
|  | 284 | if (CheckButton.checked) { | 
|  | 285 | SetDisplay(ClassName, ""); | 
|  | 286 | } | 
|  | 287 | else { | 
|  | 288 | SetDisplay(ClassName, "none"); | 
|  | 289 | } | 
|  | 290 | } | 
|  | 291 | </script> | 
|  | 292 | </head> | 
|  | 293 | <body> | 
|  | 294 | ENDTEXT | 
|  | 295 |  | 
|  | 296 | # Print out the summary table. | 
|  | 297 |  | 
|  | 298 | my %Totals; | 
|  | 299 |  | 
|  | 300 | for my $row ( @Index ) { | 
|  | 301 |  | 
|  | 302 | my $bug_type = lc($row->[1]); | 
|  | 303 |  | 
|  | 304 | if (!defined($Totals{$bug_type})) { | 
|  | 305 | $Totals{$bug_type} = 1; | 
|  | 306 | } | 
|  | 307 | else { | 
|  | 308 | $Totals{$bug_type}++; | 
|  | 309 | } | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | print OUT <<ENDTEXT; | 
|  | 313 | <h3>Summary</h3> | 
|  | 314 | <table class="sortable"> | 
|  | 315 | <tr> | 
|  | 316 | <td>Bug Type</td> | 
|  | 317 | <td>Quantity</td> | 
|  | 318 | <td "sorttable_nosort">Display?</td> | 
|  | 319 | </tr> | 
|  | 320 | ENDTEXT | 
|  | 321 |  | 
|  | 322 | for my $key ( sort { $a cmp $b } keys %Totals ) { | 
|  | 323 | my $x = $key; | 
|  | 324 | $x =~ s/\s/_/g; | 
|  | 325 | print OUT "<tr><td>$key</td><td>$Totals{$key}</td><td><input type=\"checkbox\" onClick=\"ToggleDisplay(this,'bt_$x');\" checked/></td></tr>\n"; | 
|  | 326 | } | 
|  | 327 |  | 
|  | 328 | # Print out the table of errors. | 
|  | 329 |  | 
|  | 330 | print OUT <<ENDTEXT; | 
|  | 331 | </table> | 
|  | 332 | <h3>Reports</h3> | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 333 | <table class="sortable"> | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 334 | <tr> | 
| Ted Kremenek | bba1cf5 | 2008-04-03 05:50:51 +0000 | [diff] [blame] | 335 | <td>Bug Type</td> | 
|  | 336 | <td>File</td> | 
|  | 337 | <td>Line</td> | 
|  | 338 | <td>Path Length</td> | 
|  | 339 | <td "sorttable_nosort"></td> | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 340 | </tr> | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 341 | ENDTEXT | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 342 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 343 | for my $row ( sort { $a->[1] cmp $b->[1] } @Index ) { | 
|  | 344 |  | 
| Ted Kremenek | 6e6eff7 | 2008-04-15 20:47:02 +0000 | [diff] [blame] | 345 | my $x = lc($row->[1]); | 
|  | 346 | $x =~ s/\s/_/g; | 
|  | 347 |  | 
|  | 348 | print OUT "<tr class=\"bt_$x\">\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 349 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 350 | my $ReportFile = $row->[0]; | 
|  | 351 |  | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 352 | print OUT " <td class=\"DESC\">"; | 
|  | 353 | print OUT lc($row->[1]); | 
|  | 354 | print OUT "</td>\n"; | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 355 |  | 
|  | 356 | for my $j ( 2 .. $#{$row} ) { | 
|  | 357 | print OUT "<td>$row->[$j]</td>\n" | 
|  | 358 | } | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 359 |  | 
|  | 360 | # Emit the "View" link. | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 361 |  | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 362 | print OUT " <td class=\"View\"><a href=\"$ReportFile#EndPath\">View</a></td>\n"; | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 363 |  | 
|  | 364 | # End the row. | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 365 | print OUT "</tr>\n"; | 
|  | 366 | } | 
|  | 367 |  | 
|  | 368 | print OUT "</table>\n</body></html>\n"; | 
|  | 369 | close(OUT); | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 370 |  | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 371 | CopyJS($Dir); | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 372 |  | 
|  | 373 | # Make sure $Dir and $BaseDir is world readable/executable. | 
|  | 374 | `chmod 755 $Dir`; | 
|  | 375 | `chmod 755 $BaseDir`; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 376 | } | 
|  | 377 |  | 
|  | 378 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 379 | # RunBuildCommand - Run the build command. | 
|  | 380 | ##----------------------------------------------------------------------------## | 
|  | 381 |  | 
|  | 382 | sub RunBuildCommand { | 
|  | 383 |  | 
|  | 384 | my $Args = shift; | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 385 | my $IgnoreErrors = shift; | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 386 | my $Cmd = $Args->[0]; | 
|  | 387 |  | 
| Ted Kremenek | 6a43ba9 | 2008-04-02 15:34:12 +0000 | [diff] [blame] | 388 | 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] | 389 | shift @$Args; | 
|  | 390 | unshift @$Args, "ccc-analyzer" | 
|  | 391 | } | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 392 | elsif ($IgnoreErrors) { | 
|  | 393 | if ($Cmd eq "make" or $Cmd eq "gmake") { | 
|  | 394 | push @$Args, "-k"; | 
|  | 395 | } | 
|  | 396 | elsif ($Cmd eq "xcodebuild") { | 
|  | 397 | push @$Args, "-PBXBuildsContinueAfterErrors=YES"; | 
|  | 398 | } | 
| Ted Kremenek | 22d6a63 | 2008-04-02 20:43:36 +0000 | [diff] [blame] | 399 | } | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 400 |  | 
|  | 401 | system(@$Args); | 
|  | 402 | } | 
|  | 403 |  | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 404 | ##----------------------------------------------------------------------------## | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 405 | # DisplayHelp - Utility function to display all help options. | 
|  | 406 | ##----------------------------------------------------------------------------## | 
|  | 407 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 408 | sub DisplayHelp { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 409 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 410 | print <<ENDTEXT; | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 411 | USAGE: $Prog [options] <build command> [build options] | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 412 |  | 
|  | 413 | OPTIONS: | 
|  | 414 |  | 
|  | 415 | -o            - Target directory for HTML report files.  Subdirectories | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 416 | will be created as needed to represent separate "runs" of | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 417 | the analyzer.  If this option is not specified, a directory | 
|  | 418 | is created in /tmp to store the reports. | 
|  | 419 |  | 
| Ted Kremenek | 10f883f | 2008-04-03 07:11:44 +0000 | [diff] [blame] | 420 | -h            - Display this message. | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 421 | --help | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 422 |  | 
| Ted Kremenek | af08f64 | 2008-04-02 16:31:58 +0000 | [diff] [blame] | 423 | -k            - Add a "keep on going" option to the specified build command. | 
| Ted Kremenek | f02e8db | 2008-04-02 16:41:25 +0000 | [diff] [blame] | 424 | --keep-going    This option currently supports make and xcodebuild. | 
|  | 425 | This is a convenience option; one can specify this | 
|  | 426 | behavior directly using build options. | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 427 |  | 
| Ted Kremenek | dab1110 | 2008-04-02 04:43:42 +0000 | [diff] [blame] | 428 | -v            - Verbose output from $Prog and the analyzer. | 
|  | 429 | A second "-v" increases verbosity. | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 430 |  | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 431 | -V            - View analysis results in a web browser when the build | 
|  | 432 | --view          completes. | 
|  | 433 |  | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 434 | BUILD OPTIONS | 
|  | 435 |  | 
| Ted Kremenek | 39eefde | 2008-04-02 16:47:27 +0000 | [diff] [blame] | 436 | You can specify any build option acceptable to the build command. | 
|  | 437 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 438 | EXAMPLE | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 439 |  | 
| Ted Kremenek | 5744dc2 | 2008-04-02 18:03:36 +0000 | [diff] [blame] | 440 | $Prog -o /tmp/myhtmldir make -j4 | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 441 |  | 
| Ted Kremenek | 39eefde | 2008-04-02 16:47:27 +0000 | [diff] [blame] | 442 | The above example causes analysis reports to be deposited into | 
|  | 443 | a subdirectory of "/tmp/myhtmldir" and to run "make" with the "-j4" option. | 
|  | 444 | A different subdirectory is created each time $Prog analyzes a project. | 
|  | 445 | The analyzer should support most parallel builds, but not distributed builds. | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 446 |  | 
|  | 447 | ENDTEXT | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 448 | } | 
|  | 449 |  | 
|  | 450 | ##----------------------------------------------------------------------------## | 
|  | 451 | # Process command-line arguments. | 
|  | 452 | ##----------------------------------------------------------------------------## | 
|  | 453 |  | 
|  | 454 | my $HtmlDir;           # Parent directory to store HTML files. | 
|  | 455 | my $IgnoreErrors = 0;  # Ignore build errors. | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 456 | my $ViewResults  = 0;  # View results when the build terminates. | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 457 |  | 
|  | 458 | if (!@ARGV) { | 
|  | 459 | DisplayHelp(); | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 460 | exit 1; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 461 | } | 
|  | 462 |  | 
|  | 463 | while (@ARGV) { | 
|  | 464 |  | 
|  | 465 | # Scan for options we recognize. | 
|  | 466 |  | 
|  | 467 | my $arg = $ARGV[0]; | 
|  | 468 |  | 
| Sam Bishop | 2f2418e | 2008-04-03 14:29:47 +0000 | [diff] [blame] | 469 | if ($arg eq "-h" or $arg eq "--help") { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 470 | DisplayHelp(); | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 471 | exit 0; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 472 | } | 
|  | 473 |  | 
|  | 474 | if ($arg eq "-o") { | 
|  | 475 | shift @ARGV; | 
|  | 476 |  | 
|  | 477 | if (!@ARGV) { | 
| Ted Kremenek | 0062ad4 | 2008-04-02 16:35:01 +0000 | [diff] [blame] | 478 | die "$Prog: '-o' option requires a target directory name.\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 479 | } | 
|  | 480 |  | 
|  | 481 | $HtmlDir = shift @ARGV; | 
|  | 482 | next; | 
|  | 483 | } | 
|  | 484 |  | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame] | 485 | if ($arg eq "-k" or $arg eq "--keep-going") { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 486 | shift @ARGV; | 
|  | 487 | $IgnoreErrors = 1; | 
|  | 488 | next; | 
|  | 489 | } | 
|  | 490 |  | 
|  | 491 | if ($arg eq "-v") { | 
|  | 492 | shift @ARGV; | 
|  | 493 | $Verbose++; | 
|  | 494 | next; | 
|  | 495 | } | 
|  | 496 |  | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 497 | if ($arg eq "-V" or $arg eq "--view") { | 
|  | 498 | shift @ARGV; | 
|  | 499 | $ViewResults = 1; | 
|  | 500 | next; | 
|  | 501 | } | 
|  | 502 |  | 
| Ted Kremenek | 0062ad4 | 2008-04-02 16:35:01 +0000 | [diff] [blame] | 503 | die "$Prog: unrecognized option '$arg'\n" if ($arg =~ /^-/); | 
|  | 504 |  | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 505 | last; | 
|  | 506 | } | 
|  | 507 |  | 
|  | 508 | if (!@ARGV) { | 
|  | 509 | print STDERR "$Prog: No build command specified.\n\n"; | 
|  | 510 | DisplayHelp(); | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 511 | exit 1; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 512 | } | 
|  | 513 |  | 
|  | 514 | # Determine the output directory for the HTML reports. | 
|  | 515 |  | 
|  | 516 | if (!defined($HtmlDir)) { | 
|  | 517 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 518 | $HtmlDir = mkdtemp("/tmp/$Prog-XXXXXX"); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 519 |  | 
|  | 520 | if (!defined($HtmlDir)) { | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 521 | die "error: Cannot create HTML directory in /tmp.\n"; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 522 | } | 
|  | 523 |  | 
|  | 524 | if (!$Verbose) { | 
|  | 525 | print "$Prog: Using '$HtmlDir' as base HTML report directory.\n"; | 
|  | 526 | } | 
|  | 527 | } | 
|  | 528 |  | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 529 | my $BaseDir = $HtmlDir; | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 530 | $HtmlDir = GetHTMLRunDir($HtmlDir); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 531 |  | 
|  | 532 | # Set the appropriate environment variables. | 
|  | 533 |  | 
| Sam Bishop | a0e2266 | 2008-04-02 03:35:43 +0000 | [diff] [blame] | 534 | SetHtmlEnv(\@ARGV, $HtmlDir); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 535 |  | 
| Ted Kremenek | 0b6c153 | 2008-04-08 20:22:12 +0000 | [diff] [blame] | 536 | my $Cmd = "$RealBin/ccc-analyzer"; | 
|  | 537 |  | 
|  | 538 | die "$Prog: Executable 'ccc-analyzer' does not exist at '$Cmd'\n" | 
|  | 539 | if (! -x $Cmd); | 
|  | 540 |  | 
| Ted Kremenek | 4f4b17d | 2008-04-03 20:08:18 +0000 | [diff] [blame] | 541 | $ENV{'CC'} = $Cmd; | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 542 |  | 
|  | 543 | if ($Verbose >= 2) { | 
|  | 544 | $ENV{'CCC_ANALYZER_VERBOSE'} = 1; | 
|  | 545 | } | 
|  | 546 |  | 
|  | 547 | # Run the build. | 
|  | 548 |  | 
| Ted Kremenek | 7442ca6 | 2008-04-02 16:04:51 +0000 | [diff] [blame] | 549 | RunBuildCommand(\@ARGV, $IgnoreErrors); | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 550 |  | 
|  | 551 | # Postprocess the HTML directory. | 
|  | 552 |  | 
| Ted Kremenek | 684bb09 | 2008-04-18 15:18:20 +0000 | [diff] [blame] | 553 | Postprocess($HtmlDir, $BaseDir); | 
| Ted Kremenek | 7f8a325 | 2008-04-02 18:42:49 +0000 | [diff] [blame] | 554 |  | 
|  | 555 | if ($ViewResults and -r "$HtmlDir/index.html") { | 
|  | 556 | # Only works on Mac OS X (for now). | 
|  | 557 | print "Viewing analysis results: '$HtmlDir/index.html'\n"; | 
|  | 558 | `open $HtmlDir/index.html` | 
|  | 559 | } |