gcc_link_wrapper.py 3.2 KB

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