From 77c9719d429337239b3f5e3d7fc5c17492682a77 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Sun, 28 Oct 2012 15:40:37 -0400 Subject: [PATCH] Numerous fixes related to compiling for mingw --- configure.ac | 1 + libhashkit/common.h | 6 ++++ libmemcached-1.0/platform.h | 26 ++++++++---------- libmemcached/behavior.cc | 4 +-- libmemcached/common.h | 8 ++++++ libmemcached/connect.cc | 30 +++++++++++--------- libmemcached/csl/server.h | 5 +++- libmemcached/do.cc | 4 +++ libmemcached/error.cc | 5 +++- libmemcached/instance.h | 12 ++++---- libmemcached/io.cc | 2 +- libmemcached/socket.hpp | 22 +++++++-------- libtest/test.hpp | 8 ++++++ libtest/timer.hpp | 5 ++-- libtest/wait.cc | 2 +- m4/ax_libmemcached.m4 | 55 ++++++++++++++++++------------------- poll/poll.c | 8 ++++++ win32/wrappers.h | 43 +++++++++++++++++++++++------ 18 files changed, 157 insertions(+), 89 deletions(-) diff --git a/configure.ac b/configure.ac index 9e3f72ac..90d5abde 100644 --- a/configure.ac +++ b/configure.ac @@ -144,6 +144,7 @@ AC_CHECK_HEADERS([ws2tcpip.h]) AC_CHECK_FUNCS([alarm]) AC_CHECK_FUNCS([dup2]) +AC_CHECK_FUNCS([fcntl]) AC_CHECK_FUNCS([getline]) AC_CHECK_FUNCS([gettimeofday]) AC_CHECK_FUNCS([memchr]) diff --git a/libhashkit/common.h b/libhashkit/common.h index e1343971..14423597 100644 --- a/libhashkit/common.h +++ b/libhashkit/common.h @@ -45,6 +45,12 @@ #include #include +#ifndef __WORDSIZE +# ifdef __MINGW32__ +# define __WORDSIZE 32 +# endif +#endif + #include #include #include diff --git a/libmemcached-1.0/platform.h b/libmemcached-1.0/platform.h index 6f43ca2c..01ce276c 100644 --- a/libmemcached-1.0/platform.h +++ b/libmemcached-1.0/platform.h @@ -38,25 +38,21 @@ #pragma once -#ifdef WIN32 +#if defined(WIN32) || defined(__MINGW32__) +# include +# include -#include -#include typedef short in_port_t; typedef SOCKET memcached_socket_t; -#else -typedef int memcached_socket_t; - -#include - -#include -#include - -#include - -#include +#else +# include +# include +# include +# include +# include +# include -#include +typedef int memcached_socket_t; #endif /* WIN32 */ diff --git a/libmemcached/behavior.cc b/libmemcached/behavior.cc index c381a513..d9a7cf60 100644 --- a/libmemcached/behavior.cc +++ b/libmemcached/behavior.cc @@ -427,7 +427,7 @@ uint64_t memcached_behavior_get(memcached_st *ptr, return 0; } - if (getsockopt(instance->fd, SOL_SOCKET, SO_SNDBUF, &sock_size, &sock_length) < 0) + if (getsockopt(instance->fd, SOL_SOCKET, SO_SNDBUF, (char*)&sock_size, &sock_length) < 0) { memcached_set_errno(*ptr, get_socket_errno(), MEMCACHED_AT); return 0; /* Zero means error */ @@ -463,7 +463,7 @@ uint64_t memcached_behavior_get(memcached_st *ptr, return 0; } - if (getsockopt(instance->fd, SOL_SOCKET, SO_RCVBUF, &sock_size, &sock_length) < 0) + if (getsockopt(instance->fd, SOL_SOCKET, SO_RCVBUF, (char*)&sock_size, &sock_length) < 0) { memcached_set_errno(*ptr, get_socket_errno(), MEMCACHED_AT); return 0; /* Zero means error */ diff --git a/libmemcached/common.h b/libmemcached/common.h index 5a76faf6..81088632 100644 --- a/libmemcached/common.h +++ b/libmemcached/common.h @@ -120,6 +120,14 @@ # include "poll/poll.h" #endif +#ifndef POLLHUP +# define POLLHUP 0x0010 +#endif + +#ifndef POLLNVAL +# define POLLNVAL 0x0020 +#endif + #ifdef __cplusplus extern "C" { #endif diff --git a/libmemcached/connect.cc b/libmemcached/connect.cc index 4e7e24bf..da6cf802 100644 --- a/libmemcached/connect.cc +++ b/libmemcached/connect.cc @@ -93,7 +93,7 @@ static memcached_return_t connect_poll(org::libmemcached::Instance* server) { int err; socklen_t len= sizeof(err); - if (getsockopt(server->fd, SOL_SOCKET, SO_ERROR, &err, &len) == 0) + if (getsockopt(server->fd, SOL_SOCKET, SO_ERROR, (char*)&err, &len) == 0) { if (err == 0) { @@ -124,7 +124,7 @@ static memcached_return_t connect_poll(org::libmemcached::Instance* server) { int err; socklen_t len= sizeof (err); - if (getsockopt(fds[0].fd, SOL_SOCKET, SO_ERROR, &err, &len) == 0) + if (getsockopt(fds[0].fd, SOL_SOCKET, SO_ERROR, (char*)&err, &len) == 0) { // We check the value to see what happened wth the socket. if (err == 0) @@ -298,7 +298,7 @@ static void set_socket_options(org::libmemcached::Instance* server) waittime.tv_usec= server->root->snd_timeout % 1000000; int error= setsockopt(server->fd, SOL_SOCKET, SO_SNDTIMEO, - &waittime, (socklen_t)sizeof(struct timeval)); + (char*)&waittime, (socklen_t)sizeof(struct timeval)); (void)error; assert(error == 0); } @@ -313,7 +313,7 @@ static void set_socket_options(org::libmemcached::Instance* server) waittime.tv_usec= server->root->rcv_timeout % 1000000; int error= setsockopt(server->fd, SOL_SOCKET, SO_RCVTIMEO, - &waittime, (socklen_t)sizeof(struct timeval)); + (char*)&waittime, (socklen_t)sizeof(struct timeval)); (void)(error); assert(error == 0); } @@ -344,7 +344,7 @@ static void set_socket_options(org::libmemcached::Instance* server) linger.l_onoff= 1; linger.l_linger= 0; /* By default on close() just drop the socket */ int error= setsockopt(server->fd, SOL_SOCKET, SO_LINGER, - &linger, (socklen_t)sizeof(struct linger)); + (char*)&linger, (socklen_t)sizeof(struct linger)); (void)(error); assert(error == 0); } @@ -354,7 +354,7 @@ static void set_socket_options(org::libmemcached::Instance* server) int flag= 1; int error= setsockopt(server->fd, IPPROTO_TCP, TCP_NODELAY, - &flag, (socklen_t)sizeof(int)); + (char*)&flag, (socklen_t)sizeof(int)); (void)(error); assert(error == 0); } @@ -364,7 +364,7 @@ static void set_socket_options(org::libmemcached::Instance* server) int flag= 1; int error= setsockopt(server->fd, SOL_SOCKET, SO_KEEPALIVE, - &flag, (socklen_t)sizeof(int)); + (char*)&flag, (socklen_t)sizeof(int)); (void)(error); assert(error == 0); } @@ -373,7 +373,7 @@ static void set_socket_options(org::libmemcached::Instance* server) if (server->root->tcp_keepidle > 0) { int error= setsockopt(server->fd, IPPROTO_TCP, TCP_KEEPIDLE, - &server->root->tcp_keepidle, (socklen_t)sizeof(int)); + (char*)&server->root->tcp_keepidle, (socklen_t)sizeof(int)); (void)(error); assert(error == 0); } @@ -382,7 +382,7 @@ static void set_socket_options(org::libmemcached::Instance* server) if (server->root->send_size > 0) { int error= setsockopt(server->fd, SOL_SOCKET, SO_SNDBUF, - &server->root->send_size, (socklen_t)sizeof(int)); + (char*)&server->root->send_size, (socklen_t)sizeof(int)); (void)(error); assert(error == 0); } @@ -390,7 +390,7 @@ static void set_socket_options(org::libmemcached::Instance* server) if (server->root->recv_size > 0) { int error= setsockopt(server->fd, SOL_SOCKET, SO_RCVBUF, - &server->root->recv_size, (socklen_t)sizeof(int)); + (char*)&server->root->recv_size, (socklen_t)sizeof(int)); (void)(error); assert(error == 0); } @@ -510,13 +510,16 @@ static memcached_return_t network_connect(org::libmemcached::Instance* server) type|= SOCK_NONBLOCK; } - if ((server->fd= socket(server->address_info_next->ai_family, - type, - server->address_info_next->ai_protocol)) < 0) + server->fd= socket(server->address_info_next->ai_family, + type, + server->address_info_next->ai_protocol); + + if (int(server->fd) == SOCKET_ERROR) { return memcached_set_errno(*server, get_socket_errno(), NULL); } +#ifdef HAVE_FCNTL // If SOCK_CLOEXEC exists then we don't need to call the following if (SOCK_CLOEXEC == 0) { @@ -529,6 +532,7 @@ static memcached_return_t network_connect(org::libmemcached::Instance* server) } while (rval == -1 && (errno == EINTR or errno == EAGAIN)); } } +#endif set_socket_options(server); diff --git a/libmemcached/csl/server.h b/libmemcached/csl/server.h index fe97eb91..21915d64 100644 --- a/libmemcached/csl/server.h +++ b/libmemcached/csl/server.h @@ -38,7 +38,10 @@ #pragma once #include -#include + +#ifdef HAVE_ARPA_INET_H +# include +#endif struct server_t { diff --git a/libmemcached/do.cc b/libmemcached/do.cc index db533680..ffb9a48b 100644 --- a/libmemcached/do.cc +++ b/libmemcached/do.cc @@ -34,6 +34,7 @@ memcached_return_t memcached_vdo(org::libmemcached::Instance* instance, **/ if (memcached_is_udp(instance->root)) { +#ifndef __MINGW32__ if (vector[0].buffer or vector[0].length) { return memcached_set_error(*instance->root, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT, @@ -74,6 +75,9 @@ memcached_return_t memcached_vdo(org::libmemcached::Instance* instance, } return MEMCACHED_SUCCESS; +#else + return MEMCACHED_FAILURE; +#endif } bool sent_success= memcached_io_writev(instance, vector, count, with_flush); diff --git a/libmemcached/error.cc b/libmemcached/error.cc index d15f9843..f963e124 100644 --- a/libmemcached/error.cc +++ b/libmemcached/error.cc @@ -141,9 +141,12 @@ static void _set(memcached_st& memc, memcached_string_t *str, memcached_return_t { #ifdef STRERROR_R_CHAR_P errmsg_ptr= strerror_r(local_errno, errmsg, sizeof(errmsg)); -#else +#elif defined(HAVE_STRERROR_R) && HAVE_STRERROR_R strerror_r(local_errno, errmsg, sizeof(errmsg)); errmsg_ptr= errmsg; +#elif defined(HAVE_STRERROR) && HAVE_STRERROR + snprintf(errmsg, sizeof(errmsg), "%s", strerror(local_errno)); + errmsg_ptr= errmsg; #endif } diff --git a/libmemcached/instance.h b/libmemcached/instance.h index dc9e45ed..e564afaa 100644 --- a/libmemcached/instance.h +++ b/libmemcached/instance.h @@ -39,19 +39,21 @@ #pragma once #ifndef WIN32 -#include +# ifdef HAVE_NETDB_H +# include +# endif #endif #ifdef NI_MAXHOST -#define MEMCACHED_NI_MAXHOST NI_MAXHOST +# define MEMCACHED_NI_MAXHOST NI_MAXHOST #else -#define MEMCACHED_NI_MAXHOST 1025 +# define MEMCACHED_NI_MAXHOST 1025 #endif #ifdef NI_MAXSERV -#define MEMCACHED_NI_MAXSERV NI_MAXSERV +# define MEMCACHED_NI_MAXSERV NI_MAXSERV #else -#define MEMCACHED_NI_MAXSERV 32 +# define MEMCACHED_NI_MAXSERV 32 #endif #ifdef __cplusplus diff --git a/libmemcached/io.cc b/libmemcached/io.cc index 019eafb6..886476f6 100644 --- a/libmemcached/io.cc +++ b/libmemcached/io.cc @@ -240,7 +240,7 @@ static memcached_return_t io_wait(org::libmemcached::Instance* ptr, int local_errno= EINVAL; int err; socklen_t len= sizeof (err); - if (getsockopt(ptr->fd, SOL_SOCKET, SO_ERROR, &err, &len) == 0) + if (getsockopt(ptr->fd, SOL_SOCKET, SO_ERROR, (char*)&err, &len) == 0) { if (err == 0) // treat this as EINTR { diff --git a/libmemcached/socket.hpp b/libmemcached/socket.hpp index fb589931..30e296ff 100644 --- a/libmemcached/socket.hpp +++ b/libmemcached/socket.hpp @@ -46,15 +46,15 @@ * a lot easier to map back to the unix way with macros than going the other * way without side effect ;-) */ -#ifdef WIN32 -#include "win32/wrappers.h" -#define get_socket_errno() WSAGetLastError() +#if defined(WIN32) || defined(__MINGW32__) +# include "win32/wrappers.h" +# define get_socket_errno() WSAGetLastError() #else -#include -#define INVALID_SOCKET -1 -#define SOCKET_ERROR -1 -#define closesocket(a) close(a) -#define get_socket_errno() errno +# include +# define INVALID_SOCKET -1 +# define SOCKET_ERROR -1 +# define closesocket(a) close(a) +# define get_socket_errno() errno #endif #ifdef __cplusplus @@ -66,15 +66,15 @@ static inline void memcached_close_socket(int& socket_fd) #endif #ifndef HAVE_MSG_NOSIGNAL -#define MSG_NOSIGNAL 0 +# define MSG_NOSIGNAL 0 #endif #ifndef HAVE_MSG_DONTWAIT -#define MSG_DONTWAIT 0 +# define MSG_DONTWAIT 0 #endif #ifndef HAVE_MSG_MORE -#define MSG_MORE 0 +# define MSG_MORE 0 #endif diff --git a/libtest/test.hpp b/libtest/test.hpp index 576f3014..864552fb 100644 --- a/libtest/test.hpp +++ b/libtest/test.hpp @@ -41,6 +41,14 @@ #include #include +#if defined(WIN32) || defined(__MINGW32__) +# include +# include + +typedef short in_port_t; + +#endif + #ifdef HAVE_ARPA_INET_H # include #endif diff --git a/libtest/timer.hpp b/libtest/timer.hpp index 28ec4741..73de26c6 100644 --- a/libtest/timer.hpp +++ b/libtest/timer.hpp @@ -114,7 +114,9 @@ public: private: void _time(struct timespec& ts) { -#ifdef __MACH__ // OSX lacks clock_gettime() +#if defined(HAVE_CLOCK_GETTIME) && HAVE_CLOCK_GETTIME + clock_gettime(CLOCK_REALTIME, &ts); +#elif defined(__MACH__) && __MACH__ // OSX lacks clock_gettime() clock_serv_t _clock_serv; mach_timespec_t _mach_timespec; host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &_clock_serv); @@ -123,7 +125,6 @@ private: ts.tv_sec= _mach_timespec.tv_sec; ts.tv_nsec= _mach_timespec.tv_nsec; #else - clock_gettime(CLOCK_REALTIME, &ts); #endif } diff --git a/libtest/wait.cc b/libtest/wait.cc index f934f5b3..a0d60d1f 100644 --- a/libtest/wait.cc +++ b/libtest/wait.cc @@ -34,7 +34,7 @@ * */ -#include +#include "config.h" #include #include diff --git a/m4/ax_libmemcached.m4 b/m4/ax_libmemcached.m4 index ea385d0f..95e5c9f7 100644 --- a/m4/ax_libmemcached.m4 +++ b/m4/ax_libmemcached.m4 @@ -4,7 +4,7 @@ # # SYNOPSIS # -# AX_LIBMEMCACHED, AX_LIBMEMCACHED_UTIL, AX_WITH_LIBMEMCACHED +# AX_LIBMEMCACHED, AX_LIBMEMCACHED_UTIL, AX_ENABLE_LIBMEMCACHED # # DESCRIPTION # @@ -73,10 +73,6 @@ AC_DEFUN([AX_LIBMEMCACHED], [ AS_IF([test "x$ax_cv_libmemcached" = "xyes"], [ AC_DEFINE([HAVE_LIBMEMCACHED_MEMCACHED_H], [1], [Have libmemcached-1.0/memcached.h]) - LIBMEMCACHED_CFLAGS= - AC_SUBST([LIBMEMCACHED_CFLAGS]) - LIBMEMCACHED_LDFLAGS="-lmemcached" - AC_SUBST([LIBMEMCACHED_LDFLAGS], [-lmemcached]) ],[ AC_DEFINE([HAVE_LIBMEMCACHED_MEMCACHED_H], [0], [Have libmemcached-1.0/memcached.h]) ]) @@ -106,34 +102,37 @@ AC_DEFUN([AX_LIBMEMCACHED], [ AS_IF([test "x$ax_cv_libmemcached_util" = "xyes"], [ AC_DEFINE([HAVE_LIBMEMCACHED_UTIL_H], [1], [Have libmemcachedutil-1.0/util.h]) - LIBMEMCACHED_UTIL_LDFLAGS="-lmemcached -lmemcachedutil" - AC_SUBST([LIBMEMCACHED_UTIL_LDFLAGS]) ],[ AC_DEFINE([HAVE_LIBMEMCACHED_UTIL_H], [0], [Have libmemcachedutil-1.0/util.h]) ]) ]) - AC_DEFUN([_WITH_LIBMEMCACHED], [ - AC_REQUIRE([AX_LIBMEMCACHED_UTIL]) - AC_ARG_ENABLE([libmemcached], - [AS_HELP_STRING([--disable-libmemcached], - [Build with libmemcached support @<:@default=on@:>@])], - [ac_enable_libmemcached="$enableval"], - [ac_enable_libmemcached="yes"]) +AC_DEFUN([_ENABLE_LIBMEMCACHED], [ + AC_REQUIRE([AX_LIBMEMCACHED_UTIL]) + AC_ARG_ENABLE([libmemcached], + [AS_HELP_STRING([--disable-libmemcached], + [Build with libmemcached support @<:@default=on@:>@])], + [ax_enable_libmemcached="$enableval"], + [ax_enable_libmemcached="yes"]) - ac_with_libmemcached="no" - AS_IF([test "x$ac_enable_libmemcached" = "xyes"], [ - AS_IF([test "x$ax_cv_libmemcached" = "xyes"], [ - ac_with_libmemcached="yes" - ]) - ]) + AS_IF([test "x$ax_cv_libmemcached" != "xyes"], [ + ax_enable_libmemcached="not found" + ]) - AS_IF([test "x$ac_with_libmemcached" = "xyes"], [ - AC_DEFINE([HAVE_LIBMEMCACHED], [1], [Enable libmemcached support]) - ],[ - AC_DEFINE([HAVE_LIBMEMCACHED], [0], [Enable libmemcached support]) - ]) - AM_CONDITIONAL(HAVE_LIBMEMCACHED, test "x${ac_with_libmemcached}" = "xyes") - ]) + AS_IF([test "x$ax_enable_libmemcached" = "xyes"], [ + AC_DEFINE([HAVE_LIBMEMCACHED], [1], [Enable libmemcached support]) + LIBMEMCACHED_CFLAGS= + AC_SUBST([LIBMEMCACHED_CFLAGS]) + LIBMEMCACHED_LDFLAGS="-lmemcached" + AC_SUBST([LIBMEMCACHED_LDFLAGS]) + AS_IF([test "x$ax_cv_libmemcached_util" = "xyes"], [ + LIBMEMCACHED_UTIL_LDFLAGS="-lmemcached -lmemcachedutil" + AC_SUBST([LIBMEMCACHED_UTIL_LDFLAGS]) + ]) + ],[ + AC_DEFINE([HAVE_LIBMEMCACHED], [0], [Enable libmemcached support]) + ]) + AM_CONDITIONAL(HAVE_LIBMEMCACHED, test "x${ax_enable_libmemcached}" = "xyes") + ]) -AC_DEFUN([AX_WITH_LIBMEMCACHED], [ AC_REQUIRE([_WITH_LIBMEMCACHED]) ]) +AC_DEFUN([AX_ENABLE_LIBMEMCACHED], [ AC_REQUIRE([_ENABLE_LIBMEMCACHED]) ]) diff --git a/poll/poll.c b/poll/poll.c index c5aa1115..d7a7d9c7 100644 --- a/poll/poll.c +++ b/poll/poll.c @@ -9,9 +9,17 @@ * */ #include "config.h" + +#if defined(WIN32) || defined(__MINGW32__) +# include +# include +#endif + #include #include +#include "poll/poll.h" + int poll(struct pollfd fds[], nfds_t nfds, int tmo) { fd_set readfds, writefds, errorfds; diff --git a/win32/wrappers.h b/win32/wrappers.h index 80b44651..976303cb 100644 --- a/win32/wrappers.h +++ b/win32/wrappers.h @@ -29,17 +29,42 @@ * WinSock use a separate range for error codes. Let's just map to the * WinSock ones. */ -#define EADDRINUSE WSAEADDRINUSE -#define EWOULDBLOCK WSAEWOULDBLOCK -#define EINPROGRESS WSAEINPROGRESS -#define EALREADY WSAEALREADY -#define EISCONN WSAEISCONN -#define ENOTCONN WSAENOTCONN -#define ENOBUFS WSAENOBUFS -#define SHUT_RDWR SD_BOTH +#ifndef EADDRINUSE +# define EADDRINUSE WSAEADDRINUSE +#endif + +#ifndef EWOULDBLOCK +# define EWOULDBLOCK WSAEWOULDBLOCK +#endif + +#ifndef EINPROGRESS +# define EINPROGRESS WSAEINPROGRESS +#endif + +#ifndef EALREADY +# define EALREADY WSAEALREADY +#endif + +#ifndef EISCONN +# define EISCONN WSAEISCONN +#endif + +#ifndef ENOTCONN +# define ENOTCONN WSAENOTCONN +#endif + +#ifndef ENOBUFS +# define ENOBUFS WSAENOBUFS +#endif + +#ifndef SHUT_RDWR +# define SHUT_RDWR SD_BOTH +#endif /* EAI_SYSTEM isn't defined anywhere... just set it to... 11? */ -#define EAI_SYSTEM 11 +#ifndef EAI_SYSTEM +# define EAI_SYSTEM 11 +#endif /* Best effort mapping of functions to alternative functions */ #define index(a,b) strchr(a,b) -- 2.30.2