blob: e91836b96886771062eea0ac40ab0435ce6c773c [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
Vedant Kumare56ddc52019-09-05 21:24:23 +000016import subprocess
17import sys
18
19
20def main():
Louis Dionnea6d477a2020-03-20 18:11:38 -040021 parser = argparse.ArgumentParser()
Louis Dionne53129a62020-04-07 15:42:00 -040022 parser.add_argument('--execdir', type=str, required=True)
Louis Dionne00fddf42020-04-03 17:50:39 -040023 parser.add_argument('--codesign_identity', type=str, required=False, default=None)
Louis Dionne00fddf42020-04-03 17:50:39 -040024 parser.add_argument('--env', type=str, nargs='*', required=False, default=dict())
Louis Dionnea6d477a2020-03-20 18:11:38 -040025 (args, remaining) = parser.parse_known_args(sys.argv[1:])
Vedant Kumare56ddc52019-09-05 21:24:23 +000026
Louis Dionnea6d477a2020-03-20 18:11:38 -040027 if len(remaining) < 2:
28 sys.stderr.write('Missing actual commands to run')
29 exit(1)
Louis Dionneac78e5b2020-04-17 16:43:35 -040030 commandLine = remaining[1:] # Skip the '--'
Vedant Kumare56ddc52019-09-05 21:24:23 +000031
32 # Do any necessary codesigning.
Louis Dionnea6d477a2020-03-20 18:11:38 -040033 if args.codesign_identity:
Louis Dionneac78e5b2020-04-17 16:43:35 -040034 exe = commandLine[0]
Louis Dionnea6d477a2020-03-20 18:11:38 -040035 rc = subprocess.call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})
36 if rc != 0:
37 sys.stderr.write('Failed to codesign: ' + exe)
38 return rc
Vedant Kumare56ddc52019-09-05 21:24:23 +000039
Louis Dionnea6d477a2020-03-20 18:11:38 -040040 # Extract environment variables into a dictionary
Louis Dionneab950ac2020-03-20 19:28:36 -040041 env = {k : v for (k, v) in map(lambda s: s.split('=', 1), args.env)}
Louis Dionnea6d477a2020-03-20 18:11:38 -040042
Louis Dionnef3fe5f32020-06-10 14:41:47 -040043 # Run the command line with the given environment in the execution directory.
44 return subprocess.call(subprocess.list2cmdline(commandLine), cwd=args.execdir, env=env, shell=True)
Louis Dionne53129a62020-04-07 15:42:00 -040045
Vedant Kumare56ddc52019-09-05 21:24:23 +000046
47if __name__ == '__main__':
48 exit(main())