link_fat_obj.py 3.1 KB

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