]> Savannah Git Hosting - gnulib.git/commitdiff
sys_un: Avoid compilation error in C++ <string.h> on glibc systems.
authorBruno Haible <bruno@clisp.org>
Fri, 26 Jul 2024 11:04:09 +0000 (13:04 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 26 Jul 2024 11:04:09 +0000 (13:04 +0200)
* m4/sys_un_h.m4 (gl_SYS_UN_H): Require AC_CANONICAL_HOST. Set
GL_GENERATE_SYS_UN_H to true on glibc systems. Invoke
gl_CHECK_NEXT_HEADERS. Initialize HAVE_SYS_UN_H.
* lib/sys_un.in.h: Use include_next and a split double-inclusion guard.
In C++ mode, include <stddef.h> and <string.h> first.
* modules/sys_un (Depends-on): Add include_next.
(Makefile.am): Substitute INCLUDE_NEXT, PRAGMA_SYSTEM_HEADER,
PRAGMA_COLUMNS, NEXT_SYS_UN_H, HAVE_SYS_UN_H. Arrange to remove 'sys'
directory at "make mostlyclean".

ChangeLog
lib/sys_un.in.h
m4/sys_un_h.m4
modules/sys_un

index 2f2a949b77c587024a177cdabdc6bce8e402b4c4..a9fa667880b509fd7e86a185457570293c9da116 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2024-07-26  Bruno Haible  <bruno@clisp.org>
+
+       sys_un: Avoid compilation error in C++ <string.h> on glibc systems.
+       * m4/sys_un_h.m4 (gl_SYS_UN_H): Require AC_CANONICAL_HOST. Set
+       GL_GENERATE_SYS_UN_H to true on glibc systems. Invoke
+       gl_CHECK_NEXT_HEADERS. Initialize HAVE_SYS_UN_H.
+       * lib/sys_un.in.h: Use include_next and a split double-inclusion guard.
+       In C++ mode, include <stddef.h> and <string.h> first.
+       * modules/sys_un (Depends-on): Add include_next.
+       (Makefile.am): Substitute INCLUDE_NEXT, PRAGMA_SYSTEM_HEADER,
+       PRAGMA_COLUMNS, NEXT_SYS_UN_H, HAVE_SYS_UN_H. Arrange to remove 'sys'
+       directory at "make mostlyclean".
+
 2024-07-26  Bruno Haible  <bruno@clisp.org>
 
        xstrtol, xstrtoll tests: Improve comment.
index 52993d65f51815b9f4c949f13d3994d2797e3a91..c137e7cef420427c3709f8f6465a9a2bfd19fc19 100644 (file)
 /* Written by Collin Funk.  */
 
 #ifndef _@GUARD_PREFIX@_SYS_UN_H
-#define _@GUARD_PREFIX@_SYS_UN_H 1
 
-/* Windows requires <winsock2.h> to be included before <afunix.h>.  */
-#if @HAVE_WINSOCK2_H@
-# include <winsock2.h>
+#if __GNUC__ >= 3
+@PRAGMA_SYSTEM_HEADER@
 #endif
-#if @HAVE_AFUNIX_H@
-# include <afunix.h>
+@PRAGMA_COLUMNS@
+
+#if @HAVE_SYS_UN_H@
+
+/* On glibc, in C++ mode with clang, <stddef.h> and <string.h> need to be
+   included before <sys/un.h>, because otherwise <sys/un.h> includes them inside
+   an 'extern "C" { ... }' block, which leads to compilation errors in gnulib's
+   <string.h> override.  */
+# if defined __cplusplus
+#  include <stddef.h>
+#  include <string.h>
+# endif
+
+/* The include_next requires a split double-inclusion guard.  */
+# @INCLUDE_NEXT@ @NEXT_SYS_UN_H@
+
 #endif
 
+#ifndef _@GUARD_PREFIX@_SYS_UN_H
+#define _@GUARD_PREFIX@_SYS_UN_H 1
+
+#if !@HAVE_SYS_UN_H@
+
+/* A platform that lacks <sys/un.h>.  */
+
+/* Windows requires <winsock2.h> to be included before <afunix.h>.  */
+# if @HAVE_WINSOCK2_H@
+#  include <winsock2.h>
+# endif
+# if @HAVE_AFUNIX_H@
+#  include <afunix.h>
+# endif
+
 /* If a platform does not support AF_UNIX sockets 'struct sockaddr_un' will
    not be defined.  You may use HAVE_UNIXSOCKET after including <config.h>.  */
 
 #endif
+
+#endif /* _@GUARD_PREFIX@_SYS_UN_H */
+#endif /* _@GUARD_PREFIX@_SYS_UN_H */
index ed19b70cb5319a3238ee6e308f9a1ab7cb9588ef..959cd86c0a137ad451f0186aa40b2b770b81833a 100644 (file)
@@ -1,5 +1,5 @@
 # sys_un_h.m4
-# serial 1
+# serial 2
 dnl Copyright 2024 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -7,26 +7,43 @@ dnl with or without modifications, as long as this notice is preserved.
 
 AC_DEFUN_ONCE([gl_SYS_UN_H],
 [
+  AC_REQUIRE([AC_CANONICAL_HOST])
+
   dnl Check if UNIX domain sockets are supported.
   AC_REQUIRE([gl_SOCKET_FAMILY_UNIX])
 
   GL_GENERATE_SYS_UN_H=false
-
   if test $gl_cv_socket_unix = yes; then
-
     dnl Check if using a Windows version that supports AF_UNIX.
     dnl See <https://devblogs.microsoft.com/commandline/af_unix-comes-to-windows/>.
     if test "$ac_cv_header_winsock2_h" = yes \
        && test "$ac_cv_header_afunix_h" = yes; then
       GL_GENERATE_SYS_UN_H=true
     fi
+    case "$host_os" in
+      dnl On glibc systems, we need to include <string.h> before <sys/un.h>,
+      dnl at least in C++ mode with clang.
+      *-gnu* | gnu*) GL_GENERATE_SYS_UN_H=true ;;
+    esac
   fi
 
-  dnl Checked in gl_SOCKET_FAMILY_UNIX.
-  if test "$ac_cv_header_afunix_h" = yes; then
-    HAVE_AFUNIX_H=1
-  else
-    HAVE_AFUNIX_H=0
+  if $GL_GENERATE_SYS_UN_H; then
+    AC_CHECK_HEADERS([sys/un.h])
+    gl_CHECK_NEXT_HEADERS([sys/un.h])
+
+    if test $ac_cv_header_sys_un_h = yes; then
+      HAVE_SYS_UN_H=1
+    else
+      HAVE_SYS_UN_H=0
+    fi
+    AC_SUBST([HAVE_SYS_UN_H])
+
+    dnl Checked in gl_SOCKET_FAMILY_UNIX.
+    if test "$ac_cv_header_afunix_h" = yes; then
+      HAVE_AFUNIX_H=1
+    else
+      HAVE_AFUNIX_H=0
+    fi
+    AC_SUBST([HAVE_AFUNIX_H])
   fi
-  AC_SUBST([HAVE_AFUNIX_H])
 ])
index af72a80aab61a0cf02dfd100393c5de941ab1af8..21505946a87434973e1355bd5d1ef468524c344b 100644 (file)
@@ -7,6 +7,7 @@ m4/sys_un_h.m4
 
 Depends-on:
 gen-header
+include_next
 sys_socket
 
 configure.ac:
@@ -24,15 +25,21 @@ sys/un.h: sys_un.in.h $(top_builddir)/config.status
 @NMD@  $(AM_V_GEN)$(MKDIR_P) '%reldir%'
        $(gl_V_at)$(SED_HEADER_STDOUT) \
              -e 's|@''GUARD_PREFIX''@|${gl_include_guard_prefix}|g' \
+             -e 's|@''INCLUDE_NEXT''@|$(INCLUDE_NEXT)|g' \
+             -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \
+             -e 's|@''PRAGMA_COLUMNS''@|@PRAGMA_COLUMNS@|g' \
+             -e 's|@''NEXT_SYS_UN_H''@|$(NEXT_SYS_UN_H)|g' \
+             -e 's|@''HAVE_SYS_UN_H''@|$(HAVE_SYS_UN_H)|g' \
              -e 's|@''HAVE_WINSOCK2_H''@|$(HAVE_WINSOCK2_H)|g' \
              -e 's|@''HAVE_AFUNIX_H''@|$(HAVE_AFUNIX_H)|g' \
-       $(srcdir)/sys_un.in.h > $@-t
+             $(srcdir)/sys_un.in.h > $@-t
        $(AM_V_at)mv $@-t $@
 else
 sys/un.h: $(top_builddir)/config.status
        rm -f $@
 endif
 MOSTLYCLEANFILES += sys/un.h sys/un.h-t
+MOSTLYCLEANDIRS += sys
 
 Include:
 <sys/un.h>