15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#!/bin/bash
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Copyright (c) 2012 The Chromium Authors. All rights reserved.
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# found in the LICENSE file.
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# This program wraps around pkg-config to generate the correct include and
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# library paths when cross-compiling using a sysroot.
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# The assumption is that the sysroot contains the .pc files in usr/lib/pkgconfig
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# and usr/share/pkgconfig (relative to the sysroot) and that they output paths
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# relative to some parent path of the sysroot.
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# This assumption is valid for a range of sysroots, in particular: a
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# LSB-compliant root filesystem mounted at the sysroot, and a board build
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# directory of a Chromium OS chroot.
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# Additional directories containing .pc files may be specified by setting
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# the PKG_CONFIG_PATH environment variable- these will be prepended to the
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# generated paths.
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)root="$1"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)shift
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)target_arch="$1"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)shift
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)libpath="$1"
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)shift
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)if [ -z "$root" -o -z "$target_arch" ]
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)then
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  echo "usage: $0 /path/to/sysroot target_arch libdir [pkg-config-arguments] package" >&2
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  exit 1
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)fi
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)if [ "$target_arch" = "x64" ]
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)then
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  : ${libpath:="lib64"}
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)else
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  : ${libpath:="lib"}
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)fi
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)rewrite=`dirname $0`/rewrite_dirs.py
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)package=${!#}
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)config_path=$root/usr/$libpath/pkgconfig:$root/usr/share/pkgconfig
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)# prepend any paths specified by the environment
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)if [ -n "$PKG_CONFIG_PATH" ]
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)then
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  config_path="$PKG_CONFIG_PATH:$config_path"
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)fi
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)set -e
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Some sysroots, like the Chromium OS ones, may generate paths that are not
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# relative to the sysroot. For example,
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# /path/to/chroot/build/x86-generic/usr/lib/pkgconfig/pkg.pc may have all paths
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# relative to /path/to/chroot (i.e. prefix=/build/x86-generic/usr) instead of
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# relative to /path/to/chroot/build/x86-generic (i.e prefix=/usr).
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# To support this correctly, it's necessary to extract the prefix to strip from
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# pkg-config's |prefix| variable.
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)prefix=`PKG_CONFIG_PATH=$config_path pkg-config --variable=prefix "$package" | sed -e 's|/usr$||'`
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)result=`PKG_CONFIG_PATH=$config_path pkg-config "$@"`
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)echo "$result"| $rewrite --sysroot "$root" --strip-prefix "$prefix"
60