changeset 12070:f098f1865179

getopt: fix inclusion guards for cygwin 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>
author Eric Blake <ebb9@byu.net>
date Thu, 24 Sep 2009 10:57:14 -0600
parents 1a57a12a6e7f
children 8ae42b7a9fe9
files ChangeLog lib/getopt.in.h m4/getopt.m4 modules/getopt-posix
diffstat 4 files changed, 47 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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.
--- a/lib/getopt.in.h
+++ b/lib/getopt.in.h
@@ -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.
 
@@ -18,6 +18,17 @@
 
 #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
@@ -26,14 +37,17 @@
    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 @@
 #undef __need_getopt
 
 #endif /* getopt.h */
+#endif /* getopt.h */
--- a/m4/getopt.m4
+++ b/m4/getopt.m4
@@ -54,6 +54,15 @@
   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.
--- a/modules/getopt-posix
+++ b/modules/getopt-posix
@@ -12,6 +12,7 @@
 gettext-h
 unistd
 extensions
+include_next
 
 configure.ac:
 gl_FUNC_GETOPT_POSIX
@@ -24,7 +25,12 @@
 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