Ensure that getopt() gets declared by <unistd.h>.
[pspp] / m4 / getopt.m4
1 # getopt.m4 serial 17
2 dnl Copyright (C) 2002-2006, 2008-2009 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 # The getopt module assume you want GNU getopt, with getopt_long etc,
8 # rather than vanilla POSIX getopt.  This means your code should
9 # always include <getopt.h> for the getopt prototypes.
10
11 AC_DEFUN([gl_GETOPT_SUBSTITUTE],
12 [
13   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
14   dnl Arrange for getopt.h to be created.
15   gl_GETOPT_SUBSTITUTE_HEADER
16   dnl Arrange for unistd.h to include getopt.h.
17   GNULIB_UNISTD_H_GETOPT=1
18   dnl Arrange to compile the getopt implementation.
19   AC_LIBOBJ([getopt])
20   AC_LIBOBJ([getopt1])
21   gl_PREREQ_GETOPT
22 ])
23
24 # emacs' configure.in uses this.
25 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
26 [
27   GETOPT_H=getopt.h
28   AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
29     [Define to rpl_ if the getopt replacement functions and variables
30      should be used.])
31   AC_SUBST([GETOPT_H])
32 ])
33
34 AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
35 [
36   gl_replace_getopt=
37   if test -z "$gl_replace_getopt"; then
38     AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes])
39   fi
40
41   if test -z "$gl_replace_getopt"; then
42     AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes])
43   fi
44
45   dnl BSD getopt_long uses an incompatible method to reset option processing,
46   dnl and (as of 2004-10-15) mishandles optional option-arguments.
47   if test -z "$gl_replace_getopt"; then
48     AC_CHECK_DECL([optreset], [gl_replace_getopt=yes], [],
49       [#include <getopt.h>])
50   fi
51
52   dnl Solaris 10 getopt doesn't handle `+' as a leading character in an
53   dnl option string (as of 2005-05-05).
54   if test -z "$gl_replace_getopt"; then
55     AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_gnu_getopt],
56       [AC_RUN_IFELSE(
57         [AC_LANG_PROGRAM([[#include <getopt.h>]],
58            [[
59              char *myargv[3];
60              myargv[0] = "conftest";
61              myargv[1] = "-+";
62              myargv[2] = 0;
63              return getopt (2, myargv, "+a") != '?';
64            ]])],
65         [gl_cv_func_gnu_getopt=yes],
66         [gl_cv_func_gnu_getopt=no],
67         [dnl cross compiling - pessimistically guess based on decls
68          dnl Solaris 10 getopt doesn't handle `+' as a leading character in an
69          dnl option string (as of 2005-05-05).
70          AC_CHECK_DECL([getopt_clip],
71            [gl_cv_func_gnu_getopt=no], [gl_cv_func_gnu_getopt=yes],
72            [#include <getopt.h>])])])
73     if test "$gl_cv_func_gnu_getopt" = "no"; then
74       gl_replace_getopt=yes
75     fi
76   fi
77 ])
78
79 # emacs' configure.in uses this.
80 AC_DEFUN([gl_GETOPT_IFELSE],
81 [
82   AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
83   AS_IF([test -n "$gl_replace_getopt"], [$1], [$2])
84 ])
85
86 # This is gnulib's entry-point.
87 AC_DEFUN([gl_GETOPT], [gl_GETOPT_IFELSE([gl_GETOPT_SUBSTITUTE])])
88
89 # Prerequisites of lib/getopt*.
90 # emacs' configure.in uses this.
91 AC_DEFUN([gl_PREREQ_GETOPT],
92 [
93   AC_CHECK_DECLS_ONCE([getenv])
94 ])