2012-01-04 06:05:57 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
# [PR 11661] Note that we hardwire to /usr/bin/python because we
|
|
|
|
# want to the use the system version of Python on Mac OS X.
|
|
|
|
# This one has the scripting bridge enabled.
|
2010-02-09 04:54:01 +08:00
|
|
|
|
2013-01-05 03:04:42 +08:00
|
|
|
import sys
|
|
|
|
if sys.version_info < (2, 7):
|
|
|
|
print "set-xcode-analyzer requires Python 2.7 or later"
|
|
|
|
sys.exit(1)
|
|
|
|
|
2010-02-09 04:54:01 +08:00
|
|
|
import os
|
2012-02-23 02:44:35 +08:00
|
|
|
import subprocess
|
2010-02-09 04:54:01 +08:00
|
|
|
import re
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
|
|
|
import stat
|
2010-02-10 02:46:58 +08:00
|
|
|
from AppKit import *
|
2010-02-09 04:54:01 +08:00
|
|
|
|
|
|
|
def FindClangSpecs(path):
|
2012-02-23 02:44:35 +08:00
|
|
|
print "(+) Searching for xcspec file in: ", path
|
2010-02-09 04:54:01 +08:00
|
|
|
for root, dirs, files in os.walk(path):
|
|
|
|
for f in files:
|
|
|
|
if f.endswith(".xcspec") and f.startswith("Clang LLVM"):
|
|
|
|
yield os.path.join(root, f)
|
|
|
|
|
2012-10-11 08:40:41 +08:00
|
|
|
def ModifySpec(path, isBuiltinAnalyzer, pathToChecker):
|
2010-02-09 04:54:01 +08:00
|
|
|
t = tempfile.NamedTemporaryFile(delete=False)
|
|
|
|
foundAnalyzer = False
|
|
|
|
with open(path) as f:
|
2012-10-11 08:40:41 +08:00
|
|
|
if isBuiltinAnalyzer:
|
|
|
|
# First search for CLANG_ANALYZER_EXEC. Newer
|
|
|
|
# versions of Xcode set EXEC_PATH to be CLANG_ANALYZER_EXEC.
|
|
|
|
with open(path) as f2:
|
|
|
|
for line in f2:
|
|
|
|
if line.find("CLANG_ANALYZER_EXEC") >= 0:
|
|
|
|
pathToChecker = "$(CLANG_ANALYZER_EXEC)"
|
|
|
|
break
|
|
|
|
# Now create a new file.
|
2010-02-09 04:54:01 +08:00
|
|
|
for line in f:
|
|
|
|
if not foundAnalyzer:
|
|
|
|
if line.find("Static Analyzer") >= 0:
|
|
|
|
foundAnalyzer = True
|
|
|
|
else:
|
|
|
|
m = re.search('^(\s*ExecPath\s*=\s*")', line)
|
|
|
|
if m:
|
|
|
|
line = "".join([m.group(0), pathToChecker, '";\n'])
|
2013-01-05 15:31:54 +08:00
|
|
|
# Do not modify further ExecPath's later in the xcspec.
|
|
|
|
foundAnalyzer = False
|
2010-02-09 04:54:01 +08:00
|
|
|
t.write(line)
|
|
|
|
t.close()
|
2010-02-10 02:51:44 +08:00
|
|
|
print "(+) processing:", path
|
2010-02-09 05:19:27 +08:00
|
|
|
try:
|
|
|
|
shutil.copy(t.name, path)
|
|
|
|
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
|
|
|
|
except IOError, why:
|
2010-02-10 02:51:44 +08:00
|
|
|
print " (-) Cannot update file:", why, "\n"
|
2010-02-09 05:19:27 +08:00
|
|
|
except OSError, why:
|
2010-02-10 02:51:44 +08:00
|
|
|
print " (-) Cannot update file:", why, "\n"
|
2010-02-09 04:54:01 +08:00
|
|
|
os.unlink(t.name)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
from optparse import OptionParser
|
|
|
|
parser = OptionParser('usage: %prog [options]')
|
|
|
|
parser.set_description(__doc__)
|
|
|
|
parser.add_option("--use-checker-build", dest="path",
|
|
|
|
help="Use the Clang located at the provided absolute path, e.g. /Users/foo/checker-1")
|
|
|
|
parser.add_option("--use-xcode-clang", action="store_const",
|
|
|
|
const="$(CLANG)", dest="default",
|
|
|
|
help="Use the Clang bundled with Xcode")
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
if options.path is None and options.default is None:
|
|
|
|
parser.error("You must specify a version of Clang to use for static analysis. Specify '-h' for details")
|
|
|
|
|
2010-02-10 02:46:58 +08:00
|
|
|
# determine if Xcode is running
|
|
|
|
for x in NSWorkspace.sharedWorkspace().runningApplications():
|
|
|
|
if x.localizedName().find("Xcode") >= 0:
|
2010-02-10 02:51:44 +08:00
|
|
|
print "(-) You must quit Xcode first before modifying its configuration files."
|
2013-01-12 05:49:36 +08:00
|
|
|
sys.exit(1)
|
2010-02-10 02:46:58 +08:00
|
|
|
|
2012-10-11 08:40:41 +08:00
|
|
|
isBuiltinAnalyzer = False
|
2010-02-09 04:54:01 +08:00
|
|
|
if options.path:
|
|
|
|
# Expand tildes.
|
|
|
|
path = os.path.expanduser(options.path)
|
|
|
|
if not path.endswith("clang"):
|
2010-02-10 02:51:44 +08:00
|
|
|
print "(+) Using Clang bundled with checker build:", path
|
2010-02-09 04:54:01 +08:00
|
|
|
path = os.path.join(path, "bin", "clang");
|
|
|
|
else:
|
2010-02-10 02:51:44 +08:00
|
|
|
print "(+) Using Clang located at:", path
|
2010-02-09 04:54:01 +08:00
|
|
|
else:
|
2010-02-10 02:51:44 +08:00
|
|
|
print "(+) Using the Clang bundled with Xcode"
|
2010-02-09 04:54:01 +08:00
|
|
|
path = options.default
|
2012-10-11 08:40:41 +08:00
|
|
|
isBuiltinAnalyzer = True
|
2010-02-10 02:46:58 +08:00
|
|
|
|
2012-07-17 05:39:29 +08:00
|
|
|
try:
|
|
|
|
xcode_path = subprocess.check_output(["xcode-select", "-print-path"])
|
|
|
|
except AttributeError:
|
|
|
|
# Fall back to the default install location when using Python < 2.7.0
|
|
|
|
xcode_path = "/Developer"
|
2012-09-27 02:13:03 +08:00
|
|
|
if (xcode_path.find(".app/") != -1):
|
2012-02-23 02:44:35 +08:00
|
|
|
# Cut off the 'Developer' dir, as the xcspec lies in another part
|
|
|
|
# of the Xcode.app subtree.
|
|
|
|
xcode_path = os.path.dirname(xcode_path)
|
|
|
|
|
2012-09-27 02:19:55 +08:00
|
|
|
foundSpec = False
|
2012-02-23 02:44:35 +08:00
|
|
|
for x in FindClangSpecs(xcode_path):
|
2012-09-27 02:19:55 +08:00
|
|
|
foundSpec = True
|
2012-10-11 08:40:41 +08:00
|
|
|
ModifySpec(x, isBuiltinAnalyzer, path)
|
2012-09-27 02:19:55 +08:00
|
|
|
|
|
|
|
if foundSpec == False:
|
|
|
|
print "(-) No compiler configuration file was found. Xcode's analyzer has not been updated."
|
2010-02-09 04:54:01 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
|