blob: 98fd306be1975c5f11e45f80bdf319f7b354d515 [file] [log] [blame]
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +00001#!/usr/bin/python
2#
3# libjingle
4# Copyright 2015 Google Inc.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are met:
8#
9# 1. Redistributions of source code must retain the above copyright notice,
10# this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright notice,
12# this list of conditions and the following disclaimer in the documentation
13# and/or other materials provided with the distribution.
14# 3. The name of the author may not be used to endorse or promote products
15# derived from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28"""Script for merging generated iOS libraries."""
29
30import optparse
31import os
32import re
33import subprocess
34import sys
35
36
37def MergeLibs(lib_base_dir):
38 """Merges generated iOS libraries for different archs.
39
40 Uses libtool to generate FAT archive files for each generated library.
41
42 Args:
43 lib_base_dir: directory whose subdirectories are named by architecture and
44 contain the built libraries for that architecture
45
46 Returns:
47 Exit code of libtool.
48 """
49 output_dir_name = 'fat'
50 archs = [arch for arch in os.listdir(lib_base_dir)
51 if arch[:1] != '.' and arch != output_dir_name]
52 # For each arch, find (library name, libary path) for arch. We will merge
53 # all libraries with the same name.
54 libs = {}
55 for dirpath, _, filenames in os.walk(lib_base_dir):
56 if dirpath.endswith(output_dir_name):
57 continue
58 for filename in filenames:
59 if not filename.endswith('.a'):
60 continue
61 entry = libs.get(filename, [])
62 entry.append(os.path.join(dirpath, filename))
63 libs[filename] = entry
64 # Some sublibaries are only present in certain architectures. We merge
65 # them into their parent library so that the library list is consistent
66 # across architectures.
67 libs_copy = dict(libs)
68 for library, paths in libs.items():
69 if len(paths) < len(archs):
70 # Hacky: we find parent libraries by stripping off each name component.
71 components = library.strip('.a').split('_')[:-1]
72 found = False
73 while components:
74 parent_library = '_'.join(components) + '.a'
75 if (parent_library in libs_copy
76 and len(libs_copy[parent_library]) >= len(archs)):
77 libs[parent_library].extend(paths)
78 del libs[library]
79 found = True
80 break
81 components = components[:-1]
82 assert found
83
84 # Create output directory.
85 output_dir_path = os.path.join(lib_base_dir, output_dir_name)
86 if not os.path.exists(output_dir_path):
87 os.mkdir(output_dir_path)
88
89 # Use this so libtool merged binaries are always the same.
90 env = os.environ.copy()
91 env['ZERO_AR_DATE'] = '1'
92
93 # Ignore certain errors.
94 libtool_re = re.compile(r'^.*libtool:.*file: .* has no symbols$')
95
96 # Merge libraries using libtool.
97 for library, paths in libs.items():
98 cmd_list = ['libtool', '-static', '-v', '-o',
99 os.path.join(output_dir_path, library)] + paths
100 libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE, env=env)
101 _, err = libtoolout.communicate()
102 for line in err.splitlines():
103 if not libtool_re.match(line):
104 print >>sys.stderr, line
105 # Unconditionally touch the output .a file on the command line if present
106 # and the command succeeded. A bit hacky.
107 if not libtoolout.returncode:
108 for i in range(len(cmd_list) - 1):
109 if cmd_list[i] == '-o' and cmd_list[i+1].endswith('.a'):
110 os.utime(cmd_list[i+1], None)
111 break
112 else:
113 return libtoolout.returncode
114 return libtoolout.returncode
115
116
117def Main():
118 parser = optparse.OptionParser()
119 _, args = parser.parse_args()
120 if len(args) != 1:
121 parser.error('Error: Exactly 1 argument required.')
122 lib_base_dir = args[0]
123 MergeLibs(lib_base_dir)
124
125if __name__ == '__main__':
126 sys.exit(Main())