1dnl
2dnl @synopsis ST_LIB_ARCHIVE([ENABLED-DEFAULT])
3dnl
4dnl This macro figures out what's necessary to link a program against an
5dnl instance of the BSD libarchive package by Tim Kientzle.
6dnl 
7dnl See http://people.freebsd.org/~kientzle/libarchive/ for more info.
8dnl
9dnl It exports and substitutes the variables LIBARCHIVE_LIBS, LIBARCHIVE_LDFLAGS,
10dnl and LIBARCHIVE_CPPFLAGS to appropriate values for the identified instance of
11dnl libarchive.  The values are AC_SUBST'd, so a user could, for example, simply
12dnl include @LIBARCHIVE_CPPFLAGS@ in the definition of AM_CPPFLAGS in a Makefile.am.
13dnl
14dnl ENABLED-DEFAULT is either "yes" or "no" and determines whether the default value
15dnl is --with-libarchive or --without-libarchive.  It is not possible to specify a
16dnl default directory.  More simply, any reasonable choice for a default should just
17dnl go into the auto-detect list.
18dnl
19dnl The macro defines the symbol HAVE_LIBARCHIVE if the library is found. You
20dnl should use autoheader to include a definition for this symbol in a config.h
21dnl file. Sample usage in a C/C++ source is as follows:
22dnl
23dnl   #ifdef HAVE_LIBARCHIVE
24dnl   #include <archive.h>
25dnl   #endif /* HAVE_LIBARCHIVE */
26dnl
27dnl @category InstalledPackages
28dnl @author Andre Stechert <andre@splunk.com>
29dnl @version 2006-04-20
30dnl @license GPLWithACException
31
32AC_DEFUN([ST_LIB_ARCHIVE],
33[
34#
35# Handle input from the configurer and blend with the requirements from the maintainer.
36# We go through the trouble of creating a second set of variables other than the with_foo
37# variables in order to be sure that error/corner cases have been cleaned up.
38#
39# After this statement, three trusted variable are defined.
40#
41# st_lib_archive_ENABLED will be either "yes" or "no".  its value determines whether
42# or not we bother with the rest of the checks and whether or not we export a
43# bunch of variables.
44#
45# st_lib_archive_LOCATION will be either "auto" or "defined".  if it is "auto", then
46# we try a bunch of standard locations.  if it is "defined", then we just try the value
47# provided in st_lib_archive_DIR.
48#
49# st_lib_archive_DIR will contain the string provided by the user, provided that it's
50# actually a directory.
51#
52AC_MSG_CHECKING([if libarchive is wanted])
53AC_ARG_WITH([libarchive],
54	AS_HELP_STRING([--with-libarchive=DIR], [libarchive installation directory]),
55	[if test "x$with_libarchive" = "xno" ; then
56		st_lib_archive_ENABLED=no
57	elif test "x$with_libarchive" = "xyes" ; then
58		st_lib_archive_ENABLED=yes
59		st_lib_archive_LOCATION=auto
60	else
61		st_lib_archive_ENABLED=yes
62		st_lib_archive_LOCATION=defined
63		if test -d "$with_libarchive" ; then
64			st_lib_archive_DIR="$with_libarchive"
65		else
66			AC_MSG_ERROR([$with_libarchive is not a directory])
67		fi
68	fi],
69	[if test "x$1" = "xno" ; then
70		st_lib_archive_ENABLED=no
71	elif test "x$1" = "xyes" ; then
72		st_lib_archive_ENABLED=yes
73	else
74		st_lib_archive_ENABLED=yes
75	fi])
76
77if test "$st_lib_archive_ENABLED" = "yes" ; then
78	AC_MSG_RESULT([yes])
79#
80# After this statement, one trusted variable is defined.
81#
82# st_lib_archive_LIB will be either "lib" or "lib64", depending on whether the configurer
83# specified 32, 64.  The default is "lib".
84#
85	AC_MSG_CHECKING([whether to use lib or lib64])
86	AC_ARG_WITH([libarchive-bits],
87		AS_HELP_STRING([--with-libarchive-bits=32/64], [if 64, look in /lib64 on hybrid systems]),
88		[if test "x$with_libarchive_bits" = "x32" ; then
89			st_lib_archive_LIB=lib
90		elif test "x$with_libarchive_bits" = "x64" ; then
91			st_lib_archive_LIB=lib64
92		else
93			AC_MSG_ERROR([the argument must be either 32 or 64])
94		fi],
95		[st_lib_archive_LIB=lib])
96	AC_MSG_RESULT($st_lib_archive_LIB)
97#
98# Save the environment before verifying libarchive availability
99#
100	st_lib_archive_SAVECPPFLAGS="$CPPFLAGS"
101	st_lib_archive_SAVELDFLAGS="$LDFLAGS"
102	AC_LANG_SAVE
103	AC_LANG_C
104
105	if test "x$st_lib_archive_LOCATION" = "xdefined" ; then
106		CPPFLAGS="-I$st_lib_archive_DIR/include $st_lib_archive_SAVECPPFLAGS"
107		LDFLAGS="-L$st_lib_archive_DIR/$st_lib_archive_LIB $st_lib_archive_SAVELDFLAGS"
108		AC_CHECK_LIB(archive, archive_read_new, [st_lib_archive_found_lib=yes], [st_lib_archive_found_lib=no])
109		AC_CHECK_HEADER(archive.h, [st_lib_archive_found_hdr=yes], [st_lib_archive_found_hdr=no])
110		if test "x$st_lib_archive_found_lib" = "xyes" && test "x$st_lib_archive_found_hdr" = "xyes"; then
111			LIBARCHIVE_CPPFLAGS="-I$dir/include"
112			LIBARCHIVE_LDFLAGS="-L$dir/$st_lib_archive_LIB"
113		else
114			AC_MSG_ERROR([could not find libarchive in the requested location])
115		fi
116	else
117		#
118		# These are the common install directories for Linux, FreeBSD, Solaris, and Mac.
119		#
120		for dir in /usr /usr/local /usr/sfw /opt/csw /opt/local /sw
121		do
122			if test -d "$dir" ; then
123				CPPFLAGS="-I$dir/include $st_lib_archive_SAVECPPFLAGS"
124				LDFLAGS="-L$dir/$st_lib_archive_LIB $st_lib_archive_SAVELDFLAGS"
125				AC_CHECK_LIB(archive, archive_read_new, [st_lib_archive_found_lib=yes], [st_lib_archive_found_lib=no])
126				AC_CHECK_HEADER(archive.h, [st_lib_archive_found_hdr=yes], [st_lib_archive_found_hdr=no])
127				if test "x$st_lib_archive_found_lib" = "xyes" && test "x$st_lib_archive_found_hdr" = "xyes"; then
128					LIBARCHIVE_CPPFLAGS="-I$dir/include"
129					LIBARCHIVE_LDFLAGS="-L$dir/$st_lib_archive_LIB"
130					break
131				fi
132			fi
133		done
134	fi
135
136	if test "x$st_lib_archive_found_hdr" = "xyes" && test "x$st_lib_archive_found_lib" = "xyes" ; then
137		LIBARCHIVE_LIBS="-larchive"
138		AC_DEFINE([HAVE_LIBARCHIVE], [1], [Defined to 1 if libarchive is available for use.])
139		AC_SUBST(LIBARCHIVE_LIBS)
140		AC_SUBST(LIBARCHIVE_CPPFLAGS)
141		AC_SUBST(LIBARCHIVE_LDFLAGS)
142	fi
143
144#
145# Restore the environment now that we're done.
146#
147	AC_LANG_RESTORE
148	CPPFLAGS="$st_lib_archive_SAVECPPFLAGS"
149	LDFLAGS="$st_lib_archive_SAVELDFLAGS"
150else
151	AC_MSG_RESULT([no])
152fi
153AM_CONDITIONAL(LIBARCHIVE, test "x$st_lib_archive_found_lib" = "xyes" && test "x$st_lib_archive_found_hdr" = "xyes")
154])
155