OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Rewrites paths in -I, -L and other option to be relative to a sysroot.""" | 6 """Rewrites paths in -I, -L and other option to be relative to a sysroot.""" |
7 | 7 |
8 import sys | 8 import sys |
9 import os | 9 import os |
10 import optparse | 10 import optparse |
11 | 11 |
(...skipping 12 matching lines...) Expand all Loading... |
24 sysroot = opts.sysroot | 24 sysroot = opts.sysroot |
25 prefix = opts.strip_prefix | 25 prefix = opts.strip_prefix |
26 if os.path.isabs(path) and not path.startswith(sysroot): | 26 if os.path.isabs(path) and not path.startswith(sysroot): |
27 if path.startswith(prefix): | 27 if path.startswith(prefix): |
28 path = path[len(prefix):] | 28 path = path[len(prefix):] |
29 path = path.lstrip('/') | 29 path = path.lstrip('/') |
30 return os.path.join(sysroot, path) | 30 return os.path.join(sysroot, path) |
31 else: | 31 else: |
32 return path | 32 return path |
33 | 33 |
| 34 |
34 def RewriteLine(line, opts): | 35 def RewriteLine(line, opts): |
35 """Rewrites all the paths in recognized options.""" | 36 """Rewrites all the paths in recognized options.""" |
36 args = line.split() | 37 args = line.split() |
37 count = len(args) | 38 count = len(args) |
38 i = 0 | 39 i = 0 |
39 while i < count: | 40 while i < count: |
40 for prefix in REWRITE_PREFIX: | 41 for prefix in REWRITE_PREFIX: |
41 # The option can be either in the form "-I /path/to/dir" or | 42 # The option can be either in the form "-I /path/to/dir" or |
42 # "-I/path/to/dir" so handle both. | 43 # "-I/path/to/dir" so handle both. |
43 if args[i] == prefix: | 44 if args[i] == prefix: |
44 i += 1 | 45 i += 1 |
45 try: | 46 try: |
46 args[i] = RewritePath(args[i], opts) | 47 args[i] = RewritePath(args[i], opts) |
47 except IndexError: | 48 except IndexError: |
48 sys.stderr.write('Missing argument following %s\n' % prefix) | 49 sys.stderr.write('Missing argument following %s\n' % prefix) |
49 break | 50 break |
50 elif args[i].startswith(prefix): | 51 elif args[i].startswith(prefix): |
51 args[i] = prefix + RewritePath(args[i][len(prefix):], opts) | 52 args[i] = prefix + RewritePath(args[i][len(prefix):], opts) |
52 i += 1 | 53 i += 1 |
53 | 54 |
54 return ' '.join(args) | 55 return ' '.join(args) |
55 | 56 |
| 57 |
56 def main(argv): | 58 def main(argv): |
57 parser = optparse.OptionParser() | 59 parser = optparse.OptionParser() |
58 parser.add_option('-s', '--sysroot', default='/', help='sysroot to prepend') | 60 parser.add_option('-s', '--sysroot', default='/', help='sysroot to prepend') |
59 parser.add_option('-p', '--strip-prefix', default='', help='prefix to strip') | 61 parser.add_option('-p', '--strip-prefix', default='', help='prefix to strip') |
60 opts, args = parser.parse_args(argv[1:]) | 62 opts, args = parser.parse_args(argv[1:]) |
61 | 63 |
62 for line in sys.stdin.readlines(): | 64 for line in sys.stdin.readlines(): |
63 line = RewriteLine(line.strip(), opts) | 65 line = RewriteLine(line.strip(), opts) |
64 print line | 66 print line |
65 return 0 | 67 return 0 |
66 | 68 |
| 69 |
67 if __name__ == '__main__': | 70 if __name__ == '__main__': |
68 sys.exit(main(sys.argv)) | 71 sys.exit(main(sys.argv)) |
OLD | NEW |