dnl Process this file with autoconf to produce a configure script. -*-m4-*-
dnl
dnl %CopyrightBegin%
dnl
dnl Copyright Ericsson AB 2001-2016. All Rights Reserved.
dnl
dnl Licensed under the Apache License, Version 2.0 (the "License");
dnl you may not use this file except in compliance with the License.
dnl You may obtain a copy of the License at
dnl
dnl     http://www.apache.org/licenses/LICENSE-2.0
dnl
dnl Unless required by applicable law or agreed to in writing, software
dnl distributed under the License is distributed on an "AS IS" BASIS,
dnl WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dnl See the License for the specific language governing permissions and
dnl limitations under the License.
dnl
dnl %CopyrightEnd%
dnl

dnl define([AC_CACHE_LOAD], )dnl
dnl define([AC_CACHE_SAVE], )dnl

if test "x$no_recursion" != "xyes" -a "x$OVERRIDE_CONFIG_CACHE" = "x"; then
    # We do not want to use a common cache!
    cache_file=/dev/null
fi

AC_INIT(vsn.mk)

if test -z "$ERL_TOP" || test ! -d $ERL_TOP ; then
  AC_CONFIG_AUX_DIRS(autoconf)
else
  erl_top=${ERL_TOP}
  AC_CONFIG_AUX_DIRS($erl_top/erts/autoconf)
fi

if test "X$host" != "Xfree_source" -a "X$host" != "Xwin32"; then
    AC_CANONICAL_HOST
else
    host_os=win32
fi


dnl ----------------------------------------------------------------------
dnl Checks for programs.
dnl ----------------------------------------------------------------------

AC_DEFUN(ERL_REENTRANT_FLEX,
[flex_compile='$LEX -R -Pconftest -oconftest.c conftest.flex 1>&AC_FD_CC'
changequote(253, 273)dnl
cat > conftest.flex <<EOF
/*
 * This (reentrant) example code comes from the flex manual
 */

%option reentrant stack noyywrap
%x COMMENT

%%

"//"                 yy_push_state( COMMENT, yyscanner);
.|\n

<COMMENT>\n          yy_pop_state( yyscanner );
<COMMENT>[^\n]+      fprintf( yyout, "%s\n", yytext);

%%

int main ( int argc, char * argv[] )
{
  yyscan_t scanner;

  yylex_init ( &scanner );
  yylex ( scanner );
  yylex_destroy ( scanner );
  return 0;
}
EOF
changequote([, ])dnl
AC_MSG_CHECKING(for reentrant capable flex)
if AC_TRY_EVAL(flex_compile) && test -s conftest.c; then
   ifelse([$1], , :, [
   $1])
   AC_MSG_RESULT([yes])
else
   echo "configure: failed program was:" 1>&AC_FD_CC
   cat conftest.flex 1>&AC_FD_CC
   echo "configure: PATH was $PATH" 1>&AC_FD_CC
ifelse([$2], , , [
  $2
])dnl
   AC_MSG_RESULT([no])
fi
])


dnl
dnl Shall we attempt to use reentrant flex scanner or not
dnl
AC_ARG_ENABLE(megaco_reentrant_flex_scanner,
[  --enable-megaco-reentrant-flex-scanner  enable reentrant megaco flex scanner
  --disable-megaco-reentrant-flex-scanner  disable reentrant megaco flex scanner],
  if test x${enable_megaco_reentrant_flex_scanner} = xno ; then
    ENABLE_REENTRANT_MEGACO_FLEX_SCANNER=false
  else
    ENABLE_REENTRANT_MEGACO_FLEX_SCANNER=true
  fi, 
  ENABLE_REENTRANT_MEGACO_FLEX_SCANNER=true)


dnl
dnl flex is needed by megaco. lex wont do!
dnl

AC_PROG_LEX
if test "$LEX" != flex; then
  ENABLE_MEGACO_FLEX_SCANNER=false
else
  ENABLE_MEGACO_FLEX_SCANNER=true
  dnl Check if we can generate a reentrant scanner
  dnl ENABLE_REENTRANT_MEGACO_FLEX_SCANNER=true
  if ${ENABLE_REENTRANT_MEGACO_FLEX_SCANNER} = true ; then
    ERL_REENTRANT_FLEX(ENABLE_REENTRANT_MEGACO_FLEX_SCANNER=true,
                       ENABLE_REENTRANT_MEGACO_FLEX_SCANNER=false)
  fi
fi
AC_SUBST(ENABLE_MEGACO_FLEX_SCANNER)
AC_SUBST(ENABLE_REENTRANT_MEGACO_FLEX_SCANNER)



dnl
dnl For increased performance it is possible to disable lineno
dnl
AC_ARG_ENABLE(megaco_flex_scanner_lineno,
[  --enable-megaco-flex-scanner-lineno  enable megaco flex scanner lineno 
  --disable-megaco-flex-scanner-lineno  disable megaco flex scanner lineno],
  if test x${enable_megaco_flex_scanner_lineno} = xno ; then
    ENABLE_MEGACO_FLEX_SCANNER_LINENO=false
  else
    ENABLE_MEGACO_FLEX_SCANNER_LINENO=true
  fi, 
  ENABLE_MEGACO_FLEX_SCANNER_LINENO=true)
AC_SUBST(ENABLE_MEGACO_FLEX_SCANNER_LINENO)


dnl This is the os flavour, should be unix or win32
if test "X$host" = "Xwin32"; then
	ERLANG_OSTYPE=win32
else
	ERLANG_OSTYPE=unix
fi

AC_SUBST(ERLANG_OSTYPE)

dnl Magic test for clearcase.
if test -d ../../system; then
	OTP_EXTRA_FLAGS=-DOTP_RELEASE
else
	OTP_EXTRA_FLAGS=
fi
AC_SUBST(OTP_EXTRA_FLAGS)

if test "x$GCC" = xyes; then
  # Treat certain GCC warnings as errors
  LM_TRY_ENABLE_CFLAG([-Werror=return-type], [CFLAGS])
fi

dnl ----------------------------------------------------------------------
dnl Enable -fsanitize= flags.
dnl ----------------------------------------------------------------------

m4_define(DEFAULT_SANITIZERS, [address,undefined])
AC_ARG_ENABLE(
    sanitizers,
    AS_HELP_STRING(
        [--enable-sanitizers@<:@=comma-separated list of sanitizers@:>@],
	    [Default=DEFAULT_SANITIZERS]),
[
case "$enableval" in
    no) sanitizers= ;;
    yes) sanitizers="-fsanitize=DEFAULT_SANITIZERS" ;;
    *) sanitizers="-fsanitize=$enableval" ;;
esac
CFLAGS="$CFLAGS $sanitizers"
LDFLAGS="$LDFLAGS $sanitizers"
])

dnl
dnl If ${ERL_TOP}/make/otp_ded.mk.in exists and contains DED_MK_VSN > 0,
dnl every thing releted to compiling Dynamic Erlang Drivers can be found
dnl in $(ERL_TOP)/make/$(TARGET)/ded.mk at compile time. If not, try to
dnl figure these things out.
dnl

AC_MSG_CHECKING([for usable Dynamic Erlang Driver configuration])
[
  ded_mk_in="${ERL_TOP}/make/otp_ded.mk.in"
  ded_mk_vsn=
  test -r "$ded_mk_in" &&
    ded_mk_vsn=`sed -n "s/^DED_MK_VSN[	 ]*=[	 ]*\(.*\)/\1/p" < "$ded_mk_in"`
  test "$ded_mk_vsn" != "" || ded_mk_vsn=0
]

if test $ded_mk_vsn -gt 0; then

HAVE_USABLE_OTP_DED_MK=yes
AC_MSG_RESULT([yes])

CC=false
AC_SUBST(CC)
DED_LD=false
AC_SUBST(DED_LD)

else dnl --- begin no usable otp_ded.mk.in ---

HAVE_USABLE_OTP_DED_MK=no
AC_MSG_RESULT([no])

dnl
dnl C compiler (related) defs
dnl

AC_PROG_CC

dnl
dnl Flags to the C compiler
dnl

if test "X$host" = "Xwin32"; then
  DED_CFLAGS="$CFLAGS"
else
  case $host_os in
    darwin*)
	CFLAGS="$CFLAGS -fno-common"
	;;
  esac

  if test "x$GCC" = xyes; then
	DED_CFLAGS="$CFLAGS -fPIC $DED_CFLAGS"
  else
	DED_CFLAGS="$CFLAGS $DED_CFLAGS"
  fi
fi

dnl emulator includes needed
DED_INCLUDES="-I${ERL_TOP}/erts/emulator/beam -I${ERL_TOP}/erts/include -I${ERL_TOP}/erts/include/$host -I${ERL_TOP}/erts/include/internal -I${ERL_TOP}/erts/include/internal/$host -I${ERL_TOP}/erts/emulator/sys/$ERLANG_OSTYPE"

DED_THR_DEFS="-D_THREAD_SAFE -D_REENTRANT"

case $host_os in
	win32)
		DED_LDFLAGS="-dll"
	;;
	solaris2*|sysv4*)
		DED_LDFLAGS="-G"
	;;
	aix4*)
		DED_LDFLAGS="-G -bnoentry -bexpall"
	;;
	freebsd2*)
		# Non-ELF GNU linker
		DED_LDFLAGS="-Bshareable"
	;;
	darwin*)
		# Mach-O linker, a shared lib and a loadable
		# object file is not the same thing.
		DED_LDFLAGS="-bundle -flat_namespace -undefined suppress"
		DED_LD="$CC"
	;;
	*)
		# assume GNU linker and ELF
		DED_LDFLAGS="-shared"
	;;
esac

AC_CHECK_PROGS(DED_LD, [$LD ld.sh])
AC_CHECK_TOOL(DED_LD, ld, no_ld)
if test "$DED_LD" = no_ld; then
  AC_MSG_ERROR([ld is required to build the flex scanner!])
fi

AC_MSG_CHECKING(for linker flags for loadable drivers)
DED_LDFLAGS="$LDFLAGS $DED_LDFLAGS"
AC_MSG_RESULT([$DED_LDFLAGS])

fi dnl --- end no usable otp_ded.mk.in ---

AC_SUBST(HAVE_USABLE_OTP_DED_MK)
AC_SUBST(DED_CFLAGS)
AC_SUBST(DED_INCLUDES)
AC_SUBST(DED_THR_DEFS)
AC_SUBST(DED_LDFLAGS)

AC_CHECK_PROG(PERL, perl, perl, no_perl)
if test "$PERL" = no_perl; then
  AC_MSG_ERROR([Perl is required to build the flex scanner!])
fi

AC_OUTPUT(examples/meas/Makefile:examples/meas/Makefile.in)
AC_OUTPUT(src/flex/$host/Makefile:src/flex/Makefile.in)

