blob: f8951ee43f5963541b1336a77f6c1c0a47cb6179 [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())
Alex Richardson5b312d22020-07-21 08:35:47 +010026 parser.add_argument("command", nargs=argparse.ONE_OR_MORE)
27 args = parser.parse_args()
28 commandLine = args.command
Vedant Kumare56ddc52019-09-05 21:24:23 +000029
30 # Do any necessary codesigning.
Louis Dionnea6d477a2020-03-20 18:11:38 -040031 if args.codesign_identity:
Louis Dionneac78e5b2020-04-17 16:43:35 -040032 exe = commandLine[0]
Louis Dionnea6d477a2020-03-20 18:11:38 -040033 rc = subprocess.call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})
34 if rc != 0:
35 sys.stderr.write('Failed to codesign: ' + exe)
36 return rc
Vedant Kumare56ddc52019-09-05 21:24:23 +000037
Louis Dionnea6d477a2020-03-20 18:11:38 -040038 # Extract environment variables into a dictionary
Louis Dionneab950ac2020-03-20 19:28:36 -040039 env = {k : v for (k, v) in map(lambda s: s.split('=', 1), args.env)}
Louis Dionnea6d477a2020-03-20 18:11:38 -040040
Louis Dionnef3fe5f32020-06-10 14:41:47 -040041 # Run the command line with the given environment in the execution directory.
Louis Dionnee57654f2020-11-03 14:45:52 -050042 return subprocess.call(commandLine, cwd=args.execdir, env=env, shell=False)
Louis Dionne53129a62020-04-07 15:42:00 -040043
Vedant Kumare56ddc52019-09-05 21:24:23 +000044
45if __name__ == '__main__':
46 exit(main())