2020-06-12 22:28:19 +08:00
|
|
|
#!/usr/bin/env python
|
2019-09-06 05:24:23 +08:00
|
|
|
#===----------------------------------------------------------------------===##
|
|
|
|
#
|
|
|
|
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
# See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#
|
|
|
|
#===----------------------------------------------------------------------===##
|
|
|
|
|
|
|
|
"""run.py is a utility for running a program.
|
|
|
|
|
|
|
|
It can perform code signing, forward arguments to the program, and return the
|
|
|
|
program's error code.
|
|
|
|
"""
|
|
|
|
|
2020-03-21 07:28:36 +08:00
|
|
|
import argparse
|
2020-10-18 04:57:19 +08:00
|
|
|
import os
|
|
|
|
import platform
|
2019-09-06 05:24:23 +08:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2020-03-21 06:11:38 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
2020-04-08 03:42:00 +08:00
|
|
|
parser.add_argument('--execdir', type=str, required=True)
|
2020-04-04 05:50:39 +08:00
|
|
|
parser.add_argument('--codesign_identity', type=str, required=False, default=None)
|
|
|
|
parser.add_argument('--env', type=str, nargs='*', required=False, default=dict())
|
2020-07-21 15:35:47 +08:00
|
|
|
parser.add_argument("command", nargs=argparse.ONE_OR_MORE)
|
|
|
|
args = parser.parse_args()
|
|
|
|
commandLine = args.command
|
2019-09-06 05:24:23 +08:00
|
|
|
|
2021-04-01 21:47:49 +08:00
|
|
|
# HACK:
|
|
|
|
# If an argument is a file that ends in `.tmp.exe`, assume it is the name
|
|
|
|
# of an executable generated by a test file. We call these test-executables
|
|
|
|
# below. This allows us to do custom processing like codesigning test-executables.
|
|
|
|
# It's also possible for there to be no such executable, for example in the case
|
|
|
|
# of a .sh.cpp test.
|
|
|
|
isTestExe = lambda exe: exe.endswith('.tmp.exe') and os.path.exists(exe)
|
|
|
|
|
|
|
|
# Do any necessary codesigning of test-executables found in the command line.
|
2020-03-21 06:11:38 +08:00
|
|
|
if args.codesign_identity:
|
2021-04-01 21:47:49 +08:00
|
|
|
for exe in filter(isTestExe, commandLine):
|
|
|
|
subprocess.check_call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})
|
2020-03-21 06:11:38 +08:00
|
|
|
|
|
|
|
# Extract environment variables into a dictionary
|
2020-03-21 07:28:36 +08:00
|
|
|
env = {k : v for (k, v) in map(lambda s: s.split('=', 1), args.env)}
|
2020-10-18 04:57:19 +08:00
|
|
|
if platform.system() == 'Windows':
|
|
|
|
# Pass some extra variables through on Windows:
|
|
|
|
# COMSPEC is needed for running subprocesses via std::system().
|
|
|
|
if 'COMSPEC' in os.environ:
|
|
|
|
env['COMSPEC'] = os.environ.get('COMSPEC')
|
|
|
|
# TEMP is needed for placing temp files in a sensible directory.
|
|
|
|
if 'TEMP' in os.environ:
|
|
|
|
env['TEMP'] = os.environ.get('TEMP')
|
2020-03-21 06:11:38 +08:00
|
|
|
|
2020-06-11 02:41:47 +08:00
|
|
|
# Run the command line with the given environment in the execution directory.
|
2020-11-04 03:45:52 +08:00
|
|
|
return subprocess.call(commandLine, cwd=args.execdir, env=env, shell=False)
|
2020-04-08 03:42:00 +08:00
|
|
|
|
2019-09-06 05:24:23 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
exit(main())
|