copy-debug-symbols.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python3
  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 in ('x86', 'arm', 'arm64'):
  16. # Skip because no objcopy binary on the given target.
  17. return
  18. debug_name = get_debug_name(binary_path)
  19. cmd = ['objcopy', '--only-keep-debug']
  20. if compress:
  21. cmd.extend(['--compress-debug-sections'])
  22. cmd.extend([binary_path, os.path.join(out_dir, debug_name)])
  23. execute(cmd)
  24. def get_debug_name(binary_path):
  25. return os.path.basename(binary_path) + '.debug'
  26. def main():
  27. args = parse_args()
  28. safe_mkdir(args.out_dir)
  29. if args.file:
  30. copy_debug_from_binary(args.file, args.out_dir, args.target_cpu,
  31. args.compress)
  32. else:
  33. copy_debug_from_binaries(args.directory, args.out_dir, args.target_cpu,
  34. args.compress)
  35. def parse_args():
  36. parser = argparse.ArgumentParser(description='Copy debug from binaries')
  37. parser.add_argument('-d', '--directory',
  38. help='Path to the dir that contains files to copy',
  39. default=get_out_dir(),
  40. required=False)
  41. parser.add_argument('-f', '--file',
  42. help='Path to a specific file to copy debug symbols',
  43. required=False)
  44. parser.add_argument('-o', '--out-dir',
  45. help='Path to the dir that will contain the debugs',
  46. default=None,
  47. required=True)
  48. parser.add_argument('-v', '--verbose',
  49. action='store_true',
  50. help='Prints the output of the subprocesses')
  51. parser.add_argument('--target-cpu',
  52. default='',
  53. required=False,
  54. help='Target cpu of binaries to copy debug symbols')
  55. parser.add_argument('--compress',
  56. action='store_true',
  57. required=False,
  58. help='Compress the debug symbols')
  59. return parser.parse_args()
  60. if __name__ == '__main__':
  61. sys.exit(main())