link_lib.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import sys
  2. import subprocess
  3. import tempfile
  4. import os
  5. import shutil
  6. class Opts(object):
  7. def __init__(self, args):
  8. self.archiver = args[0]
  9. self.arch_type = args[1]
  10. self.llvm_ar_format = args[2]
  11. self.build_root = args[3]
  12. self.plugin = args[4]
  13. self.output = args[5]
  14. auto_input = args[6:]
  15. self.need_modify = False
  16. self.extra_args = []
  17. if self.arch_type.endswith('_AR'):
  18. if self.arch_type == 'GNU_AR':
  19. self.create_flags = ['rcs']
  20. self.modify_flags = ['-M']
  21. elif self.arch_type == 'LLVM_AR':
  22. self.create_flags = ['rcs', '--format=%s' % self.llvm_ar_format]
  23. self.modify_flags = ['-M']
  24. self.need_modify = any(item.endswith('.a') for item in auto_input)
  25. if self.need_modify:
  26. self.objs = list(filter(lambda x: x.endswith('.o'), auto_input))
  27. self.libs = list(filter(lambda x: x.endswith('.a'), auto_input))
  28. else:
  29. self.objs = auto_input
  30. self.libs = []
  31. self.output_opts = [self.output]
  32. elif self.arch_type == 'LIBTOOL':
  33. self.create_flags = ['-static']
  34. self.objs = auto_input
  35. self.libs = []
  36. self.output_opts = ['-o', self.output]
  37. elif self.arch_type == 'LIB':
  38. self.create_flags = []
  39. self.extra_args = list(filter(lambda x: x.startswith('/'), auto_input))
  40. self.objs = list(filter(lambda x: not x.startswith('/'), auto_input))
  41. self.libs = []
  42. self.output_opts = ['/OUT:' + self.output]
  43. self.plugin_flags = ['--plugin', self.plugin] if self.plugin != 'None' else []
  44. def get_opts(args):
  45. return Opts(args)
  46. if __name__ == "__main__":
  47. opts = get_opts(sys.argv[1:])
  48. # There is a bug in llvm-ar. Some files with size slightly greater 2^32
  49. # still have GNU format instead of GNU64 and cause link problems.
  50. # Workaround just lowers llvm-ar's GNU64 threshold to 2^31.
  51. if opts.arch_type == 'LLVM_AR':
  52. os.environ['SYM64_THRESHOLD'] = '31'
  53. def call():
  54. try:
  55. p = subprocess.Popen(cmd, stdin=stdin, cwd=opts.build_root)
  56. rc = p.wait()
  57. return rc
  58. except OSError as e:
  59. raise Exception('while running %s: %s' % (' '.join(cmd), e))
  60. try:
  61. os.unlink(opts.output)
  62. except OSError:
  63. pass
  64. if not opts.need_modify:
  65. cmd = [opts.archiver] + opts.create_flags + opts.plugin_flags + opts.extra_args + opts.output_opts + opts.objs
  66. stdin = None
  67. exit_code = call()
  68. elif len(opts.objs) == 0 and len(opts.libs) == 1:
  69. shutil.copy(opts.libs[0], opts.output)
  70. exit_code = 0
  71. else:
  72. temp = tempfile.NamedTemporaryFile(dir=os.path.dirname(opts.output), delete=False)
  73. with open(temp.name, 'w') as tmp:
  74. tmp.write('CREATE {0}\n'.format(opts.output))
  75. for lib in opts.libs:
  76. tmp.write('ADDLIB {0}\n'.format(lib))
  77. for obj in opts.objs:
  78. tmp.write('ADDMOD {0}\n'.format(obj))
  79. tmp.write('SAVE\n')
  80. tmp.write('END\n')
  81. cmd = [opts.archiver] + opts.modify_flags + opts.plugin_flags
  82. stdin = open(temp.name)
  83. exit_code = call()
  84. os.remove(temp.name)
  85. if exit_code != 0:
  86. raise Exception('{0} returned non-zero exit code {1}. Stop.'.format(' '.join(cmd), exit_code))