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