[nginx] Win32: MinGW GCC compatibility.

Maxim Dounin mdounin at mdounin.ru
Wed Sep 4 17:15:38 UTC 2013


details:   http://hg.nginx.org/nginx/rev/3d2d3e1cf427
branches:  
changeset: 5360:3d2d3e1cf427
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Wed Sep 04 20:48:28 2013 +0400
description:
Win32: MinGW GCC compatibility.

Several warnings silenced, notably (ngx_socket_t) -1 is now checked
on socket operations instead of -1, as ngx_socket_t is unsigned on win32
and gcc complains on comparison.

With this patch, it's now possible to compile nginx using mingw gcc,
with options we normally compile on win32.

diffstat:

 auto/lib/openssl/conf                       |   4 ++++
 auto/lib/pcre/conf                          |   5 +++++
 auto/lib/pcre/make                          |  19 +++++++++----------
 auto/lib/zlib/make                          |  23 ++++++++++++++++++++++-
 auto/os/win32                               |  13 ++++++++++++-
 src/core/ngx_connection.c                   |   6 +++---
 src/core/ngx_cycle.c                        |   6 +++---
 src/core/ngx_resolver.c                     |   2 +-
 src/event/modules/ngx_iocp_module.c         |   2 +-
 src/event/modules/ngx_win32_select_module.c |   4 ++--
 src/event/ngx_event_accept.c                |   2 +-
 src/event/ngx_event_acceptex.c              |   2 +-
 src/event/ngx_event_connect.c               |   2 +-
 src/event/ngx_event_pipe.c                  |   6 ++++--
 src/os/win32/ngx_atomic.h                   |   3 ++-
 src/os/win32/ngx_process_cycle.c            |   4 ++--
 src/os/win32/ngx_win32_config.h             |   7 +++++++
 src/os/win32/ngx_win32_init.c               |   2 +-
 src/os/win32/ngx_wsarecv.c                  |   4 ++--
 19 files changed, 83 insertions(+), 33 deletions(-)

diffs (truncated from 394 to 300 lines):

diff --git a/auto/lib/openssl/conf b/auto/lib/openssl/conf
--- a/auto/lib/openssl/conf
+++ b/auto/lib/openssl/conf
@@ -33,6 +33,10 @@ if [ $OPENSSL != NONE ]; then
             CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libssl.a"
             CORE_LIBS="$CORE_LIBS $OPENSSL/.openssl/lib/libcrypto.a"
             CORE_LIBS="$CORE_LIBS $NGX_LIBDL"
+
+            if [ "$NGX_PLATFORM" = win32 ]; then
+                CORE_LIBS="$CORE_LIBS -lgdi32 -lcrypt32 -lws2_32"
+            fi
         ;;
     esac
 
diff --git a/auto/lib/pcre/conf b/auto/lib/pcre/conf
--- a/auto/lib/pcre/conf
+++ b/auto/lib/pcre/conf
@@ -73,6 +73,11 @@ if [ $PCRE != NONE ]; then
 
         *)
             have=NGX_PCRE . auto/have
+
+            if [ "$NGX_PLATFORM" = win32 ]; then
+                have=PCRE_STATIC . auto/have
+            fi
+
             CORE_DEPS="$CORE_DEPS $PCRE/pcre.h"
             LINK_DEPS="$LINK_DEPS $PCRE/.libs/libpcre.a"
             CORE_LIBS="$CORE_LIBS $PCRE/.libs/libpcre.a"
diff --git a/auto/lib/pcre/make b/auto/lib/pcre/make
--- a/auto/lib/pcre/make
+++ b/auto/lib/pcre/make
@@ -23,14 +23,16 @@ case "$NGX_CC_NAME" in
         ngx_pcre=`echo \-DPCRE=\"$PCRE\" | sed -e "s/\//$ngx_regex_dirsep/g"`
     ;;
 
+    *)
+        ngx_makefile=
+    ;;
+
 esac
 
 
-case "$NGX_PLATFORM" in
+if [ -n "$ngx_makefile" ]; then
 
-    win32)
-
-        cat << END                                            >> $NGX_MAKEFILE
+    cat << END                                                >> $NGX_MAKEFILE
 
 `echo "$PCRE/pcre.lib:	$PCRE/pcre.h $NGX_MAKEFILE"			\
 	| sed -e "s/\//$ngx_regex_dirsep/g"`
@@ -41,10 +43,9 @@ case "$NGX_PLATFORM" in
 
 END
 
-    ;;
+else
 
-    *)
-        cat << END                                            >> $NGX_MAKEFILE
+    cat << END                                                >> $NGX_MAKEFILE
 
 $PCRE/pcre.h:	$PCRE/Makefile
 
@@ -60,6 +61,4 @@ END
 
 END
 
-    ;;
-
-esac
+fi
diff --git a/auto/lib/zlib/make b/auto/lib/zlib/make
--- a/auto/lib/zlib/make
+++ b/auto/lib/zlib/make
@@ -24,6 +24,10 @@ case "$NGX_CC_NAME" in
         ngx_zlib=`echo \-DZLIB=\"$ZLIB\" | sed -e "s/\//$ngx_regex_dirsep/g"`
     ;;
 
+    *)
+        ngx_makefile=
+    ;;
+
 esac
 
 
@@ -33,13 +37,30 @@ done=NO
 case "$NGX_PLATFORM" in
 
     win32)
-        cat << END                                            >> $NGX_MAKEFILE
+
+        if [ -n "$ngx_makefile" ]; then
+            cat << END                                        >> $NGX_MAKEFILE
 
 `echo "$ZLIB/zlib.lib:	$NGX_MAKEFILE" | sed -e "s/\//$ngx_regex_dirsep/g"`
 	\$(MAKE) -f auto/lib/zlib/$ngx_makefile $ngx_opt $ngx_zlib
 
 END
 
+        else
+
+            cat << END                                        >> $NGX_MAKEFILE
+
+$ZLIB/libz.a:	$NGX_MAKEFILE
+	cd $ZLIB \\
+	&& \$(MAKE) distclean \\
+	&& \$(MAKE) -f win32/Makefile.gcc \\
+		CFLAGS="$ZLIB_OPT" CC="\$(CC)" \\
+		libz.a
+
+END
+
+        fi
+
         done=YES
     ;;
 
diff --git a/auto/os/win32 b/auto/os/win32
--- a/auto/os/win32
+++ b/auto/os/win32
@@ -9,10 +9,21 @@ CORE_INCS="$WIN32_INCS"
 CORE_DEPS="$WIN32_DEPS"
 CORE_SRCS="$WIN32_SRCS $IOCP_SRCS"
 OS_CONFIG="$WIN32_CONFIG"
-CORE_LIBS="$CORE_LIBS advapi32.lib ws2_32.lib"
 NGX_ICONS="$NGX_WIN32_ICONS"
 SELECT_SRCS=$WIN32_SELECT_SRCS
 
+case "$NGX_CC_NAME" in
+
+    gcc)
+        CORE_LIBS="$CORE_LIBS -ladvapi32 -lws2_32"
+    ;;
+
+    *)
+        CORE_LIBS="$CORE_LIBS advapi32.lib ws2_32.lib"
+    ;;
+
+esac
+
 EVENT_MODULES="$EVENT_MODULES $IOCP_MODULE"
 EVENT_FOUND=YES
 
diff --git a/src/core/ngx_connection.c b/src/core/ngx_connection.c
--- a/src/core/ngx_connection.c
+++ b/src/core/ngx_connection.c
@@ -297,7 +297,7 @@ ngx_open_listening_sockets(ngx_cycle_t *
                 continue;
             }
 
-            if (ls[i].fd != -1) {
+            if (ls[i].fd != (ngx_socket_t) -1) {
                 continue;
             }
 
@@ -312,7 +312,7 @@ ngx_open_listening_sockets(ngx_cycle_t *
 
             s = ngx_socket(ls[i].sockaddr->sa_family, ls[i].type, 0);
 
-            if (s == -1) {
+            if (s == (ngx_socket_t) -1) {
                 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
                               ngx_socket_n " %V failed", &ls[i].addr_text);
                 return NGX_ERROR;
@@ -863,7 +863,7 @@ ngx_close_connection(ngx_connection_t *c
     ngx_uint_t    log_error, level;
     ngx_socket_t  fd;
 
-    if (c->fd == -1) {
+    if (c->fd == (ngx_socket_t) -1) {
         ngx_log_error(NGX_LOG_ALERT, c->log, 0, "connection already closed");
         return;
     }
diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c
--- a/src/core/ngx_cycle.c
+++ b/src/core/ngx_cycle.c
@@ -543,7 +543,7 @@ ngx_init_cycle(ngx_cycle_t *old_cycle)
                 }
             }
 
-            if (nls[n].fd == -1) {
+            if (nls[n].fd == (ngx_socket_t) -1) {
                 nls[n].open = 1;
             }
         }
@@ -649,7 +649,7 @@ old_shm_zone_done:
     ls = old_cycle->listening.elts;
     for (i = 0; i < old_cycle->listening.nelts; i++) {
 
-        if (ls[i].remain || ls[i].fd == -1) {
+        if (ls[i].remain || ls[i].fd == (ngx_socket_t) -1) {
             continue;
         }
 
@@ -813,7 +813,7 @@ failed:
 
     ls = cycle->listening.elts;
     for (i = 0; i < cycle->listening.nelts; i++) {
-        if (ls[i].fd == -1 || !ls[i].open) {
+        if (ls[i].fd == (ngx_socket_t) -1 || !ls[i].open) {
             continue;
         }
 
diff --git a/src/core/ngx_resolver.c b/src/core/ngx_resolver.c
--- a/src/core/ngx_resolver.c
+++ b/src/core/ngx_resolver.c
@@ -2221,7 +2221,7 @@ ngx_udp_connect(ngx_udp_connection_t *uc
 
     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &uc->log, 0, "UDP socket %d", s);
 
-    if (s == -1) {
+    if (s == (ngx_socket_t) -1) {
         ngx_log_error(NGX_LOG_ALERT, &uc->log, ngx_socket_errno,
                       ngx_socket_n " failed");
         return NGX_ERROR;
diff --git a/src/event/modules/ngx_iocp_module.c b/src/event/modules/ngx_iocp_module.c
--- a/src/event/modules/ngx_iocp_module.c
+++ b/src/event/modules/ngx_iocp_module.c
@@ -170,7 +170,7 @@ ngx_iocp_timer(void *data)
 #endif
     }
 
-#ifdef __WATCOMC__
+#if defined(__WATCOMC__) || defined(__GNUC__)
     return 0;
 #endif
 }
diff --git a/src/event/modules/ngx_win32_select_module.c b/src/event/modules/ngx_win32_select_module.c
--- a/src/event/modules/ngx_win32_select_module.c
+++ b/src/event/modules/ngx_win32_select_module.c
@@ -148,8 +148,8 @@ ngx_select_add_event(ngx_event_t *ev, ng
         return NGX_ERROR;
     }
 
-    if ((event == NGX_READ_EVENT) && (max_read >= FD_SETSIZE)
-        || (event == NGX_WRITE_EVENT) && (max_write >= FD_SETSIZE))
+    if ((event == NGX_READ_EVENT && max_read >= FD_SETSIZE)
+        || (event == NGX_WRITE_EVENT && max_write >= FD_SETSIZE))
     {
         ngx_log_error(NGX_LOG_ERR, ev->log, 0,
                       "maximum number of descriptors "
diff --git a/src/event/ngx_event_accept.c b/src/event/ngx_event_accept.c
--- a/src/event/ngx_event_accept.c
+++ b/src/event/ngx_event_accept.c
@@ -70,7 +70,7 @@ ngx_event_accept(ngx_event_t *ev)
         s = accept(lc->fd, (struct sockaddr *) sa, &socklen);
 #endif
 
-        if (s == -1) {
+        if (s == (ngx_socket_t) -1) {
             err = ngx_socket_errno;
 
             if (err == NGX_EAGAIN) {
diff --git a/src/event/ngx_event_acceptex.c b/src/event/ngx_event_acceptex.c
--- a/src/event/ngx_event_acceptex.c
+++ b/src/event/ngx_event_acceptex.c
@@ -108,7 +108,7 @@ ngx_event_post_acceptex(ngx_listening_t 
         ngx_log_debug1(NGX_LOG_DEBUG_EVENT, &ls->log, 0,
                        ngx_socket_n " s:%d", s);
 
-        if (s == -1) {
+        if (s == (ngx_socket_t) -1) {
             ngx_log_error(NGX_LOG_ALERT, &ls->log, ngx_socket_errno,
                           ngx_socket_n " failed");
 
diff --git a/src/event/ngx_event_connect.c b/src/event/ngx_event_connect.c
--- a/src/event/ngx_event_connect.c
+++ b/src/event/ngx_event_connect.c
@@ -31,7 +31,7 @@ ngx_event_connect_peer(ngx_peer_connecti
 
     ngx_log_debug1(NGX_LOG_DEBUG_EVENT, pc->log, 0, "socket %d", s);
 
-    if (s == -1) {
+    if (s == (ngx_socket_t) -1) {
         ngx_log_error(NGX_LOG_ALERT, pc->log, ngx_socket_errno,
                       ngx_socket_n " failed");
         return NGX_ERROR;
diff --git a/src/event/ngx_event_pipe.c b/src/event/ngx_event_pipe.c
--- a/src/event/ngx_event_pipe.c
+++ b/src/event/ngx_event_pipe.c
@@ -57,7 +57,7 @@ ngx_event_pipe(ngx_event_pipe_t *p, ngx_
         do_write = 1;
     }
 
-    if (p->upstream->fd != -1) {
+    if (p->upstream->fd != (ngx_socket_t) -1) {
         rev = p->upstream->read;
 
         flags = (rev->eof || rev->error) ? NGX_CLOSE_EVENT : 0;
@@ -74,7 +74,9 @@ ngx_event_pipe(ngx_event_pipe_t *p, ngx_
         }
     }
 
-    if (p->downstream->fd != -1 && p->downstream->data == p->output_ctx) {
+    if (p->downstream->fd != (ngx_socket_t) -1
+        && p->downstream->data == p->output_ctx)
+    {



More information about the nginx-devel mailing list