xref: /openbsd-src/gnu/llvm/libcxx/utils/run.py (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
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 os
18import platform
19import subprocess
20
21
22def main():
23    parser = argparse.ArgumentParser()
24    parser.add_argument('--execdir', type=str, required=True)
25    parser.add_argument('--codesign_identity', type=str, required=False, default=None)
26    parser.add_argument('--env', type=str, nargs='*', required=False, default=dict())
27    parser.add_argument("command", nargs=argparse.ONE_OR_MORE)
28    args = parser.parse_args()
29    commandLine = args.command
30
31    # HACK:
32    # If an argument is a file that ends in `.tmp.exe`, assume it is the name
33    # of an executable generated by a test file. We call these test-executables
34    # below. This allows us to do custom processing like codesigning test-executables.
35    # It's also possible for there to be no such executable, for example in the case
36    # of a .sh.cpp test.
37    isTestExe = lambda exe: exe.endswith('.tmp.exe') and os.path.exists(exe)
38
39    # Do any necessary codesigning of test-executables found in the command line.
40    if args.codesign_identity:
41        for exe in filter(isTestExe, commandLine):
42            subprocess.check_call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})
43
44    # Extract environment variables into a dictionary
45    env = {k : v  for (k, v) in map(lambda s: s.split('=', 1), args.env)}
46    if platform.system() == 'Windows':
47        # Pass some extra variables through on Windows:
48        # COMSPEC is needed for running subprocesses via std::system().
49        if 'COMSPEC' in os.environ:
50            env['COMSPEC'] = os.environ.get('COMSPEC')
51        # TEMP is needed for placing temp files in a sensible directory.
52        if 'TEMP' in os.environ:
53            env['TEMP'] = os.environ.get('TEMP')
54
55    # Run the command line with the given environment in the execution directory.
56    return subprocess.call(commandLine, cwd=args.execdir, env=env, shell=False)
57
58
59if __name__ == '__main__':
60    exit(main())
61