blob: 2320652619ed3ddb8ce37f1086205f0b69255f69 [file] [log] [blame]
George Karpenkovbf92c442017-10-24 23:52:48 +00001import 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
George Karpenkovbf92c442017-10-24 23:52:48 +000040def hasNoExtension(FileName):
41 (Root, Ext) = os.path.splitext(FileName)
42 return (Ext == "")
43
44
45def isValidSingleInputFile(FileName):
46 (Root, Ext) = os.path.splitext(FileName)
47 return Ext in (".i", ".ii", ".c", ".cpp", ".m", "")
48
49
50def getSDKPath(SDKName):
51 """
52 Get the path to the SDK for the given SDK name. Returns None if
53 the path cannot be determined.
54 """
55 if which("xcrun") is None:
56 return None
57
58 Cmd = "xcrun --sdk " + SDKName + " --show-sdk-path"
59 return check_output(Cmd, shell=True).rstrip()
60
61
George Karpenkovf37d3a52018-02-08 21:22:42 +000062def runScript(ScriptPath, PBuildLogFile, Cwd, Stdout=sys.stdout,
63 Stderr=sys.stderr):
George Karpenkovbf92c442017-10-24 23:52:48 +000064 """
65 Run the provided script if it exists.
66 """
67 if os.path.exists(ScriptPath):
68 try:
69 if Verbose == 1:
George Karpenkovf37d3a52018-02-08 21:22:42 +000070 Stdout.write(" Executing: %s\n" % (ScriptPath,))
George Karpenkovbf92c442017-10-24 23:52:48 +000071 check_call("chmod +x '%s'" % ScriptPath, cwd=Cwd,
72 stderr=PBuildLogFile,
73 stdout=PBuildLogFile,
74 shell=True)
75 check_call("'%s'" % ScriptPath, cwd=Cwd,
76 stderr=PBuildLogFile,
77 stdout=PBuildLogFile,
78 shell=True)
79 except:
George Karpenkovf37d3a52018-02-08 21:22:42 +000080 Stderr.write("Error: Running %s failed. See %s for details.\n" % (
81 ScriptPath, PBuildLogFile.name))
George Karpenkovbf92c442017-10-24 23:52:48 +000082 sys.exit(-1)
83
84
George Karpenkovbf92c442017-10-24 23:52:48 +000085def isCommentCSVLine(Entries):
86 """
87 Treat CSV lines starting with a '#' as a comment.
88 """
89 return len(Entries) > 0 and Entries[0].startswith("#")