blob: 1e78c68125f387fbe19aec6736dee5dc0df845df [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
andrew@webrtc.org8f35afa2014-01-16 00:31:57 +000024 under |path|. Filenames containing the string 'do_not_use' or 'protoc' are
25 excluded.
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000026
27 Args:
28 path: The root path for the search.
29 pattern: A shell-style wildcard pattern to match filenames against.
30 (e.g. '*.a')
31
32 Returns:
33 A list of file paths, relative to the current working directory.
34 """
35 files = []
36 for root, _, filenames in os.walk(path):
37 for filename in fnmatch.filter(filenames, pattern):
andrew@webrtc.org8f35afa2014-01-16 00:31:57 +000038 if 'do_not_use' not in filename and 'protoc' not in filename:
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000039 # We use the relative path here to avoid "argument list too long"
40 # errors on Linux.
41 files.append(os.path.relpath(os.path.join(root, filename)))
42 return files
43
44
45def main(argv):
46 if len(argv) != 3:
47 sys.stderr.write('Usage: ' + argv[0] + ' <search_path> <output_lib>\n')
48 return 1
49
50 search_path = os.path.normpath(argv[1])
51 output_lib = os.path.normpath(argv[2])
52
53 if not os.path.exists(search_path):
54 sys.stderr.write('search_path does not exist: %s\n' % search_path)
55 return 1
56
57 if os.path.isfile(output_lib):
58 os.remove(output_lib)
59
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000060 if sys.platform.startswith('linux'):
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000061 objects = FindFiles(search_path, '*.o')
62 cmd = 'ar crs '
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000063 elif sys.platform == 'darwin':
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000064 objects = FindFiles(search_path, '*.a')
65 cmd = 'libtool -static -v -o '
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000066 elif sys.platform == 'win32':
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000067 objects = FindFiles(search_path, '*.lib')
68 cmd = 'lib /OUT:'
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000069 else:
70 sys.stderr.write('Platform not supported: %r\n\n' % sys.platform)
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000071 return 1
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000072
andrew@webrtc.orgdd5482c2013-02-26 02:47:50 +000073 cmd += output_lib + ' ' + ' '.join(objects)
74 print cmd
75 subprocess.check_call(cmd, shell=True)
76 return 0
77
78if __name__ == '__main__':
79 sys.exit(main(sys.argv))
zakkhoyt@google.comd9e11b42011-09-01 00:54:32 +000080