[patch] autoconf cleanups, build on osx (incl. freebsd patches)

Evan Martin martine@danga.com
Sun, 10 Aug 2003 03:40:33 -0700


--Apple-Mail-2-600455367
Content-Disposition: attachment;
	filename=memcached-bsd.patch
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
	x-unix-mode=0644;
	name="memcached-bsd.patch"

Index: ChangeLog
===================================================================
RCS file: /home/cvspub/wcmtools/memcached/ChangeLog,v
retrieving revision 1.11
diff -u -r1.11 ChangeLog
--- ChangeLog	30 Jul 2003 05:53:49 -0000	1.11
+++ ChangeLog	10 Aug 2003 10:38:20 -0000
@@ -1,3 +1,14 @@
+2003-08-10  (Evan Martin)
+	* Makefile.am: debug, optimization, and static flags are controlled
+	  by the configure script.
+	* configure.ac:
+	  - allow specifying libevent directory with --with-libevent=DIR
+	  - check for malloc.h (unavailable on BSDs)
+	  - check for socklen_t (unavailable on OSX)
+	* assoc.c, items.c, slabs.c:  Remove some unused headers.
+	* memcached.c:  allow for nonexistence of malloc.h; #define a POSIX
+	  macro to import mlockall flags.
+
 2003-07-29
 	* version 1.1.7
 	* big bug fix: item exptime 0 meant expire immediately, not never
Index: Makefile.am
===================================================================
RCS file: /home/cvspub/wcmtools/memcached/Makefile.am,v
retrieving revision 1.8
diff -u -r1.8 Makefile.am
--- Makefile.am	7 Aug 2003 17:31:17 -0000	1.8
+++ Makefile.am	10 Aug 2003 10:38:20 -0000
@@ -5,6 +5,5 @@
 DIST_SUBDIRS = doc
 EXTRA_DIST = doc TODO
 
-CFLAGS=-g -O2 -static -DNDEBUG
-
+AM_CFLAGS=-DNDEBUG
 
Index: assoc.c
===================================================================
RCS file: /home/cvspub/wcmtools/memcached/assoc.c,v
retrieving revision 1.5
diff -u -r1.5 assoc.c
--- assoc.c	27 Jun 2003 21:15:55 -0000	1.5
+++ assoc.c	10 Aug 2003 10:38:20 -0000
@@ -19,7 +19,6 @@
 #include <sys/socket.h>
 #include <sys/signal.h>
 #include <sys/resource.h>
-#include <sys/mman.h>
 #include <fcntl.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -28,7 +27,6 @@
 #include <netinet/in.h>
 #include <errno.h>
 #include <event.h>
-#include <malloc.h>
 #include <assert.h>
 
 #include "memcached.h"
Index: configure.ac
===================================================================
RCS file: /home/cvspub/wcmtools/memcached/configure.ac,v
retrieving revision 1.15
diff -u -r1.15 configure.ac
--- configure.ac	30 Jul 2003 05:53:49 -0000	1.15
+++ configure.ac	10 Aug 2003 10:38:20 -0000
@@ -7,6 +7,42 @@
 AC_PROG_CC
 AC_PROG_INSTALL
 
+AC_ARG_WITH(libevent,
+	AC_HELP_STRING([--with-libevent=DIRECTORY],[base directory for libevent]))
+if test ${with_libevent+set} = set && test $with_libevent != no; then
+	CFLAGS="$CFLAGS -I$with_libevent/include"
+	LDFLAGS="$LDFLAGS -L$with_libevent/lib"
+fi
+
+LIBEVENT_URL=http://www.monkey.org/~provos/libevent/
+AC_CHECK_LIB(event, event_set, ,
+	[AC_MSG_ERROR(libevent is required.  You can get it from $LIBEVENT_URL)])
+
+AC_CHECK_HEADER(malloc.h, AC_DEFINE(HAVE_MALLOC_H,,[do we have malloc.h?]))
+
+dnl From licq: Copyright (c) 2000 Dirk Mueller
+dnl Check if the type socklen_t is defined anywhere
+AC_DEFUN(AC_C_SOCKLEN_T,
+[AC_CACHE_CHECK(for socklen_t, ac_cv_c_socklen_t,
+[
+  AC_TRY_COMPILE([
+    #include <sys/types.h>
+    #include <sys/socket.h>
+  ],[
+    socklen_t foo;
+  ],[
+    ac_cv_c_socklen_t=yes
+  ],[
+    ac_cv_c_socklen_t=no
+  ])
+])
+if test $ac_cv_c_socklen_t = no; then
+  AC_DEFINE(socklen_t, int, [define to int if socklen_t not available])
+fi
+])
+
+AC_C_SOCKLEN_T
+
 dnl Default to building a static executable.
 AC_ARG_ENABLE(static,
 	AC_HELP_STRING([--disable-static],[build a dynamically linked executable]),
@@ -19,10 +55,6 @@
 	CFLAGS="$CFLAGS -static"
 	AC_MSG_RESULT(yes)
 fi
-
-LIBEVENT_URL=http://www.monkey.org/~provos/libevent/
-AC_CHECK_LIB(event, event_set, ,
-	[AC_MSG_ERROR(libevent is required.  You can get it from $LIBEVENT_URL)])
 
 AC_CONFIG_FILES(Makefile)
 AC_OUTPUT
Index: items.c
===================================================================
RCS file: /home/cvspub/wcmtools/memcached/items.c,v
retrieving revision 1.18
diff -u -r1.18 items.c
--- items.c	23 Jul 2003 00:00:20 -0000	1.18
+++ items.c	10 Aug 2003 10:38:20 -0000
@@ -7,7 +7,6 @@
 #include <sys/socket.h>
 #include <sys/signal.h>
 #include <sys/resource.h>
-#include <sys/mman.h>
 #include <fcntl.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -17,7 +16,6 @@
 #include <errno.h>
 #include <time.h>
 #include <event.h>
-#include <malloc.h>
 #include <assert.h>
 
 #include "memcached.h"
Index: memcached.c
===================================================================
RCS file: /home/cvspub/wcmtools/memcached/memcached.c,v
retrieving revision 1.29
diff -u -r1.29 memcached.c
--- memcached.c	30 Jul 2003 05:53:49 -0000	1.29
+++ memcached.c	10 Aug 2003 10:38:22 -0000
@@ -23,6 +23,11 @@
 #include <sys/socket.h>
 #include <sys/signal.h>
 #include <sys/resource.h>
+/* some POSIX systems need the following definition
+ * to get mlockall flags out of sys/mman.h.  */
+#ifndef _P1003_1B_VISIBLE
+#define _P1003_1B_VISIBLE
+#endif
 #include <sys/mman.h>
 #include <fcntl.h>
 #include <stdlib.h>
@@ -34,9 +39,12 @@
 #include <errno.h>
 #include <time.h>
 #include <event.h>
-#include <malloc.h>
 #include <assert.h>
 
+#ifdef HAVE_MALLOC_H
+#include <malloc.h>
+#endif
+
 #include "memcached.h"
 
 struct stats stats;
@@ -314,6 +322,7 @@
         return;
     }
 
+#ifdef HAVE_MALLOC_H
     if (strcmp(command, "stats malloc") == 0) {
         char temp[512];
         struct mallinfo info;
@@ -333,6 +342,7 @@
         out_string(c, temp);
         return;
     }
+#endif /* HAVE_MALLOC_H */
 
     if (strcmp(command, "stats maps") == 0) {
         char *wbuf;
Index: slabs.c
===================================================================
RCS file: /home/cvspub/wcmtools/memcached/slabs.c,v
retrieving revision 1.13
diff -u -r1.13 slabs.c
--- slabs.c	27 Jul 2003 05:04:37 -0000	1.13
+++ slabs.c	10 Aug 2003 10:38:22 -0000
@@ -11,7 +11,6 @@
 #include <sys/socket.h>
 #include <sys/signal.h>
 #include <sys/resource.h>
-#include <sys/mman.h>
 #include <fcntl.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -20,7 +19,6 @@
 #include <netinet/in.h>
 #include <errno.h>
 #include <event.h>
-#include <malloc.h>
 #include <assert.h>
 
 #include "memcached.h"

--Apple-Mail-2-600455367
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed

The ChangeLog:
         * Makefile.am: debug, optimization, and static flags are 
controlled
           by the configure script.
         * configure.ac:
           - allow specifying libevent directory with --with-libevent=DIR
           - check for malloc.h (unavailable on BSDs)
           - check for socklen_t (unavailable on OSX)
         * assoc.c, items.c, slabs.c:  Remove some unused headers.
         * memcached.c:  allow for nonexistence of malloc.h; #define a 
POSIX
	 macro to import mlockall flags.

My notes:
This now builds on OSX, though I get some weird linker errors unless I 
--disable-static.  But in theory, working on OSX means it probably 
works on FreeBSD.

That socklen_t check looks hairy, but even libevent does a similar 
check.  (Ours is a bit more complicated to allow autoconf caching but 
it's a cut'n'paste job so it should work.)

Special thanks to Sean Chittenden <seanc@FreeBSD.org> (have we met?  I 
swear I know that name); I used his FreeBSD patches as a reference for 
what doesn't work on BSD.  Rather than removing blocks of code as his 
patches did, this patch #ifdefs out code that is platform-specific 
(only the slab stats stuff, really).

Hopefully, mlockall actually does work on FreeBSD and the problem there 
was only that MCL_* weren't getting set; it looks like mlockall is in 
POSIX[1] and should work everywhere.
The +#define _P1003_1B_VISIBLE needed to get those flags at first 
glance looks like a huge hack but some googling indicates it's 
moderately standard[2].  I'm afraid it's a derived setting from some 
other setting (possibly simple P1003_B, as some things I'm finding on 
Google indicate), but at least on OSX it's never set and only read in 
one file, as far as I can tell:
lulu:~% grep -lrs _P1003_1B_VISIBLE /usr/include/*
/usr/include/sys/mman.h

In retrospect the patch is tiny, but damn did that all take me a while 
to figure out.
Untested on Linux or FreeBSD, so comments are welcome.  :)

[1] http://www.opengroup.org/onlinepubs/007908799/xsh/mlockall.html
[2] http://www.chedong.com/phpMan.php/man/posix4/9

-- 
Evan Martin
martine@danga.com
http://neugierig.org
--Apple-Mail-2-600455367--