tkchin@webrtc.org | 64eb2ff | 2015-03-23 19:07:37 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
hjon | bc73fe1 | 2016-03-21 11:38:26 -0700 | [diff] [blame] | 2 | |
| 3 | # Copyright 2016 The WebRTC project authors. All Rights Reserved. |
tkchin@webrtc.org | 64eb2ff | 2015-03-23 19:07:37 +0000 | [diff] [blame] | 4 | # |
hjon | bc73fe1 | 2016-03-21 11:38:26 -0700 | [diff] [blame] | 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 |
| 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.org | 64eb2ff | 2015-03-23 19:07:37 +0000 | [diff] [blame] | 10 | """Script for merging generated iOS libraries.""" |
| 11 | |
tkchin | 5209d67 | 2016-04-16 12:06:33 -0700 | [diff] [blame] | 12 | import sys |
| 13 | |
| 14 | import argparse |
tkchin@webrtc.org | 64eb2ff | 2015-03-23 19:07:37 +0000 | [diff] [blame] | 15 | import os |
| 16 | import re |
| 17 | import subprocess |
tkchin | 5209d67 | 2016-04-16 12:06:33 -0700 | [diff] [blame] | 18 | |
| 19 | # Valid arch subdir names. |
| 20 | VALID_ARCHS = ['arm_libs', 'arm64_libs', 'ia32_libs', 'x64_libs'] |
tkchin@webrtc.org | 64eb2ff | 2015-03-23 19:07:37 +0000 | [diff] [blame] | 21 | |
| 22 | |
| 23 | def MergeLibs(lib_base_dir): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 24 | """Merges generated iOS libraries for different archs. |
tkchin@webrtc.org | 64eb2ff | 2015-03-23 19:07:37 +0000 | [diff] [blame] | 25 | |
| 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 Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 35 | 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.org | 64eb2ff | 2015-03-23 19:07:37 +0000 | [diff] [blame] | 77 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 78 | # 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.org | 64eb2ff | 2015-03-23 19:07:37 +0000 | [diff] [blame] | 82 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 83 | # Use this so libtool merged binaries are always the same. |
| 84 | env = os.environ.copy() |
| 85 | env['ZERO_AR_DATE'] = '1' |
tkchin@webrtc.org | 64eb2ff | 2015-03-23 19:07:37 +0000 | [diff] [blame] | 86 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 87 | # Ignore certain errors. |
| 88 | libtool_re = re.compile(r'^.*libtool:.*file: .* has no symbols$') |
tkchin@webrtc.org | 64eb2ff | 2015-03-23 19:07:37 +0000 | [diff] [blame] | 89 | |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 90 | # 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.org | 64eb2ff | 2015-03-23 19:07:37 +0000 | [diff] [blame] | 113 | |
| 114 | |
| 115 | def Main(): |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 116 | 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.org | 64eb2ff | 2015-03-23 19:07:37 +0000 | [diff] [blame] | 125 | |
| 126 | if __name__ == '__main__': |
Mirko Bonadei | 8cc6695 | 2020-10-30 10:13:45 +0100 | [diff] [blame] | 127 | sys.exit(Main()) |