1#!/usr/bin/env python 2 3# Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 4# 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. 10 11# Searches for libraries or object files on the specified path and merges them 12# them into a single library. Assumes ninja is used on all platforms. 13 14import fnmatch 15import os 16import subprocess 17import sys 18 19IGNORE_PATTERNS = ['do_not_use', 'protoc', 'genperf'] 20 21def FindFiles(path, pattern): 22 """Finds files matching |pattern| under |path|. 23 24 Returns a list of file paths matching |pattern|, by walking the directory tree 25 under |path|. Filenames containing the string 'do_not_use' or 'protoc' are 26 excluded. 27 28 Args: 29 path: The root path for the search. 30 pattern: A shell-style wildcard pattern to match filenames against. 31 (e.g. '*.a') 32 33 Returns: 34 A list of file paths, relative to the current working directory. 35 """ 36 files = [] 37 for root, _, filenames in os.walk(path): 38 for filename in fnmatch.filter(filenames, pattern): 39 if all(pattern not in filename for pattern in IGNORE_PATTERNS): 40 # We use the relative path here to avoid "argument list too 41 # long" errors on Linux. Note: This doesn't always work, so 42 # we use the find command on Linux. 43 files.append(os.path.relpath(os.path.join(root, filename))) 44 return files 45 46 47def main(argv): 48 if len(argv) != 3: 49 sys.stderr.write('Usage: ' + argv[0] + ' <search_path> <output_lib>\n') 50 return 1 51 52 search_path = os.path.normpath(argv[1]) 53 output_lib = os.path.normpath(argv[2]) 54 55 if not os.path.exists(search_path): 56 sys.stderr.write('search_path does not exist: %s\n' % search_path) 57 return 1 58 59 if os.path.isfile(output_lib): 60 os.remove(output_lib) 61 62 if sys.platform.startswith('linux'): 63 pattern = '*.o' 64 cmd = 'ar crs' 65 elif sys.platform == 'darwin': 66 pattern = '*.a' 67 cmd = 'libtool -static -v -o ' 68 elif sys.platform == 'win32': 69 pattern = '*.lib' 70 cmd = 'lib /OUT:' 71 else: 72 sys.stderr.write('Platform not supported: %r\n\n' % sys.platform) 73 return 1 74 75 if sys.platform.startswith('linux'): 76 cmd = ' '.join(['find', search_path, '-name "' + pattern + '"' + 77 ' -and -not -name ' + 78 ' -and -not -name '.join(IGNORE_PATTERNS) + 79 ' -exec', cmd, output_lib, '{} +']) 80 else: 81 cmd = ' '.join([cmd + output_lib] + FindFiles(search_path, pattern)) 82 print cmd 83 subprocess.check_call(cmd, shell=True) 84 return 0 85 86if __name__ == '__main__': 87 sys.exit(main(sys.argv)) 88