copy-debug-symbols.py 2.5 KB

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