summaryrefslogtreecommitdiffstats
path: root/scripts/pkg-config-wrapper
blob: d9e111d6cea44d3f69765a168252551028efcaa9 (plain) (blame)
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
#!/usr/bin/env bash

basedir="$(basename "$(dirname "$(dirname "$0")")")"

declare -a prefix
if [ "${basedir}" = "sysroot-cross" ]; then
    sysroot="$(dirname "$(dirname "$(dirname "$0")")")/sysroot-target"
    prefix="${sysroot}/usr"
    whitelist="${PKGCONFIG_WHITELIST_TARGET}"
elif [ "${basedir}" = "sysroot-host" ]; then
    sysroot="$(dirname "$(dirname "$0")")"
    prefix="${sysroot}"
    whitelist="${PKGCONFIG_WHITELIST_HOST}"
else
    echo "$(basename ${0}): failed to determine prefix" >&2
    exit 1
fi

declare -a libdir system_path system_incpath
libdir=( "${prefix/%//lib/pkgconfig}" "${prefix/%//share/pkgconfig}" )
system_libpath=( "${libdir[@]/%//../../lib}" "${libdir[@]/%//../lib}" "/usr/lib" "/lib" )
system_incpath=( "${libdir[@]/%//../../include}" "${libdir[@]/%//../include}" "/usr/include" "/include" )

orig_IFS="${IFS}"
IFS=":"
# make sure no other sysroot is set
# it conflicts with our own sysroot handling
unset PKG_CONFIG_SYSROOT_DIR
# never allow systemd paths
# they may include /usr/{lib,include} which is never correct
unset PKG_CONFIG_ALLOW_SYSTEM_CFLAGS
unset PKG_CONFIG_ALLOW_SYSTEM_LIBS
# default pkg-config searchs
export PKG_CONFIG_LIBDIR="${libdir[*]}"
# default search path that will be dropped from --libs
export PKG_CONFIG_SYSTEM_LIBRARY_PATH="${system_libpath[*]}"
# default search path that will be dropped from --cflags
export PKG_CONFIG_SYSTEM_INCLUDE_PATH="${system_incpath[*]}"
IFS="${orig_IFS}"

for ((i = 1; i <= ${#}; i++)); do
    case "${!i}" in
	--variable*)
	    found_var=true
	    break
	    ;;
	*)
	    ;;
    esac
done

#
# this sed will sanitize pkg-config's output. it will remove:
#
# "/usr/lib/pkgconfig/../../.."
# "/lib/pkgconfig/../.."
#
declare -a args
args=( \
    "-e" "s~/usr/lib/pkgconfig/\.\./\.\./\.\.~~g" \
    "-e" "s~/lib/pkgconfig/\.\./\.\.~~g" \
    )

if [ -n "${PTXDIST_PKG_CONFIG_VAR_NO_SYSROOT}" -a \
    -n "${found_var}" ]; then
    #
    # remove sysroot for variables that return a path
    #
    args[${#args[@]}]="-e"
    args[${#args[@]}]="s~^${sysroot}/~/~"
fi

check_pipe_status() {
	for _pipe_status in "${PIPESTATUS[@]}"; do
		if [ ${_pipe_status} -ne 0 ]; then
			return ${_pipe_status}
		fi
	done
}

if [ -n "${PKGCONFIG_WHITELIST_SRC}" ]; then
    pkgs="$(pkg-config.real --print-provides "${@}" 2>/dev/null | awk '{print $1}' && check_pipe_status)" &&
    for pkg in ${pkgs}; do
	if [[ ! " ${whitelist} " =~ " ${pkg} " && ! "${pkg}" =~ '-uninstalled' ]]; then
	    echo "$(basename ${0}): warning: blocking '${pkg}': not selected by '${PKGCONFIG_WHITELIST_SRC}'" >&2
	    exit 1
	fi
    done
fi
pkg-config.real "${@}" | sed "${args[@]}"
check_pipe_status