dnl CF_WITH_PKG_CONFIG_LIBDIR version: 25 updated: 2024/08/03 13:34:29
dnl -------------------------
dnl Allow the choice of the pkg-config library directory to be overridden.
dnl
dnl pkg-config uses a search-list built from these colon-separated lists of
dnl directories:
dnl a) $PKG_CONFIG_PATH (tested first, added if set)
dnl b) $PKG_CONFIG_LIBDIR (tested second, added if set)
dnl c) builtin-list (added if $PKG_CONFIG_LIBDIR is not set)
dnl
dnl pkgconf (used with some systems such as FreeBSD in place of pkg-config)
dnl optionally ignores $PKG_CONFIG_LIBDIR.  Very old versions of pkg-config,
dnl e.g., Solaris 10 also do not recognize $PKG_CONFIG_LIBDIR.
AC_DEFUN([CF_WITH_PKG_CONFIG_LIBDIR],[
AC_REQUIRE([CF_GLOB_FULLPATH])dnl

case "$PKG_CONFIG" in
(no|none|yes)
	AC_MSG_CHECKING(for pkg-config library directory)
	;;
(*)
	AC_MSG_CHECKING(for $PKG_CONFIG library directory)
	;;
esac

# if $PKG_CONFIG_LIBDIR is set, try to use that
if test -n "$PKG_CONFIG_PATH"; then
	cf_search_path=`echo "$PKG_CONFIG_PATH" | sed -e 's/:/ /g' -e 's,^[[ 	]]*,,' -e 's,[[ 	]]*$,,'`
elif test -n "$PKG_CONFIG_LIBDIR"; then
	cf_search_path=`echo "$PKG_CONFIG_LIBDIR" | sed -e 's/:/ /g' -e 's,^[[ 	]]*,,' -e 's,[[ 	]]*$,,'`
else
	cf_search_path=auto
fi

# if the option is used, let that override.  otherwise default to "libdir"
AC_ARG_WITH(pkg-config-libdir,
	[[  --with-pkg-config-libdir[=XXX] use given directory for installing pc-files]],
	[cf_search_path=$withval],
	[test "x$PKG_CONFIG" != xnone && test -z "$cf_search_path" && cf_search_path=libdir])

case "x$cf_search_path" in
(xlibdir)
	PKG_CONFIG_LIBDIR='${libdir}/pkgconfig'
	AC_MSG_RESULT($PKG_CONFIG_LIBDIR)
	cf_search_path=
	;;
(x)
	;;
(x$GLOB_FULLPATH_POSIX\ *|x$GLOB_FULLPATH_OTHER\ *)
	PKG_CONFIG_LIBDIR=
	;;
(x$GLOB_FULLPATH_POSIX|x$GLOB_FULLPATH_OTHER)
	PKG_CONFIG_LIBDIR="$cf_search_path"
	AC_MSG_RESULT($PKG_CONFIG_LIBDIR)
	cf_search_path=
	;;
(xyes|xauto)
	AC_MSG_RESULT(auto)
	cf_search_path=
	# Look for the library directory using the same prefix as the executable
	AC_MSG_CHECKING(for search-list)
	if test "x$PKG_CONFIG" != xnone
	then
		# works for pkg-config since version 0.24 (2009)
		# works for pkgconf since version 0.8.3 (2012)
		for cf_pkg_program in \
			`echo "$PKG_CONFIG" | sed -e 's,^.*/,,'` \
			pkg-config \
			pkgconf
		do
			cf_raw_search_path=`"$PKG_CONFIG" --variable=pc_path "$cf_pkg_program" 2>/dev/null`
			case "$cf_raw_search_path" in
			(*\\*)
				cf_search_path=`echo "$cf_raw_search_path" | tr ';' ' ' | tr '\' '/'`
				;;
			(*/*)
				cf_search_path=`echo "$cf_raw_search_path" | tr : ' '`
				;;
			esac
			test -n "$cf_search_path" && break
		done

		# works for pkg-config since import in 2005 of original 2001 HP code.
		test -z "$cf_search_path" && \
		cf_search_path=`
		"$PKG_CONFIG" --debug --exists no-such-package 2>&1 | $AWK "\
/^Scanning directory (#[1-9][0-9]* )?'.*'$/{ \
	sub(\"^[[^']]*'\",\"\"); \
	sub(\"'.*\",\"\"); \
	printf \" %s\", \\[$]0; } \
{ next; } \
"`
	fi

	AC_MSG_RESULT($cf_search_path)
	;;
(*)
	AC_MSG_ERROR(Unexpected option value: $cf_search_path)
	;;
esac

if test -n "$cf_search_path"
then
	AC_MSG_CHECKING(for first directory)
	cf_pkg_config_path=none
	for cf_config in $cf_search_path
	do
		if test -d "$cf_config"
		then
			cf_pkg_config_path=$cf_config
			break
		fi
	done
	AC_MSG_RESULT($cf_pkg_config_path)

	if test "x$cf_pkg_config_path" != xnone ; then
		# limit this to the first directory found
		PKG_CONFIG_LIBDIR="$cf_pkg_config_path"
	fi

	if test -z "$PKG_CONFIG_LIBDIR" && test -n "$cf_search_path"
	then
		AC_MSG_CHECKING(for workaround)
		if test "$prefix" = "NONE" ; then
			cf_prefix="$ac_default_prefix"
		else
			cf_prefix="$prefix"
		fi
		eval cf_libdir=$libdir
		cf_libdir=`echo "$cf_libdir" | sed -e "s,^NONE,$cf_prefix,"`
		cf_backup=
		for cf_config in $cf_search_path
		do
			case $cf_config in
			$cf_libdir/pkgconfig)
				PKG_CONFIG_LIBDIR=$cf_libdir/pkgconfig
				break
				;;
			*)
				test -z "$cf_backup" && cf_backup=$cf_config
				;;
			esac
		done
		test -z "$PKG_CONFIG_LIBDIR" && PKG_CONFIG_LIBDIR=$cf_backup
		AC_MSG_RESULT($PKG_CONFIG_LIBDIR)
	fi
fi

AC_SUBST(PKG_CONFIG_LIBDIR)
])dnl
