copy-debug-symbols.py 2.7 KB

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