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