summaryrefslogtreecommitdiffstats
path: root/scripts/pkg-config-wrapper
blob: dca2c58a9e7c76bdd8187245f3c6dad663a97b26 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env bash
sysroot="$(dirname "$(dirname "$(dirname "$0")")")"
basedir="$(basename "${sysroot}")"

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

declare -a libdirs libpaths incpaths
# try to find all our pkgconfig paths.
libdirs=( $(find ${prefix} -maxdepth 3 -type d -name pkgconfig) )
for libdir in "${libdirs[@]}"; do
    lib="$(basename $(dirname ${libdir}))"
    libpaths+=( "${libdir[@]/%//../../${lib}}" "${libdir[@]/%//../${lib}}" )
    incpaths+=( "${libdir[@]/%//../../include}" "${libdir[@]/%//../include}" )
    libpaths+=( "/usr/${lib}" "/${lib}" )
done
incpaths+=( "/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 search path
export PKG_CONFIG_LIBDIR="${libdirs[*]}"
# default search path that will be dropped from --libs
export PKG_CONFIG_SYSTEM_LIBRARY_PATH="${libpaths[*]}"
# default search path that will be dropped from --cflags
export PKG_CONFIG_SYSTEM_INCLUDE_PATH="${incpaths[*]}"
IFS="${orig_IFS}"

for ((i = 1; i <= ${#}; i++)); do
    case "${!i}" in
	--variable*)
	    found_var=${!i#"--variable="}
	    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 "${found_var}" -a \
    "${PTXDIST_PKG_CONFIG_VAR_NO_SYSROOT#*${found_var}}" != "${PTXDIST_PKG_CONFIG_VAR_NO_SYSROOT}" ]; 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
}

print_pkg() {
    args=()
    while [ $# -gt 0 ]; do
	case "${1}" in
	    --variable|--define-variable|--atleast-pkgconfig-version|--*-version)
		shift
		;;
	    --*)
		;;
	    *)
		args[${#args[@]}]="${1}"
		;;
	esac
	shift
    done
    if [ ${#args[*]} -ne 0 ]; then
	pkgconf --print-provides "${args[@]}" 2> "${error}" | awk '{print $1}' &&
	check_pipe_status
    fi
}

#
# meson suppresses any pkg-config output, so dump it into the logfile instead
#
if [ "${pkg_conf_tool}" = "meson" -a -n "${PTXDIST_FD_LOGFILE}" ]; then
    ERROR_FD=${PTXDIST_FD_LOGFILE}
else
    ERROR_FD=2
fi

if [ -n "${PKGCONFIG_WHITELIST_SRC}" -a "${1}" != "--version" -a "${1}" != "--help" ]; then
    error="$(mktemp "${PTXDIST_TEMPDIR}/pkg-config.XXXXXX")"
    pkgs="$(print_pkg "${@}")"
    if [ $? -ne 0 ]; then
	if [ "${ERROR_FD}" != "2" ]; then
	    echo "$(basename ${0}) $*" >&${PTXDIST_FD_LOGFILE}
	fi
	cat "${error}" >&${ERROR_FD}
	rm "${error}"
	exit 1
    fi
    rm "${error}"
    for pkg in ${pkgs}; do
	if [[ ! " ${whitelist} " =~ " ${pkg} " && ! "${pkg}" =~ '-uninstalled' ]]; then
	    echo "$(basename ${0}): warning: blocking '${pkg}': not selected by '${PKGCONFIG_WHITELIST_SRC}'" >&${ERROR_FD}
	    exit 1
	fi
    done
fi
pkgconf "${@}" | sed "${args[@]}"
check_pipe_status