merge_archives.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env python
  2. #===----------------------------------------------------------------------===##
  3. #
  4. # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. # See https://llvm.org/LICENSE.txt for license information.
  6. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. #
  8. #===----------------------------------------------------------------------===##
  9. from argparse import ArgumentParser
  10. from ctypes.util import find_library
  11. import distutils.spawn
  12. import glob
  13. import tempfile
  14. import os
  15. import shutil
  16. import subprocess
  17. import signal
  18. import sys
  19. temp_directory_root = None
  20. def exit_with_cleanups(status):
  21. if temp_directory_root is not None:
  22. shutil.rmtree(temp_directory_root)
  23. sys.exit(status)
  24. def print_and_exit(msg):
  25. sys.stderr.write(msg + '\n')
  26. exit_with_cleanups(1)
  27. def find_and_diagnose_missing(lib, search_paths):
  28. if os.path.exists(lib):
  29. return os.path.abspath(lib)
  30. if not lib.startswith('lib') or not lib.endswith('.a'):
  31. print_and_exit(("input file '%s' not not name a static library. "
  32. "It should start with 'lib' and end with '.a") % lib)
  33. for sp in search_paths:
  34. assert type(sp) is list and len(sp) == 1
  35. path = os.path.join(sp[0], lib)
  36. if os.path.exists(path):
  37. return os.path.abspath(path)
  38. print_and_exit("input '%s' does not exist" % lib)
  39. def execute_command(cmd, cwd=None):
  40. """
  41. Execute a command, capture and return its output.
  42. """
  43. kwargs = {
  44. 'stdin': subprocess.PIPE,
  45. 'stdout': subprocess.PIPE,
  46. 'stderr': subprocess.PIPE,
  47. 'cwd': cwd
  48. }
  49. p = subprocess.Popen(cmd, **kwargs)
  50. out, err = p.communicate()
  51. exitCode = p.wait()
  52. if exitCode == -signal.SIGINT:
  53. raise KeyboardInterrupt
  54. return out, err, exitCode
  55. def execute_command_verbose(cmd, cwd=None, verbose=False):
  56. """
  57. Execute a command and print its output on failure.
  58. """
  59. out, err, exitCode = execute_command(cmd, cwd=cwd)
  60. if exitCode != 0 or verbose:
  61. report = "Command: %s\n" % ' '.join(["'%s'" % a for a in cmd])
  62. if exitCode != 0:
  63. report += "Exit Code: %d\n" % exitCode
  64. if out:
  65. report += "Standard Output:\n--\n%s--" % out
  66. if err:
  67. report += "Standard Error:\n--\n%s--" % err
  68. if exitCode != 0:
  69. report += "\n\nFailed!"
  70. sys.stderr.write('%s\n' % report)
  71. if exitCode != 0:
  72. exit_with_cleanups(exitCode)
  73. return out
  74. def main():
  75. parser = ArgumentParser(
  76. description="Merge multiple archives into a single library")
  77. parser.add_argument(
  78. '-v', '--verbose', dest='verbose', action='store_true', default=False)
  79. parser.add_argument(
  80. '-o', '--output', dest='output', required=True,
  81. help='The output file. stdout is used if not given',
  82. type=str, action='store')
  83. parser.add_argument(
  84. '-L', dest='search_paths',
  85. help='Paths to search for the libraries along', action='append',
  86. nargs=1)
  87. parser.add_argument(
  88. '--ar', dest='ar_exe', required=False,
  89. help='The ar executable to use, finds \'ar\' in the path if not given',
  90. type=str, action='store')
  91. parser.add_argument(
  92. '--use-libtool', dest='use_libtool', action='store_true', default=False)
  93. parser.add_argument(
  94. '--libtool', dest='libtool_exe', required=False,
  95. help='The libtool executable to use, finds \'libtool\' in the path if not given',
  96. type=str, action='store')
  97. parser.add_argument(
  98. 'archives', metavar='archives', nargs='+',
  99. help='The archives to merge')
  100. args = parser.parse_args()
  101. ar_exe = args.ar_exe
  102. if not ar_exe:
  103. ar_exe = distutils.spawn.find_executable('ar')
  104. if not ar_exe:
  105. print_and_exit("failed to find 'ar' executable")
  106. if args.use_libtool:
  107. libtool_exe = args.libtool_exe
  108. if not libtool_exe:
  109. libtool_exe = distutils.spawn.find_executable('libtool')
  110. if not libtool_exe:
  111. print_and_exit("failed to find 'libtool' executable")
  112. if len(args.archives) < 2:
  113. print_and_exit('fewer than 2 inputs provided')
  114. archives = [find_and_diagnose_missing(ar, args.search_paths)
  115. for ar in args.archives]
  116. print ('Merging archives: %s' % archives)
  117. if not os.path.exists(os.path.dirname(args.output)):
  118. print_and_exit("output path doesn't exist: '%s'" % args.output)
  119. global temp_directory_root
  120. temp_directory_root = tempfile.mkdtemp('.libcxx.merge.archives')
  121. files = []
  122. for arc in archives:
  123. execute_command_verbose([ar_exe, 'x', arc],
  124. cwd=temp_directory_root, verbose=args.verbose)
  125. out = execute_command_verbose([ar_exe, 't', arc])
  126. files.extend(out.splitlines())
  127. if args.use_libtool:
  128. files = [f for f in files if not f.startswith('__.SYMDEF')]
  129. execute_command_verbose([libtool_exe, '-static', '-o', args.output] + files,
  130. cwd=temp_directory_root, verbose=args.verbose)
  131. else:
  132. execute_command_verbose([ar_exe, 'rcs', args.output] + files,
  133. cwd=temp_directory_root, verbose=args.verbose)
  134. if __name__ == '__main__':
  135. main()
  136. exit_with_cleanups(0)