aboutsummaryrefslogtreecommitdiff
blob: 9c12899d98c27c70b47563edfb6717a4ce71740e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/sh
# Copyright 2008-2010 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

#
# This creates a pkg-config frontend that has the form TARGET-pkg-config
# as this is the utility that autoconf scripts will automatically search
# for when cross-compiling for TARGET.  Here we setup the pkg config env
# paths so that the .pc files that are searched and used come from the
# staging directory rather than the host system.
#

#
# Helper functions.  So very helpful.
#
msg_to_stderr() { echo "cross-pkg-config: $*" 1>&2 ; }
warn() { msg_to_stderr "warning: $*" ; }
error() {
	msg_to_stderr "error: $*"
	exit 1
}

#
# Sanity/distro checks
#
MIN_VER="0.23" # needs PKG_CONFIG_SYSROOT_DIR
if ! pkg-config --atleast-pkgconfig-version ${MIN_VER} ; then
	error pkg-config is too old ... upgrade to at least v${MIN_VER}
fi

if [ "$1" = "--cross-pkg-config-install" ] ; then
	# --cross-pkg-config-install <sysroot dir> [pkg-config wrapper]
	pkg_path="$2"
	pkg_config="${3:-$0}"
	sed -i.tmp \
		-e "s:@CROSS_PKG_CONFIG_INSTALLED@:installed:" \
		-e "s:@CROSS_PKG_CONFIG_PATH@:$pkg_path:" \
		"$pkg_config"
	rm -f "$pkg_config".tmp
	chmod a+rx "$pkg_config"
	exit 0
fi

unset EXTRA_PKG_CONFIG_LIBDIR
if [ "@CROSS_PKG_CONFIG_INSTALLED@" = "installed" ] ; then
	# Manual install
	SYSROOT="@CROSS_PKG_CONFIG_PATH@"
elif [ -n "${ROOT}" ] ; then
	# Gentoo
	SYSROOT=${ROOT}
elif [ -n "${STAGEDIR}" ] ; then
	# uClinux-dist
	SYSROOT=${STAGEDIR}
	EXTRA_PKG_CONFIG_LIBDIR=${UCLINUX_PKG_CONFIG_LIBDIR}
else
	# /usr/<target>
	CHOST=${0##*/}
	CHOST=${CHOST%-pkg-config}
	SYSROOT="/usr/${CHOST}"
	if [ -z "${CHOST}" ] || [ ! -d "${SYSROOT}" ] ; then
		error "Need \$ROOT or \$STAGEDIR set first"
	fi
fi
# abort infinite loop due to misconfiguration
[ "${0##*/}" = "pkg-config" ] && error "aborting infinite loop! (make sure to delete uClinux-dist/tools/pkg-config)"

#
# Some distributions pollute the pkg-config environment.
# Time to pull a captain planet on them.
#
unset PKG_CONFIG_PATH
unset PKG_CONFIG_ALLOW_SYSTEM_CFLAGS
unset PKG_CONFIG_ALLOW_SYSTEM_LIBS

#
# Set the pkg-config search paths to our staging directory.
#
export PKG_CONFIG_LIBDIR="${SYSROOT}/usr/lib/pkgconfig:${SYSROOT}/usr/share/pkgconfig"
if [ -n "${EXTRA_PKG_CONFIG_LIBDIR}" ] ; then
	PKG_CONFIG_LIBDIR="${EXTRA_PKG_CONFIG_LIBDIR}:${PKG_CONFIG_LIBDIR}"
fi
#
# This guy is horribly broken in pkg-config <=0.23:
#	https://bugs.freedesktop.org/show_bug.cgi?id=16905
#
#export PKG_CONFIG_SYSROOT_DIR="${SYSROOT}"
unset PKG_CONFIG_SYSROOT_DIR

#
# Sanity check the output to catch common errors that do not
# cause failures until much later on.
#
output=$(pkg-config "$@")
ret=$?

#
# Inject PKG_CONFIG_SYSROOT_DIR ourselves until pkg-config is fixed.
# We can't mung the .pc files as some of the vars are used at compile
# time to encode runtime paths.
#
output=$(echo "${output}" | sed -e 's:\(-[IL][[:space:]]*\)\(/usr\):\1'"${SYSROOT}"'\2:g')

# We turn the output into a newline separate string of options, then use grep
# to look for bad -Is and -Ls.  Bad -Is and -Ls are ones that point to things
# outside the ${SYSROOT}.
bad_lines=$(echo "$output" |         # Get the pkg-config output.
	tr -s ' ' | tr ' ' '\n' |    # Put each flags on its own line.
	grep -E '^(-L|-I)' |         # Find all -I and -L lines.
	grep -v -E "..:?${SYSROOT}"  # Find all things outside the SYSROOT.
	)
if [ -n "$bad_lines" ]; then
	warn "### falling down so here is a dump state ######"
	pkg-config --debug "$@" 1>&2
	warn "### end of dump ###############################"
	warn "### suspicious compile flags dumped here ######"
	echo "$bad_lines"
	warn "### end of flag dump ##########################"
	error "host -I or -L paths detected: ${output}"
fi
[ -n "${output}" ] && echo "${output}"
exit ${ret}