2 # Copyright (c) 2011 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.
6 """Rewrites paths in -I, -L and other option to be relative to a sysroot."""
12 REWRITE_PREFIX
= ['-I',
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
26 if os
.path
.isabs(path
) and not path
.startswith(sysroot
):
27 if path
.startswith(prefix
):
28 path
= path
[len(prefix
):]
29 path
= path
.lstrip('/')
30 return os
.path
.join(sysroot
, path
)
35 def RewriteLine(line
, opts
):
36 """Rewrites all the paths in recognized options."""
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.
47 args
[i
] = RewritePath(args
[i
], opts
)
49 sys
.stderr
.write('Missing argument following %s\n' % prefix
)
51 elif args
[i
].startswith(prefix
):
52 args
[i
] = prefix
+ RewritePath(args
[i
][len(prefix
):], opts
)
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:])
64 for line
in sys
.stdin
.readlines():
65 line
= RewriteLine(line
.strip(), opts
)
70 if __name__
== '__main__':
71 sys
.exit(main(sys
.argv
))