OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/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 | 11 |
11 REWRITE_PREFIX = ['-I', | 12 REWRITE_PREFIX = ['-I', |
12 '-idirafter', | 13 '-idirafter', |
13 '-imacros', | 14 '-imacros', |
14 '-imultilib', | 15 '-imultilib', |
15 '-include', | 16 '-include', |
16 '-iprefix', | 17 '-iprefix', |
17 '-iquote', | 18 '-iquote', |
18 '-isystem', | 19 '-isystem', |
19 '-L'] | 20 '-L'] |
20 | 21 |
21 def RewritePath(path, sysroot): | 22 def RewritePath(path, opts): |
22 """Rewrites a path by prefixing it with the sysroot if it is absolute.""" | 23 """Rewrites a path by stripping the prefix and prepending the sysroot.""" |
| 24 sysroot = opts.sysroot |
| 25 prefix = opts.strip_prefix |
23 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): |
| 28 path = path[len(prefix):] |
24 path = path.lstrip('/') | 29 path = path.lstrip('/') |
25 return os.path.join(sysroot, path) | 30 return os.path.join(sysroot, path) |
26 else: | 31 else: |
27 return path | 32 return path |
28 | 33 |
29 def RewriteLine(line, sysroot): | 34 def RewriteLine(line, opts): |
30 """Rewrites all the paths in recognized options.""" | 35 """Rewrites all the paths in recognized options.""" |
31 args = line.split() | 36 args = line.split() |
32 count = len(args) | 37 count = len(args) |
33 i = 0 | 38 i = 0 |
34 while i < count: | 39 while i < count: |
35 for prefix in REWRITE_PREFIX: | 40 for prefix in REWRITE_PREFIX: |
36 # The option can be either in the form "-I /path/to/dir" or | 41 # The option can be either in the form "-I /path/to/dir" or |
37 # "-I/path/to/dir" so handle both. | 42 # "-I/path/to/dir" so handle both. |
38 if args[i] == prefix: | 43 if args[i] == prefix: |
39 i += 1 | 44 i += 1 |
40 try: | 45 try: |
41 args[i] = RewritePath(args[i], sysroot) | 46 args[i] = RewritePath(args[i], opts) |
42 except IndexError: | 47 except IndexError: |
43 sys.stderr.write('Missing argument following %s\n' % prefix) | 48 sys.stderr.write('Missing argument following %s\n' % prefix) |
44 break | 49 break |
45 elif args[i].startswith(prefix): | 50 elif args[i].startswith(prefix): |
46 args[i] = prefix + RewritePath(args[i][len(prefix):], sysroot) | 51 args[i] = prefix + RewritePath(args[i][len(prefix):], opts) |
47 i += 1 | 52 i += 1 |
48 | 53 |
49 return ' '.join(args) | 54 return ' '.join(args) |
50 | 55 |
51 def main(argv): | 56 def main(argv): |
52 try: | 57 parser = optparse.OptionParser() |
53 sysroot = argv[1] | 58 parser.add_option('-s', '--sysroot', default='/', help='sysroot to prepend') |
54 except IndexError: | 59 parser.add_option('-p', '--strip-prefix', default='', help='prefix to strip') |
55 sys.stderr.write('usage: %s /path/to/sysroot\n' % argv[0]) | 60 opts, args = parser.parse_args(argv[1:]) |
56 return 1 | |
57 | 61 |
58 for line in sys.stdin.readlines(): | 62 for line in sys.stdin.readlines(): |
59 line = RewriteLine(line.strip(), sysroot) | 63 line = RewriteLine(line.strip(), opts) |
60 print line | 64 print line |
61 return 0 | 65 return 0 |
62 | 66 |
63 if __name__ == '__main__': | 67 if __name__ == '__main__': |
64 sys.exit(main(sys.argv)) | 68 sys.exit(main(sys.argv)) |
OLD | NEW |