| 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 'ld -shared' and generates a .TOC file that's untouched when unchanged. | 6 """Runs 'ld -shared' and generates a .TOC file that's untouched when unchanged. |
| 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("solink"), in case the host running the compiler | 9 gcc_toolchain.gni's tool("solink"), 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 re | |
| 16 import subprocess | 15 import subprocess |
| 17 import sys | 16 import sys |
| 18 | 17 |
| 19 | 18 import wrapper_utils |
| 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 | 19 |
| 34 | 20 |
| 35 def CollectSONAME(args): | 21 def CollectSONAME(args): |
| 36 """Replaces: readelf -d $sofile | grep SONAME""" | 22 """Replaces: readelf -d $sofile | grep SONAME""" |
| 37 toc = '' | 23 toc = '' |
| 38 readelf = subprocess.Popen(CommandToRun([args.readelf, '-d', args.sofile]), | 24 readelf = subprocess.Popen(wrapper_utils.CommandToRun( |
| 39 stdout=subprocess.PIPE, bufsize=-1) | 25 [args.readelf, '-d', args.sofile]), stdout=subprocess.PIPE, bufsize=-1) |
| 40 for line in readelf.stdout: | 26 for line in readelf.stdout: |
| 41 if 'SONAME' in line: | 27 if 'SONAME' in line: |
| 42 toc += line | 28 toc += line |
| 43 return readelf.wait(), toc | 29 return readelf.wait(), toc |
| 44 | 30 |
| 45 | 31 |
| 46 def CollectDynSym(args): | 32 def CollectDynSym(args): |
| 47 """Replaces: nm --format=posix -g -D $sofile | cut -f1-2 -d' '""" | 33 """Replaces: nm --format=posix -g -D $sofile | cut -f1-2 -d' '""" |
| 48 toc = '' | 34 toc = '' |
| 49 nm = subprocess.Popen(CommandToRun([ | 35 nm = subprocess.Popen(wrapper_utils.CommandToRun([ |
| 50 args.nm, '--format=posix', '-g', '-D', args.sofile]), | 36 args.nm, '--format=posix', '-g', '-D', args.sofile]), |
| 51 stdout=subprocess.PIPE, bufsize=-1) | 37 stdout=subprocess.PIPE, bufsize=-1) |
| 52 for line in nm.stdout: | 38 for line in nm.stdout: |
| 53 toc += ' '.join(line.split(' ', 2)[:2]) + '\n' | 39 toc += ' '.join(line.split(' ', 2)[:2]) + '\n' |
| 54 return nm.wait(), toc | 40 return nm.wait(), toc |
| 55 | 41 |
| 56 | 42 |
| 57 def CollectTOC(args): | 43 def CollectTOC(args): |
| 58 result, toc = CollectSONAME(args) | 44 result, toc = CollectSONAME(args) |
| 59 if result == 0: | 45 if result == 0: |
| (...skipping 29 matching lines...) Expand all Loading... |
| 89 help='Shared object file produced by linking command', | 75 help='Shared object file produced by linking command', |
| 90 metavar='FILE') | 76 metavar='FILE') |
| 91 parser.add_argument('--tocfile', | 77 parser.add_argument('--tocfile', |
| 92 required=True, | 78 required=True, |
| 93 help='Output table-of-contents file', | 79 help='Output table-of-contents file', |
| 94 metavar='FILE') | 80 metavar='FILE') |
| 95 parser.add_argument('--output', | 81 parser.add_argument('--output', |
| 96 required=True, | 82 required=True, |
| 97 help='Final output shared object file', | 83 help='Final output shared object file', |
| 98 metavar='FILE') | 84 metavar='FILE') |
| 85 parser.add_argument('--resource-whitelist', |
| 86 help='Merge all resource whitelists into a single file.', |
| 87 metavar='PATH') |
| 99 parser.add_argument('command', nargs='+', | 88 parser.add_argument('command', nargs='+', |
| 100 help='Linking command') | 89 help='Linking command') |
| 101 args = parser.parse_args() | 90 args = parser.parse_args() |
| 102 | 91 |
| 103 # Work-around for gold being slow-by-default. http://crbug.com/632230 | 92 # Work-around for gold being slow-by-default. http://crbug.com/632230 |
| 104 fast_env = dict(os.environ) | 93 fast_env = dict(os.environ) |
| 105 fast_env['LC_ALL'] = 'C' | 94 fast_env['LC_ALL'] = 'C' |
| 106 | 95 |
| 96 if args.resource_whitelist: |
| 97 whitelist_candidates = wrapper_utils.ResolveRspLinks(args.command) |
| 98 wrapper_utils.CombineResourceWhitelists( |
| 99 whitelist_candidates, args.resource_whitelist) |
| 100 |
| 107 # First, run the actual link. | 101 # First, run the actual link. |
| 108 result = subprocess.call(CommandToRun(args.command), env=fast_env) | 102 result = subprocess.call( |
| 103 wrapper_utils.CommandToRun(args.command), env=fast_env) |
| 109 if result != 0: | 104 if result != 0: |
| 110 return result | 105 return result |
| 111 | 106 |
| 112 # Next, generate the contents of the TOC file. | 107 # Next, generate the contents of the TOC file. |
| 113 result, toc = CollectTOC(args) | 108 result, toc = CollectTOC(args) |
| 114 if result != 0: | 109 if result != 0: |
| 115 return result | 110 return result |
| 116 | 111 |
| 117 # If there is an existing TOC file with identical contents, leave it alone. | 112 # If there is an existing TOC file with identical contents, leave it alone. |
| 118 # Otherwise, write out the TOC file. | 113 # Otherwise, write out the TOC file. |
| 119 UpdateTOC(args.tocfile, toc) | 114 UpdateTOC(args.tocfile, toc) |
| 120 | 115 |
| 121 # Finally, strip the linked shared object file (if desired). | 116 # Finally, strip the linked shared object file (if desired). |
| 122 if args.strip: | 117 if args.strip: |
| 123 result = subprocess.call(CommandToRun([args.strip, '--strip-unneeded', | 118 result = subprocess.call(wrapper_utils.CommandToRun( |
| 124 '-o', args.output, args.sofile])) | 119 [args.strip, '--strip-unneeded', '-o', args.output, args.sofile])) |
| 125 | 120 |
| 126 return result | 121 return result |
| 127 | 122 |
| 128 | 123 |
| 129 if __name__ == "__main__": | 124 if __name__ == "__main__": |
| 130 sys.exit(main()) | 125 sys.exit(main()) |
| OLD | NEW |