import subprocess
import sys
+def to_bytes(str):
+ # Encode to UTF-8 to get binary data.
+ return str.encode('utf-8')
+
+def to_string(bytes):
+ if isinstance(bytes, str):
+ return bytes
+ return to_bytes(bytes)
+
+def convert_string(bytes):
+ try:
+ return to_string(bytes.decode('utf-8'))
+ except UnicodeError:
+ return str(bytes)
+
def detectCPUs():
"""
Detects the number of CPUs on a system. Cribbed from pp.
if not path or os.path.exists(path):
return
- parent = os.path.dirname(path)
+ parent = os.path.dirname(path)
if parent != path:
mkdir_p(parent)
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env=env)
out,_ = p.communicate()
- return out
+ return convert_string(out)
def which(command, paths = None):
"""which(command, [paths]) - Look up the given command in the paths string
for path in paths.split(os.pathsep):
for ext in pathext:
p = os.path.join(path, command + ext)
- if os.path.exists(p):
+ if os.path.exists(p) and not os.path.isdir(p):
return p
return None
# Close extra file handles on UNIX (on Windows this cannot be done while
# also redirecting input).
kUseCloseFDs = not (platform.system() == 'Windows')
-def executeCommand(command, cwd=None, env=None):
+def executeCommand(command, cwd=None, env=None, input=None):
p = subprocess.Popen(command, cwd=cwd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=env, close_fds=kUseCloseFDs)
- out,err = p.communicate()
+ out,err = p.communicate(input=input)
exitCode = p.wait()
# Detect Ctrl-C in subprocess.
raise KeyboardInterrupt
# Ensure the resulting output is always of string type.
- try:
- out = str(out.decode('ascii'))
- except:
- out = str(out)
- try:
- err = str(err.decode('ascii'))
- except:
- err = str(err)
+ out = convert_string(out)
+ err = convert_string(err)
return out, err, exitCode
+
+def usePlatformSdkOnDarwin(config, lit_config):
+ # On Darwin, support relocatable SDKs by providing Clang with a
+ # default system root path.
+ if 'darwin' in config.target_triple:
+ try:
+ cmd = subprocess.Popen(['xcrun', '--show-sdk-path'],
+ stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+ out, err = cmd.communicate()
+ out = out.strip()
+ res = cmd.wait()
+ except OSError:
+ res = -1
+ if res == 0 and out:
+ sdk_path = out
+ lit_config.note('using SDKROOT: %r' % sdk_path)
+ config.environment['SDKROOT'] = sdk_path