zakkhoyt@google.com | d9e11b4 | 2011-09-01 00:54:32 +0000 | [diff] [blame] | 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 | |
andrew@webrtc.org | dd5482c | 2013-02-26 02:47:50 +0000 | [diff] [blame] | 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. |
zakkhoyt@google.com | d9e11b4 | 2011-09-01 00:54:32 +0000 | [diff] [blame] | 13 | |
andrew@webrtc.org | dd5482c | 2013-02-26 02:47:50 +0000 | [diff] [blame] | 14 | import fnmatch |
| 15 | import os |
zakkhoyt@google.com | d9e11b4 | 2011-09-01 00:54:32 +0000 | [diff] [blame] | 16 | import subprocess |
| 17 | import sys |
| 18 | |
zakkhoyt@google.com | d9e11b4 | 2011-09-01 00:54:32 +0000 | [diff] [blame] | 19 | |
andrew@webrtc.org | dd5482c | 2013-02-26 02:47:50 +0000 | [diff] [blame] | 20 | def FindFiles(path, pattern): |
| 21 | """Finds files matching |pattern| under |path|. |
zakkhoyt@google.com | d9e11b4 | 2011-09-01 00:54:32 +0000 | [diff] [blame] | 22 | |
andrew@webrtc.org | dd5482c | 2013-02-26 02:47:50 +0000 | [diff] [blame] | 23 | Returns a list of file paths matching |pattern|, by walking the directory tree |
andrew@webrtc.org | 8f35afa | 2014-01-16 00:31:57 +0000 | [diff] [blame^] | 24 | under |path|. Filenames containing the string 'do_not_use' or 'protoc' are |
| 25 | excluded. |
andrew@webrtc.org | dd5482c | 2013-02-26 02:47:50 +0000 | [diff] [blame] | 26 | |
| 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.org | 8f35afa | 2014-01-16 00:31:57 +0000 | [diff] [blame^] | 38 | if 'do_not_use' not in filename and 'protoc' not in filename: |
andrew@webrtc.org | dd5482c | 2013-02-26 02:47:50 +0000 | [diff] [blame] | 39 | # 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 | |
| 45 | def 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.com | d9e11b4 | 2011-09-01 00:54:32 +0000 | [diff] [blame] | 60 | if sys.platform.startswith('linux'): |
andrew@webrtc.org | dd5482c | 2013-02-26 02:47:50 +0000 | [diff] [blame] | 61 | objects = FindFiles(search_path, '*.o') |
| 62 | cmd = 'ar crs ' |
zakkhoyt@google.com | d9e11b4 | 2011-09-01 00:54:32 +0000 | [diff] [blame] | 63 | elif sys.platform == 'darwin': |
andrew@webrtc.org | dd5482c | 2013-02-26 02:47:50 +0000 | [diff] [blame] | 64 | objects = FindFiles(search_path, '*.a') |
| 65 | cmd = 'libtool -static -v -o ' |
zakkhoyt@google.com | d9e11b4 | 2011-09-01 00:54:32 +0000 | [diff] [blame] | 66 | elif sys.platform == 'win32': |
andrew@webrtc.org | dd5482c | 2013-02-26 02:47:50 +0000 | [diff] [blame] | 67 | objects = FindFiles(search_path, '*.lib') |
| 68 | cmd = 'lib /OUT:' |
zakkhoyt@google.com | d9e11b4 | 2011-09-01 00:54:32 +0000 | [diff] [blame] | 69 | else: |
| 70 | sys.stderr.write('Platform not supported: %r\n\n' % sys.platform) |
andrew@webrtc.org | dd5482c | 2013-02-26 02:47:50 +0000 | [diff] [blame] | 71 | return 1 |
zakkhoyt@google.com | d9e11b4 | 2011-09-01 00:54:32 +0000 | [diff] [blame] | 72 | |
andrew@webrtc.org | dd5482c | 2013-02-26 02:47:50 +0000 | [diff] [blame] | 73 | cmd += output_lib + ' ' + ' '.join(objects) |
| 74 | print cmd |
| 75 | subprocess.check_call(cmd, shell=True) |
| 76 | return 0 |
| 77 | |
| 78 | if __name__ == '__main__': |
| 79 | sys.exit(main(sys.argv)) |
zakkhoyt@google.com | d9e11b4 | 2011-09-01 00:54:32 +0000 | [diff] [blame] | 80 | |