xref: /llvm-project/libcxx/utils/run.py (revision f1a96de1bc8db527b5eb820c36c17e275900ca2b)
1#!/usr/bin/env python
2#===----------------------------------------------------------------------===##
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
16import argparse
17import subprocess
18import sys
19
20
21def main():
22    parser = argparse.ArgumentParser()
23    parser.add_argument('--execdir', type=str, required=True)
24    parser.add_argument('--codesign_identity', type=str, required=False, default=None)
25    parser.add_argument('--env', type=str, nargs='*', required=False, default=dict())
26    parser.add_argument("command", nargs=argparse.ONE_OR_MORE)
27    args = parser.parse_args()
28    commandLine = args.command
29
30    # Do any necessary codesigning.
31    if args.codesign_identity:
32        exe = commandLine[0]
33        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
37
38    # Extract environment variables into a dictionary
39    env = {k : v  for (k, v) in map(lambda s: s.split('=', 1), args.env)}
40
41    # Run the command line with the given environment in the execution directory.
42    return subprocess.call(commandLine, cwd=args.execdir, env=env, shell=False)
43
44
45if __name__ == '__main__':
46    exit(main())
47