Author: Aurelien Jarno <aurel32@debian.org>
Author: Sergei Golovan <sgolovan@debian.org>
Author: Pino Toscano <pino@debian.org>

(1) Defines GNU macros not only for Linux but also for any system
    with 'gnu' substring in OS name. Fixes FTBFS on GNU/kFreeBSD and GNU/Hurd.

(2) Undefines BSD4_4 for os_mon application for GNU/Hurd;

(3) Undefines AF_LINK for GNU/Hurd;

(4) Switches some PATH_MAX occurrences to MAXPATHLEN;

(5) Adds a workaround for 'erlc -M | sed' being stuck for GNU/Hurd.

--- a/erts/configure.ac
+++ b/erts/configure.ac
@@ -526,7 +526,7 @@
 
 dnl NOTE: CPPFLAGS will be included in CFLAGS at the end
 case $host_os in
-    linux*) CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE";; 
+    linux*|*gnu*) CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE";; 
     aix*|os400*)
         # * _ALL_SOURCE: Required to get the winsize structure for TIOCSWINSZ.
         # * _LINUX_SOURCE_COMPAT: Not required, but makes some libc functions
@@ -1157,7 +1157,7 @@
 fi
 
 case $host_os in
-    linux*)
+    linux*|*gnu*)
 	AC_MSG_CHECKING([whether dlopen() needs to be called before first call to dlerror()])
 	if test "x$ETHR_THR_LIB_BASE_TYPE" != "xposix_nptl"; then
 	    AC_DEFINE(ERTS_NEED_DLOPEN_BEFORE_DLERROR,[1],
--- a/lib/os_mon/c_src/memsup.c
+++ b/lib/os_mon/c_src/memsup.c
@@ -99,6 +99,11 @@
 #include <time.h>
 #include <errno.h>
 
+#ifdef __GNU__
+#warning "Undefined BSD4_4 for GNU/Hurd"
+#undef BSD4_4
+#endif
+
 #ifdef BSD4_4
 #include <sys/types.h>
 #include <sys/sysctl.h>
--- a/erts/emulator/drivers/common/inet_drv.c
+++ b/erts/emulator/drivers/common/inet_drv.c
@@ -5308,6 +5308,11 @@
     *x = addr;
     return 0;
 }
+#ifdef __GNU__
+#warning "Undefined AF_LINK for GNU/Hurd"
+#undef AF_LINK
+#endif
+
 
 #endif
 
--- a/erts/etc/common/erlexec.c
+++ b/erts/etc/common/erlexec.c
@@ -1699,7 +1699,7 @@
     if (!bindir) {
 	/* Determine bindir from absolute path to executable */
 	char *p;
-	char buffer[PATH_MAX];
+	char buffer[MAXPATHLEN];
 	strncpy(buffer, argv[0], sizeof(buffer));
 	buffer[sizeof(buffer)-1] = '\0';
 	
@@ -1713,7 +1713,7 @@
     if (!rootdir) {
 	/* Determine rootdir from absolute path to bindir */
 	char *p;
-	char buffer[PATH_MAX];
+	char buffer[MAXPATHLEN];
 	strncpy(buffer, bindir, sizeof(buffer));
 	buffer[sizeof(buffer)-1] = '\0';
 	
@@ -1947,7 +1947,7 @@
         char cwd[MAX_PATH];
         if (_getcwd(cwd, sizeof(cwd)) == NULL) {
 #else
-        char cwd[PATH_MAX];
+        char cwd[MAXPATHLEN];
         if (getcwd(cwd, sizeof(cwd)) == NULL) {
 #endif
             cwd[0] = '\0';
--- a/erts/etc/common/escript.c
+++ b/erts/etc/common/escript.c
@@ -47,7 +47,7 @@
 #  define IS_DIRSEP(c) ((c) == '/')
 #  define DIRSEPSTR "/"
 #  define PATHSEPSTR ":"
-#  define PMAX PATH_MAX
+#  define PMAX MAXPATHLEN
 #  define ERL_NAME "erl"
 #endif
 
--- a/erts/etc/unix/dyn_erl.c
+++ b/erts/etc/unix/dyn_erl.c
@@ -144,8 +144,8 @@
 static char *
 find_prog(char *origpath)
 {
-    char relpath[PATH_MAX];
-    char abspath[PATH_MAX];
+    char relpath[MAXPATHLEN];
+    char abspath[MAXPATHLEN];
 
     strcpy(relpath, origpath);
 
@@ -156,7 +156,7 @@
         envpath = get_env("PATH");
         if (envpath) {
             /* Try to find the executable in the path */
-            char dir[PATH_MAX];
+            char dir[MAXPATHLEN];
             char *beg = envpath;
             char *end;
             int sz;
@@ -275,7 +275,7 @@
     /* List install dir and look for latest erts-vsn */
     DIR *dp;                    /* Pointer to directory structure. */
     struct dirent* dirp;        /* Pointer to directory entry.     */
-    char latest_vsn[PATH_MAX];  /* Latest erts-vsn directory name. */
+    char latest_vsn[MAXPATHLEN];/* Latest erts-vsn directory name. */
 
     dp = opendir(erl_top);
     if (dp == NULL) {
@@ -315,7 +315,7 @@
 
     char *p;
     char *p2;
-    char buffer[PATH_MAX];
+    char buffer[MAXPATHLEN];
 
     strcpy(buffer, erlpath);
 
@@ -358,9 +358,9 @@
     char *p;
     char *abspath;
     char *bindir;            /* Location of executables. */
-    char rootdir[PATH_MAX];  /* Root location of Erlang installation. */
-    char progname[PATH_MAX]; /* Name of this program. */
-    char erlexec[PATH_MAX];  /* Path to erlexec */
+    char rootdir[MAXPATHLEN];/* Root location of Erlang installation. */
+    char progname[MAXPATHLEN];/* Name of this program. */
+    char erlexec[MAXPATHLEN];/* Path to erlexec */
 
     if (argc == 2 && strcmp(argv[1], "--realpath") == 0) {
         abspath = find_prog(argv[0]);
--- a/lib/ssl/src/Makefile
+++ b/lib/ssl/src/Makefile
@@ -188,7 +188,8 @@
 
 $(DEP_FILE): $(ERL_FILES)
 	@echo SED $(TARGET) $(ERL_TOP_NATIVE)
-	$(gen_verbose)erlc -M $(ERL_FILES) \
+	$(gen_verbose)erlc -M $(ERL_FILES) > $(DEP_FILE).tmp
+	cat $(DEP_FILE).tmp \
 	| perl -pe "s@ [a-zA-Z]?$(ERL_TOP_NATIVE)/(?:bootstrap/)?lib/([^/]+)@ ../../\1@g" 2> /dev/null \
 	| sed "s/\.$(EMULATOR)/\.$$\(EMULATOR\)/" \
 	| sed 's@^dtls_@$$(EBIN)/dtls_@' \
--- a/make/autoconf/otp.m4
+++ b/make/autoconf/otp.m4
@@ -1818,6 +1818,8 @@
 		    ETHR_DEFS="$ETHR_DEFS -DETHR_UNUSABLE_SIGALTSTACK"
 		fi
 		;;
+	    *gnu*)
+		ETHR_DEFS="$ETHR_DEFS -D_GNU_SOURCE" ;;
 	    *) ;;
 	esac
 
@@ -3079,7 +3081,7 @@
 DED_CFLAGS=
 DED_OSTYPE=unix
 case $host_os in
-     linux*)
+     linux*|*gnu*)
 	DED_CFLAGS="-D_GNU_SOURCE" ;;
      win32)
 	DED_CFLAGS="-D_WIN32_WINNT=0x0600 -DWINVER=0x0600"
@@ -3217,7 +3219,7 @@
 		DED_LD="$CC"
 		DED_LD_FLAG_RUNTIME_LIBRARY_PATH="$CFLAG_RUNTIME_LIBRARY_PATH"
 	;;
-	linux*)
+	linux*|*gnu*)
 		DED_LD="$CC"
 		DED_LD_FLAG_RUNTIME_LIBRARY_PATH="$CFLAG_RUNTIME_LIBRARY_PATH"
 		DED_LDFLAGS="-shared -Wl,-Bsymbolic"
