blob: bcb7b8412700c66113725a527ecfd4daaddf8140 [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
Martin Storsjöcd89c582020-10-17 20:57:19 +000017import os
18import platform
Vedant Kumare56ddc52019-09-05 21:24:23 +000019import subprocess
Vedant Kumare56ddc52019-09-05 21:24:23 +000020
21
22def main():
Louis Dionnea6d477a2020-03-20 18:11:38 -040023 parser = argparse.ArgumentParser()
Louis Dionne53129a62020-04-07 15:42:00 -040024 parser.add_argument('--execdir', type=str, required=True)
Louis Dionne00fddf42020-04-03 17:50:39 -040025 parser.add_argument('--codesign_identity', type=str, required=False, default=None)
Louis Dionne00fddf42020-04-03 17:50:39 -040026 parser.add_argument('--env', type=str, nargs='*', required=False, default=dict())
Alex Richardson5b312d22020-07-21 08:35:47 +010027 parser.add_argument("command", nargs=argparse.ONE_OR_MORE)
28 args = parser.parse_args()
29 commandLine = args.command
Vedant Kumare56ddc52019-09-05 21:24:23 +000030
Louis Dionne4ae591d2021-04-01 09:47:49 -040031 # 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.
Louis Dionnea6d477a2020-03-20 18:11:38 -040040 if args.codesign_identity:
Louis Dionne4ae591d2021-04-01 09:47:49 -040041 for exe in filter(isTestExe, commandLine):
42 subprocess.check_call(['xcrun', 'codesign', '-f', '-s', args.codesign_identity, exe], env={})
Vedant Kumare56ddc52019-09-05 21:24:23 +000043
Louis Dionnea6d477a2020-03-20 18:11:38 -040044 # Extract environment variables into a dictionary
Louis Dionneab950ac2020-03-20 19:28:36 -040045 env = {k : v for (k, v) in map(lambda s: s.split('=', 1), args.env)}
Martin Storsjöcd89c582020-10-17 20:57:19 +000046 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')
Louis Dionnea6d477a2020-03-20 18:11:38 -040054
Louis Dionnef3fe5f32020-06-10 14:41:47 -040055 # Run the command line with the given environment in the execution directory.
Louis Dionnee57654f2020-11-03 14:45:52 -050056 return subprocess.call(commandLine, cwd=args.execdir, env=env, shell=False)
Louis Dionne53129a62020-04-07 15:42:00 -040057
Vedant Kumare56ddc52019-09-05 21:24:23 +000058
59if __name__ == '__main__':
60 exit(main())