OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # | 2 # |
3 # libjingle | 3 # libjingle |
4 # Copyright 2015 Google Inc. | 4 # Copyright 2015 Google Inc. |
5 # | 5 # |
6 # Redistribution and use in source and binary forms, with or without | 6 # Redistribution and use in source and binary forms, with or without |
7 # modification, are permitted provided that the following conditions are met: | 7 # modification, are permitted provided that the following conditions are met: |
8 # | 8 # |
9 # 1. Redistributions of source code must retain the above copyright notice, | 9 # 1. Redistributions of source code must retain the above copyright notice, |
10 # this list of conditions and the following disclaimer. | 10 # this list of conditions and the following disclaimer. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 libs = {} | 54 libs = {} |
55 for dirpath, _, filenames in os.walk(lib_base_dir): | 55 for dirpath, _, filenames in os.walk(lib_base_dir): |
56 if dirpath.endswith(output_dir_name): | 56 if dirpath.endswith(output_dir_name): |
57 continue | 57 continue |
58 for filename in filenames: | 58 for filename in filenames: |
59 if not filename.endswith('.a'): | 59 if not filename.endswith('.a'): |
60 continue | 60 continue |
61 entry = libs.get(filename, []) | 61 entry = libs.get(filename, []) |
62 entry.append(os.path.join(dirpath, filename)) | 62 entry.append(os.path.join(dirpath, filename)) |
63 libs[filename] = entry | 63 libs[filename] = entry |
64 # Some sublibaries are only present in certain architectures. We merge | 64 |
65 # them into their parent library so that the library list is consistent | 65 orphaned_libs = {} |
66 # across architectures. | 66 valid_libs = {} |
67 libs_copy = dict(libs) | |
68 for library, paths in libs.items(): | 67 for library, paths in libs.items(): |
69 if len(paths) < len(archs): | 68 if len(paths) < len(archs): |
70 # Hacky: we find parent libraries by stripping off each name component. | 69 orphaned_libs[library] = paths |
71 components = library.strip('.a').split('_')[:-1] | 70 else: |
72 found = False | 71 valid_libs[library] = paths |
73 while components: | 72 for library, paths in orphaned_libs.items(): |
74 parent_library = '_'.join(components) + '.a' | 73 components = library[:-2].split('_')[:-1] |
75 if (parent_library in libs_copy | 74 found = False |
76 and len(libs_copy[parent_library]) >= len(archs)): | 75 # Find directly matching parent libs by stripping suffix. |
77 libs[parent_library].extend(paths) | 76 while components and not found: |
78 del libs[library] | 77 parent_library = '_'.join(components) + '.a' |
| 78 if parent_library in valid_libs: |
| 79 valid_libs[parent_library].extend(paths) |
| 80 found = True |
| 81 break |
| 82 components = components[:-1] |
| 83 # Find next best match by finding parent libs with the same prefix. |
| 84 if not found: |
| 85 base_prefix = library[:-2].split('_')[0] |
| 86 for valid_lib, valid_paths in valid_libs.items(): |
| 87 prefix = '_'.join(components) |
| 88 if valid_lib[:len(base_prefix)] == base_prefix: |
| 89 valid_paths.extend(paths) |
79 found = True | 90 found = True |
80 break | 91 break |
81 components = components[:-1] | 92 assert found |
82 assert found | |
83 | 93 |
84 # Create output directory. | 94 # Create output directory. |
85 output_dir_path = os.path.join(lib_base_dir, output_dir_name) | 95 output_dir_path = os.path.join(lib_base_dir, output_dir_name) |
86 if not os.path.exists(output_dir_path): | 96 if not os.path.exists(output_dir_path): |
87 os.mkdir(output_dir_path) | 97 os.mkdir(output_dir_path) |
88 | 98 |
89 # Use this so libtool merged binaries are always the same. | 99 # Use this so libtool merged binaries are always the same. |
90 env = os.environ.copy() | 100 env = os.environ.copy() |
91 env['ZERO_AR_DATE'] = '1' | 101 env['ZERO_AR_DATE'] = '1' |
92 | 102 |
93 # Ignore certain errors. | 103 # Ignore certain errors. |
94 libtool_re = re.compile(r'^.*libtool:.*file: .* has no symbols$') | 104 libtool_re = re.compile(r'^.*libtool:.*file: .* has no symbols$') |
95 | 105 |
96 # Merge libraries using libtool. | 106 # Merge libraries using libtool. |
97 for library, paths in libs.items(): | 107 for library, paths in valid_libs.items(): |
98 cmd_list = ['libtool', '-static', '-v', '-o', | 108 cmd_list = ['libtool', '-static', '-v', '-o', |
99 os.path.join(output_dir_path, library)] + paths | 109 os.path.join(output_dir_path, library)] + paths |
100 libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE, env=env) | 110 libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE, env=env) |
101 _, err = libtoolout.communicate() | 111 _, err = libtoolout.communicate() |
102 for line in err.splitlines(): | 112 for line in err.splitlines(): |
103 if not libtool_re.match(line): | 113 if not libtool_re.match(line): |
104 print >>sys.stderr, line | 114 print >>sys.stderr, line |
105 # Unconditionally touch the output .a file on the command line if present | 115 # Unconditionally touch the output .a file on the command line if present |
106 # and the command succeeded. A bit hacky. | 116 # and the command succeeded. A bit hacky. |
107 if not libtoolout.returncode: | 117 if not libtoolout.returncode: |
108 for i in range(len(cmd_list) - 1): | 118 for i in range(len(cmd_list) - 1): |
109 if cmd_list[i] == '-o' and cmd_list[i+1].endswith('.a'): | 119 if cmd_list[i] == '-o' and cmd_list[i+1].endswith('.a'): |
110 os.utime(cmd_list[i+1], None) | 120 os.utime(cmd_list[i+1], None) |
111 break | 121 break |
112 else: | 122 else: |
113 return libtoolout.returncode | 123 return libtoolout.returncode |
114 return libtoolout.returncode | 124 return libtoolout.returncode |
115 | 125 |
116 | 126 |
117 def Main(): | 127 def Main(): |
118 parser = optparse.OptionParser() | 128 parser = optparse.OptionParser() |
119 _, args = parser.parse_args() | 129 _, args = parser.parse_args() |
120 if len(args) != 1: | 130 if len(args) != 1: |
121 parser.error('Error: Exactly 1 argument required.') | 131 parser.error('Error: Exactly 1 argument required.') |
122 lib_base_dir = args[0] | 132 lib_base_dir = args[0] |
123 MergeLibs(lib_base_dir) | 133 MergeLibs(lib_base_dir) |
124 | 134 |
125 if __name__ == '__main__': | 135 if __name__ == '__main__': |
126 sys.exit(Main()) | 136 sys.exit(Main()) |
OLD | NEW |