copy-debug-symbols.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import argparse
  4. import os
  5. import sys
  6. from lib.config import LINUX_BINARIES, PLATFORM
  7. from lib.util import execute, get_out_dir, safe_mkdir
  8. # It has to be done before stripping the binaries.
  9. def copy_debug_from_binaries(directory, out_dir, target_cpu, compress):
  10. for binary in LINUX_BINARIES:
  11. binary_path = os.path.join(directory, binary)
  12. if os.path.isfile(binary_path):
  13. copy_debug_from_binary(binary_path, out_dir, target_cpu, compress)
  14. def copy_debug_from_binary(binary_path, out_dir, target_cpu, compress):
  15. if PLATFORM == 'linux' and (target_cpu == 'x86' or target_cpu == 'arm' or
  16. target_cpu == 'arm64'):
  17. # Skip because no objcopy binary on the given target.
  18. return
  19. debug_name = get_debug_name(binary_path)
  20. cmd = ['objcopy', '--only-keep-debug']
  21. if compress:
  22. cmd.extend(['--compress-debug-sections'])
  23. cmd.extend([binary_path, os.path.join(out_dir, debug_name)])
  24. execute(cmd)
  25. return debug_name
  26. def get_debug_name(binary_path):
  27. return os.path.basename(binary_path) + '.debug'
  28. def main():
  29. args = parse_args()
  30. safe_mkdir(args.out_dir)
  31. if args.file:
  32. copy_debug_from_binary(args.file, args.out_dir, args.target_cpu,
  33. args.compress)
  34. else:
  35. copy_debug_from_binaries(args.directory, args.out_dir, args.target_cpu,
  36. args.compress)
  37. def parse_args():
  38. parser = argparse.ArgumentParser(description='Copy debug from binaries')
  39. parser.add_argument('-d', '--directory',
  40. help='Path to the dir that contains files to copy',
  41. default=get_out_dir(),
  42. required=False)
  43. parser.add_argument('-f', '--file',
  44. help='Path to a specific file to copy debug symbols',
  45. required=False)
  46. parser.add_argument('-o', '--out-dir',
  47. help='Path to the dir that will contain the debugs',
  48. default=None,
  49. required=True)
  50. parser.add_argument('-v', '--verbose',
  51. action='store_true',
  52. help='Prints the output of the subprocesses')
  53. parser.add_argument('--target-cpu',
  54. default='',
  55. required=False,
  56. help='Target cpu of binaries to copy debug symbols')
  57. parser.add_argument('--compress',
  58. action='store_true',
  59. required=False,
  60. help='Compress the debug symbols')
  61. return parser.parse_args()
  62. if __name__ == '__main__':
  63. sys.exit(main())