blob: 5edae3964e61895e73da9e1e0776259547c831f5 [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
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000019
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000020def FindFiles(path, pattern):
21 """Finds files matching |pattern| under |path|.
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000022
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000023 Returns a list of file paths matching |pattern|, by walking the directory tree
24 under |path|. Filenames containing the string 'do_not_use' are excluded.
25
26 Args:
27 path: The root path for the search.
28 pattern: A shell-style wildcard pattern to match filenames against.
29 (e.g. '*.a')
30
31 Returns:
32 A list of file paths, relative to the current working directory.
33 """
34 files = []
35 for root, _, filenames in os.walk(path):
36 for filename in fnmatch.filter(filenames, pattern):
37 if 'do_not_use' not in filename:
38 # We use the relative path here to avoid "argument list too long"
39 # errors on Linux.
40 files.append(os.path.relpath(os.path.join(root, filename)))
41 return files
42
43
44def main(argv):
45 if len(argv) != 3:
46 sys.stderr.write('Usage: ' + argv[0] + ' <search_path> <output_lib>\n')
47 return 1
48
49 search_path = os.path.normpath(argv[1])
50 output_lib = os.path.normpath(argv[2])
51
52 if not os.path.exists(search_path):
53 sys.stderr.write('search_path does not exist: %s\n' % search_path)
54 return 1
55
56 if os.path.isfile(output_lib):
57 os.remove(output_lib)
58
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000059 if sys.platform.startswith('linux'):
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000060 objects = FindFiles(search_path, '*.o')
61 cmd = 'ar crs '
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000062 elif sys.platform == 'darwin':
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000063 objects = FindFiles(search_path, '*.a')
64 cmd = 'libtool -static -v -o '
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000065 elif sys.platform == 'win32':
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000066 objects = FindFiles(search_path, '*.lib')
67 cmd = 'lib /OUT:'
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000068 else:
69 sys.stderr.write('Platform not supported: %r\n\n' % sys.platform)
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000070 return 1
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000071
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000072 cmd += output_lib + ' ' + ' '.join(objects)
73 print cmd
74 subprocess.check_call(cmd, shell=True)
75 return 0
76
77if __name__ == '__main__':
78 sys.exit(main(sys.argv))
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000079