dnl ===========================================================================
dnl
dnl LCOV
dnl
cairo_has_lcov=no
AC_ARG_ENABLE(gcov,
  AS_HELP_STRING([--enable-gcov],
		 [Enable gcov]),
  [use_gcov=$enableval], [use_gcov=no])

if test "x$use_gcov" = "xyes"; then
  dnl we need gcc:
  if test "$GCC" != "yes"; then
    AC_MSG_ERROR([GCC is required for --enable-gcov])
  fi

  dnl Check if ccache is being used
  AC_CHECK_PROG(SHTOOL, shtool, shtool)
  case `$SHTOOL path $CC` in
    *ccache*[)] gcc_ccache=yes;;
    *[)] gcc_ccache=no;;
  esac

  if test "$gcc_ccache" = "yes" && (test -z "$CCACHE_DISABLE" || test "$CCACHE_DISABLE" != "1"); then
    AC_MSG_ERROR([ccache must be disabled when --enable-gcov option is used. You can disable ccache by setting environment variable CCACHE_DISABLE=1.])
  fi

  ltp_version_list="1.7 1.6 1.5 1.4"
  AC_CHECK_PROG(LTP, lcov, lcov)
  AC_CHECK_PROG(LTP_GENHTML, genhtml, genhtml)

  if test "$LTP"; then
    AC_CACHE_CHECK([for ltp version], cairo_cv_ltp_version, [
      cairo_cv_ltp_version=invalid
      ltp_version=`$LTP -v 2>/dev/null | $SED -e 's/^.* //'`
      for ltp_check_version in $ltp_version_list; do
        if test "$ltp_version" = "$ltp_check_version"; then
          cairo_cv_ltp_version="$ltp_check_version (ok)"
        fi
      done
    ])
  fi

  case $cairo_cv_ltp_version in
    ""|invalid[)]
      ;;
    *)
      cairo_has_lcov=yes
      ;;
  esac

  if test "x$cairo_has_lcov" != "xyes"; then
    AC_MSG_ERROR([[To enable code coverage reporting you must have one of the following LTP versions installed: $ltp_version_list.
Please install the Linux Test Project [http://ltp.sourceforge.net/], and try again.]])
   fi

  if test -z "$LTP_GENHTML"; then
    AC_MSG_ERROR([[Could not find genhtml from the LTP package.
Please install the Linux Test Project [http://ltp.sourceforge.net/], and try again.]])
  fi

  AC_DEFINE(HAVE_GCOV, 1, [Whether you have gcov])
dnl  PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/Makefile.gcov, $abs_srcdir)

  dnl Remove all optimization flags from CFLAGS
  changequote({,})
  CFLAGS=`echo "$CFLAGS" | $SED -e 's/-O[0-9]*//g'`
  CAIRO_CFLAGS=`echo "$CAIRO_CFLAGS" | $SED -e 's/-O[0-9]*//g'`
  changequote([,])

  dnl Add the special gcc flags
  dnl In order to workaround a debian bug in libtool where they strip
  dnl $dependency_libs from the link line and CFLAGS, we need to pass
  dnl --coverage via LDFLAGS.
  CAIRO_CC_TRY_FLAG([--coverage],,
		    [
		    CAIRO_CFLAGS="$CAIRO_CFLAGS -O0 --coverage"
		    CAIRO_LDFLAGS="$CAIRO_LDFLAGS -O0 --coverage"
		    ])
fi
AM_CONDITIONAL(CAIRO_HAS_LCOV, test "x$cairo_has_lcov" = "xyes")

dnl ===========================================================================
dnl Check for some custom valgrind modules
AC_ARG_ENABLE(valgrind,
  AS_HELP_STRING([--disable-valgrind],
		 [Disable valgrind support]),
  [use_valgrind=$enableval], [use_valgrind=yes])

if test "x$use_valgrind" = "xyes"; then
    PKG_CHECK_MODULES(VALGRIND, valgrind, [
	    _save_CFLAGS="$CFLAGS"
	    _save_CPPFLAGS="$CPPFLAGS"
	    CFLAGS="$CFLAGS $VALGRIND_CFLAGS"
	    CPPFLAGS="$CPPFLAGS $VALGRIND_CFLAGS"
	    AC_CHECK_HEADER([valgrind.h], [AC_DEFINE([HAVE_VALGRIND], [1],
			    [Define to 1 if you have Valgrind])])
	    AC_CHECK_HEADER([lockdep.h], [AC_DEFINE([HAVE_LOCKDEP], [1],
			    [Define to 1 if you have the Valgrind lockdep tool])])
	    AC_CHECK_HEADER([memfault.h], [AC_DEFINE([HAVE_MEMFAULT], [1],
			    [Define to 1 if you have the Valgrind memfault tool])])
	    CAIRO_CFLAGS="$VALGRIND_CFLAGS $CAIRO_CFLAGS"
	    CFLAGS="$_save_CFLAGS"
	    CPPFLAGS="$_save_CPPFLAGS"
	], AC_MSG_RESULT(no))
fi
