blob: 3e9a4703c8c7e138ca5f9dc23df152388a9e5a8e [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
Vedant Kumare56ddc52019-09-05 21:24:23 +000017import subprocess
18import sys
19
20
21def main():
Louis Dionnea6d477a2020-03-20 18:11:38 -040022 parser = argparse.ArgumentParser()
23 parser.add_argument('--codesign_identity', type=str, required=False)
24 parser.add_argument('--working_directory', type=str, required=True)
25 parser.add_argument('--dependencies', type=str, nargs='*', required=True)
26 parser.add_argument('--env', type=str, nargs='*', required=True)
27 (args, remaining) = parser.parse_known_args(sys.argv[1:])
Vedant Kumare56ddc52019-09-05 21:24:23 +000028
Louis Dionnea6d477a2020-03-20 18:11:38 -040029 if len(remaining) < 2:
30 sys.stderr.write('Missing actual commands to run')
31 exit(1)
32 remaining = remaining[1:] # Skip the '--'
Vedant Kumare56ddc52019-09-05 21:24:23 +000033
34 # Do any necessary codesigning.
Louis Dionnea6d477a2020-03-20 18:11:38 -040035 if args.codesign_identity:
36 exe = remaining[0]
37 rc = subprocess.call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})
38 if rc != 0:
39 sys.stderr.write('Failed to codesign: ' + exe)
40 return rc
Vedant Kumare56ddc52019-09-05 21:24:23 +000041
Louis Dionnea6d477a2020-03-20 18:11:38 -040042 # Extract environment variables into a dictionary
Louis Dionneab950ac2020-03-20 19:28:36 -040043 env = {k : v for (k, v) in map(lambda s: s.split('=', 1), args.env)}
Louis Dionnea6d477a2020-03-20 18:11:38 -040044
45 # Ensure the file dependencies exist
46 for file in args.dependencies:
47 if not os.path.exists(file):
48 sys.stderr.write('Missing file {} marked as a dependency of a test'.format(file))
49 exit(1)
50
51 # Run the executable with the given environment in the given working directory
52 return subprocess.call(remaining, cwd=args.working_directory, env=env)
Vedant Kumare56ddc52019-09-05 21:24:23 +000053
54if __name__ == '__main__':
55 exit(main())