1*c8dee2aaSAndroid Build Coastguard Worker#!/usr/bin/env python 2*c8dee2aaSAndroid Build Coastguard Worker# Copyright (c) 2012 The Chromium Authors. All rights reserved. 3*c8dee2aaSAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be 4*c8dee2aaSAndroid Build Coastguard Worker# found in the LICENSE file. 5*c8dee2aaSAndroid Build Coastguard Worker 6*c8dee2aaSAndroid Build Coastguard Worker 7*c8dee2aaSAndroid Build Coastguard Workerfrom __future__ import print_function 8*c8dee2aaSAndroid Build Coastguard Workerimport os 9*c8dee2aaSAndroid Build Coastguard Workerimport shutil 10*c8dee2aaSAndroid Build Coastguard Workerimport subprocess 11*c8dee2aaSAndroid Build Coastguard Workerimport sys 12*c8dee2aaSAndroid Build Coastguard Workerimport tempfile 13*c8dee2aaSAndroid Build Coastguard Worker 14*c8dee2aaSAndroid Build Coastguard Worker 15*c8dee2aaSAndroid Build Coastguard Workerdef _Usage(): 16*c8dee2aaSAndroid Build Coastguard Worker print('Usage: merge_static_libs OUTPUT_LIB INPUT_LIB [INPUT_LIB]*') 17*c8dee2aaSAndroid Build Coastguard Worker sys.exit(1) 18*c8dee2aaSAndroid Build Coastguard Worker 19*c8dee2aaSAndroid Build Coastguard Worker 20*c8dee2aaSAndroid Build Coastguard Workerdef MergeLibs(in_libs, out_lib): 21*c8dee2aaSAndroid Build Coastguard Worker """ Merges multiple static libraries into one. 22*c8dee2aaSAndroid Build Coastguard Worker 23*c8dee2aaSAndroid Build Coastguard Worker in_libs: list of paths to static libraries to be merged 24*c8dee2aaSAndroid Build Coastguard Worker out_lib: path to the static library which will be created from in_libs 25*c8dee2aaSAndroid Build Coastguard Worker """ 26*c8dee2aaSAndroid Build Coastguard Worker if os.name == 'posix': 27*c8dee2aaSAndroid Build Coastguard Worker tempdir = tempfile.mkdtemp() 28*c8dee2aaSAndroid Build Coastguard Worker abs_in_libs = [] 29*c8dee2aaSAndroid Build Coastguard Worker for in_lib in in_libs: 30*c8dee2aaSAndroid Build Coastguard Worker abs_in_libs.append(os.path.abspath(in_lib)) 31*c8dee2aaSAndroid Build Coastguard Worker curdir = os.getcwd() 32*c8dee2aaSAndroid Build Coastguard Worker os.chdir(tempdir) 33*c8dee2aaSAndroid Build Coastguard Worker objects = [] 34*c8dee2aaSAndroid Build Coastguard Worker ar = os.environ.get('AR', 'ar') 35*c8dee2aaSAndroid Build Coastguard Worker for in_lib in abs_in_libs: 36*c8dee2aaSAndroid Build Coastguard Worker proc = subprocess.Popen([ar, '-t', in_lib], stdout=subprocess.PIPE) 37*c8dee2aaSAndroid Build Coastguard Worker proc.wait() 38*c8dee2aaSAndroid Build Coastguard Worker obj_str = proc.communicate()[0] 39*c8dee2aaSAndroid Build Coastguard Worker current_objects = obj_str.rstrip().split('\n') 40*c8dee2aaSAndroid Build Coastguard Worker proc = subprocess.Popen([ar, '-x', in_lib], stdout=subprocess.PIPE, 41*c8dee2aaSAndroid Build Coastguard Worker stderr=subprocess.STDOUT) 42*c8dee2aaSAndroid Build Coastguard Worker proc.wait() 43*c8dee2aaSAndroid Build Coastguard Worker if proc.poll() == 0: 44*c8dee2aaSAndroid Build Coastguard Worker # The static library is non-thin, and we extracted objects 45*c8dee2aaSAndroid Build Coastguard Worker for obj in current_objects: 46*c8dee2aaSAndroid Build Coastguard Worker objects.append(os.path.abspath(obj)) 47*c8dee2aaSAndroid Build Coastguard Worker elif 'thin archive' in proc.communicate()[0]: 48*c8dee2aaSAndroid Build Coastguard Worker # The static library is thin, so it contains the paths to its objects 49*c8dee2aaSAndroid Build Coastguard Worker for obj in current_objects: 50*c8dee2aaSAndroid Build Coastguard Worker objects.append(obj) 51*c8dee2aaSAndroid Build Coastguard Worker else: 52*c8dee2aaSAndroid Build Coastguard Worker raise Exception('Failed to extract objects from %s.' % in_lib) 53*c8dee2aaSAndroid Build Coastguard Worker os.chdir(curdir) 54*c8dee2aaSAndroid Build Coastguard Worker if not subprocess.call([ar, '-crs', out_lib] + objects) == 0: 55*c8dee2aaSAndroid Build Coastguard Worker raise Exception('Failed to add object files to %s' % out_lib) 56*c8dee2aaSAndroid Build Coastguard Worker shutil.rmtree(tempdir) 57*c8dee2aaSAndroid Build Coastguard Worker elif os.name == 'nt': 58*c8dee2aaSAndroid Build Coastguard Worker subprocess.call(['lib', '/OUT:%s' % out_lib] + in_libs) 59*c8dee2aaSAndroid Build Coastguard Worker else: 60*c8dee2aaSAndroid Build Coastguard Worker raise Exception('Error: Your platform is not supported') 61*c8dee2aaSAndroid Build Coastguard Worker 62*c8dee2aaSAndroid Build Coastguard Worker 63*c8dee2aaSAndroid Build Coastguard Workerdef Main(): 64*c8dee2aaSAndroid Build Coastguard Worker if len(sys.argv) < 3: 65*c8dee2aaSAndroid Build Coastguard Worker _Usage() 66*c8dee2aaSAndroid Build Coastguard Worker out_lib = sys.argv[1] 67*c8dee2aaSAndroid Build Coastguard Worker in_libs = sys.argv[2:] 68*c8dee2aaSAndroid Build Coastguard Worker MergeLibs(in_libs, out_lib) 69*c8dee2aaSAndroid Build Coastguard Worker 70*c8dee2aaSAndroid Build Coastguard Worker 71*c8dee2aaSAndroid Build Coastguard Workerif '__main__' == __name__: 72*c8dee2aaSAndroid Build Coastguard Worker sys.exit(Main()) 73