OLD | NEW |
| (Empty) |
1 #!/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 | |
30 import optparse | |
31 import os | |
32 import re | |
33 import subprocess | |
34 import sys | |
35 | |
36 | |
37 def 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 include_dir_name = 'include' | |
50 output_dir_name = 'lib' | |
51 archs = [arch for arch in os.listdir(lib_base_dir) | |
52 if arch[:1] != '.' and arch != output_dir_name | |
53 and arch != include_dir_name] | |
54 # For each arch, find (library name, libary path) for arch. We will merge | |
55 # all libraries with the same name. | |
56 libs = {} | |
57 for dirpath, _, filenames in os.walk(lib_base_dir): | |
58 if dirpath.endswith(output_dir_name): | |
59 continue | |
60 for filename in filenames: | |
61 if not filename.endswith('.a'): | |
62 continue | |
63 entry = libs.get(filename, []) | |
64 entry.append(os.path.join(dirpath, filename)) | |
65 libs[filename] = entry | |
66 | |
67 orphaned_libs = {} | |
68 valid_libs = {} | |
69 for library, paths in libs.items(): | |
70 if len(paths) < len(archs): | |
71 orphaned_libs[library] = paths | |
72 else: | |
73 valid_libs[library] = paths | |
74 for library, paths in orphaned_libs.items(): | |
75 components = library[:-2].split('_')[:-1] | |
76 found = False | |
77 # Find directly matching parent libs by stripping suffix. | |
78 while components and not found: | |
79 parent_library = '_'.join(components) + '.a' | |
80 if parent_library in valid_libs: | |
81 valid_libs[parent_library].extend(paths) | |
82 found = True | |
83 break | |
84 components = components[:-1] | |
85 # Find next best match by finding parent libs with the same prefix. | |
86 if not found: | |
87 base_prefix = library[:-2].split('_')[0] | |
88 for valid_lib, valid_paths in valid_libs.items(): | |
89 prefix = '_'.join(components) | |
90 if valid_lib[:len(base_prefix)] == base_prefix: | |
91 valid_paths.extend(paths) | |
92 found = True | |
93 break | |
94 assert found | |
95 | |
96 # Create output directory. | |
97 output_dir_path = os.path.join(lib_base_dir, output_dir_name) | |
98 if not os.path.exists(output_dir_path): | |
99 os.mkdir(output_dir_path) | |
100 | |
101 # Use this so libtool merged binaries are always the same. | |
102 env = os.environ.copy() | |
103 env['ZERO_AR_DATE'] = '1' | |
104 | |
105 # Ignore certain errors. | |
106 libtool_re = re.compile(r'^.*libtool:.*file: .* has no symbols$') | |
107 | |
108 # Merge libraries using libtool. | |
109 for library, paths in valid_libs.items(): | |
110 cmd_list = ['libtool', '-static', '-v', '-o', | |
111 os.path.join(output_dir_path, library)] + paths | |
112 libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE, env=env) | |
113 _, err = libtoolout.communicate() | |
114 for line in err.splitlines(): | |
115 if not libtool_re.match(line): | |
116 print >>sys.stderr, line | |
117 # Unconditionally touch the output .a file on the command line if present | |
118 # and the command succeeded. A bit hacky. | |
119 if not libtoolout.returncode: | |
120 for i in range(len(cmd_list) - 1): | |
121 if cmd_list[i] == '-o' and cmd_list[i+1].endswith('.a'): | |
122 os.utime(cmd_list[i+1], None) | |
123 break | |
124 else: | |
125 return libtoolout.returncode | |
126 return libtoolout.returncode | |
127 | |
128 | |
129 def Main(): | |
130 parser = optparse.OptionParser() | |
131 _, args = parser.parse_args() | |
132 if len(args) != 1: | |
133 parser.error('Error: Exactly 1 argument required.') | |
134 lib_base_dir = args[0] | |
135 MergeLibs(lib_base_dir) | |
136 | |
137 if __name__ == '__main__': | |
138 sys.exit(Main()) | |
OLD | NEW |