blob: 33d0095a17ea28f0f2c92b7c5c914a7141e66061 [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
15import subprocess
16import sys
Louis Dionnea6d477a2020-03-20 18:11:38 -040017import argparse
Vedant Kumare56ddc52019-09-05 21:24:23 +000018
19
20def main():
Louis Dionnea6d477a2020-03-20 18:11:38 -040021 parser = argparse.ArgumentParser()
22 parser.add_argument('--codesign_identity', type=str, required=False)
23 parser.add_argument('--working_directory', type=str, required=True)
24 parser.add_argument('--dependencies', type=str, nargs='*', required=True)
25 parser.add_argument('--env', type=str, nargs='*', required=True)
26 (args, remaining) = parser.parse_known_args(sys.argv[1:])
Vedant Kumare56ddc52019-09-05 21:24:23 +000027
Louis Dionnea6d477a2020-03-20 18:11:38 -040028 if len(remaining) < 2:
29 sys.stderr.write('Missing actual commands to run')
30 exit(1)
31 remaining = remaining[1:] # Skip the '--'
Vedant Kumare56ddc52019-09-05 21:24:23 +000032
33 # Do any necessary codesigning.
Louis Dionnea6d477a2020-03-20 18:11:38 -040034 if args.codesign_identity:
35 exe = remaining[0]
36 rc = subprocess.call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})
37 if rc != 0:
38 sys.stderr.write('Failed to codesign: ' + exe)
39 return rc
Vedant Kumare56ddc52019-09-05 21:24:23 +000040
Louis Dionnea6d477a2020-03-20 18:11:38 -040041 # Extract environment variables into a dictionary
42 env = {k : v for (k, v) in map(lambda s: s.split('='), args.env)}
43
44 # Ensure the file dependencies exist
45 for file in args.dependencies:
46 if not os.path.exists(file):
47 sys.stderr.write('Missing file {} marked as a dependency of a test'.format(file))
48 exit(1)
49
50 # Run the executable with the given environment in the given working directory
51 return subprocess.call(remaining, cwd=args.working_directory, env=env)
Vedant Kumare56ddc52019-09-05 21:24:23 +000052
53if __name__ == '__main__':
54 exit(main())