9ad305f41d5515201cda7025aec7aa2039b75c59
[gnulib.git] / m4 / getopt.m4
1 # getopt.m4 serial 18
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 # This is gnulib's entry-point.
12 AC_DEFUN([gl_GETOPT],
13 [
14   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
15   gl_GETOPT_IFELSE([
16     dnl Arrange for getopt.h to be created.
17     gl_GETOPT_SUBSTITUTE_HEADER
18     dnl Arrange for unistd.h to include getopt.h.
19     GNULIB_UNISTD_H_GETOPT=1
20     dnl Arrange to compile the getopt implementation.
21     AC_LIBOBJ([getopt])
22     AC_LIBOBJ([getopt1])
23     gl_PREREQ_GETOPT
24   ])
25 ])
26
27 # emacs' configure.in uses this.
28 AC_DEFUN([gl_GETOPT_IFELSE],
29 [
30   AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
31   AS_IF([test -n "$gl_replace_getopt"], [$1], [$2])
32 ])
33
34 # Determine whether to replace the entire getopt facility.
35 AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
36 [
37   gl_replace_getopt=
38   if test -z "$gl_replace_getopt"; then
39     AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes])
40   fi
41
42   if test -z "$gl_replace_getopt"; then
43     AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes])
44   fi
45
46   dnl BSD getopt_long uses an incompatible method to reset option processing,
47   dnl and (as of 2004-10-15) mishandles optional option-arguments.
48   if test -z "$gl_replace_getopt"; then
49     AC_CHECK_DECL([optreset], [gl_replace_getopt=yes], [],
50       [#include <getopt.h>])
51   fi
52
53   dnl Solaris 10 getopt doesn't handle `+' as a leading character in an
54   dnl option string (as of 2005-05-05).
55   if test -z "$gl_replace_getopt"; then
56     AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_gnu_getopt],
57       [AC_RUN_IFELSE(
58         [AC_LANG_PROGRAM([[#include <getopt.h>]],
59            [[
60              char *myargv[3];
61              myargv[0] = "conftest";
62              myargv[1] = "-+";
63              myargv[2] = 0;
64              return getopt (2, myargv, "+a") != '?';
65            ]])],
66         [gl_cv_func_gnu_getopt=yes],
67         [gl_cv_func_gnu_getopt=no],
68         [dnl cross compiling - pessimistically guess based on decls
69          dnl Solaris 10 getopt doesn't handle `+' as a leading character in an
70          dnl option string (as of 2005-05-05).
71          AC_CHECK_DECL([getopt_clip],
72            [gl_cv_func_gnu_getopt=no], [gl_cv_func_gnu_getopt=yes],
73            [#include <getopt.h>])])])
74     if test "$gl_cv_func_gnu_getopt" = "no"; then
75       gl_replace_getopt=yes
76     fi
77   fi
78 ])
79
80 # emacs' configure.in uses this.
81 AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
82 [
83   GETOPT_H=getopt.h
84   AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
85     [Define to rpl_ if the getopt replacement functions and variables
86      should be used.])
87   AC_SUBST([GETOPT_H])
88 ])
89
90 # Prerequisites of lib/getopt*.
91 # emacs' configure.in uses this.
92 AC_DEFUN([gl_PREREQ_GETOPT],
93 [
94   AC_CHECK_DECLS_ONCE([getenv])
95 ])