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