| 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 |  | 
|  | 26 | sub 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 |  | 
|  | 97 | sub 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 |  | 
|  | 123 | sub 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 |  | 
|  | 150 | sub DisplayHelp() { | 
|  | 151 |  | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame^] | 152 | print <<ENDTEXT | 
|  | 153 | USAGE: scan-build [options] <build command> [build options] | 
|  | 154 |  | 
|  | 155 | OPTIONS: | 
|  | 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 Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 164 |  | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame^] | 165 | -k            - Add "keep on going option" to the specified build command. | 
|  | 166 | --keep-going    This command currently supports "make" and "xcodebuild." You | 
|  | 167 | can also specify the corresponding option to the build command | 
|  | 168 | directly as a build option if you know it. | 
|  | 169 |  | 
|  | 170 | -v            - Verbose output from scan-build and the analyzer. A second | 
|  | 171 | "-v" increases verbosity. | 
|  | 172 |  | 
|  | 173 |  | 
|  | 174 | BUILD OPTIONS | 
|  | 175 |  | 
|  | 176 | You can specify any build option acceptable to the build command. For example: | 
|  | 177 |  | 
|  | 178 | scan-build -o /tmp/myhtmldir make -j4 | 
|  | 179 |  | 
|  | 180 | The above causes analysis reports to be deposited in /tmp/myhtmldir (or | 
|  | 181 | rather a subdirectory corresponding to this particular running of the | 
|  | 182 | analyzer), and causes "make" to be run with the "-j4" option, allowing | 
|  | 183 | parallel builds (and parallel invocations of the analyzer). | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 184 |  | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame^] | 185 | NOTE: The analyzer will work for most parallel builds, but not distributed | 
|  | 186 | builds (such as using distcc). | 
|  | 187 |  | 
|  | 188 | ENDTEXT | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 189 | } | 
|  | 190 |  | 
|  | 191 | ##----------------------------------------------------------------------------## | 
|  | 192 | # Process command-line arguments. | 
|  | 193 | ##----------------------------------------------------------------------------## | 
|  | 194 |  | 
|  | 195 | my $HtmlDir;           # Parent directory to store HTML files. | 
|  | 196 | my $IgnoreErrors = 0;  # Ignore build errors. | 
|  | 197 |  | 
|  | 198 | if (!@ARGV) { | 
|  | 199 | DisplayHelp(); | 
|  | 200 | exit 1 | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | while (@ARGV) { | 
|  | 204 |  | 
|  | 205 | # Scan for options we recognize. | 
|  | 206 |  | 
|  | 207 | my $arg = $ARGV[0]; | 
|  | 208 |  | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame^] | 209 | if ($arg eq "-?" or $arg eq "--help") { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 210 | DisplayHelp(); | 
|  | 211 | exit 1; | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | if ($arg eq "-o") { | 
|  | 215 | shift @ARGV; | 
|  | 216 |  | 
|  | 217 | if (!@ARGV) { | 
|  | 218 | print STDERR "'-o' option requires a target directory name."; | 
|  | 219 | exit 0; | 
|  | 220 | } | 
|  | 221 |  | 
|  | 222 | $HtmlDir = shift @ARGV; | 
|  | 223 | next; | 
|  | 224 | } | 
|  | 225 |  | 
| Ted Kremenek | 2b74ab6 | 2008-04-01 21:22:03 +0000 | [diff] [blame^] | 226 | if ($arg eq "-k" or $arg eq "--keep-going") { | 
| Ted Kremenek | 9cc8c2c | 2008-04-01 20:47:38 +0000 | [diff] [blame] | 227 | shift @ARGV; | 
|  | 228 | $IgnoreErrors = 1; | 
|  | 229 | next; | 
|  | 230 | } | 
|  | 231 |  | 
|  | 232 | if ($arg eq "-v") { | 
|  | 233 | shift @ARGV; | 
|  | 234 | $Verbose++; | 
|  | 235 | next; | 
|  | 236 | } | 
|  | 237 |  | 
|  | 238 | last; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | if (!@ARGV) { | 
|  | 242 | print STDERR "$Prog: No build command specified.\n\n"; | 
|  | 243 | DisplayHelp(); | 
|  | 244 | exit 0; | 
|  | 245 | } | 
|  | 246 |  | 
|  | 247 | # Determine the output directory for the HTML reports. | 
|  | 248 |  | 
|  | 249 | if (!defined($HtmlDir)) { | 
|  | 250 |  | 
|  | 251 | $HtmlDir = mkdtemp("/tmp/scan-build-XXXXXX"); | 
|  | 252 |  | 
|  | 253 | if (!defined($HtmlDir)) { | 
|  | 254 | print STDERR "error: Cannot create HTML directory in /tmp.\n"; | 
|  | 255 | exit 0; | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | if (!$Verbose) { | 
|  | 259 | print "$Prog: Using '$HtmlDir' as base HTML report directory.\n"; | 
|  | 260 | } | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | $HtmlDir = &GetHTMLRunDir($HtmlDir); | 
|  | 264 |  | 
|  | 265 | # Set the appropriate environment variables. | 
|  | 266 |  | 
|  | 267 | &SetHtmlEnv(\@ARGV, $HtmlDir); | 
|  | 268 |  | 
|  | 269 | $ENV{'CC'} = "ccc-analyzer"; | 
|  | 270 |  | 
|  | 271 | if ($Verbose >= 2) { | 
|  | 272 | $ENV{'CCC_ANALYZER_VERBOSE'} = 1; | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | # Run the build. | 
|  | 276 |  | 
|  | 277 | system(@ARGV); | 
|  | 278 |  | 
|  | 279 | # Postprocess the HTML directory. | 
|  | 280 |  | 
|  | 281 | &Postprocess($HtmlDir); |