blob: 9e166d0efd1b3d889ae689b3a2f7777c05e88613 [file] [log] [blame]
[email protected]ee28c9f2009-09-04 01:53:011#!/usr/bin/python
[email protected]3f2b9092009-12-29 00:59:312# 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
4# found in the LICENSE file.
[email protected]ee28c9f2009-09-04 01:53:015
6"""Rewrites paths in -I, -L and other option to be relative to a sysroot."""
7
8import sys
9import os
[email protected]e06c3be2010-11-19 00:43:4910import optparse
[email protected]ee28c9f2009-09-04 01:53:0111
12REWRITE_PREFIX = ['-I',
13 '-idirafter',
14 '-imacros',
15 '-imultilib',
16 '-include',
17 '-iprefix',
18 '-iquote',
19 '-isystem',
20 '-L']
21
[email protected]e06c3be2010-11-19 00:43:4922def 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]9362cdc42010-02-22 23:14:4426 if os.path.isabs(path) and not path.startswith(sysroot):
[email protected]e06c3be2010-11-19 00:43:4927 if path.startswith(prefix):
28 path = path[len(prefix):]
[email protected]ee28c9f2009-09-04 01:53:0129 path = path.lstrip('/')
30 return os.path.join(sysroot, path)
31 else:
32 return path
33
[email protected]e06c3be2010-11-19 00:43:4934def RewriteLine(line, opts):
[email protected]ee28c9f2009-09-04 01:53:0135 """Rewrites all the paths in recognized options."""
36 args = line.split()
37 count = len(args)
38 i = 0
39 while i < count:
40 for prefix in REWRITE_PREFIX:
41 # The option can be either in the form "-I /path/to/dir" or
42 # "-I/path/to/dir" so handle both.
43 if args[i] == prefix:
44 i += 1
45 try:
[email protected]e06c3be2010-11-19 00:43:4946 args[i] = RewritePath(args[i], opts)
[email protected]ee28c9f2009-09-04 01:53:0147 except IndexError:
48 sys.stderr.write('Missing argument following %s\n' % prefix)
49 break
50 elif args[i].startswith(prefix):
[email protected]e06c3be2010-11-19 00:43:4951 args[i] = prefix + RewritePath(args[i][len(prefix):], opts)
[email protected]ee28c9f2009-09-04 01:53:0152 i += 1
53
54 return ' '.join(args)
55
56def main(argv):
[email protected]e06c3be2010-11-19 00:43:4957 parser = optparse.OptionParser()
58 parser.add_option('-s', '--sysroot', default='/', help='sysroot to prepend')
59 parser.add_option('-p', '--strip-prefix', default='', help='prefix to strip')
60 opts, args = parser.parse_args(argv[1:])
[email protected]ee28c9f2009-09-04 01:53:0161
62 for line in sys.stdin.readlines():
[email protected]e06c3be2010-11-19 00:43:4963 line = RewriteLine(line.strip(), opts)
[email protected]ee28c9f2009-09-04 01:53:0164 print line
65 return 0
66
67if __name__ == '__main__':
68 sys.exit(main(sys.argv))