xref: /llvm-project/clang/utils/analyzer/SATestUtils.py (revision a932c8745c778b03ffb8f6e7fb2e7d8134d40be3)
1import os
2from subprocess import check_output, check_call
3import sys
4
5
6Verbose = 1
7
8def which(command, paths=None):
9    """which(command, [paths]) - Look up the given command in the paths string
10    (or the PATH environment variable, if unspecified)."""
11
12    if paths is None:
13        paths = os.environ.get('PATH', '')
14
15    # Check for absolute match first.
16    if os.path.exists(command):
17        return command
18
19    # Would be nice if Python had a lib function for this.
20    if not paths:
21        paths = os.defpath
22
23    # Get suffixes to search.
24    # On Cygwin, 'PATHEXT' may exist but it should not be used.
25    if os.pathsep == ';':
26        pathext = os.environ.get('PATHEXT', '').split(';')
27    else:
28        pathext = ['']
29
30    # Search the paths...
31    for path in paths.split(os.pathsep):
32        for ext in pathext:
33            p = os.path.join(path, command + ext)
34            if os.path.exists(p):
35                return p
36
37    return None
38
39
40class flushfile(object):
41    """
42    Wrapper to flush the output after every print statement.
43    """
44    def __init__(self, f):
45        self.f = f
46
47    def write(self, x):
48        self.f.write(x)
49        self.f.flush()
50
51
52def hasNoExtension(FileName):
53    (Root, Ext) = os.path.splitext(FileName)
54    return (Ext == "")
55
56
57def isValidSingleInputFile(FileName):
58    (Root, Ext) = os.path.splitext(FileName)
59    return Ext in (".i", ".ii", ".c", ".cpp", ".m", "")
60
61
62def getSDKPath(SDKName):
63    """
64    Get the path to the SDK for the given SDK name. Returns None if
65    the path cannot be determined.
66    """
67    if which("xcrun") is None:
68        return None
69
70    Cmd = "xcrun --sdk " + SDKName + " --show-sdk-path"
71    return check_output(Cmd, shell=True).rstrip()
72
73
74def runScript(ScriptPath, PBuildLogFile, Cwd):
75    """
76    Run the provided script if it exists.
77    """
78    if os.path.exists(ScriptPath):
79        try:
80            if Verbose == 1:
81                print "  Executing: %s" % (ScriptPath,)
82            check_call("chmod +x '%s'" % ScriptPath, cwd=Cwd,
83                       stderr=PBuildLogFile,
84                       stdout=PBuildLogFile,
85                       shell=True)
86            check_call("'%s'" % ScriptPath, cwd=Cwd,
87                       stderr=PBuildLogFile,
88                       stdout=PBuildLogFile,
89                       shell=True)
90        except:
91            print "Error: Running %s failed. See %s for details." % (
92                  ScriptPath, PBuildLogFile.name)
93            sys.exit(-1)
94
95
96def isCommentCSVLine(Entries):
97    """
98    Treat CSV lines starting with a '#' as a comment.
99    """
100    return len(Entries) > 0 and Entries[0].startswith("#")
101