OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 # Copyright 2016 The WebRTC project authors. All Rights Reserved. | 3 # Copyright 2016 The WebRTC project authors. All Rights Reserved. |
4 # | 4 # |
5 # Use of this source code is governed by a BSD-style license | 5 # Use of this source code is governed by a BSD-style license |
6 # that can be found in the LICENSE file in the root of the source | 6 # that can be found in the LICENSE file in the root of the source |
7 # tree. An additional intellectual property rights grant can be found | 7 # tree. An additional intellectual property rights grant can be found |
8 # in the file PATENTS. All contributing project authors may | 8 # in the file PATENTS. All contributing project authors may |
9 # be found in the AUTHORS file in the root of the source tree. | 9 # be found in the AUTHORS file in the root of the source tree. |
10 | 10 |
11 """Script for merging generated iOS libraries.""" | 11 """Script for merging generated iOS libraries.""" |
12 | 12 |
13 import optparse | 13 import optparse |
14 import os | 14 import os |
15 import re | 15 import re |
16 import subprocess | 16 import subprocess |
17 import sys | 17 import sys |
18 | 18 |
19 # Valid arch subdir names. | |
20 VALID_ARCHS = ['arm_libs', 'arm64_libs', 'ia32_libs', 'x64_libs'] | |
19 | 21 |
20 def MergeLibs(lib_base_dir): | 22 def MergeLibs(lib_base_dir): |
kjellander_webrtc
2016/04/15 04:59:41
nit: +1 blank line before top-level definition
htt
tkchin_webrtc
2016/04/16 00:34:19
Done.
| |
21 """Merges generated iOS libraries for different archs. | 23 """Merges generated iOS libraries for different archs. |
22 | 24 |
23 Uses libtool to generate FAT archive files for each generated library. | 25 Uses libtool to generate FAT archive files for each generated library. |
24 | 26 |
25 Args: | 27 Args: |
26 lib_base_dir: directory whose subdirectories are named by architecture and | 28 lib_base_dir: directory whose subdirectories are named by architecture and |
27 contain the built libraries for that architecture | 29 contain the built libraries for that architecture |
28 | 30 |
29 Returns: | 31 Returns: |
30 Exit code of libtool. | 32 Exit code of libtool. |
31 """ | 33 """ |
32 include_dir_name = 'include' | 34 output_dir_name = 'fat_libs' |
33 output_dir_name = 'lib' | |
34 archs = [arch for arch in os.listdir(lib_base_dir) | 35 archs = [arch for arch in os.listdir(lib_base_dir) |
35 if arch[:1] != '.' and arch != output_dir_name | 36 if arch in VALID_ARCHS] |
36 and arch != include_dir_name] | |
37 # For each arch, find (library name, libary path) for arch. We will merge | 37 # For each arch, find (library name, libary path) for arch. We will merge |
38 # all libraries with the same name. | 38 # all libraries with the same name. |
39 libs = {} | 39 libs = {} |
40 for dirpath, _, filenames in os.walk(lib_base_dir): | 40 for lib_dir in [os.path.join(lib_base_dir, arch) for arch in VALID_ARCHS]: |
41 if dirpath.endswith(output_dir_name): | 41 if not os.path.exists(lib_dir): |
42 continue | 42 continue |
43 for filename in filenames: | 43 for dirpath, _, filenames in os.walk(lib_dir): |
44 if not filename.endswith('.a'): | 44 for filename in filenames: |
45 continue | 45 if not filename.endswith('.a'): |
46 entry = libs.get(filename, []) | 46 continue |
47 entry.append(os.path.join(dirpath, filename)) | 47 entry = libs.get(filename, []) |
48 libs[filename] = entry | 48 entry.append(os.path.join(dirpath, filename)) |
49 | 49 libs[filename] = entry |
50 orphaned_libs = {} | 50 orphaned_libs = {} |
51 valid_libs = {} | 51 valid_libs = {} |
52 for library, paths in libs.items(): | 52 for library, paths in libs.items(): |
53 if len(paths) < len(archs): | 53 if len(paths) < len(archs): |
54 orphaned_libs[library] = paths | 54 orphaned_libs[library] = paths |
55 else: | 55 else: |
56 valid_libs[library] = paths | 56 valid_libs[library] = paths |
57 for library, paths in orphaned_libs.items(): | 57 for library, paths in orphaned_libs.items(): |
58 components = library[:-2].split('_')[:-1] | 58 components = library[:-2].split('_')[:-1] |
59 found = False | 59 found = False |
60 # Find directly matching parent libs by stripping suffix. | 60 # Find directly matching parent libs by stripping suffix. |
61 while components and not found: | 61 while components and not found: |
62 parent_library = '_'.join(components) + '.a' | 62 parent_library = '_'.join(components) + '.a' |
63 if parent_library in valid_libs: | 63 if parent_library in valid_libs: |
64 valid_libs[parent_library].extend(paths) | 64 valid_libs[parent_library].extend(paths) |
65 found = True | 65 found = True |
66 break | 66 break |
67 components = components[:-1] | 67 components = components[:-1] |
68 # Find next best match by finding parent libs with the same prefix. | 68 # Find next best match by finding parent libs with the same prefix. |
69 if not found: | 69 if not found: |
70 base_prefix = library[:-2].split('_')[0] | 70 base_prefix = library[:-2].split('_')[0] |
71 for valid_lib, valid_paths in valid_libs.items(): | 71 for valid_lib, valid_paths in valid_libs.items(): |
72 prefix = '_'.join(components) | |
73 if valid_lib[:len(base_prefix)] == base_prefix: | 72 if valid_lib[:len(base_prefix)] == base_prefix: |
74 valid_paths.extend(paths) | 73 valid_paths.extend(paths) |
75 found = True | 74 found = True |
76 break | 75 break |
77 assert found | 76 assert found |
78 | 77 |
79 # Create output directory. | 78 # Create output directory. |
80 output_dir_path = os.path.join(lib_base_dir, output_dir_name) | 79 output_dir_path = os.path.join(lib_base_dir, output_dir_name) |
81 if not os.path.exists(output_dir_path): | 80 if not os.path.exists(output_dir_path): |
82 os.mkdir(output_dir_path) | 81 os.mkdir(output_dir_path) |
83 | 82 |
84 # Use this so libtool merged binaries are always the same. | 83 # Use this so libtool merged binaries are always the same. |
85 env = os.environ.copy() | 84 env = os.environ.copy() |
86 env['ZERO_AR_DATE'] = '1' | 85 env['ZERO_AR_DATE'] = '1' |
87 | 86 |
88 # Ignore certain errors. | 87 # Ignore certain errors. |
89 libtool_re = re.compile(r'^.*libtool:.*file: .* has no symbols$') | 88 libtool_re = re.compile(r'^.*libtool:.*file: .* has no symbols$') |
90 | 89 |
91 # Merge libraries using libtool. | 90 # Merge libraries using libtool. |
91 libtool_returncode = 0 | |
92 for library, paths in valid_libs.items(): | 92 for library, paths in valid_libs.items(): |
93 cmd_list = ['libtool', '-static', '-v', '-o', | 93 cmd_list = ['libtool', '-static', '-v', '-o', |
94 os.path.join(output_dir_path, library)] + paths | 94 os.path.join(output_dir_path, library)] + paths |
95 libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE, env=env) | 95 libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE, env=env) |
96 _, err = libtoolout.communicate() | 96 _, err = libtoolout.communicate() |
97 for line in err.splitlines(): | 97 for line in err.splitlines(): |
98 if not libtool_re.match(line): | 98 if not libtool_re.match(line): |
99 print >>sys.stderr, line | 99 print >>sys.stderr, line |
100 # Unconditionally touch the output .a file on the command line if present | 100 # Unconditionally touch the output .a file on the command line if present |
101 # and the command succeeded. A bit hacky. | 101 # and the command succeeded. A bit hacky. |
102 if not libtoolout.returncode: | 102 libtool_returncode = libtoolout.returncode |
103 if not libtool_returncode: | |
103 for i in range(len(cmd_list) - 1): | 104 for i in range(len(cmd_list) - 1): |
104 if cmd_list[i] == '-o' and cmd_list[i+1].endswith('.a'): | 105 if cmd_list[i] == '-o' and cmd_list[i+1].endswith('.a'): |
105 os.utime(cmd_list[i+1], None) | 106 os.utime(cmd_list[i+1], None) |
106 break | 107 break |
107 else: | 108 else: |
108 return libtoolout.returncode | 109 return libtool_returncode |
kjellander_webrtc
2016/04/15 04:59:42
You can skip this else statement.
tkchin_webrtc
2016/04/16 00:34:19
Done.
| |
109 return libtoolout.returncode | 110 return libtool_returncode |
110 | 111 |
111 | 112 |
112 def Main(): | 113 def Main(): |
113 parser = optparse.OptionParser() | 114 parser = optparse.OptionParser() |
114 _, args = parser.parse_args() | 115 _, args = parser.parse_args() |
kjellander_webrtc
2016/04/15 04:59:41
You coulld use https://docs.python.org/2/library/s
tkchin_webrtc
2016/04/16 00:34:19
Done.
| |
115 if len(args) != 1: | 116 if len(args) != 1: |
116 parser.error('Error: Exactly 1 argument required.') | 117 parser.error('Error: Exactly 1 argument required.') |
117 lib_base_dir = args[0] | 118 lib_base_dir = args[0] |
118 MergeLibs(lib_base_dir) | 119 MergeLibs(lib_base_dir) |
119 | 120 |
120 if __name__ == '__main__': | 121 if __name__ == '__main__': |
121 sys.exit(Main()) | 122 sys.exit(Main()) |
OLD | NEW |