dnl CF_WITH_PERL version: 5 updated: 2010/08/13 04:43:13
dnl ------------
dnl Check if perl-extension (using embedded perl interpreter) is wanted, and
dnl update symbols if we are able to use the extension.
AC_DEFUN([CF_WITH_PERL],[
AC_MSG_CHECKING(if you want to use perl as an extension language)
AC_ARG_WITH(perl,
	[  --with-perl             enable use of Perl as an extension language],
	[with_perl="$withval"],
	[with_perl=no])
AC_MSG_RESULT($with_perl)

PERLLIB=

if test "$with_perl" = yes ; then
	CF_PROG_PERL(5.004)
	if test "$PERL" = no; then
		AC_WARN([perl not found])
	else
		cf_perl_CFLAGS="$CFLAGS"
		cf_perl_CPPFLAGS="$CPPFLAGS"
		cf_perl_link="$ac_link"
		cf_perl_LIBS="$LIBS"

		cf_perl_prefix=`$PERL -MConfig -e 'print $Config{shrpenv}'`
		cf_perl_ccopts=`$PERL -MExtUtils::Embed -e ccopts`
		cf_perl_ldopts=`$PERL -MExtUtils::Embed -e ldopts`

		ac_link="$cf_perl_prefix $ac_link"
		CF_CHECK_CFLAGS($cf_perl_ccopts)
		LIBS="$LIBS $cf_perl_ldopts"

		AC_TRY_LINK([#define main perl_main
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>
#undef main],[
		PerlInterpreter* interp = perl_alloc();
		perl_construct(interp);
		perl_parse(interp, 0, 0, (char **)0, (char **)0);
		Perl_croak("Why:%s\n", "Bye!");
],[
		PERLLIB=`$PERL -MConfig -e 'print $Config{privlib}'`

		CF_VERBOSE(adding perl library $PERLLIB)

		AC_DEFINE(OPT_PERL)

		EXTRAOBJS="$EXTRAOBJS perl\$o"
		BUILTSRCS="$BUILTSRCS perl.c"

		EXTRA_INSTALL_DIRS="$EXTRA_INSTALL_DIRS \$(INSTALL_PERL_DIRS)"
		EXTRA_INSTALL_FILES="$EXTRA_INSTALL_FILES \$(INSTALL_PERL_FILES)"
],[
		AC_MSG_WARN(Cannot link with Perl interpreter)

		CF_VERBOSE([undoing changes to CFLAGS, etc])

		CFLAGS="$cf_perl_CFLAGS"
		CPPFLAGS="$cf_perl_CPPFLAGS"
		ac_link="$cf_perl_link"
		LIBS="$cf_perl_LIBS"
])
	fi
fi
AC_SUBST(PERLLIB)
AC_SUBST(PERL)
AC_SUBST(EXTRA_INSTALL_DIRS)
AC_SUBST(EXTRA_INSTALL_FILES)
])
