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 a linking command and optionally a strip command. | 6 """Runs a linking command and optionally a strip command. |
7 | 7 |
8 This script exists to avoid using complex shell commands in | 8 This script exists to avoid using complex shell commands in |
9 gcc_toolchain.gni's tool("link"), in case the host running the compiler | 9 gcc_toolchain.gni's tool("link"), in case the host running the compiler |
10 does not have a POSIX-like shell (e.g. Windows). | 10 does not have a POSIX-like shell (e.g. Windows). |
11 """ | 11 """ |
12 | 12 |
13 import argparse | 13 import argparse |
14 import os | 14 import os |
15 import subprocess | 15 import subprocess |
16 import sys | 16 import sys |
17 | 17 |
| 18 import wrapper_utils |
| 19 |
18 | 20 |
19 # When running on a Windows host and using a toolchain whose tools are | 21 # When running on a Windows host and using a toolchain whose tools are |
20 # actually wrapper scripts (i.e. .bat files on Windows) rather than binary | 22 # actually wrapper scripts (i.e. .bat files on Windows) rather than binary |
21 # executables, the "command" to run has to be prefixed with this magic. | 23 # executables, the "command" to run has to be prefixed with this magic. |
22 # The GN toolchain definitions take care of that for when GN/Ninja is | 24 # The GN toolchain definitions take care of that for when GN/Ninja is |
23 # running the tool directly. When that command is passed in to this | 25 # running the tool directly. When that command is passed in to this |
24 # script, it appears as a unitary string but needs to be split up so that | 26 # script, it appears as a unitary string but needs to be split up so that |
25 # just 'cmd' is the actual command given to Python's subprocess module. | 27 # just 'cmd' is the actual command given to Python's subprocess module. |
26 BAT_PREFIX = 'cmd /c call ' | 28 BAT_PREFIX = 'cmd /c call ' |
27 | 29 |
28 def CommandToRun(command): | 30 def CommandToRun(command): |
29 if command[0].startswith(BAT_PREFIX): | 31 if command[0].startswith(BAT_PREFIX): |
30 command = command[0].split(None, 3) + command[1:] | 32 command = command[0].split(None, 3) + command[1:] |
31 return command | 33 return command |
32 | 34 |
33 | 35 |
34 def main(): | 36 def main(): |
35 parser = argparse.ArgumentParser(description=__doc__) | 37 parser = argparse.ArgumentParser(description=__doc__) |
36 parser.add_argument('--strip', | 38 parser.add_argument('--strip', |
37 help='The strip binary to run', | 39 help='The strip binary to run', |
38 metavar='PATH') | 40 metavar='PATH') |
39 parser.add_argument('--unstripped-file', | 41 parser.add_argument('--unstripped-file', |
40 required=True, | |
41 help='Executable file produced by linking command', | 42 help='Executable file produced by linking command', |
42 metavar='FILE') | 43 metavar='FILE') |
| 44 parser.add_argument('--map-file', |
| 45 help=('Use --Wl,-Map to generate a map file. Will be ' |
| 46 'gzipped if extension ends with .gz'), |
| 47 metavar='FILE') |
43 parser.add_argument('--output', | 48 parser.add_argument('--output', |
44 required=True, | 49 required=True, |
45 help='Final output executable file', | 50 help='Final output executable file', |
46 metavar='FILE') | 51 metavar='FILE') |
47 parser.add_argument('command', nargs='+', | 52 parser.add_argument('command', nargs='+', |
48 help='Linking command') | 53 help='Linking command') |
49 args = parser.parse_args() | 54 args = parser.parse_args() |
50 | 55 |
51 # Work-around for gold being slow-by-default. http://crbug.com/632230 | 56 # Work-around for gold being slow-by-default. http://crbug.com/632230 |
52 fast_env = dict(os.environ) | 57 fast_env = dict(os.environ) |
53 fast_env['LC_ALL'] = 'C' | 58 fast_env['LC_ALL'] = 'C' |
54 result = subprocess.call(CommandToRun(args.command), env=fast_env) | 59 result = wrapper_utils.RunLinkWithOptionalMapFile(args.command, env=fast_env, |
| 60 map_file=args.map_file) |
55 if result != 0: | 61 if result != 0: |
56 return result | 62 return result |
57 | 63 |
58 # Finally, strip the linked executable (if desired). | 64 # Finally, strip the linked executable (if desired). |
59 if args.strip: | 65 if args.strip: |
60 result = subprocess.call(CommandToRun([ | 66 result = subprocess.call(CommandToRun([ |
61 args.strip, '--strip-unneeded', '-o', args.output, args.unstripped_file | 67 args.strip, '--strip-unneeded', '-o', args.output, args.unstripped_file |
62 ])) | 68 ])) |
63 | 69 |
64 return result | 70 return result |
65 | 71 |
66 | 72 |
67 if __name__ == "__main__": | 73 if __name__ == "__main__": |
68 sys.exit(main()) | 74 sys.exit(main()) |
OLD | NEW |