blob: e88fff12824821f584a11527d791f5de6915f47f [file] [log] [blame]
Louis Dionne1c28a702020-06-12 10:28:19 -04001#!/usr/bin/env python
Vedant Kumare56ddc52019-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 Dionneab950ac2020-03-20 19:28:36 -040016import argparse
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()
Louis Dionne53129a62020-04-07 15:42:00 -040023 parser.add_argument('--execdir', type=str, required=True)
Louis Dionne00fddf42020-04-03 17:50:39 -040024 parser.add_argument('--codesign_identity', type=str, required=False, default=None)
Louis Dionne00fddf42020-04-03 17:50:39 -040025 parser.add_argument('--env', type=str, nargs='*', required=False, default=dict())
Louis Dionnea6d477a2020-03-20 18:11:38 -040026 (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)
Louis Dionneac78e5b2020-04-17 16:43:35 -040031 commandLine = 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:
Louis Dionneac78e5b2020-04-17 16:43:35 -040035 exe = commandLine[0]
Louis Dionnea6d477a2020-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 Kumare56ddc52019-09-05 21:24:23 +000040
Louis Dionnea6d477a2020-03-20 18:11:38 -040041 # Extract environment variables into a dictionary
Louis Dionneab950ac2020-03-20 19:28:36 -040042 env = {k : v for (k, v) in map(lambda s: s.split('=', 1), args.env)}
Louis Dionnea6d477a2020-03-20 18:11:38 -040043
Louis Dionnef3fe5f32020-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 Dionne53129a62020-04-07 15:42:00 -040046
Vedant Kumare56ddc52019-09-05 21:24:23 +000047
48if __name__ == '__main__':
49 exit(main())