Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Runs the 'ar' command after removing its output file first. |
| 7 | |
| 8 | This script is invoked like: |
| 9 | python gcc_ar_wrapper.py --ar=$AR --output=$OUT $OP $INPUTS |
| 10 | to do the equivalent of: |
| 11 | rm -f $OUT && $AR $OP $OUT $INPUTS |
| 12 | """ |
| 13 | |
| 14 | import argparse |
| 15 | import os |
| 16 | import subprocess |
| 17 | import sys |
| 18 | |
| 19 | |
| 20 | # When running on a Windows host and using a toolchain whose tools are |
| 21 | # actually wrapper scripts (i.e. .bat files on Windows) rather than binary |
| 22 | # executables, the "command" to run has to be prefixed with this magic. |
| 23 | # The GN toolchain definitions take care of that for when GN/Ninja is |
| 24 | # running the tool directly. When that command is passed in to this |
| 25 | # script, it appears as a unitary string but needs to be split up so that |
| 26 | # just 'cmd' is the actual command given to Python's subprocess module. |
| 27 | BAT_PREFIX = 'cmd /c call ' |
| 28 | |
| 29 | def CommandToRun(command): |
| 30 | if command[0].startswith(BAT_PREFIX): |
| 31 | command = command[0].split(None, 3) + command[1:] |
| 32 | return command |
| 33 | |
| 34 | |
| 35 | def main(): |
| 36 | parser = argparse.ArgumentParser(description=__doc__) |
| 37 | parser.add_argument('--ar', |
| 38 | required=True, |
| 39 | help='The ar binary to run', |
| 40 | metavar='PATH') |
| 41 | parser.add_argument('--output', |
| 42 | required=True, |
| 43 | help='Output archive file', |
| 44 | metavar='ARCHIVE') |
| 45 | parser.add_argument('--plugin', |
| 46 | help='Load plugin') |
| 47 | parser.add_argument('operation', |
| 48 | help='Operation on the archive') |
| 49 | parser.add_argument('inputs', nargs='+', |
| 50 | help='Input files') |
| 51 | args = parser.parse_args() |
| 52 | |
| 53 | command = [args.ar, args.operation] |
| 54 | if args.plugin is not None: |
| 55 | command += ['--plugin', args.plugin] |
| 56 | command.append(args.output) |
| 57 | command += args.inputs |
| 58 | |
| 59 | # Remove the output file first. |
| 60 | try: |
| 61 | os.remove(args.output) |
| 62 | except OSError as e: |
| 63 | if e.errno != os.errno.ENOENT: |
| 64 | raise |
| 65 | |
| 66 | # Now just run the ar command. |
| 67 | return subprocess.call(CommandToRun(command)) |
| 68 | |
| 69 | |
| 70 | if __name__ == "__main__": |
| 71 | sys.exit(main()) |