dnl Process this file with autoconf to produce a configure script.
AC_INIT(dwarfdump.c)
AC_CONFIG_HEADER(config.h)

AC_PROG_CC
AC_GCC_TRADITIONAL
AC_PROG_INSTALL
AC_CHECK_TOOL(RANLIB, ranlib, :)
AC_CHECK_TOOL(AR, ar)
dnl AC_ARFLAGS

AC_CHECK_HEADERS(elf.h unistd.h libelf.h libelf/libelf.h sgidefs.h sys/types.h)

dnl Windows seems to have libelf.h with the normal elf.h
dnl data embedded. No literal elf.h  is present.
dnl Get a best estimate of elf header data location.
AC_MSG_CHECKING(where is basic elf data)
if test "$ac_cv_header_elf_h" = yes; then
 AC_DEFINE(HAVE_LOCATION_OF_LIBELFHEADER,[<elf.h>], [Define to header that first defines elf])
 AC_MSG_RESULT(<elf.h>)
elif test "$ac_cv_header_libelf_h" = yes; then
 AC_DEFINE(HAVE_LOCATION_OF_LIBELFHEADER, [<libelf.h>],
	[Define to header that first defines elf.])
 AC_MSG_RESULT(<libelf.h>)
elif test "$ac_cv_header_libelf_libelf_h" = yes; then
 AC_DEFINE(HAVE_LOCATION_OF_LIBELFHEADER,[<libelf/libelf.h>],
	[Define to header that first defines elf.])
 AC_MSG_RESULT(<libelf/libelf.h>)
else
 AC_MSG_RESULT(NO standard ELF HEADERS found)
fi

AC_CHECK_LIB(elf,elf64_getehdr,
  AC_DEFINE(HAVE_ELF64_GETEHDR,1, 
	[Define to 1 if the elf64_getehdr function is in libelf.a.]))

dnl default-disabled shared
dnl we allow the argument but the result is ignored.
shrd=''
AC_ARG_ENABLE(shared,AC_HELP_STRING([--enable-shared],
                [build shared library libdwarf.so and use it if present]))
AS_IF([ test "x$enable_shared" = "xyes"], [
   shrd='--enable-shared'])

nonshrd=''
dnl default-enabled nonshared
dnl we allow the argument but the result is ignored.
AC_SUBST(build_nonshared,[none.a])
AC_ARG_ENABLE(nonshared,AC_HELP_STRING([--disable-nonshared],
                [do not build archive library libdwarf.a]))
AS_IF([ test "x$enable_nonshared" = "xno"], [
   nonshrd='--disable-shared'
])


dnl This adds compiler options for gcc to get as complete
dnl diagnostics as seems to make sense (subject to change...).
AC_MSG_CHECKING(compile with -Wall)
AC_SUBST(dwfwall,[])
AC_ARG_ENABLE(wall,AC_HELP_STRING([--enable-wall],
        [Add -Wall (default is none)]),
        [ AC_MSG_RESULT(yes)
          AC_SUBST(dwfwall,["-Wall -O0 -Wpointer-arith -Wmissing-declarations -Wmissing-prototypes -Wdeclaration-after-statement -Wextra -Wcomment -Wformat -Wpedantic -Wuninitialized -Wno-long-long -Wshadow"]) ],
        [ AC_SUBST(dwfwall,[])
          AC_MSG_RESULT(no) ])

dnl This adds compiler option -fsanitize=address etc (gcc compiler run-time checks))
AC_SUBST(dwfsanitize,[])
AC_MSG_CHECKING(build -fsanitize-address) 
AC_ARG_ENABLE(sanitize,AC_HELP_STRING([--enable-sanitize],
       [Add -fsanitize (default is not to)]),
       [ AC_SUBST(dwfsanitize,["-fsanitize=address -fsanitize=leak -fsanitize=undefined"])
          AC_MSG_RESULT(yes) ],
       [ AC_SUBST(dwfsanitize,[]) 
          AC_MSG_RESULT(no)
        ]
        )

AC_TRY_COMPILE([#include "stdafx.h"],[  int p; p = 27;] ,
  AC_DEFINE(HAVE_STDAFX_H,1,
        [Define 1 if we have the Windows specific header stdafx.h]))

AC_TRY_COMPILE([#include HAVE_LOCATION_OF_LIBELFHEADER],  Elf64_Rel *p; int i; i = p->r_info; ,AC_DEFINE(HAVE_ELF64_R_INFO,1,
	[Define to 1 if the Elf64_Rel structure has r_info field.]))
AC_TRY_COMPILE([],  __uint32_t p; p = 3; ,AC_DEFINE(HAVE___UINT32_T,
	1,[See if __uint32_t is predefined in the compiler.  ]))
AC_TRY_COMPILE([],  __uint64_t p; p = 3; ,AC_DEFINE(HAVE___UINT64_T,
	1,[See if __uint64_t is predefined in the compiler. ]))
AC_TRY_COMPILE([#include <sys/types.h>],[  __uint32_t p; p = 3;] ,
  AC_DEFINE(HAVE___UINT32_T_IN_SYS_TYPES_H,1,
        [Define 1 if sys/types.h defines __uint32_t.]))
AC_TRY_COMPILE([#include <sys/types.h>],[  __uint64_t p; p = 3;] ,
  AC_DEFINE(HAVE___UINT64_T_IN_SYS_TYPES_H,1,
        [Define 1 if sys/types.h defines __uint64_t.]))

AC_TRY_COMPILE([#include <sys/types.h>
        #include <regex.h>],[  int i; 
        regex_t r;
        int cflags = REG_EXTENDED;
        const char *s = "abc";
        i = regcomp(&r,s,cflags);
        regfree(&r);
        ]; ,
	AC_DEFINE(HAVE_REGEX,1,
		[Define 1 if regex seems to be defined]))

AC_MSG_CHECKING(checking if __attribute__ unused compiles ok)
AC_TRY_RUN([
  static unsigned foo( unsigned x, __attribute__ ((unused)) int y)
  {  unsigned x2 = x + 1;
     return x2;
  }
  int main(void) {
     unsigned y = 0;
     y = foo(12,y);
     return 0;
  }
] ,
  [AC_DEFINE(HAVE_UNUSED_ATTRIBUTE,1,
        [Define 1 if __attribute__ ((unused)) compiles ok.])
    AC_MSG_RESULT(yes) ],
  [AC_MSG_RESULT(no) ])

AC_SUBST(dwfzlib,[])
AC_MSG_CHECKING(checking zlib present )
AC_TRY_COMPILE([#include "zlib.h"],[
    Bytef dest[100];
    uLongf destlen = 100;
    Bytef *src = 0;
    uLong srclen = 3;
    int res = uncompress(dest,&destlen,src,srclen);
    if (res == Z_OK) {
         /* ALL IS WELL */
    }
    ] ,
    [AC_DEFINE(HAVE_ZLIB,1,
        [Define 1 if zlib (decompression library) seems available.])
     AC_SUBST(dwfzlib,[-lz])
     AC_MSG_RESULT(yes)
    ],
    [AC_MSG_RESULT(no)])


dnl this is ignored in dwarfdump.
AC_ARG_ENABLE(nonstandardprintf,AC_HELP_STRING([--enable-nonstandardprintf],
    [Use a special printf format for 64bit (default is NO)]),
    [ AC_DEFINE([HAVE_NONSTANDARD_PRINTF_64_FORMAT],[1],
        [Define 1 if need nonstandard printf format for  64bit] )],
    [ ])

AC_TRY_COMPILE([
#include <libelf.h>
],[  int p; p = 0; ] ,
  AC_DEFINE(HAVE_RAW_LIBELF_OK,1,
        [Define 1 if plain libelf builds.]))

AC_MSG_CHECKING(is off64_t type supported)
AC_TRY_COMPILE([
#define _GNU_SOURCE
#include <libelf.h>
],[  off64_t  p; p = 0;] ,
  [AC_DEFINE(HAVE_LIBELF_OFF64_OK,1,
        [Define 1 if  off64 is defined via libelf with GNU_SOURCE.])
   AC_MSG_RESULT(yes)],
  [ AC_MSG_RESULT(no) ])


AC_OUTPUT(Makefile)
