Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / utils / run.py
blob6b4d615444bcfa549cdbbf3e8f7c2ca96f759753
1 #!/usr/bin/env python
2 # ===----------------------------------------------------------------------===##
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
8 # ===----------------------------------------------------------------------===##
10 """run.py is a utility for running a program.
12 It can perform code signing, forward arguments to the program, and return the
13 program's error code.
14 """
16 import argparse
17 import os
18 import platform
19 import subprocess
22 def 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=[])
27 parser.add_argument(
28 "--prepend_env", type=str, nargs="*", required=False, default=[]
30 parser.add_argument("command", nargs=argparse.ONE_OR_MORE)
31 args = parser.parse_args()
32 commandLine = args.command
34 # HACK:
35 # If an argument is a file that ends in `.tmp.exe`, assume it is the name
36 # of an executable generated by a test file. We call these test-executables
37 # below. This allows us to do custom processing like codesigning test-executables.
38 # It's also possible for there to be no such executable, for example in the case
39 # of a .sh.cpp test.
40 isTestExe = lambda exe: exe.endswith(".tmp.exe") and os.path.exists(exe)
42 # Do any necessary codesigning of test-executables found in the command line.
43 if args.codesign_identity:
44 for exe in filter(isTestExe, commandLine):
45 codesign = ["codesign", "-f", "-s", args.codesign_identity, exe]
46 subprocess.check_call(codesign, env={})
48 # Extract environment variables into a dictionary
49 env = {k: v for (k, v) in map(lambda s: s.split("=", 1), args.env)}
51 # Set environment variables where we prepend the given value to the
52 # existing environment variable.
53 for (k, v) in map(lambda s: s.split("=", 1), args.prepend_env):
54 if k in os.environ:
55 v = v + os.pathsep + os.environ[k]
56 env[k] = v
58 if platform.system() == "Windows":
59 # Pass some extra variables through on Windows:
60 # COMSPEC is needed for running subprocesses via std::system().
61 if "COMSPEC" in os.environ:
62 env["COMSPEC"] = os.environ.get("COMSPEC")
63 # TEMP is needed for placing temp files in a sensible directory.
64 if "TEMP" in os.environ:
65 env["TEMP"] = os.environ.get("TEMP")
67 # Run the command line with the given environment in the execution directory.
68 return subprocess.call(commandLine, cwd=args.execdir, env=env, shell=False)
71 if __name__ == "__main__":
72 exit(main())