getopt: fix inclusion guards for cygwin
authorEric Blake <ebb9@byu.net>
Thu, 24 Sep 2009 16:57:14 +0000 (10:57 -0600)
committerEric Blake <ebb9@byu.net>
Fri, 25 Sep 2009 01:27:18 +0000 (19:27 -0600)
cygwin <unistd.h> includes <getopt.h>, but gnulib <getopt.h> includes
<unistd.h>, so system declarations were happening after gnulib
replacements, leading to multiply-defined rpl_readlink error on
cygwin 1.5.  The solution is to ensure that the system <unistd.h>
is complete prior to any of gnulib's contents, by picking up
system <getopt.h> in preference to a cycle to <unistd.h>.

* modules/getopt-posix (Depends-on): Add include-next.
(Makefile.am): Substitute more items in replacement header.
* m4/getopt.m4 (gl_GETOPT_CHECK_HEADERS): Also check for native
<getopt.h>.
* lib/getopt.in.h (includes): Use split inclusion guard, and
prefer <getopt.h> over include <unistd.h> when one is present.
(option): Also override name of 'struct option'.

Signed-off-by: Eric Blake <ebb9@byu.net>
ChangeLog
lib/getopt.in.h
m4/getopt.m4
modules/getopt-posix

index 697d2c1..35f84c4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2009-09-24  Eric Blake  <ebb9@byu.net>
 
+       getopt: fix inclusion guards for cygwin
+       * modules/getopt-posix (Depends-on): Add include-next.
+       (Makefile.am): Substitute more items in replacement header.
+       * m4/getopt.m4 (gl_GETOPT_CHECK_HEADERS): Also check for native
+       <getopt.h>.
+       * lib/getopt.in.h (includes): Use split inclusion guard, and
+       prefer <getopt.h> over include <unistd.h> when one is present.
+       (option): Also override name of 'struct option'.
+
        same-inode: revert prior change; it is not yet ready
        * NEWS: Undo mention of this change.
        * lib/same-inode.h (same-inode.h): Undo tri-state change.
index d2d3e6e..e23259c 100644 (file)
@@ -1,5 +1,5 @@
 /* Declarations for getopt.
-   Copyright (C) 1989-1994,1996-1999,2001,2003,2004,2005,2006,2007
+   Copyright (C) 1989-1994,1996-1999,2001,2003,2004,2005,2006,2007,2009
    Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
 
 #ifndef _GETOPT_H
 
+#if __GNUC__ >= 3
+@PRAGMA_SYSTEM_HEADER@
+#endif
+
+/* The include_next requires a split double-inclusion guard.  */
+#if @HAVE_GETOPT_H@
+# @INCLUDE_NEXT@ @NEXT_GETOPT_H@
+#endif
+
+#ifndef _GETOPT_H
+
 #ifndef __need_getopt
 # define _GETOPT_H 1
 #endif
    identifier that prefixes the external functions and variables
    defined in this header.  When this happens, include the
    headers that might declare getopt so that they will not cause
-   confusion if included after this file.  Then systematically rename
+   confusion if included after this file (if the system had <getopt.h>,
+   we have already included it).  Then systematically rename
    identifiers so that they do not collide with the system functions
    and variables.  Renaming avoids problems with some compilers and
    linkers.  */
 #if defined __GETOPT_PREFIX && !defined __need_getopt
-# include <stdlib.h>
-# include <stdio.h>
-# include <unistd.h>
+# if !@HAVE_GETOPT_H@
+#  include <stdlib.h>
+#  include <stdio.h>
+#  include <unistd.h>
+# endif
 # undef __need_getopt
 # undef getopt
 # undef getopt_long
@@ -42,6 +56,7 @@
 # undef opterr
 # undef optind
 # undef optopt
+# undef option
 # define __GETOPT_CONCAT(x, y) x ## y
 # define __GETOPT_XCONCAT(x, y) __GETOPT_CONCAT (x, y)
 # define __GETOPT_ID(y) __GETOPT_XCONCAT (__GETOPT_PREFIX, y)
@@ -52,6 +67,7 @@
 # define opterr __GETOPT_ID (opterr)
 # define optind __GETOPT_ID (optind)
 # define optopt __GETOPT_ID (optopt)
+# define option __GETOPT_ID (option)
 #endif
 
 /* Standalone applications get correct prototypes for getopt_long and
@@ -223,3 +239,4 @@ extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv,
 #undef __need_getopt
 
 #endif /* getopt.h */
+#endif /* getopt.h */
index 7443dfe..53a1a7e 100644 (file)
@@ -54,6 +54,15 @@ AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
   dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
 
+  gl_CHECK_NEXT_HEADERS([getopt.h])
+  AC_CHECK_HEADERS_ONCE([getopt.h])
+  if test $ac_cv_header_getopt_h = yes; then
+    HAVE_GETOPT_H=1
+  else
+    HAVE_GETOPT_H=0
+  fi
+  AC_SUBST([HAVE_GETOPT_H])
+
   gl_replace_getopt=
 
   dnl Test whether <getopt.h> is available.
index b9d2658..94932fc 100644 (file)
@@ -12,6 +12,7 @@ Depends-on:
 gettext-h
 unistd
 extensions
+include_next
 
 configure.ac:
 gl_FUNC_GETOPT_POSIX
@@ -24,7 +25,12 @@ BUILT_SOURCES += $(GETOPT_H)
 getopt.h: getopt.in.h
        $(AM_V_GEN)rm -f $@-t $@ && \
        { echo '/* DO NOT EDIT! GENERATED AUTOMATICALLY! */'; \
-         cat $(srcdir)/getopt.in.h; \
+         sed -e 's|@''HAVE_GETOPT_H''@|$(HAVE_GETOPT_H)|g' \
+             -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
+             -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \
+             -e 's|@''NEXT_GETOPT_H''@|$(NEXT_GETOPT_H)|g' \
+             -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \
+             < $(srcdir)/getopt.in.h; \
        } > $@-t && \
        mv -f $@-t $@
 MOSTLYCLEANFILES += getopt.h getopt.h-t