blob: ba98fc618b1bdbd4a3e34f4d8f1e7aa5dd2201ff [file] [log] [blame]
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +00001#!/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
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000011# 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.
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000013
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000014import fnmatch
15import os
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000016import subprocess
17import sys
18
henrike@webrtc.orgc7b8f392014-07-25 18:36:55 +000019IGNORE_PATTERNS = ['do_not_use', 'protoc']
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000020
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000021def FindFiles(path, pattern):
22 """Finds files matching |pattern| under |path|.
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000023
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000024 Returns a list of file paths matching |pattern|, by walking the directory tree
andrew@webrtc.org8f35afa2014-01-16 00:31:57 +000025 under |path|. Filenames containing the string 'do_not_use' or 'protoc' are
26 excluded.
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000027
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):
henrike@webrtc.orgc7b8f392014-07-25 18:36:55 +000039 if filename not 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.
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000043 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
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000062 if sys.platform.startswith('linux'):
henrike@webrtc.orgc7b8f392014-07-25 18:36:55 +000063 pattern = '*.o'
64 cmd = 'ar crs'
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000065 elif sys.platform == 'darwin':
henrike@webrtc.orgc7b8f392014-07-25 18:36:55 +000066 pattern = '*.a'
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000067 cmd = 'libtool -static -v -o '
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000068 elif sys.platform == 'win32':
henrike@webrtc.orgc7b8f392014-07-25 18:36:55 +000069 pattern = '*.lib'
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000070 cmd = 'lib /OUT:'
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000071 else:
72 sys.stderr.write('Platform not supported: %r\n\n' % sys.platform)
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000073 return 1
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000074
henrike@webrtc.orgc7b8f392014-07-25 18:36:55 +000075 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))
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000082 print cmd
83 subprocess.check_call(cmd, shell=True)
84 return 0
85
86if __name__ == '__main__':
87 sys.exit(main(sys.argv))