blob: 36199cb281afb5759bedfb623806066ce322fd67 [file] [log] [blame]
Anna Zaks1b417162011-10-06 23:26:27 +00001#!/usr/bin/env python
2
3"""
4Static Analyzer qualification infrastructure.
5
6The goal is to test the analyzer against different projects, check for failures,
7compare results, and measure performance.
8
9Repository Directory will contain sources of the projects as well as the
10information on how to build them and the expected output.
11Repository Directory structure:
12 - ProjectMap file
13 - Historical Performance Data
14 - Project Dir1
15 - ReferenceOutput
16 - Project Dir2
17 - ReferenceOutput
18 ..
19
20To test the build of the analyzer one would:
21 - Copy over a copy of the Repository Directory. (TODO: Prefer to ensure that
22 the build directory does not pollute the repository to min network traffic).
23 - Build all projects, until error. Produce logs to report errors.
24 - Compare results.
25
26The files which should be kept around for failure investigations:
27 RepositoryCopy/Project DirI/ScanBuildResults
28 RepositoryCopy/Project DirI/run_static_analyzer.log
29
30Assumptions (TODO: shouldn't need to assume these.):
31 The script is being run from the Repository Directory.
Anna Zaks5fa3f132011-11-02 20:46:50 +000032 The compiler for scan-build and scan-build are in the PATH.
Anna Zaks1b417162011-10-06 23:26:27 +000033 export PATH=/Users/zaks/workspace/c2llvm/build/Release+Asserts/bin:$PATH
34
35For more logging, set the env variables:
36 zaks:TI zaks$ export CCC_ANALYZER_LOG=1
37 zaks:TI zaks$ export CCC_ANALYZER_VERBOSE=1
38"""
39import CmpRuns
40
41import os
42import csv
43import sys
44import glob
Ted Kremenek82778af2012-08-28 20:40:04 +000045import math
Anna Zaks1b417162011-10-06 23:26:27 +000046import shutil
47import time
48import plistlib
Anna Zaks45518b12011-11-05 05:20:48 +000049from subprocess import check_call, CalledProcessError
Anna Zaks1b417162011-10-06 23:26:27 +000050
Ted Kremenek7c14c442012-08-28 20:40:02 +000051#------------------------------------------------------------------------------
52# Helper functions.
53#------------------------------------------------------------------------------
Anna Zaks1b417162011-10-06 23:26:27 +000054
Ted Kremenek82778af2012-08-28 20:40:04 +000055def detectCPUs():
56 """
57 Detects the number of CPUs on a system. Cribbed from pp.
58 """
59 # Linux, Unix and MacOS:
60 if hasattr(os, "sysconf"):
61 if os.sysconf_names.has_key("SC_NPROCESSORS_ONLN"):
62 # Linux & Unix:
63 ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
64 if isinstance(ncpus, int) and ncpus > 0:
65 return ncpus
66 else: # OSX:
67 return int(capture(['sysctl', '-n', 'hw.ncpu']))
68 # Windows:
69 if os.environ.has_key("NUMBER_OF_PROCESSORS"):
70 ncpus = int(os.environ["NUMBER_OF_PROCESSORS"])
71 if ncpus > 0:
72 return ncpus
73 return 1 # Default
74
Ted Kremenek5abd3d22012-08-28 20:20:52 +000075def which(command, paths = None):
76 """which(command, [paths]) - Look up the given command in the paths string
77 (or the PATH environment variable, if unspecified)."""
78
79 if paths is None:
80 paths = os.environ.get('PATH','')
81
82 # Check for absolute match first.
83 if os.path.exists(command):
84 return command
85
86 # Would be nice if Python had a lib function for this.
87 if not paths:
88 paths = os.defpath
89
90 # Get suffixes to search.
91 # On Cygwin, 'PATHEXT' may exist but it should not be used.
92 if os.pathsep == ';':
93 pathext = os.environ.get('PATHEXT', '').split(';')
94 else:
95 pathext = ['']
96
97 # Search the paths...
98 for path in paths.split(os.pathsep):
99 for ext in pathext:
100 p = os.path.join(path, command + ext)
101 if os.path.exists(p):
102 return p
103
104 return None
105
Anna Zaksd3e29ef2012-01-10 18:10:25 +0000106# Make sure we flush the output after every print statement.
107class flushfile(object):
108 def __init__(self, f):
109 self.f = f
110 def write(self, x):
111 self.f.write(x)
112 self.f.flush()
113
114sys.stdout = flushfile(sys.stdout)
115
Anna Zaks1b417162011-10-06 23:26:27 +0000116def getProjectMapPath():
117 ProjectMapPath = os.path.join(os.path.abspath(os.curdir),
118 ProjectMapFile)
119 if not os.path.exists(ProjectMapPath):
120 print "Error: Cannot find the Project Map file " + ProjectMapPath +\
121 "\nRunning script for the wrong directory?"
122 sys.exit(-1)
123 return ProjectMapPath
124
125def getProjectDir(ID):
126 return os.path.join(os.path.abspath(os.curdir), ID)
127
Jordan Rose6d7e3722012-06-01 16:24:38 +0000128def getSBOutputDirName(IsReferenceBuild) :
Anna Zaks45518b12011-11-05 05:20:48 +0000129 if IsReferenceBuild == True :
130 return SBOutputDirReferencePrefix + SBOutputDirName
131 else :
132 return SBOutputDirName
133
Ted Kremenek7c14c442012-08-28 20:40:02 +0000134#------------------------------------------------------------------------------
135# Configuration setup.
136#------------------------------------------------------------------------------
137
138# Find Clang for static analysis.
139Clang = which("clang", os.environ['PATH'])
140if not Clang:
141 print "Error: cannot find 'clang' in PATH"
142 sys.exit(-1)
143
Ted Kremenek82778af2012-08-28 20:40:04 +0000144# Number of jobs.
Jordan Rose58782be2012-11-16 17:41:21 +0000145Jobs = int(math.ceil(detectCPUs() * 0.75))
Ted Kremenek82778af2012-08-28 20:40:04 +0000146
Ted Kremenek7c14c442012-08-28 20:40:02 +0000147# Project map stores info about all the "registered" projects.
148ProjectMapFile = "projectMap.csv"
149
150# Names of the project specific scripts.
151# The script that needs to be executed before the build can start.
152CleanupScript = "cleanup_run_static_analyzer.sh"
153# This is a file containing commands for scan-build.
154BuildScript = "run_static_analyzer.cmd"
155
156# The log file name.
157LogFolderName = "Logs"
158BuildLogName = "run_static_analyzer.log"
159# Summary file - contains the summary of the failures. Ex: This info can be be
160# displayed when buildbot detects a build failure.
161NumOfFailuresInSummary = 10
162FailuresSummaryFileName = "failures.txt"
163# Summary of the result diffs.
164DiffsSummaryFileName = "diffs.txt"
165
166# The scan-build result directory.
167SBOutputDirName = "ScanBuildResults"
168SBOutputDirReferencePrefix = "Ref"
169
170# The list of checkers used during analyzes.
171# Currently, consists of all the non experimental checkers.
Anna Zaks765029b2012-11-02 21:30:07 +0000172Checkers="alpha.unix.SimpleStream,alpha.security.taint,core,deadcode,security,unix,osx"
Ted Kremenek7c14c442012-08-28 20:40:02 +0000173
174Verbose = 1
175
176#------------------------------------------------------------------------------
177# Test harness logic.
178#------------------------------------------------------------------------------
179
Anna Zaks1b417162011-10-06 23:26:27 +0000180# Run pre-processing script if any.
Anna Zaks5fa3f132011-11-02 20:46:50 +0000181def runCleanupScript(Dir, PBuildLogFile):
182 ScriptPath = os.path.join(Dir, CleanupScript)
Anna Zaks1b417162011-10-06 23:26:27 +0000183 if os.path.exists(ScriptPath):
184 try:
185 if Verbose == 1:
186 print " Executing: %s" % (ScriptPath,)
187 check_call("chmod +x %s" % ScriptPath, cwd = Dir,
188 stderr=PBuildLogFile,
189 stdout=PBuildLogFile,
190 shell=True)
191 check_call(ScriptPath, cwd = Dir, stderr=PBuildLogFile,
192 stdout=PBuildLogFile,
193 shell=True)
194 except:
195 print "Error: The pre-processing step failed. See ", \
196 PBuildLogFile.name, " for details."
197 sys.exit(-1)
198
199# Build the project with scan-build by reading in the commands and
200# prefixing them with the scan-build options.
201def runScanBuild(Dir, SBOutputDir, PBuildLogFile):
202 BuildScriptPath = os.path.join(Dir, BuildScript)
203 if not os.path.exists(BuildScriptPath):
204 print "Error: build script is not defined: %s" % BuildScriptPath
Ted Kremenek5abd3d22012-08-28 20:20:52 +0000205 sys.exit(-1)
206 SBOptions = "--use-analyzer " + Clang + " "
207 SBOptions += "-plist-html -o " + SBOutputDir + " "
Anna Zaks8d4a5152011-11-08 22:41:25 +0000208 SBOptions += "-enable-checker " + Checkers + " "
Anna Zaks1b417162011-10-06 23:26:27 +0000209 try:
210 SBCommandFile = open(BuildScriptPath, "r")
211 SBPrefix = "scan-build " + SBOptions + " "
212 for Command in SBCommandFile:
Ted Kremenek82778af2012-08-28 20:40:04 +0000213 # If using 'make', auto imply a -jX argument
214 # to speed up analysis. xcodebuild will
215 # automatically use the maximum number of cores.
Jordan Rose7bd51ea2012-11-26 19:59:57 +0000216 if (Command.startswith("make ") or Command == "make") and \
217 "-j" not in Command:
Jordan Rose58782be2012-11-16 17:41:21 +0000218 Command += " -j%d" % Jobs
Anna Zaks1b417162011-10-06 23:26:27 +0000219 SBCommand = SBPrefix + Command
220 if Verbose == 1:
221 print " Executing: %s" % (SBCommand,)
222 check_call(SBCommand, cwd = Dir, stderr=PBuildLogFile,
223 stdout=PBuildLogFile,
224 shell=True)
225 except:
226 print "Error: scan-build failed. See ",PBuildLogFile.name,\
227 " for details."
Anna Zaks45518b12011-11-05 05:20:48 +0000228 raise
Anna Zaks1b417162011-10-06 23:26:27 +0000229
Anna Zaks45518b12011-11-05 05:20:48 +0000230def hasNoExtension(FileName):
231 (Root, Ext) = os.path.splitext(FileName)
232 if ((Ext == "")) :
233 return True
234 return False
235
236def isValidSingleInputFile(FileName):
237 (Root, Ext) = os.path.splitext(FileName)
238 if ((Ext == ".i") | (Ext == ".ii") |
239 (Ext == ".c") | (Ext == ".cpp") |
240 (Ext == ".m") | (Ext == "")) :
241 return True
242 return False
Ted Kremenek5abd3d22012-08-28 20:20:52 +0000243
Anna Zaks45518b12011-11-05 05:20:48 +0000244# Run analysis on a set of preprocessed files.
Anna Zaks817ce3d2012-09-06 23:30:27 +0000245def runAnalyzePreprocessed(Dir, SBOutputDir, Mode):
Anna Zaks45518b12011-11-05 05:20:48 +0000246 if os.path.exists(os.path.join(Dir, BuildScript)):
247 print "Error: The preprocessed files project should not contain %s" % \
248 BuildScript
249 raise Exception()
250
Ted Kremenek5abd3d22012-08-28 20:20:52 +0000251 CmdPrefix = Clang + " -cc1 -analyze -analyzer-output=plist -w "
Anna Zaks8c345c02012-01-21 01:11:35 +0000252 CmdPrefix += "-analyzer-checker=" + Checkers +" -fcxx-exceptions -fblocks "
Anna Zaks45518b12011-11-05 05:20:48 +0000253
Anna Zaks817ce3d2012-09-06 23:30:27 +0000254 if (Mode == 2) :
255 CmdPrefix += "-std=c++11 "
256
Anna Zaks45518b12011-11-05 05:20:48 +0000257 PlistPath = os.path.join(Dir, SBOutputDir, "date")
258 FailPath = os.path.join(PlistPath, "failures");
259 os.makedirs(FailPath);
260
261 for FullFileName in glob.glob(Dir + "/*"):
262 FileName = os.path.basename(FullFileName)
263 Failed = False
264
265 # Only run the analyzes on supported files.
266 if (hasNoExtension(FileName)):
267 continue
268 if (isValidSingleInputFile(FileName) == False):
269 print "Error: Invalid single input file %s." % (FullFileName,)
270 raise Exception()
271
272 # Build and call the analyzer command.
273 OutputOption = "-o " + os.path.join(PlistPath, FileName) + ".plist "
274 Command = CmdPrefix + OutputOption + os.path.join(Dir, FileName)
275 LogFile = open(os.path.join(FailPath, FileName + ".stderr.txt"), "w+b")
276 try:
277 if Verbose == 1:
278 print " Executing: %s" % (Command,)
279 check_call(Command, cwd = Dir, stderr=LogFile,
280 stdout=LogFile,
281 shell=True)
282 except CalledProcessError, e:
283 print "Error: Analyzes of %s failed. See %s for details." \
284 "Error code %d." % \
285 (FullFileName, LogFile.name, e.returncode)
286 Failed = True
287 finally:
288 LogFile.close()
289
290 # If command did not fail, erase the log file.
291 if Failed == False:
292 os.remove(LogFile.name);
293
Anna Zaks817ce3d2012-09-06 23:30:27 +0000294def buildProject(Dir, SBOutputDir, ProjectBuildMode, IsReferenceBuild):
Anna Zaks1b417162011-10-06 23:26:27 +0000295 TBegin = time.time()
296
Anna Zaks45518b12011-11-05 05:20:48 +0000297 BuildLogPath = os.path.join(SBOutputDir, LogFolderName, BuildLogName)
Anna Zaks1b417162011-10-06 23:26:27 +0000298 print "Log file: %s" % (BuildLogPath,)
Anna Zaks45518b12011-11-05 05:20:48 +0000299 print "Output directory: %s" %(SBOutputDir, )
300
Anna Zaks1b417162011-10-06 23:26:27 +0000301 # Clean up the log file.
302 if (os.path.exists(BuildLogPath)) :
303 RmCommand = "rm " + BuildLogPath
304 if Verbose == 1:
Anna Zaks5fa3f132011-11-02 20:46:50 +0000305 print " Executing: %s" % (RmCommand,)
Anna Zaks1b417162011-10-06 23:26:27 +0000306 check_call(RmCommand, shell=True)
Anna Zaks45518b12011-11-05 05:20:48 +0000307
308 # Clean up scan build results.
309 if (os.path.exists(SBOutputDir)) :
310 RmCommand = "rm -r " + SBOutputDir
311 if Verbose == 1:
312 print " Executing: %s" % (RmCommand,)
313 check_call(RmCommand, shell=True)
314 assert(not os.path.exists(SBOutputDir))
315 os.makedirs(os.path.join(SBOutputDir, LogFolderName))
Anna Zaks1b417162011-10-06 23:26:27 +0000316
317 # Open the log file.
318 PBuildLogFile = open(BuildLogPath, "wb+")
Anna Zaks1b417162011-10-06 23:26:27 +0000319
Anna Zaks45518b12011-11-05 05:20:48 +0000320 # Build and analyze the project.
321 try:
Anna Zaks5fa3f132011-11-02 20:46:50 +0000322 runCleanupScript(Dir, PBuildLogFile)
Anna Zaks5fa3f132011-11-02 20:46:50 +0000323
Anna Zaks817ce3d2012-09-06 23:30:27 +0000324 if (ProjectBuildMode == 1):
Anna Zaks45518b12011-11-05 05:20:48 +0000325 runScanBuild(Dir, SBOutputDir, PBuildLogFile)
326 else:
Anna Zaks817ce3d2012-09-06 23:30:27 +0000327 runAnalyzePreprocessed(Dir, SBOutputDir, ProjectBuildMode)
Anna Zaks45518b12011-11-05 05:20:48 +0000328
329 if IsReferenceBuild :
Anna Zaks5fa3f132011-11-02 20:46:50 +0000330 runCleanupScript(Dir, PBuildLogFile)
331
Anna Zaks1b417162011-10-06 23:26:27 +0000332 finally:
333 PBuildLogFile.close()
334
335 print "Build complete (time: %.2f). See the log for more details: %s" % \
336 ((time.time()-TBegin), BuildLogPath)
337
338# A plist file is created for each call to the analyzer(each source file).
339# We are only interested on the once that have bug reports, so delete the rest.
340def CleanUpEmptyPlists(SBOutputDir):
341 for F in glob.glob(SBOutputDir + "/*/*.plist"):
342 P = os.path.join(SBOutputDir, F)
343
344 Data = plistlib.readPlist(P)
345 # Delete empty reports.
346 if not Data['files']:
347 os.remove(P)
348 continue
349
350# Given the scan-build output directory, checks if the build failed
351# (by searching for the failures directories). If there are failures, it
352# creates a summary file in the output directory.
353def checkBuild(SBOutputDir):
354 # Check if there are failures.
355 Failures = glob.glob(SBOutputDir + "/*/failures/*.stderr.txt")
356 TotalFailed = len(Failures);
357 if TotalFailed == 0:
Jordan Rose191e2b12012-08-31 00:36:30 +0000358 CleanUpEmptyPlists(SBOutputDir)
359 Plists = glob.glob(SBOutputDir + "/*/*.plist")
360 print "Number of bug reports (non empty plist files) produced: %d" %\
361 len(Plists)
Anna Zaks1b417162011-10-06 23:26:27 +0000362 return;
363
364 # Create summary file to display when the build fails.
Anna Zaks45518b12011-11-05 05:20:48 +0000365 SummaryPath = os.path.join(SBOutputDir, LogFolderName, FailuresSummaryFileName)
Anna Zaks1b417162011-10-06 23:26:27 +0000366 if (Verbose > 0):
Anna Zaks45518b12011-11-05 05:20:48 +0000367 print " Creating the failures summary file %s" % (SummaryPath,)
Anna Zaks1b417162011-10-06 23:26:27 +0000368
369 SummaryLog = open(SummaryPath, "w+")
370 try:
371 SummaryLog.write("Total of %d failures discovered.\n" % (TotalFailed,))
372 if TotalFailed > NumOfFailuresInSummary:
373 SummaryLog.write("See the first %d below.\n"
374 % (NumOfFailuresInSummary,))
375 # TODO: Add a line "See the results folder for more."
376
377 FailuresCopied = NumOfFailuresInSummary
378 Idx = 0
Jordan Rose04bc0142012-06-01 16:24:43 +0000379 for FailLogPathI in Failures:
Anna Zaks1b417162011-10-06 23:26:27 +0000380 if Idx >= NumOfFailuresInSummary:
381 break;
382 Idx += 1
383 SummaryLog.write("\n-- Error #%d -----------\n" % (Idx,));
384 FailLogI = open(FailLogPathI, "r");
385 try:
386 shutil.copyfileobj(FailLogI, SummaryLog);
387 finally:
388 FailLogI.close()
389 finally:
390 SummaryLog.close()
391
Anna Zaksf063a3b2012-01-04 23:53:50 +0000392 print "Error: analysis failed. See ", SummaryPath
Anna Zaks1b417162011-10-06 23:26:27 +0000393 sys.exit(-1)
394
395# Auxiliary object to discard stdout.
396class Discarder(object):
397 def write(self, text):
398 pass # do nothing
399
400# Compare the warnings produced by scan-build.
401def runCmpResults(Dir):
402 TBegin = time.time()
403
404 RefDir = os.path.join(Dir, SBOutputDirReferencePrefix + SBOutputDirName)
405 NewDir = os.path.join(Dir, SBOutputDirName)
406
407 # We have to go one level down the directory tree.
408 RefList = glob.glob(RefDir + "/*")
409 NewList = glob.glob(NewDir + "/*")
Anna Zaks45518b12011-11-05 05:20:48 +0000410
411 # Log folders are also located in the results dir, so ignore them.
412 RefList.remove(os.path.join(RefDir, LogFolderName))
413 NewList.remove(os.path.join(NewDir, LogFolderName))
414
Anna Zaks1b417162011-10-06 23:26:27 +0000415 if len(RefList) == 0 or len(NewList) == 0:
416 return False
417 assert(len(RefList) == len(NewList))
418
419 # There might be more then one folder underneath - one per each scan-build
420 # command (Ex: one for configure and one for make).
421 if (len(RefList) > 1):
422 # Assume that the corresponding folders have the same names.
423 RefList.sort()
424 NewList.sort()
425
426 # Iterate and find the differences.
Anna Zaksa7a25642011-11-08 19:56:31 +0000427 NumDiffs = 0
Anna Zaks1b417162011-10-06 23:26:27 +0000428 PairList = zip(RefList, NewList)
429 for P in PairList:
430 RefDir = P[0]
431 NewDir = P[1]
432
433 assert(RefDir != NewDir)
434 if Verbose == 1:
435 print " Comparing Results: %s %s" % (RefDir, NewDir)
436
437 DiffsPath = os.path.join(NewDir, DiffsSummaryFileName)
438 Opts = CmpRuns.CmpOptions(DiffsPath)
439 # Discard everything coming out of stdout (CmpRun produces a lot of them).
440 OLD_STDOUT = sys.stdout
441 sys.stdout = Discarder()
442 # Scan the results, delete empty plist files.
Anna Zaks7acc4072012-07-16 20:21:42 +0000443 NumDiffs = CmpRuns.dumpScanBuildResultsDiff(RefDir, NewDir, Opts, False)
Anna Zaks1b417162011-10-06 23:26:27 +0000444 sys.stdout = OLD_STDOUT
Anna Zaksa7a25642011-11-08 19:56:31 +0000445 if (NumDiffs > 0) :
446 print "Warning: %r differences in diagnostics. See %s" % \
447 (NumDiffs, DiffsPath,)
Anna Zaks1b417162011-10-06 23:26:27 +0000448
449 print "Diagnostic comparison complete (time: %.2f)." % (time.time()-TBegin)
Anna Zaksa7a25642011-11-08 19:56:31 +0000450 return (NumDiffs > 0)
Anna Zaks45518b12011-11-05 05:20:48 +0000451
Anna Zaks09e9cf02012-02-03 06:35:23 +0000452def updateSVN(Mode, ProjectsMap):
453 try:
454 ProjectsMap.seek(0)
455 for I in csv.reader(ProjectsMap):
456 ProjName = I[0]
Jordan Rose6d7e3722012-06-01 16:24:38 +0000457 Path = os.path.join(ProjName, getSBOutputDirName(True))
Anna Zaks09e9cf02012-02-03 06:35:23 +0000458
459 if Mode == "delete":
460 Command = "svn delete %s" % (Path,)
461 else:
462 Command = "svn add %s" % (Path,)
Anna Zaks1b417162011-10-06 23:26:27 +0000463
Anna Zaks09e9cf02012-02-03 06:35:23 +0000464 if Verbose == 1:
465 print " Executing: %s" % (Command,)
Jordan Rose04bc0142012-06-01 16:24:43 +0000466 check_call(Command, shell=True)
Anna Zaks09e9cf02012-02-03 06:35:23 +0000467
468 if Mode == "delete":
469 CommitCommand = "svn commit -m \"[analyzer tests] Remove " \
470 "reference results.\""
471 else:
472 CommitCommand = "svn commit -m \"[analyzer tests] Add new " \
473 "reference results.\""
474 if Verbose == 1:
475 print " Executing: %s" % (CommitCommand,)
Jordan Rose04bc0142012-06-01 16:24:43 +0000476 check_call(CommitCommand, shell=True)
Anna Zaks09e9cf02012-02-03 06:35:23 +0000477 except:
478 print "Error: SVN update failed."
479 sys.exit(-1)
480
Anna Zaks817ce3d2012-09-06 23:30:27 +0000481def testProject(ID, ProjectBuildMode, IsReferenceBuild=False, Dir=None):
Anna Zaks45518b12011-11-05 05:20:48 +0000482 print " \n\n--- Building project %s" % (ID,)
483
Anna Zaks1b417162011-10-06 23:26:27 +0000484 TBegin = time.time()
485
486 if Dir is None :
487 Dir = getProjectDir(ID)
488 if Verbose == 1:
489 print " Build directory: %s." % (Dir,)
490
491 # Set the build results directory.
Jordan Rose6d7e3722012-06-01 16:24:38 +0000492 RelOutputDir = getSBOutputDirName(IsReferenceBuild)
Anna Zaks09e9cf02012-02-03 06:35:23 +0000493 SBOutputDir = os.path.join(Dir, RelOutputDir)
494
Anna Zaks817ce3d2012-09-06 23:30:27 +0000495 buildProject(Dir, SBOutputDir, ProjectBuildMode, IsReferenceBuild)
Anna Zaks1b417162011-10-06 23:26:27 +0000496
497 checkBuild(SBOutputDir)
498
Jordan Rose191e2b12012-08-31 00:36:30 +0000499 if IsReferenceBuild == False:
500 runCmpResults(Dir)
Anna Zaks1b417162011-10-06 23:26:27 +0000501
502 print "Completed tests for project %s (time: %.2f)." % \
503 (ID, (time.time()-TBegin))
504
Jordan Rose6d7e3722012-06-01 16:24:38 +0000505def testAll(IsReferenceBuild = False, UpdateSVN = False):
Anna Zaks1b417162011-10-06 23:26:27 +0000506 PMapFile = open(getProjectMapPath(), "rb")
Anna Zaks09e9cf02012-02-03 06:35:23 +0000507 try:
508 # Validate the input.
509 for I in csv.reader(PMapFile):
Anna Zaks45518b12011-11-05 05:20:48 +0000510 if (len(I) != 2) :
511 print "Error: Rows in the ProjectMapFile should have 3 entries."
512 raise Exception()
Anna Zaks817ce3d2012-09-06 23:30:27 +0000513 if (not ((I[1] == "0") | (I[1] == "1") | (I[1] == "2"))):
514 print "Error: Second entry in the ProjectMapFile should be 0" \
515 " (single file), 1 (project), or 2(single file c++11)."
Anna Zaks45518b12011-11-05 05:20:48 +0000516 raise Exception()
Anna Zaks09e9cf02012-02-03 06:35:23 +0000517
518 # When we are regenerating the reference results, we might need to
519 # update svn. Remove reference results from SVN.
520 if UpdateSVN == True:
Jordan Rose6d7e3722012-06-01 16:24:38 +0000521 assert(IsReferenceBuild == True);
Anna Zaks09e9cf02012-02-03 06:35:23 +0000522 updateSVN("delete", PMapFile);
523
524 # Test the projects.
525 PMapFile.seek(0)
526 for I in csv.reader(PMapFile):
Jordan Rose6d7e3722012-06-01 16:24:38 +0000527 testProject(I[0], int(I[1]), IsReferenceBuild)
Anna Zaks09e9cf02012-02-03 06:35:23 +0000528
529 # Add reference results to SVN.
530 if UpdateSVN == True:
531 updateSVN("add", PMapFile);
532
Anna Zaks45518b12011-11-05 05:20:48 +0000533 except:
534 print "Error occurred. Premature termination."
535 raise
Anna Zaks1b417162011-10-06 23:26:27 +0000536 finally:
537 PMapFile.close()
538
539if __name__ == '__main__':
Anna Zaks09e9cf02012-02-03 06:35:23 +0000540 IsReference = False
541 UpdateSVN = False
542 if len(sys.argv) >= 2:
543 if sys.argv[1] == "-r":
544 IsReference = True
545 elif sys.argv[1] == "-rs":
546 IsReference = True
547 UpdateSVN = True
548 else:
549 print >> sys.stderr, 'Usage: ', sys.argv[0],\
550 '[-r|-rs]' \
551 'Use -r to regenerate reference output' \
552 'Use -rs to regenerate reference output and update svn'
553
554 testAll(IsReference, UpdateSVN)