michael@0: #!/usr/bin/env python michael@0: # Copyright (c) 2011 The Chromium Authors. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: michael@0: """Rewrites paths in -I, -L and other option to be relative to a sysroot.""" michael@0: michael@0: import sys michael@0: import os michael@0: import optparse michael@0: michael@0: REWRITE_PREFIX = ['-I', michael@0: '-idirafter', michael@0: '-imacros', michael@0: '-imultilib', michael@0: '-include', michael@0: '-iprefix', michael@0: '-iquote', michael@0: '-isystem', michael@0: '-L'] michael@0: michael@0: def RewritePath(path, opts): michael@0: """Rewrites a path by stripping the prefix and prepending the sysroot.""" michael@0: sysroot = opts.sysroot michael@0: prefix = opts.strip_prefix michael@0: if os.path.isabs(path) and not path.startswith(sysroot): michael@0: if path.startswith(prefix): michael@0: path = path[len(prefix):] michael@0: path = path.lstrip('/') michael@0: return os.path.join(sysroot, path) michael@0: else: michael@0: return path michael@0: michael@0: michael@0: def RewriteLine(line, opts): michael@0: """Rewrites all the paths in recognized options.""" michael@0: args = line.split() michael@0: count = len(args) michael@0: i = 0 michael@0: while i < count: michael@0: for prefix in REWRITE_PREFIX: michael@0: # The option can be either in the form "-I /path/to/dir" or michael@0: # "-I/path/to/dir" so handle both. michael@0: if args[i] == prefix: michael@0: i += 1 michael@0: try: michael@0: args[i] = RewritePath(args[i], opts) michael@0: except IndexError: michael@0: sys.stderr.write('Missing argument following %s\n' % prefix) michael@0: break michael@0: elif args[i].startswith(prefix): michael@0: args[i] = prefix + RewritePath(args[i][len(prefix):], opts) michael@0: i += 1 michael@0: michael@0: return ' '.join(args) michael@0: michael@0: michael@0: def main(argv): michael@0: parser = optparse.OptionParser() michael@0: parser.add_option('-s', '--sysroot', default='/', help='sysroot to prepend') michael@0: parser.add_option('-p', '--strip-prefix', default='', help='prefix to strip') michael@0: opts, args = parser.parse_args(argv[1:]) michael@0: michael@0: for line in sys.stdin.readlines(): michael@0: line = RewriteLine(line.strip(), opts) michael@0: print line michael@0: return 0 michael@0: michael@0: michael@0: if __name__ == '__main__': michael@0: sys.exit(main(sys.argv))