From a72e975613bce74d11802f0d5c2edb085c044c82 Mon Sep 17 00:00:00 2001
From: Yucong Sun <thefallentree@user.github.invalid>
Date: Mon, 23 Nov 2020 20:21:58 +0000
Subject: [PATCH] mingw: libevent build fixes

---
 lib/event-libs/libevent/libevent.c    | 12 +++++-----
 lib/plat/windows/CMakeLists.txt       |  4 ++--
 lib/roles/http/client/client-http.c   |  2 +-
 win32port/win32helpers/gettimeofday.c | 34 ++++++++++++++-------------
 win32port/win32helpers/gettimeofday.h |  8 ++++---
 5 files changed, 32 insertions(+), 28 deletions(-)

diff --git a/lib/event-libs/libevent/libevent.c b/lib/event-libs/libevent/libevent.c
index 3521185b7..1ff170972 100644
--- a/lib/event-libs/libevent/libevent.c
+++ b/lib/event-libs/libevent/libevent.c
@@ -29,7 +29,7 @@
 #define wsi_to_priv_event(_w) ((struct lws_wsi_eventlibs_libevent *)(_w)->evlib_wsi)
 
 static void
-lws_event_hrtimer_cb(int fd, short event, void *p)
+lws_event_hrtimer_cb(evutil_socket_t fd, short event, void *p)
 {
 	struct lws_context_per_thread *pt = (struct lws_context_per_thread *)p;
 	struct lws_pt_eventlibs_libevent *ptpr = pt_to_priv_event(pt);
@@ -48,7 +48,7 @@ lws_event_hrtimer_cb(int fd, short event, void *p)
 }
 
 static void
-lws_event_idle_timer_cb(int fd, short event, void *p)
+lws_event_idle_timer_cb(evutil_socket_t fd, short event, void *p)
 {
 	struct lws_context_per_thread *pt = (struct lws_context_per_thread *)p;
 	struct lws_pt_eventlibs_libevent *ptpr = pt_to_priv_event(pt);
@@ -255,7 +255,7 @@ elops_accept_event(struct lws *wsi)
 	struct lws_context_per_thread *pt;
 	struct lws_pt_eventlibs_libevent *ptpr;
 	struct lws_wsi_eventlibs_libevent *wpr = wsi_to_priv_event(wsi);
-	int fd;
+       evutil_socket_t fd;
 
 	wpr->w_read.context = context;
 	wpr->w_write.context = context;
@@ -265,7 +265,7 @@ elops_accept_event(struct lws *wsi)
 	ptpr = pt_to_priv_event(pt);
 
 	if (wsi->role_ops->file_handle)
-		fd = wsi->desc.filefd;
+               fd = (ev_intptr_t) wsi->desc.filefd;
 	else
 		fd = wsi->desc.sockfd;
 
@@ -404,7 +404,7 @@ elops_init_vhost_listen_wsi_event(struct lws *wsi)
 	struct lws_context_per_thread *pt;
 	struct lws_pt_eventlibs_libevent *ptpr;
 	struct lws_wsi_eventlibs_libevent *w;
-	int fd;
+       evutil_socket_t fd;
 
 	if (!wsi) {
 		assert(0);
@@ -420,7 +420,7 @@ elops_init_vhost_listen_wsi_event(struct lws *wsi)
 	ptpr = pt_to_priv_event(pt);
 
 	if (wsi->role_ops->file_handle)
-		fd = wsi->desc.filefd;
+               fd = (evutil_socket_t) wsi->desc.filefd;
 	else
 		fd = wsi->desc.sockfd;
 
diff --git a/lib/plat/windows/CMakeLists.txt b/lib/plat/windows/CMakeLists.txt
index 68b71cd64..c26c8e771 100644
--- a/lib/plat/windows/CMakeLists.txt
+++ b/lib/plat/windows/CMakeLists.txt
@@ -50,7 +50,7 @@ if (LWS_WITH_SPAWN)
 endif()
 
 if (LWS_WITH_ZLIB AND LWS_WITH_BUNDLED_ZLIB)
-	set(WIN32_ZLIB_PATH "win32port/zlib")
+       set(WIN32_ZLIB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../win32port/zlib")
 	set(ZLIB_SRCS
 		${WIN32_ZLIB_PATH}/adler32.c
 		${WIN32_ZLIB_PATH}/compress.c
@@ -68,7 +68,7 @@ if (LWS_WITH_ZLIB AND LWS_WITH_BUNDLED_ZLIB)
 		${WIN32_ZLIB_PATH}/zutil.c)
 	add_library(zlib_internal STATIC ${ZLIB_SRCS})
 	set(ZLIB_INCLUDE_DIRS ${WIN32_ZLIB_PATH})
-	get_property(ZLIB_LIBRARIES TARGET zlib_internal PROPERTY LOCATION)
+       set(ZLIB_LIBRARIES "")
 	set(ZLIB_FOUND 1)
 	# Make sure zlib_internal is compiled before the libs.
 	foreach (lib ${LWS_LIBRARIES})
diff --git a/lib/roles/http/client/client-http.c b/lib/roles/http/client/client-http.c
index f4b494488..3a706a5e8 100644
--- a/lib/roles/http/client/client-http.c
+++ b/lib/roles/http/client/client-http.c
@@ -131,7 +131,7 @@ lws_http_client_socket_service(struct lws *wsi, struct lws_pollfd *pollfd)
 
 		lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
 
-		/* fallthru */
+               /* fallthrough */
 
 #endif
 
diff --git a/win32port/win32helpers/gettimeofday.c b/win32port/win32helpers/gettimeofday.c
index 08385c232..be5ac11ea 100644
--- a/win32port/win32helpers/gettimeofday.c
+++ b/win32port/win32helpers/gettimeofday.c
@@ -1,11 +1,12 @@
-#include <time.h>
-#include <windows.h> //I've omitted context line
-
-#include "gettimeofday.h"
-
-int gettimeofday(struct timeval *tv, struct timezone *tz)
-{
-	FILETIME ft;
+#include <time.h>
+#include <windows.h>
+
+#include "gettimeofday.h"
+
+#ifndef LWS_MINGW_SUPPORT
+int gettimeofday(struct timeval *tv, struct timezone *tz)
+{
+       FILETIME ft;
 	unsigned __int64 tmpres = 0;
 	static int tzflag;
 
@@ -19,11 +20,11 @@ int gettimeofday(struct timeval *tv, struct timezone *tz)
 		/*converting file time to unix epoch*/
 		tmpres /= 10;  /*convert into microseconds*/
 		tmpres -= DELTA_EPOCH_IN_MICROSECS;
-		tv->tv_sec = (long)(tmpres / 1000000UL);
-		tv->tv_usec = (long)(tmpres % 1000000UL);
-	}
- 
-	if (NULL != tz) {
+               tv->tv_sec = (long)(tmpres / 1000000UL);
+               tv->tv_usec = (long)(tmpres % 1000000UL);
+       }
+
+       if (NULL != tz) {
 		if (!tzflag) {
 			_tzset();
 			tzflag++;
@@ -31,6 +32,7 @@ int gettimeofday(struct timeval *tv, struct timezone *tz)
 		tz->tz_minuteswest = _timezone / 60;
 		tz->tz_dsttime = _daylight;
 	}
-
-	return 0;
-}
+
+       return 0;
+}
+#endif
diff --git a/win32port/win32helpers/gettimeofday.h b/win32port/win32helpers/gettimeofday.h
index 33e7a750f..4531fa80a 100644
--- a/win32port/win32helpers/gettimeofday.h
+++ b/win32port/win32helpers/gettimeofday.h
@@ -10,11 +10,11 @@
 #endif
 
 #ifdef LWS_MINGW_SUPPORT
-  #include <winsock2.h>
+  #include <sys/time.h>
 #endif
 
-#ifndef _TIMEZONE_DEFINED 
-struct timezone 
+#ifndef _TIMEZONE_DEFINED
+struct timezone
 {
   int  tz_minuteswest; /* minutes W of Greenwich */
   int  tz_dsttime;     /* type of dst correction */
@@ -22,6 +22,8 @@ struct timezone
 
 #endif
 
+#ifndef LWS_MINGW_SUPPORT
 int gettimeofday(struct timeval *tv, struct timezone *tz);
+#endif
 
 #endif