blob: e88fff12824821f584a11527d791f5de6915f47f [file] [log] [blame]
Louis Dionnef998e0d2020-06-12 10:28:19 -04001#!/usr/bin/env python
Vedant Kumar1261f1b2019-09-05 21:24:23 +00002#===----------------------------------------------------------------------===##
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7#
8#===----------------------------------------------------------------------===##
9
10"""run.py is a utility for running a program.
11
12It can perform code signing, forward arguments to the program, and return the
13program's error code.
14"""
15
Louis Dionne0feaf222020-03-20 19:28:36 -040016import argparse
Vedant Kumar1261f1b2019-09-05 21:24:23 +000017import subprocess
18import sys
19
20
21def main():
Louis Dionnee22fe982020-03-20 18:11:38 -040022 parser = argparse.ArgumentParser()
Louis Dionne3fefda62020-04-07 15:42:00 -040023 parser.add_argument('--execdir', type=str, required=True)
Louis Dionneceb58ad2020-04-03 17:50:39 -040024 parser.add_argument('--codesign_identity', type=str, required=False, default=None)
Louis Dionneceb58ad2020-04-03 17:50:39 -040025 parser.add_argument('--env', type=str, nargs='*', required=False, default=dict())
Louis Dionnee22fe982020-03-20 18:11:38 -040026 (args, remaining) = parser.parse_known_args(sys.argv[1:])
Vedant Kumar1261f1b2019-09-05 21:24:23 +000027
Louis Dionnee22fe982020-03-20 18:11:38 -040028 if len(remaining) < 2:
29 sys.stderr.write('Missing actual commands to run')
30 exit(1)
Louis Dionne5eb8d452020-04-17 16:43:35 -040031 commandLine = remaining[1:] # Skip the '--'
Vedant Kumar1261f1b2019-09-05 21:24:23 +000032
33 # Do any necessary codesigning.
Louis Dionnee22fe982020-03-20 18:11:38 -040034 if args.codesign_identity:
Louis Dionne5eb8d452020-04-17 16:43:35 -040035 exe = commandLine[0]
Louis Dionnee22fe982020-03-20 18:11:38 -040036 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 Kumar1261f1b2019-09-05 21:24:23 +000040
Louis Dionnee22fe982020-03-20 18:11:38 -040041 # Extract environment variables into a dictionary
Louis Dionne0feaf222020-03-20 19:28:36 -040042 env = {k : v for (k, v) in map(lambda s: s.split('=', 1), args.env)}
Louis Dionnee22fe982020-03-20 18:11:38 -040043
Louis Dionne1fc50102020-06-10 14:41:47 -040044 # Run the command line with the given environment in the execution directory.
45 return subprocess.call(subprocess.list2cmdline(commandLine), cwd=args.execdir, env=env, shell=True)
Louis Dionne3fefda62020-04-07 15:42:00 -040046
Vedant Kumar1261f1b2019-09-05 21:24:23 +000047
48if __name__ == '__main__':
49 exit(main())