1# $Id: acinclude.m4,v 1.8 2007/01/06 16:00:55 j_ali Exp $
2
3# Front-end of AC_ARG_ENABLE
4# Usage:
5#	NETHACK_ARG([feature], [description], [pre-processor switch], [default],
6#	  [action-if-auto])
7# Note that the action-if-auto argument is only used if the default is auto
8# (and the user doesn't specify). The action should set the shell variable
9# enable_feature, with any '-' characters changed into '_'.
10AC_DEFUN([NETHACK_ARG],
11  [AC_MSG_CHECKING(whether to enable $1)
12   AC_ARG_ENABLE([$1], AC_HELP_STRING([--enable-$1],[$2 (default=$4)]),
13     [],m4_if([$4], [auto], [$5], [enable_]m4_bpatsubst([$1], -, _)="[$4]"))
14   AC_MSG_RESULT([$enable_]m4_bpatsubst([$1], -, _))
15   if test "[$enable_]m4_bpatsubst([$1], -, _)" != "no" ; then
16      AC_DEFINE_UNQUOTED([$3], [], [$2])
17   fi
18  ])
19
20# NETHACK_ARG_WITH_PARAM([feature], [description], [pre-processor switch],
21#                        [default], [type], [default-value])
22# ------------------------------------------------------------------------
23AC_DEFUN([NETHACK_ARG_WITH_PARAM],
24  [AC_MSG_CHECKING([whether to enable m4_bpatsubst([$1], -, [ ])])
25   AC_ARG_ENABLE([$1], AC_HELP_STRING([--enable-$1=$5],
26        [$2 (default=$4, default m4_tolower($5)=[$6])]),
27        if test "[$enableval]" != no; then [enable_]m4_bpatsubst([$1], -, _)=yes;
28         if test "[$enableval]" != yes; then m4_bpatsubst([$1], -, _)=[$enableval];
29         else m4_bpatsubst([$1], -, _)=[$6]; fi;
30        else [enable_]m4_bpatsubst([$1], -, _)=no; fi,
31        [enable_]m4_bpatsubst([$1], -, _)=$4;m4_bpatsubst([$1], -, _)=[$6])
32   if test "[$enable_]m4_bpatsubst([$1], -, _)" != "no" ; then
33      AC_MSG_RESULT([yes (m4_tolower($5) [$]m4_bpatsubst([$1], -, _))])
34      AC_DEFINE_UNQUOTED([$3],
35       m4_case([NUMBER], $5, [$]m4_bpatsubst([$1], -, _), dnl NUMBER without quotes
36               "[$]m4_bpatsubst([$1], -, _)"),
37       [$2])
38      AC_SUBST([$3],["[$]m4_bpatsubst([$1], -, _)"])
39   else
40      AC_MSG_RESULT([no])
41   fi
42  ])
43
44# NETHACK_WIN_ENABLE(win, default, help-text)
45# -------------------------------------------
46AC_DEFUN([NETHACK_WIN_ENABLE],[AC_ARG_ENABLE($1-graphics,
47  [AC_HELP_STRING([--enable-$1-graphics],
48    m4_ifval([$3],[$3],[use $1-graphics]) [(default=$2)])],
49  [enable_$1_graphics="$enableval"], [enable_$1_graphics="$2"])
50  if test "$enable_$1_graphics" != "no"; then
51    nethack_wins="$1 $nethack_wins"
52  fi])
53
54# NETHACK_AUTO_WIN_DISABLE(win)
55# -----------------------------
56# Disable a windowing system previously set to auto.
57AC_DEFUN([NETHACK_AUTO_WIN_DISABLE],[enable_$1_graphics="no"
58    nethack_wins=`echo $nethack_wins | sed 's/$1 //'`
59])
60
61# NETHACK__LINK_ADD(DEST, [SOURCE])
62# ---------------------------------
63# Update if there is a newer source
64AC_DEFUN([NETHACK__LINK_ADD],
65	[m4_ifval([$2],[if test ! -f $1 -o ${srcdir}/$2 -nt $1 ; then
66	   NETHACK_LINKS="${NETHACK_LINKS} $1:$2"
67	fi],[NETHACK__LINK_ADD([$1],[$1])])])
68AC_DEFUN([NETHACK_LINKS_ADD],
69	[AC_FOREACH([NETHACK_File],[$1],
70	    [NETHACK__LINK_ADD(m4_bpatsubst(NETHACK_File,[:],[,]))])])
71
72# Autoconf's standard AC_CONFIG_HEADERS() avoids undefining switches because
73# of concern that it might undefine system switches (eg., POSIX_SOURCE).
74# However, we need to override the settings in config.h etc., so we post-process
75# the generated headers and force an explicit define and/or undefine.
76AC_DEFUN([NETHACK_CONFIG_HEADER],
77    [AC_CONFIG_HEADERS([$1],
78      [sed -e 's,^/\* \(#undef .*\) \*/,\1,' \
79        -e '/#define [^ ]*/ { h; s,#define \([^ ]*\).*,#undef \1,p; g; }'] \
80        < m4_bpatsubst([$1], [:.*]) > m4_bpatsubst([$1], [[^.]*:.*], [new])
81      [mv] m4_bpatsubst([$1], [[^.]*:.*], [new]) \
82        m4_bpatsubst([$1], [:.*]),[])])
83
84# NETHACK__ADD_FLAG(output-variable, existing-flags, flag)
85# ---------------------------------
86# Add a compiler flag to a variable. A number of flags are recognized and
87# handled specially (specifically, -I, -L and -l).
88AC_DEFUN([NETHACK__ADD_FLAG],[[
89    case "$3" in
90	-I*|-L*|-l*)
91	    for nethack__eflag in $2; do
92		if test "$nethack__eflag" = "$3"; then
93		    break
94		fi
95	    done
96	    if test "$nethack__eflag" = "$3"; then
97		$1="$2"
98	    else
99		case "$3" in
100		    -L*)$1="$3 $2";;
101		    *)  $1="$2 $3";;
102		esac
103	    fi
104	    ;;
105	*)  $1="$2 $3";;
106    esac
107]])
108
109# NETHACK__ADD_FLAGS(output-variable, existing-flags, new-flags)
110# ---------------------------------
111# Front-end to NETHACK__ADD_FLAG. Add compiler flags to a variable.
112AC_DEFUN([NETHACK__ADD_FLAGS],[
113    $1="$2"
114    for nethack__flag in $3; do
115	NETHACK__ADD_FLAG([$1], [${$1}], [$nethack__flag])
116    done
117])
118
119# NETHACK__TRY_LINK(STUFF, library, cflags, libs, includes, body, action-if,
120#	action-if-not)
121# ---------------------------------
122# Try linking using the given cflags and libs.
123AC_DEFUN([NETHACK__TRY_LINK],[
124    nethack__save_cflags=$CFLAGS
125    nethack__save_libs=$LIBS
126    CFLAGS="$CFLAGS $3"
127    LIBS="$LIBS $4"
128    AC_MSG_CHECKING([if $2 is useable with $CFLAGS and $LIBS])
129    AC_TRY_LINK([$5], [$6],
130      [$1_CFLAGS="$3"; $1_LIBS="$4"; AC_MSG_RESULT([yes]); $7],
131      [AC_MSG_RESULT([no]); $8])
132    CFLAGS=$nethack__save_cflags
133    LIBS=$nethack__save_libs])
134
135# NETHACK__TRY_LIBXAW(STUFF, cflags, libs, action-if, action-if-not)
136# ---------------------------------
137# Try linking with libXaw using the given cflags and libs.
138AC_DEFUN([NETHACK__TRY_LIBXAW], [NETHACK__TRY_LINK([$1], [libXaw], [$2], [$3],
139    [#include <X11/Xaw/List.h>], [XawListUnhighlight((Widget)0);], [$4], [$5])])
140
141# NETHACK_CHECK_LIBXAW(stuff, action-if, action-if-not)
142# ---------------------------------
143# Check if libXaw can be used
144AC_DEFUN([NETHACK_CHECK_LIBXAW],[
145    AC_ARG_VAR([LIBXAW_CFLAGS], [Compiler flags for libXaw, overriding pkg-config])
146    AC_ARG_VAR([LIBXAW_LIBS], [Linker flags for libXaw, overriding pkg-config])
147    # If the user has supplied flags, then believe them.
148    if test -n "$LIBXAW_CFLAGS" -o -n "$LIBXAW_LIBS"; then
149	NETHACK__ADD_FLAGS(nethack__cflags, [$NETHACK__LIBXAW_CFLAGS],
150	  [$LIBXAW_CFLAGS])
151	NETHACK__ADD_FLAGS(nethack__libs, [$NETHACK__LIBXAW_LIBS],
152	  [$LIBXAW_LIBS])
153	NETHACK__TRY_LIBXAW($1, [$nethack__cflags], [$nethack__libs],
154	  [nethack__succeeded=yes], [nethack__succeeded=no])
155    else
156	nethack__succeeded=maybe
157    fi
158    if test $nethack__succeeded = maybe; then
159	PKG_CHECK_EXISTS(xaw8, [nethack__succeeded=yes], [])
160    fi
161    if test $nethack__succeeded = maybe; then
162	PKG_CHECK_EXISTS(xaw7, [nethack__succeeded=yes], [])
163    fi
164    if test $nethack__succeeded = maybe; then
165	PKG_CHECK_EXISTS(xaw6, [nethack__succeeded=yes], [])
166    fi
167    if test $nethack__succeeded = yes; then
168	NETHACK__ADD_FLAGS(nethack__cflags, [$NETHACK__LIBXAW_CFLAGS],
169	  [-I/usr/X11R6/include])
170	NETHACK__ADD_FLAGS(nethack__libs, [$NETHACK__LIBXAW_LIBS],
171	  [-L/usr/X11R6/lib -lXaw -lXmu -lXext -lXt -lX11])
172	NETHACK__TRY_LIBXAW($1, [$nethack__cflags], [$nethack__libs],
173	  [nethack__succeeded=yes], [nethack__succeeded=no])
174    fi
175    if test $nethack__succeeded = yes; then
176	AC_MSG_CHECKING([$1_CFLAGS])
177	AC_MSG_RESULT([${$1_CFLAGS}])
178	AC_SUBST($1_CFLAGS)
179	AC_MSG_CHECKING([$1_LIBS])
180	AC_MSG_RESULT([${$1_LIBS}])
181	AC_SUBST($1_LIBS)
182	$2
183    m4_ifval([$3],[else $3],[])
184    fi
185])
186
187# NETHACK__TRY_LIBPNG(STUFF, cflags, libs, action-if, action-if-not)
188# ---------------------------------
189# Try linking with libpng using the given cflags and libs.
190AC_DEFUN([NETHACK__TRY_LIBPNG], [NETHACK__TRY_LINK([$1], [libpng], [$2], [$3],
191    [#include <png.h>], [png_structp ptr=
192      png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);],
193    [$4], [$5])])
194
195# NETHACK_CHECK_LIBPNG(stuff, action-if, action-if-not)
196# ---------------------------------
197# Check if libpng can be used
198AC_DEFUN([NETHACK_CHECK_LIBPNG],[
199    # Slash'EM may well work with earlier versions,
200    # but 1.2.4 is the earliest tested to date.
201    PKG_CHECK_MODULES(NETHACK__LIBPNG, libpng >= 1.2.4,
202      [nethack__succeeded=maybe], [nethack__succeeded=no])
203    # Slash'EM doesn't use zlib directly, but libpng does. This should
204    # all be handled in libpng.pc, but isn't in many versions of libpng.
205    if test $nethack__succeeded = maybe; then
206	NETHACK__ADD_FLAGS(nethack__libs, [$NETHACK__LIBPNG_LIBS], [-lz])
207	NETHACK__TRY_LIBPNG($1, $NETHACK__LIBPNG_CFLAGS, $nethack__libs,
208	  [nethack__succeeded=yes], [])
209    fi
210    # If the user has supplied flags, then believe them.
211    if test $nethack__succeeded = maybe -a \
212      \( -n "$ZLIB_CFLAGS" -o -n "$ZLIB_LIBS" \); then
213	NETHACK__ADD_FLAGS(nethack__cflags, [$NETHACK__LIBPNG_CFLAGS],
214	  [$ZLIB_CFLAGS])
215	NETHACK__ADD_FLAGS(nethack__libs, [$NETHACK__LIBPNG_LIBS],
216	  [$ZLIB_LIBS])
217	NETHACK__TRY_LIBPNG($1, [$nethack__cflags], [$nethack__libs],
218	  [nethack__succeeded=yes], [])
219    fi
220    # Otherwise, try pkg-config.
221    if test $nethack__succeeded = maybe; then
222	AC_MSG_CHECKING([for zlib])
223	if $PKG_CONFIG --exists zlib; then
224	    AC_MSG_RESULT([yes])
225	    NETHACK__ADD_FLAGS(nethack__cflags, [$NETHACK__LIBPNG_CFLAGS],
226	      [`$PKG_CONFIG --cflags zlib`])
227	    NETHACK__ADD_FLAGS(nethack__libs, [$NETHACK__LIBPNG_LIBS],
228	      [`$PKG_CONFIG --libs zlib`])
229	    NETHACK__TRY_LIBPNG($1, [$nethack__cflags], [$nethack__libs],
230	      [nethack__succeeded=yes], [])
231	else
232	    AC_MSG_RESULT([not found by pkg-config])
233	fi
234    fi
235    # Try the same prefix as libpng since zlib is
236    # likely to be installed in the same place.
237    if test $nethack__succeeded = maybe; then
238        nethack__libpng_prefix=`$PKG_CONFIG --variable=prefix libpng`
239	NETHACK__ADD_FLAG(nethack__cflags, [$NETHACK__LIBPNG_CFLAGS],
240	  [-I$nethack__libpng_prefix/include])
241	NETHACK__ADD_FLAG(nethack__libs, [$NETHACK__LIBPNG_LIBS],
242	  [-L$nethack__libpng_prefix/lib])
243	NETHACK__ADD_FLAG(nethack__libs, [$nethack__libs], [-lz])
244        NETHACK__TRY_LIBPNG($1, [$nethack__cflags], [$nethack__libs],
245          [nethack__succeeded=yes], [])
246    fi
247    if test $nethack__succeeded = yes; then
248	AC_MSG_CHECKING([$1_CFLAGS])
249	AC_MSG_RESULT([${$1_CFLAGS}])
250	AC_SUBST($1_CFLAGS)
251	AC_MSG_CHECKING([$1_LIBS])
252	AC_MSG_RESULT([${$1_LIBS}])
253	AC_SUBST($1_LIBS)
254	$2
255    m4_ifval([$3],[else $3],[])
256    fi
257])
258
259# NETHACK_COMMAND_IFELSE(command, action-if, action-if-not)
260# ---------------------------------
261# If command returns an exit status of 0 when executed, run shell commands
262# action-if. Otherwise, run shell commands action-if-not.
263AC_DEFUN([NETHACK_COMMAND_IFELSE], [
264    AS_IF([AC_TRY_COMMAND([$1])], [$2],
265    [echo "$as_me: program exited with status $ac_status" >&AS_MESSAGE_LOG_FD
266    m4_ifvaln([$3], [( exit $ac_status )
267	    $3])])])
268
269# NETHACK_COMPRESS_EXT
270# ---------------------------------
271# Check the extension used for compressed files by the compressor.
272AC_DEFUN([NETHACK_COMPRESS_EXT], [
273    AC_MSG_CHECKING([for suffix of compressed files])
274    rm -f conftest conftest.*
275    echo "Test" > conftest
276    nethack__extension=""
277    NETHACK_COMMAND_IFELSE([$COMPRESS $COMPRESS_OPTIONS conftest], [
278	for nethack__file in \
279	  `(ls conftest.bz2 conftest.gz conftest.Z conftest.*) 2>/dev/null`; do
280	    case $nethack__file in
281		*)  nethack__extension=`expr "$nethack__file" : '.*\(\..*\)'`
282		    break
283		    ;;
284	    esac
285	done
286	if test -z "$nethack__extension"; then
287	    AC_MSG_FAILURE([test failed: cannot find compressed output])
288	fi
289	rm -f conftest conftest.*
290	AC_DEFINE_UNQUOTED(COMPRESS_EXTENSION, ["$nethack__extension"],
291	  [Extension of compressed files])
292	AC_MSG_RESULT([$nethack__extension])
293	], [AC_MSG_FAILURE([test failed: cannot compress])])])
294
295# NETHACK_PROG_COMPRESS
296# ---------------------------------
297# Configure the compress program.
298AC_DEFUN([NETHACK_PROG_COMPRESS], [
299    AC_ARG_WITH([compression], [AS_HELP_STRING([--with-compression=PROGRAM],
300	    [Set the program to compress data files (default=auto)])],
301	    [], [with_compression=auto])
302    case $with_compression in
303	auto)
304	    AC_PATH_PROGS(COMPRESS, [bzip2 gzip compress], [:])
305	    ;;
306	no)
307	    COMPRESS=":"
308	    ;;
309	*)
310	    set dummy $with_compression
311	    AC_PATH_PROG(COMPRESS, [$[2]], [:])
312	    ;;
313    esac
314    AC_MSG_CHECKING([for compression support])
315    if test "$with_compression" = "auto" ; then
316	AC_DEFINE_UNQUOTED(COMPRESS, ["$COMPRESS"], [Compression program])
317	COMPRESS_OPTIONS=""
318	if test "$COMPRESS" = ":"; then
319	    AC_DEFINE(INTERNAL_COMP, [1], [Use UnNetHack's compression routines])
320	fi
321    elif test "$with_compression" = "no" ; then
322	COMPRESS=":"
323	AC_DEFINE(INTERNAL_COMP, [1], [Use UnNetHack's compression routines])
324    else
325	AC_DEFINE_UNQUOTED(COMPRESS, ["$COMPRESS"], [Compression program])
326	set dummy $with_compression
327	shift
328	shift
329	if test -n "$[1]"; then
330	    AC_DEFINE_UNQUOTED(COMPRESS_OPTIONS, ["$[@]"],
331	      [Options to pass to the compression program, if any])
332	fi
333	COMPRESS_OPTIONS="$[@]"
334    fi
335    if test "$COMPRESS" = ":"; then
336	AC_MSG_RESULT([internal])
337    else
338	AC_MSG_RESULT([$COMPRESS $COMPRESS_OPTIONS])
339	NETHACK_COMPRESS_EXT
340    fi])
341