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