| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Runs the 'ar' command after removing its output file first. | 6 """Runs the 'ar' command after removing its output file first. |
| 7 | 7 |
| 8 This script is invoked like: | 8 This script is invoked like: |
| 9 python gcc_ar_wrapper.py --ar=$AR --output=$OUT $OP $INPUTS | 9 python gcc_ar_wrapper.py --ar=$AR --output=$OUT $OP $INPUTS |
| 10 to do the equivalent of: | 10 to do the equivalent of: |
| 11 rm -f $OUT && $AR $OP $OUT $INPUTS | 11 rm -f $OUT && $AR $OP $OUT $INPUTS |
| 12 """ | 12 """ |
| 13 | 13 |
| 14 import argparse | 14 import argparse |
| 15 import os | 15 import os |
| 16 import subprocess | 16 import subprocess |
| 17 import sys | 17 import sys |
| 18 | 18 |
| 19 import wrapper_utils | 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 |
| 20 | 33 |
| 21 | 34 |
| 22 def main(): | 35 def main(): |
| 23 parser = argparse.ArgumentParser(description=__doc__) | 36 parser = argparse.ArgumentParser(description=__doc__) |
| 24 parser.add_argument('--ar', | 37 parser.add_argument('--ar', |
| 25 required=True, | 38 required=True, |
| 26 help='The ar binary to run', | 39 help='The ar binary to run', |
| 27 metavar='PATH') | 40 metavar='PATH') |
| 28 parser.add_argument('--output', | 41 parser.add_argument('--output', |
| 29 required=True, | 42 required=True, |
| 30 help='Output archive file', | 43 help='Output archive file', |
| 31 metavar='ARCHIVE') | 44 metavar='ARCHIVE') |
| 32 parser.add_argument('--plugin', | 45 parser.add_argument('--plugin', |
| 33 help='Load plugin') | 46 help='Load plugin') |
| 34 parser.add_argument('--resource-whitelist', | |
| 35 help='Merge all resource whitelists into a single file.', | |
| 36 metavar='PATH') | |
| 37 parser.add_argument('operation', | 47 parser.add_argument('operation', |
| 38 help='Operation on the archive') | 48 help='Operation on the archive') |
| 39 parser.add_argument('inputs', nargs='+', | 49 parser.add_argument('inputs', nargs='+', |
| 40 help='Input files') | 50 help='Input files') |
| 41 args = parser.parse_args() | 51 args = parser.parse_args() |
| 42 | 52 |
| 43 if args.resource_whitelist: | |
| 44 whitelist_candidates = wrapper_utils.ResolveRspLinks(args.inputs) | |
| 45 wrapper_utils.CombineResourceWhitelists( | |
| 46 whitelist_candidates, args.resource_whitelist) | |
| 47 | |
| 48 command = [args.ar, args.operation] | 53 command = [args.ar, args.operation] |
| 49 if args.plugin is not None: | 54 if args.plugin is not None: |
| 50 command += ['--plugin', args.plugin] | 55 command += ['--plugin', args.plugin] |
| 51 command.append(args.output) | 56 command.append(args.output) |
| 52 command += args.inputs | 57 command += args.inputs |
| 53 | 58 |
| 54 # Remove the output file first. | 59 # Remove the output file first. |
| 55 try: | 60 try: |
| 56 os.remove(args.output) | 61 os.remove(args.output) |
| 57 except OSError as e: | 62 except OSError as e: |
| 58 if e.errno != os.errno.ENOENT: | 63 if e.errno != os.errno.ENOENT: |
| 59 raise | 64 raise |
| 60 | 65 |
| 61 # Now just run the ar command. | 66 # Now just run the ar command. |
| 62 return subprocess.call(wrapper_utils.CommandToRun(command)) | 67 return subprocess.call(CommandToRun(command)) |
| 63 | 68 |
| 64 | 69 |
| 65 if __name__ == "__main__": | 70 if __name__ == "__main__": |
| 66 sys.exit(main()) | 71 sys.exit(main()) |
| OLD | NEW |