link_fat_obj.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import argparse
  2. import subprocess
  3. import sys
  4. from process_whole_archive_option import ProcessWholeArchiveOption
  5. YA_ARG_PREFIX = '-Ya,'
  6. def get_args():
  7. parser = argparse.ArgumentParser()
  8. parser.add_argument('--obj')
  9. parser.add_argument('--globals-lib')
  10. parser.add_argument('--lib', required=True)
  11. parser.add_argument('--arch', required=True)
  12. parser.add_argument('--build-root', default=None)
  13. parser.add_argument('--with-own-obj', action='store_true', default=False)
  14. parser.add_argument('--with-global-srcs', action='store_true', default=False)
  15. groups = {}
  16. args_list = groups.setdefault('default', [])
  17. for arg in sys.argv[1:]:
  18. if arg == '--with-own-obj':
  19. groups['default'].append(arg)
  20. elif arg == '--globals-lib':
  21. groups['default'].append(arg)
  22. elif arg == '--with-global-srcs':
  23. groups['default'].append(arg)
  24. elif arg.startswith(YA_ARG_PREFIX):
  25. group_name = arg[len(YA_ARG_PREFIX):]
  26. args_list = groups.setdefault(group_name, [])
  27. else:
  28. args_list.append(arg)
  29. return parser.parse_args(groups['default']), groups
  30. def strip_suppression_files(srcs):
  31. return [s for s in srcs if not s.endswith('.supp')]
  32. def main():
  33. args, groups = get_args()
  34. # Inputs
  35. auto_input = groups['input']
  36. # Outputs
  37. lib_output = args.lib
  38. obj_output = args.obj
  39. # Dependencies
  40. global_srcs = groups['global_srcs']
  41. global_srcs = strip_suppression_files(global_srcs)
  42. global_srcs = ProcessWholeArchiveOption(args.arch).construct_cmd(global_srcs)
  43. peers = groups['peers']
  44. # Tools
  45. linker = groups['linker']
  46. archiver = groups['archiver']
  47. do_link = linker + ['-o', obj_output, '-Wl,-r', '-nodefaultlibs', '-nostartfiles'] + global_srcs + auto_input
  48. do_archive = archiver + [lib_output] + peers
  49. do_globals = None
  50. if args.globals_lib:
  51. do_globals = archiver + [args.globals_lib] + auto_input + global_srcs
  52. if args.with_own_obj:
  53. do_archive += auto_input
  54. if args.with_global_srcs:
  55. do_archive += global_srcs
  56. def call(c):
  57. proc = subprocess.Popen(c, shell=False, stderr=sys.stderr, stdout=sys.stdout, cwd=args.build_root)
  58. proc.communicate()
  59. return proc.returncode
  60. if obj_output:
  61. link_res = call(do_link)
  62. if link_res:
  63. sys.exit(link_res)
  64. if do_globals:
  65. glob_res = call(do_globals)
  66. if glob_res:
  67. sys.exit(glob_res)
  68. sys.exit(call(do_archive))
  69. if __name__ == '__main__':
  70. main()