xref: /openbsd-src/gnu/llvm/clang/tools/scan-build/bin/set-xcode-analyzer (revision a9ac8606c53d55cee9c3a39778b249c51df111ef)
1*a9ac8606Spatrick#!/usr/bin/env python
2e5dd7070Spatrick
3e5dd7070Spatrick# [PR 11661] Note that we hardwire to /usr/bin/python because we
4e5dd7070Spatrick# want to the use the system version of Python on Mac OS X.
5e5dd7070Spatrick# This one has the scripting bridge enabled.
6e5dd7070Spatrick
7e5dd7070Spatrickimport sys
8*a9ac8606Spatrickif sys.version_info < (3, 6):
9*a9ac8606Spatrick    print "set-xcode-analyzer requires Python 3.6 or later"
10e5dd7070Spatrick    sys.exit(1)
11e5dd7070Spatrick
12e5dd7070Spatrickimport os
13e5dd7070Spatrickimport subprocess
14e5dd7070Spatrickimport re
15e5dd7070Spatrickimport tempfile
16e5dd7070Spatrickimport shutil
17e5dd7070Spatrickimport stat
18e5dd7070Spatrickfrom AppKit import *
19e5dd7070Spatrick
20e5dd7070Spatrickdef FindClangSpecs(path):
21e5dd7070Spatrick  print "(+) Searching for xcspec file in: ", path
22e5dd7070Spatrick  for root, dirs, files in os.walk(path):
23e5dd7070Spatrick    for f in files:
24e5dd7070Spatrick      if f.endswith(".xcspec") and f.startswith("Clang LLVM"):
25e5dd7070Spatrick        yield os.path.join(root, f)
26e5dd7070Spatrick
27e5dd7070Spatrickdef ModifySpec(path, isBuiltinAnalyzer, pathToChecker):
28e5dd7070Spatrick  t = tempfile.NamedTemporaryFile(delete=False)
29e5dd7070Spatrick  foundAnalyzer = False
30e5dd7070Spatrick  with open(path) as f:
31e5dd7070Spatrick    if isBuiltinAnalyzer:
32e5dd7070Spatrick      # First search for CLANG_ANALYZER_EXEC.  Newer
33e5dd7070Spatrick      # versions of Xcode set EXEC_PATH to be CLANG_ANALYZER_EXEC.
34e5dd7070Spatrick      with open(path) as f2:
35e5dd7070Spatrick        for line in f2:
36e5dd7070Spatrick          if line.find("CLANG_ANALYZER_EXEC") >= 0:
37e5dd7070Spatrick            pathToChecker = "$(CLANG_ANALYZER_EXEC)"
38e5dd7070Spatrick            break
39e5dd7070Spatrick    # Now create a new file.
40e5dd7070Spatrick    for line in f:
41e5dd7070Spatrick      if not foundAnalyzer:
42e5dd7070Spatrick        if line.find("Static Analyzer") >= 0:
43e5dd7070Spatrick          foundAnalyzer = True
44e5dd7070Spatrick      else:
45e5dd7070Spatrick        m = re.search(r'^(\s*ExecPath\s*=\s*")', line)
46e5dd7070Spatrick        if m:
47e5dd7070Spatrick          line = "".join([m.group(0), pathToChecker, '";\n'])
48e5dd7070Spatrick          # Do not modify further ExecPath's later in the xcspec.
49e5dd7070Spatrick          foundAnalyzer = False
50e5dd7070Spatrick      t.write(line)
51e5dd7070Spatrick  t.close()
52e5dd7070Spatrick  print "(+) processing:", path
53e5dd7070Spatrick  try:
54e5dd7070Spatrick    shutil.copy(t.name, path)
55e5dd7070Spatrick    os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
56e5dd7070Spatrick  except IOError, why:
57e5dd7070Spatrick    print "    (-) Cannot update file:", why, "\n"
58e5dd7070Spatrick  except OSError, why:
59e5dd7070Spatrick    print "    (-) Cannot update file:", why, "\n"
60e5dd7070Spatrick  os.unlink(t.name)
61e5dd7070Spatrick
62e5dd7070Spatrickdef main():
63e5dd7070Spatrick  from optparse import OptionParser
64e5dd7070Spatrick  parser = OptionParser('usage: %prog [options]')
65e5dd7070Spatrick  parser.set_description(__doc__)
66e5dd7070Spatrick  parser.add_option("--use-checker-build", dest="path",
67e5dd7070Spatrick                    help="Use the Clang located at the provided absolute path, e.g. /Users/foo/checker-1")
68e5dd7070Spatrick  parser.add_option("--use-xcode-clang", action="store_const",
69e5dd7070Spatrick                    const="$(CLANG)", dest="default",
70e5dd7070Spatrick                    help="Use the Clang bundled with Xcode")
71e5dd7070Spatrick  (options, args) = parser.parse_args()
72e5dd7070Spatrick  if options.path is None and options.default is None:
73e5dd7070Spatrick    parser.error("You must specify a version of Clang to use for static analysis.  Specify '-h' for details")
74e5dd7070Spatrick
75e5dd7070Spatrick  # determine if Xcode is running
76e5dd7070Spatrick  for x in NSWorkspace.sharedWorkspace().runningApplications():
77e5dd7070Spatrick    if x.localizedName().find("Xcode") >= 0:
78e5dd7070Spatrick      print "(-) You must quit Xcode first before modifying its configuration files."
79e5dd7070Spatrick      sys.exit(1)
80e5dd7070Spatrick
81e5dd7070Spatrick  isBuiltinAnalyzer = False
82e5dd7070Spatrick  if options.path:
83e5dd7070Spatrick    # Expand tildes.
84e5dd7070Spatrick    path = os.path.expanduser(options.path)
85e5dd7070Spatrick    if not path.endswith("clang"):
86e5dd7070Spatrick      print "(+) Using Clang bundled with checker build:", path
87e5dd7070Spatrick      path = os.path.join(path, "bin", "clang");
88e5dd7070Spatrick    else:
89e5dd7070Spatrick      print "(+) Using Clang located at:", path
90e5dd7070Spatrick  else:
91e5dd7070Spatrick    print "(+) Using the Clang bundled with Xcode"
92e5dd7070Spatrick    path = options.default
93e5dd7070Spatrick    isBuiltinAnalyzer = True
94e5dd7070Spatrick
95e5dd7070Spatrick  try:
96e5dd7070Spatrick    xcode_path = subprocess.check_output(["xcode-select", "-print-path"])
97e5dd7070Spatrick  except AttributeError:
98e5dd7070Spatrick    # Fall back to the default install location when using Python < 2.7.0
99e5dd7070Spatrick    xcode_path = "/Developer"
100e5dd7070Spatrick  if (xcode_path.find(".app/") != -1):
101e5dd7070Spatrick    # Cut off the 'Developer' dir, as the xcspec lies in another part
102e5dd7070Spatrick    # of the Xcode.app subtree.
103e5dd7070Spatrick    xcode_path = xcode_path.rsplit('/Developer', 1)[0]
104e5dd7070Spatrick
105e5dd7070Spatrick  foundSpec = False
106e5dd7070Spatrick  for x in FindClangSpecs(xcode_path):
107e5dd7070Spatrick    foundSpec = True
108e5dd7070Spatrick    ModifySpec(x, isBuiltinAnalyzer, path)
109e5dd7070Spatrick
110e5dd7070Spatrick  if foundSpec == False:
111e5dd7070Spatrick      print "(-) No compiler configuration file was found.  Xcode's analyzer has not been updated."
112e5dd7070Spatrick
113e5dd7070Spatrickif __name__ == '__main__':
114e5dd7070Spatrick  main()
115