blob: 31ffc1ddd551cef290cb76337324887ce621b9e4 [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"""Script for merging generated iOS libraries."""
11
tkchin5209d672016-04-16 12:06:33 -070012import sys
13
14import argparse
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +000015import os
16import re
17import subprocess
tkchin5209d672016-04-16 12:06:33 -070018
19# Valid arch subdir names.
20VALID_ARCHS = ['arm_libs', 'arm64_libs', 'ia32_libs', 'x64_libs']
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +000021
22
23def MergeLibs(lib_base_dir):
Mirko Bonadei8cc66952020-10-30 10:13:45 +010024 """Merges generated iOS libraries for different archs.
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +000025
26 Uses libtool to generate FAT archive files for each generated library.
27
28 Args:
29 lib_base_dir: directory whose subdirectories are named by architecture and
30 contain the built libraries for that architecture
31
32 Returns:
33 Exit code of libtool.
34 """
Mirko Bonadei8cc66952020-10-30 10:13:45 +010035 output_dir_name = 'fat_libs'
36 archs = [arch for arch in os.listdir(lib_base_dir) if arch in VALID_ARCHS]
37 # For each arch, find (library name, libary path) for arch. We will merge
38 # all libraries with the same name.
39 libs = {}
40 for lib_dir in [os.path.join(lib_base_dir, arch) for arch in VALID_ARCHS]:
41 if not os.path.exists(lib_dir):
42 continue
43 for dirpath, _, filenames in os.walk(lib_dir):
44 for filename in filenames:
45 if not filename.endswith('.a'):
46 continue
47 entry = libs.get(filename, [])
48 entry.append(os.path.join(dirpath, filename))
49 libs[filename] = entry
50 orphaned_libs = {}
51 valid_libs = {}
52 for library, paths in libs.items():
53 if len(paths) < len(archs):
54 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 if valid_lib[:len(base_prefix)] == base_prefix:
73 valid_paths.extend(paths)
74 found = True
75 break
76 assert found
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +000077
Mirko Bonadei8cc66952020-10-30 10:13:45 +010078 # Create output directory.
79 output_dir_path = os.path.join(lib_base_dir, output_dir_name)
80 if not os.path.exists(output_dir_path):
81 os.mkdir(output_dir_path)
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +000082
Mirko Bonadei8cc66952020-10-30 10:13:45 +010083 # Use this so libtool merged binaries are always the same.
84 env = os.environ.copy()
85 env['ZERO_AR_DATE'] = '1'
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +000086
Mirko Bonadei8cc66952020-10-30 10:13:45 +010087 # Ignore certain errors.
88 libtool_re = re.compile(r'^.*libtool:.*file: .* has no symbols$')
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +000089
Mirko Bonadei8cc66952020-10-30 10:13:45 +010090 # Merge libraries using libtool.
91 libtool_returncode = 0
92 for library, paths in valid_libs.items():
93 cmd_list = [
94 'libtool', '-static', '-v', '-o',
95 os.path.join(output_dir_path, library)
96 ] + paths
97 libtoolout = subprocess.Popen(cmd_list,
98 stderr=subprocess.PIPE,
99 env=env)
100 _, err = libtoolout.communicate()
101 for line in err.splitlines():
102 if not libtool_re.match(line):
103 print >> sys.stderr, line
104 # Unconditionally touch the output .a file on the command line if present
105 # and the command succeeded. A bit hacky.
106 libtool_returncode = libtoolout.returncode
107 if not libtool_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 return libtool_returncode
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +0000113
114
115def Main():
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100116 parser_description = 'Merge WebRTC libraries.'
117 parser = argparse.ArgumentParser(description=parser_description)
118 parser.add_argument('lib_base_dir',
119 help='Directory with built libraries. ',
120 type=str)
121 args = parser.parse_args()
122 lib_base_dir = args.lib_base_dir
123 MergeLibs(lib_base_dir)
124
tkchin@webrtc.org64eb2ff2015-03-23 19:07:37 +0000125
126if __name__ == '__main__':
Mirko Bonadei8cc66952020-10-30 10:13:45 +0100127 sys.exit(Main())