Automatically use more cores when the test build uses 'make'.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@162790 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/utils/analyzer/SATestBuild.py b/utils/analyzer/SATestBuild.py
index 8f008f1..9c99d0d 100755
--- a/utils/analyzer/SATestBuild.py
+++ b/utils/analyzer/SATestBuild.py
@@ -42,6 +42,7 @@
 import csv
 import sys
 import glob
+import math
 import shutil
 import time
 import plistlib
@@ -51,6 +52,26 @@
 # Helper functions.
 #------------------------------------------------------------------------------
 
+def detectCPUs():
+    """
+    Detects the number of CPUs on a system. Cribbed from pp.
+    """
+    # Linux, Unix and MacOS:
+    if hasattr(os, "sysconf"):
+        if os.sysconf_names.has_key("SC_NPROCESSORS_ONLN"):
+            # Linux & Unix:
+            ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
+            if isinstance(ncpus, int) and ncpus > 0:
+                return ncpus
+        else: # OSX:
+            return int(capture(['sysctl', '-n', 'hw.ncpu']))
+    # Windows:
+    if os.environ.has_key("NUMBER_OF_PROCESSORS"):
+        ncpus = int(os.environ["NUMBER_OF_PROCESSORS"])
+        if ncpus > 0:
+            return ncpus
+    return 1 # Default
+
 def which(command, paths = None):
    """which(command, [paths]) - Look up the given command in the paths string
    (or the PATH environment variable, if unspecified)."""
@@ -120,6 +141,9 @@
     print "Error: cannot find 'clang' in PATH"
     sys.exit(-1)
 
+# Number of jobs.
+Jobs = math.ceil(detectCPUs() * 0.75)
+
 # Project map stores info about all the "registered" projects.
 ProjectMapFile = "projectMap.csv"
 
@@ -186,6 +210,11 @@
         SBCommandFile = open(BuildScriptPath, "r")
         SBPrefix = "scan-build " + SBOptions + " "
         for Command in SBCommandFile:
+            # If using 'make', auto imply a -jX argument
+            # to speed up analysis.  xcodebuild will
+            # automatically use the maximum number of cores.
+            if Command.startsWith("make "):
+                Command += "-j" + Jobs
             SBCommand = SBPrefix + Command
             if Verbose == 1:        
                 print "  Executing: %s" % (SBCommand,)