blob: 0453e4a1f018498e0bf1fbf609da23e14826a892 [file] [log] [blame]
Vedant Kumare56ddc52019-09-05 21:24:23 +00001#===----------------------------------------------------------------------===##
2#
3# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4# See https://llvm.org/LICENSE.txt for license information.
5# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6#
7#===----------------------------------------------------------------------===##
8
9"""run.py is a utility for running a program.
10
11It can perform code signing, forward arguments to the program, and return the
12program's error code.
13"""
14
Louis Dionneab950ac2020-03-20 19:28:36 -040015import argparse
16import os
Louis Dionne2022e662020-04-01 18:48:22 -040017import shutil
Vedant Kumare56ddc52019-09-05 21:24:23 +000018import subprocess
19import sys
20
21
22def main():
Louis Dionnea6d477a2020-03-20 18:11:38 -040023 parser = argparse.ArgumentParser()
Louis Dionne53129a62020-04-07 15:42:00 -040024 parser.add_argument('--execdir', type=str, required=True)
Louis Dionne00fddf42020-04-03 17:50:39 -040025 parser.add_argument('--codesign_identity', type=str, required=False, default=None)
26 parser.add_argument('--dependencies', type=str, nargs='*', required=False, default=[])
27 parser.add_argument('--env', type=str, nargs='*', required=False, default=dict())
Louis Dionnea6d477a2020-03-20 18:11:38 -040028 (args, remaining) = parser.parse_known_args(sys.argv[1:])
Vedant Kumare56ddc52019-09-05 21:24:23 +000029
Louis Dionnea6d477a2020-03-20 18:11:38 -040030 if len(remaining) < 2:
31 sys.stderr.write('Missing actual commands to run')
32 exit(1)
Louis Dionneac78e5b2020-04-17 16:43:35 -040033 commandLine = remaining[1:] # Skip the '--'
Vedant Kumare56ddc52019-09-05 21:24:23 +000034
35 # Do any necessary codesigning.
Louis Dionnea6d477a2020-03-20 18:11:38 -040036 if args.codesign_identity:
Louis Dionneac78e5b2020-04-17 16:43:35 -040037 exe = commandLine[0]
Louis Dionnea6d477a2020-03-20 18:11:38 -040038 rc = subprocess.call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})
39 if rc != 0:
40 sys.stderr.write('Failed to codesign: ' + exe)
41 return rc
Vedant Kumare56ddc52019-09-05 21:24:23 +000042
Louis Dionnea6d477a2020-03-20 18:11:38 -040043 # Extract environment variables into a dictionary
Louis Dionneab950ac2020-03-20 19:28:36 -040044 env = {k : v for (k, v) in map(lambda s: s.split('=', 1), args.env)}
Louis Dionnea6d477a2020-03-20 18:11:38 -040045
Louis Dionne53129a62020-04-07 15:42:00 -040046 # Create the execution directory, and make sure we remove it at the end.
Louis Dionne2022e662020-04-01 18:48:22 -040047 try:
Louis Dionnee37be2e2020-04-14 16:20:00 -040048 os.makedirs(args.execdir)
Louis Dionnea6d477a2020-03-20 18:11:38 -040049
Louis Dionne53129a62020-04-07 15:42:00 -040050 # Ensure the file dependencies exist and copy them to the execution directory.
Louis Dionne2022e662020-04-01 18:48:22 -040051 for dep in args.dependencies:
52 if not os.path.exists(dep):
53 sys.stderr.write('Missing file or directory "{}" marked as a dependency of a test'.format(dep))
54 exit(1)
55 if os.path.isdir(dep):
Louis Dionne53129a62020-04-07 15:42:00 -040056 shutil.copytree(dep, os.path.join(args.execdir, os.path.basename(dep)), symlinks=True)
Louis Dionne2022e662020-04-01 18:48:22 -040057 else:
Louis Dionne53129a62020-04-07 15:42:00 -040058 shutil.copy2(dep, args.execdir)
Louis Dionne2022e662020-04-01 18:48:22 -040059
Louis Dionneac78e5b2020-04-17 16:43:35 -040060 # Run the command line with the given environment in the execution directory.
61 return subprocess.call(subprocess.list2cmdline(commandLine), cwd=args.execdir, env=env, shell=True)
Louis Dionne2022e662020-04-01 18:48:22 -040062 finally:
Louis Dionne53129a62020-04-07 15:42:00 -040063 shutil.rmtree(args.execdir)
64
Vedant Kumare56ddc52019-09-05 21:24:23 +000065
66if __name__ == '__main__':
67 exit(main())