From dfe1641d1e8a6ec77f3cc8f14346b3f3ab75c9b1 Mon Sep 17 00:00:00 2001 From: Robo Date: Wed, 9 Mar 2016 19:41:17 +0530 Subject: [PATCH] set path for pkg-config when using sysroot --- common.gypi | 6 +++ toolchain.gypi | 51 +++++++++++++----------- tools/linux/pkg-config-wrapper | 49 +++++++++++++++++++++++ tools/linux/rewrite_dirs.py | 71 ++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 23 deletions(-) create mode 100755 tools/linux/pkg-config-wrapper create mode 100755 tools/linux/rewrite_dirs.py diff --git a/common.gypi b/common.gypi index 6eacdc93a024..285baa84c29b 100644 --- a/common.gypi +++ b/common.gypi @@ -260,5 +260,11 @@ }, }, }], # OS=="mac" + + ['OS=="linux"', { + 'variables': { + 'pkg-config': '<(source_root)/tools/linux/pkg-config-wrapper "<(sysroot)" "<(target_arch)" "<(system_libdir)"', + }, + }], # OS=="linux" ], } diff --git a/toolchain.gypi b/toolchain.gypi index 42c2987dc685..8e07ecc497b2 100644 --- a/toolchain.gypi +++ b/toolchain.gypi @@ -5,8 +5,7 @@ # Set this to true when building with Clang. 'clang%': 1, - # Path to sysroot dir. - 'sysroot%': '', + 'system_libdir%': 'lib', 'variables': { # The minimum OS X SDK version to use. @@ -17,12 +16,20 @@ # Set NEON compilation flags. 'arm_neon%': 1, + + 'conditions': [ + # Define the abosulte version of <(DEPTH). + ['OS!="win"', { + 'source_root%': '&2 + exit 1 +fi + +rewrite=`dirname $0`/rewrite_dirs.py +package=${!#} + +libdir=$root/usr/$libpath/pkgconfig:$root/usr/share/pkgconfig + +set -e +# Some sysroots, like the Chromium OS ones, may generate paths that are not +# relative to the sysroot. For example, +# /path/to/chroot/build/x86-generic/usr/lib/pkgconfig/pkg.pc may have all paths +# relative to /path/to/chroot (i.e. prefix=/build/x86-generic/usr) instead of +# relative to /path/to/chroot/build/x86-generic (i.e prefix=/usr). +# To support this correctly, it's necessary to extract the prefix to strip from +# pkg-config's |prefix| variable. +prefix=`PKG_CONFIG_LIBDIR=$libdir pkg-config --variable=prefix "$package" | sed -e 's|/usr$||'` +result=`PKG_CONFIG_LIBDIR=$libdir pkg-config "$@"` +echo "$result"| $rewrite --sysroot "$root" --strip-prefix "$prefix" diff --git a/tools/linux/rewrite_dirs.py b/tools/linux/rewrite_dirs.py new file mode 100755 index 000000000000..30f22f0cd617 --- /dev/null +++ b/tools/linux/rewrite_dirs.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python +# Copyright (c) 2011 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""Rewrites paths in -I, -L and other option to be relative to a sysroot.""" + +import sys +import os +import optparse + +REWRITE_PREFIX = ['-I', + '-idirafter', + '-imacros', + '-imultilib', + '-include', + '-iprefix', + '-iquote', + '-isystem', + '-L'] + +def RewritePath(path, opts): + """Rewrites a path by stripping the prefix and prepending the sysroot.""" + sysroot = opts.sysroot + prefix = opts.strip_prefix + if os.path.isabs(path) and not path.startswith(sysroot): + if path.startswith(prefix): + path = path[len(prefix):] + path = path.lstrip('/') + return os.path.join(sysroot, path) + else: + return path + + +def RewriteLine(line, opts): + """Rewrites all the paths in recognized options.""" + args = line.split() + count = len(args) + i = 0 + while i < count: + for prefix in REWRITE_PREFIX: + # The option can be either in the form "-I /path/to/dir" or + # "-I/path/to/dir" so handle both. + if args[i] == prefix: + i += 1 + try: + args[i] = RewritePath(args[i], opts) + except IndexError: + sys.stderr.write('Missing argument following %s\n' % prefix) + break + elif args[i].startswith(prefix): + args[i] = prefix + RewritePath(args[i][len(prefix):], opts) + i += 1 + + return ' '.join(args) + + +def main(argv): + parser = optparse.OptionParser() + parser.add_option('-s', '--sysroot', default='/', help='sysroot to prepend') + parser.add_option('-p', '--strip-prefix', default='', help='prefix to strip') + opts, args = parser.parse_args(argv[1:]) + + for line in sys.stdin.readlines(): + line = RewriteLine(line.strip(), opts) + print line + return 0 + + +if __name__ == '__main__': + sys.exit(main(sys.argv))