From 78b79920afbdc87284c49fc27358d2854ae8fe9c Mon Sep 17 00:00:00 2001
From: David Klempner <klempner@imsanet.org>
Date: Wed, 4 Feb 2015 10:18:59 -0800
Subject: [PATCH] Fix up feature test macros

Move all feature test macros to the start of the file and check that
they aren't already defined or defined to a lower value than the file
needs.

Projects should be allowed to put these in CFLAGS and we shouldn't break
when they do.
---
 src/core/iomgr/resolve_address.c    | 2 ++
 src/core/iomgr/socket_utils_linux.c | 2 ++
 src/core/iomgr/socket_utils_posix.c | 1 -
 src/core/iomgr/tcp_server_posix.c   | 6 +++++-
 src/core/support/log_linux.c        | 6 ++++++
 src/core/support/log_posix.c        | 7 ++++++-
 src/core/support/string_posix.c     | 3 ++-
 test/core/echo/echo_test.c          | 3 +++
 test/core/fling/fling_stream_test.c | 3 +++
 test/core/fling/fling_test.c        | 3 +++
 10 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/src/core/iomgr/resolve_address.c b/src/core/iomgr/resolve_address.c
index 01681168ce..575f884d91 100644
--- a/src/core/iomgr/resolve_address.c
+++ b/src/core/iomgr/resolve_address.c
@@ -31,7 +31,9 @@
  *
  */
 
+#ifndef _POSIX_SOURCE
 #define _POSIX_SOURCE
+#endif
 
 #include "src/core/iomgr/sockaddr.h"
 #include "src/core/iomgr/resolve_address.h"
diff --git a/src/core/iomgr/socket_utils_linux.c b/src/core/iomgr/socket_utils_linux.c
index f971cb33bc..7ef58940c2 100644
--- a/src/core/iomgr/socket_utils_linux.c
+++ b/src/core/iomgr/socket_utils_linux.c
@@ -31,7 +31,9 @@
  *
  */
 
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE
+#endif
 #include <grpc/support/port_platform.h>
 
 #ifdef GPR_LINUX
diff --git a/src/core/iomgr/socket_utils_posix.c b/src/core/iomgr/socket_utils_posix.c
index 06c5033d45..9184b2a47c 100644
--- a/src/core/iomgr/socket_utils_posix.c
+++ b/src/core/iomgr/socket_utils_posix.c
@@ -35,7 +35,6 @@
 
 #ifdef GPR_POSIX_SOCKETUTILS
 
-#define _BSD_SOURCE
 #include "src/core/iomgr/socket_utils_posix.h"
 
 #include <fcntl.h>
diff --git a/src/core/iomgr/tcp_server_posix.c b/src/core/iomgr/tcp_server_posix.c
index d169d23271..091f0aab1a 100644
--- a/src/core/iomgr/tcp_server_posix.c
+++ b/src/core/iomgr/tcp_server_posix.c
@@ -31,11 +31,15 @@
  *
  */
 
+/* FIXME: "posix" files shouldn't be depending on _GNU_SOURCE */
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif
+
 #include <grpc/support/port_platform.h>
 
 #ifdef GPR_POSIX_SOCKET
 
-#define _GNU_SOURCE
 #include "src/core/iomgr/tcp_server.h"
 
 #include <limits.h>
diff --git a/src/core/support/log_linux.c b/src/core/support/log_linux.c
index a0307e1a9a..a64faa98bd 100644
--- a/src/core/support/log_linux.c
+++ b/src/core/support/log_linux.c
@@ -31,8 +31,14 @@
  *
  */
 
+#ifndef _POSIX_SOURCE
 #define _POSIX_SOURCE
+#endif
+
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE
+#endif
+
 #include <grpc/support/port_platform.h>
 
 #ifdef GPR_LINUX
diff --git a/src/core/support/log_posix.c b/src/core/support/log_posix.c
index ab2d2e5a74..05f45de130 100644
--- a/src/core/support/log_posix.c
+++ b/src/core/support/log_posix.c
@@ -31,11 +31,16 @@
  *
  */
 
-#ifndef _POSIX_C_SOURCE
+#if !defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 200112L
+#undef _POSIX_C_SOURCE
 #define _POSIX_C_SOURCE 200112L
 #endif
 
+/* FIXME: "posix" files probably shouldn't depend on _GNU_SOURCE */
+#ifndef _GNU_SOURCE
 #define _GNU_SOURCE
+#endif
+
 #include <grpc/support/port_platform.h>
 
 #if defined(GPR_POSIX_LOG)
diff --git a/src/core/support/string_posix.c b/src/core/support/string_posix.c
index 57832810ad..a6bb8058e6 100644
--- a/src/core/support/string_posix.c
+++ b/src/core/support/string_posix.c
@@ -33,7 +33,8 @@
 
 /* Posix code for gpr snprintf support. */
 
-#ifndef _POSIX_C_SOURCE
+#if !defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 200112L
+#undef _POSIX_C_SOURCE
 #define _POSIX_C_SOURCE 200112L
 #endif
 
diff --git a/test/core/echo/echo_test.c b/test/core/echo/echo_test.c
index 83b83ab7ff..5450dfbef5 100644
--- a/test/core/echo/echo_test.c
+++ b/test/core/echo/echo_test.c
@@ -31,7 +31,10 @@
  *
  */
 
+#ifndef _POSIX_SOURCE
 #define _POSIX_SOURCE
+#endif
+
 #include <unistd.h>
 #include <assert.h>
 #include <stdio.h>
diff --git a/test/core/fling/fling_stream_test.c b/test/core/fling/fling_stream_test.c
index 7f52fb1bad..1db2f1a791 100644
--- a/test/core/fling/fling_stream_test.c
+++ b/test/core/fling/fling_stream_test.c
@@ -31,7 +31,10 @@
  *
  */
 
+#ifndef _POSIX_SOURCE
 #define _POSIX_SOURCE
+#endif
+
 #include <unistd.h>
 #include <assert.h>
 #include <stdio.h>
diff --git a/test/core/fling/fling_test.c b/test/core/fling/fling_test.c
index b2272f20c8..4f41a21aaa 100644
--- a/test/core/fling/fling_test.c
+++ b/test/core/fling/fling_test.c
@@ -31,7 +31,10 @@
  *
  */
 
+#ifndef _POSIX_SOURCE
 #define _POSIX_SOURCE
+#endif
+
 #include <unistd.h>
 #include <assert.h>
 #include <stdio.h>
-- 
GitLab