blob: d96d5e3c0c69c0b93d7ae6aab39061f939bfca7c [file] [log] [blame]
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +00001#!/usr/bin/python
hjonbc73fe12016-03-21 11:38:26 -07002
3# Copyright 2016 The WebRTC project authors. All Rights Reserved.
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +00004#
hjonbc73fe12016-03-21 11:38:26 -07005# 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
7# tree. An additional intellectual property rights grant can be found
8# in the file PATENTS. All contributing project authors may
9# be found in the AUTHORS file in the root of the source tree.
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +000010
11"""Script for merging generated iOS libraries."""
12
13import optparse
14import os
15import re
16import subprocess
17import sys
18
19
20def MergeLibs(lib_base_dir):
21 """Merges generated iOS libraries for different archs.
22
23 Uses libtool to generate FAT archive files for each generated library.
24
25 Args:
26 lib_base_dir: directory whose subdirectories are named by architecture and
27 contain the built libraries for that architecture
28
29 Returns:
30 Exit code of libtool.
31 """
hjon9bf5cde2016-02-19 17:15:49 -080032 include_dir_name = 'include'
33 output_dir_name = 'lib'
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +000034 archs = [arch for arch in os.listdir(lib_base_dir)
hjon9bf5cde2016-02-19 17:15:49 -080035 if arch[:1] != '.' and arch != output_dir_name
36 and arch != include_dir_name]
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +000037 # For each arch, find (library name, libary path) for arch. We will merge
38 # all libraries with the same name.
39 libs = {}
40 for dirpath, _, filenames in os.walk(lib_base_dir):
41 if dirpath.endswith(output_dir_name):
42 continue
43 for filename in filenames:
44 if not filename.endswith('.a'):
45 continue
46 entry = libs.get(filename, [])
47 entry.append(os.path.join(dirpath, filename))
48 libs[filename] = entry
tkchindd2bd262015-10-19 12:53:53 -070049
50 orphaned_libs = {}
51 valid_libs = {}
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +000052 for library, paths in libs.items():
53 if len(paths) < len(archs):
tkchindd2bd262015-10-19 12:53:53 -070054 orphaned_libs[library] = paths
55 else:
56 valid_libs[library] = paths
57 for library, paths in orphaned_libs.items():
58 components = library[:-2].split('_')[:-1]
59 found = False
60 # Find directly matching parent libs by stripping suffix.
61 while components and not found:
62 parent_library = '_'.join(components) + '.a'
63 if parent_library in valid_libs:
64 valid_libs[parent_library].extend(paths)
65 found = True
66 break
67 components = components[:-1]
68 # Find next best match by finding parent libs with the same prefix.
69 if not found:
70 base_prefix = library[:-2].split('_')[0]
71 for valid_lib, valid_paths in valid_libs.items():
72 prefix = '_'.join(components)
73 if valid_lib[:len(base_prefix)] == base_prefix:
74 valid_paths.extend(paths)
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +000075 found = True
76 break
tkchindd2bd262015-10-19 12:53:53 -070077 assert found
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +000078
79 # Create output directory.
80 output_dir_path = os.path.join(lib_base_dir, output_dir_name)
81 if not os.path.exists(output_dir_path):
82 os.mkdir(output_dir_path)
83
84 # Use this so libtool merged binaries are always the same.
85 env = os.environ.copy()
86 env['ZERO_AR_DATE'] = '1'
87
88 # Ignore certain errors.
89 libtool_re = re.compile(r'^.*libtool:.*file: .* has no symbols$')
90
91 # Merge libraries using libtool.
tkchindd2bd262015-10-19 12:53:53 -070092 for library, paths in valid_libs.items():
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +000093 cmd_list = ['libtool', '-static', '-v', '-o',
94 os.path.join(output_dir_path, library)] + paths
95 libtoolout = subprocess.Popen(cmd_list, stderr=subprocess.PIPE, env=env)
96 _, err = libtoolout.communicate()
97 for line in err.splitlines():
98 if not libtool_re.match(line):
99 print >>sys.stderr, line
100 # Unconditionally touch the output .a file on the command line if present
101 # and the command succeeded. A bit hacky.
102 if not libtoolout.returncode:
103 for i in range(len(cmd_list) - 1):
104 if cmd_list[i] == '-o' and cmd_list[i+1].endswith('.a'):
105 os.utime(cmd_list[i+1], None)
106 break
107 else:
108 return libtoolout.returncode
109 return libtoolout.returncode
110
111
112def Main():
113 parser = optparse.OptionParser()
114 _, args = parser.parse_args()
115 if len(args) != 1:
116 parser.error('Error: Exactly 1 argument required.')
117 lib_base_dir = args[0]
118 MergeLibs(lib_base_dir)
119
120if __name__ == '__main__':
121 sys.exit(Main())