Thrift: Compile without libevent.

Summary:
- Totally re-did the libevent check.
- Thrift now compiles and instals cleanly without libevent.
- (libevent is still needed to build libthriftnb.)

Reviewed By: mcslee

Test Plan:
- Ran configure and make in various configurations in my VMware.
- Clean build and install of Thrift on devrs004 (into /tmp).

Revert Plan: ok


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665267 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/Makefile.am b/lib/cpp/Makefile.am
index e0952dd..b80bcc3 100644
--- a/lib/cpp/Makefile.am
+++ b/lib/cpp/Makefile.am
@@ -1,43 +1,42 @@
-lib_LTLIBRARIES = libthrift.la libthriftnb.la
+lib_LTLIBRARIES = libthrift.la
+## We only build the extra libraries if we have the dependencies,
+## but we install all of the headers unconditionally.
+if AMX_HAVE_LIBEVENT
+lib_LTLIBRARIES += libthriftnb.la
+endif
 
-common_cxxflags = -Wall -Isrc $(BOOST_CPPFLAGS) $(EVENT_CPPFLAGS)
-common_ldflags = -Wall $(BOOST_LDFLAGS) $(EVENT_LDFLAGS)
+common_cxxflags = -Wall -Isrc $(BOOST_CPPFLAGS)
+common_ldflags = -Wall $(BOOST_LDFLAGS)
 
 # Define the source file for the module
 
-libthrift_sources = src/Thrift.cpp \
-                    src/reflection_limited_types.cpp \
-                    src/concurrency/Mutex.cpp \
-                    src/concurrency/Monitor.cpp \
-                    src/concurrency/PosixThreadFactory.cpp \
-                    src/concurrency/ThreadManager.cpp \
-                    src/concurrency/TimerManager.cpp \
-                    src/protocol/TBinaryProtocol.cpp \
-                    src/protocol/TDebugProtocol.cpp \
-                    src/protocol/TDenseProtocol.cpp \
-                    src/transport/TTransportException.cpp \
-                    src/transport/TFileTransport.cpp \
-                    src/transport/THttpClient.cpp \
-                    src/transport/TSocket.cpp \
-                    src/transport/TSocketPool.cpp \
-                    src/transport/TServerSocket.cpp \
-                    src/transport/TTransportUtils.cpp \
-                    src/server/TSimpleServer.cpp \
-                    src/server/TThreadPoolServer.cpp \
-                    src/server/TThreadedServer.cpp \
-                    src/processor/PeekProcessor.cpp
+libthrift_la_SOURCES = src/Thrift.cpp \
+                       src/reflection_limited_types.cpp \
+                       src/concurrency/Mutex.cpp \
+                       src/concurrency/Monitor.cpp \
+                       src/concurrency/PosixThreadFactory.cpp \
+                       src/concurrency/ThreadManager.cpp \
+                       src/concurrency/TimerManager.cpp \
+                       src/protocol/TBinaryProtocol.cpp \
+                       src/protocol/TDebugProtocol.cpp \
+                       src/protocol/TDenseProtocol.cpp \
+                       src/transport/TTransportException.cpp \
+                       src/transport/TFileTransport.cpp \
+                       src/transport/THttpClient.cpp \
+                       src/transport/TSocket.cpp \
+                       src/transport/TSocketPool.cpp \
+                       src/transport/TServerSocket.cpp \
+                       src/transport/TTransportUtils.cpp \
+                       src/server/TSimpleServer.cpp \
+                       src/server/TThreadPoolServer.cpp \
+                       src/server/TThreadedServer.cpp \
+                       src/processor/PeekProcessor.cpp
 
 
-libthriftnb_sources = src/server/TNonblockingServer.cpp
+libthriftnb_la_SOURCES = src/server/TNonblockingServer.cpp
 
-libthrift_la_SOURCES = $(libthrift_sources)
-libthriftnb_la_SOURCES = $(libthriftnb_sources)
-
-libthrift_cxxflags =  $(common_cxxflags)
-libthrift_ldflags =  $(common_ldflags)
-
-libthrift_la_CXXFLAGS =  $(libthrift_cxxflags)
-libthriftnb_la_CXXFLAGS =  $(libthrift_cxxflags)
+libthrift_la_CXXFLAGS =  $(common_cxxflags)
+libthriftnb_la_CXXFLAGS =  $(common_cxxflags) $(LIBEVENT_CPPFLAGS)
 
 include_thriftdir = $(includedir)/thrift
 include_thrift_HEADERS = \
diff --git a/lib/cpp/aclocal/ax_event_base.m4 b/lib/cpp/aclocal/ax_event_base.m4
deleted file mode 100644
index 9d5c525..0000000
--- a/lib/cpp/aclocal/ax_event_base.m4
+++ /dev/null
@@ -1,84 +0,0 @@
-dnl @synopsis AX_EVENT([MINIMUM-VERSION])
-dnl
-dnl Test for the libevent libraries of a particular version (or newer)
-dnl
-dnl If no path to the installed event library is given the macro
-dnl searchs under /usr, /usr/local, and /opt, and evaluates the
-dnl $EVENT_ROOT environment variable.
-dnl
-dnl This macro calls:
-dnl
-dnl   AC_SUBST(EVENT_CPPFLAGS) / AC_SUBST(EVENT_LDFLAGS)
-dnl
-dnl And sets:
-dnl
-dnl   HAVE_EVENT
-dnl
-dnl @category InstalledPackages
-dnl @category Cxx
-dnl @author Marc Kwiatkowski <marc@facebook.com>
-dnl @version 2006-06-15
-dnl @license AllPermissive
-
-AC_DEFUN([AX_EVENT_BASE],
-[
-AC_ARG_WITH([event],
-	AS_HELP_STRING([--with-event@<:@=DIR@:>@], [use event (default is yes) - it is possible to specify an alternate root directory for event]),
-	[
-    if test "$withval" = "no"; then
-    		want_event="no"
-    elif test "$withval" = "yes"; then
-        want_event="yes"
-        ac_event_path=""
-    else
-        want_event="yes"
-        ac_event_path="$withval"
-    fi
-    ],
-    [want_event="yes"])
-
-if test "x$want_event" = "xyes"; then
-	event_lib_version_req=ifelse([$1], ,1.2.0,$1)
-	event_lib_version_req_shorten=`expr $event_lib_version_req : '\([[0-9]]*\.[[0-9]]*\)'`
-	event_lib_version_req_major=`expr $event_lib_version_req : '\([[0-9]]*\)'`
-	event_lib_version_req_minor=`expr $event_lib_version_req : '[[0-9]]*\.\([[0-9]]*\)'`
-	event_lib_version_req_sub_minor=`expr $event_lib_version_req : '[[0-9]]*\.[[0-9]]*\.\([[0-9]]*\)'`
-	if test "x$event_lib_version_req_sub_minor" = "x" ; then
-	    event_lib_version_req_sub_minor="0"
-    	fi
-	WANT_EVENT_VERSION=`expr $event_lib_version_req_major \* 10000 \+  $event_lib_version_req_minor \* 100 \+ $event_lib_version_req_sub_minor`
-	WANT_EVENT_MAJOR_VERSION=$event_lib_version_req_major
-	WANT_EVENT_MINOR_VERSION=$event_lib_version_req_minor
-	AC_MSG_CHECKING(for eventlib >= $event_lib_version_req)
-	succeeded=no
-        
-	if test "$ac_event_path" != "" && test -f "$ac_event_path/include/event.h"; then
-	    ac_event_include_path=$ac_event_path/include
-	    EVENT_CPPFLAGS="-I$ac_event_include_path"
-	    EVENT_LDFLAGS="-L$ac_event_path/lib -levent"
-            succeeded=yes
-	else
-	    for ac_event_path_tmp in /usr /usr/local /opt ; do
-		if test -f "$ac_event_path_tmp/include/event.h"; then
-		    ac_event_include_path=$ac_event_path_tmp/include
-		    EVENT_CPPFLAGS="-I$ac_event_include_path"
-		    EVENT_LDFLAGS="-L$ac_event_path_tmp/lib -levent"
-                    succeeded=yes
-		    break;
-		fi
-	    done
-	fi
-
-	if test "$succeeded" != "yes" ; then
-            AC_MSG_ERROR([[We could not detect the event libraries (version $event_lib_version_req_shorten or higher). If you have a staged event library (still not installed) please specify \$EVENT_ROOT in your environment and do not give a PATH to --with-event option.  If you are sure you have event installed, then check your version number looking in <event.h>. See http://www.monkey.org/~provos/libevent/ for more documentation.]])
-	else
-            AC_SUBST(EVENT_CPPFLAGS)
-	    AC_SUBST(EVENT_LDFLAGS)
-	    AC_DEFINE(HAVE_EVENT,,[define if the EVENT library is available])
-	fi
-
-        CPPFLAGS="$CPPFLAGS_SAVED"
-       	LDFLAGS="$LDFLAGS_SAVED"
-fi
-
-])
diff --git a/lib/cpp/aclocal/ax_lib_event.m4 b/lib/cpp/aclocal/ax_lib_event.m4
new file mode 100644
index 0000000..14f115e
--- /dev/null
+++ b/lib/cpp/aclocal/ax_lib_event.m4
@@ -0,0 +1,169 @@
+dnl @synopsis AX_LIB_EVENT([MINIMUM-VERSION])
+dnl
+dnl Test for the libz library of a particular version (or newer).
+dnl
+dnl If no path to the installed libevent is given, the macro will first try
+dnl using no -I or -L flags, then searches under /usr, /usr/local, /opt,
+dnl and /opt/libevent.
+dnl If these all fail, it will try the $LIBEVENT_ROOT environment variable.
+dnl
+dnl This macro requires that #include <sys/types.h> works and defines u_char.
+dnl
+dnl This macro calls:
+dnl   AC_SUBST(LIBEVENT_CPPFLAGS)
+dnl   AC_SUBST(LIBEVENT_LDFLAGS)
+dnl   AC_SUBST(LIBEVENT_LIBS)
+dnl
+dnl And (if libevent is found):
+dnl   AC_DEFINE(HAVE_LIBEVENT)
+dnl
+dnl It also leaves the shell variables "success" and "ax_have_libevent"
+dnl set to "yes" or "no".
+dnl
+dnl NOTE: This macro does not currently work for cross-compiling,
+dnl       but it can be easily modified to allow it.  (grep "cross").
+dnl
+dnl @category InstalledPackages
+dnl @category C
+dnl @author David Reiss <dreiss@facebook.com>
+dnl @version 2007-09-12
+dnl @license AllPermissive
+
+dnl Input: ax_libevent_path, WANT_LIBEVENT_VERSION
+dnl Output: success=yes/no
+AC_DEFUN([AX_LIB_EVENT_DO_CHECK],
+         [
+          # Save our flags.
+          CPPFLAGS_SAVED="$CPPFLAGS"
+          LDFLAGS_SAVED="$LDFLAGS"
+          LIBS_SAVED="$LIBS"
+          LD_LIBRARY_PATH_SAVED="$LD_LIBRARY_PATH"
+
+          # Set our flags if we are checking a specific directory.
+          if test -n "$ax_libevent_path" ; then
+            LIBEVENT_CPPFLAGS="-I$ax_libevent_path/include"
+            LIBEVENT_LDFLAGS="-L$ax_libevent_path/lib"
+            LD_LIBRARY_PATH="$ax_libevent_path/lib:$LD_LIBRARY_PATH"
+          else
+            LIBEVENT_CPPFLAGS=""
+            LIBEVENT_LDFLAGS=""
+          fi
+
+          # Required flag for libevent.
+          LIBEVENT_LIBS="-levent"
+
+          # Prepare the environment for compilation.
+          CPPFLAGS="$CPPFLAGS $LIBEVENT_CPPFLAGS"
+          LDFLAGS="$LDFLAGS $LIBEVENT_LDFLAGS"
+          LIBS="$LIBS $LIBEVENT_LIBS"
+          export CPPFLAGS
+          export LDFLAGS
+          export LIBS
+          export LD_LIBRARY_PATH
+
+          success=no
+
+          # Compile, link, and run the program.  This checks:
+          # - event.h is available for including.
+          # - event_get_version() is available for linking.
+          # - The event version string is lexicographically greater
+          #   than the required version.
+          AC_LANG_PUSH([C])
+          dnl This can be changed to AC_LINK_IFELSE if you are cross-compiling,
+          dnl but then the version cannot be checked.
+          AC_RUN_IFELSE([AC_LANG_PROGRAM([[
+          #include <sys/types.h>
+          #include <event.h>
+          ]], [[
+          const char* lib_version = event_get_version();
+          const char* wnt_version = "$WANT_LIBEVENT_VERSION";
+          for (;;) {
+            /* If we reached the end of the want version.  We have it. */
+            if (*wnt_version == '\0') {
+              return 0;
+            }
+            /* If the want version continues but the lib version does not, */
+            /* we are missing a letter.  We don't have it. */
+            if (*lib_version == '\0') {
+              return 1;
+            }
+            /* If we have greater than what we want.  We have it. */
+            if (*lib_version > *wnt_version) {
+              return 0;
+            }
+            /* If we have less, we don't. */
+            if (*lib_version < *wnt_version) {
+              return 1;
+            }
+            lib_version++;
+            wnt_version++;
+          }
+          return 0;
+          ]])], [
+          success=yes
+          ])
+          AC_LANG_POP([C])
+
+          # Restore flags.
+          CPPFLAGS="$CPPFLAGS_SAVED"
+          LDFLAGS="$LDFLAGS_SAVED"
+          LIBS="$LIBS_SAVED"
+          LD_LIBRARY_PATH="$LD_LIBRARY_PATH_SAVED"
+         ])
+
+
+AC_DEFUN([AX_LIB_EVENT],
+         [
+
+          dnl Allow search path to be overridden on the command line.
+          AC_ARG_WITH([libevent],
+                      AS_HELP_STRING([--with-libevent@<:@=DIR@:>@], [use libevent (default is yes) - it is possible to specify an alternate root directory for libevent]),
+                      [
+                       if test "x$withval" = "xno"; then
+                         want_libevent="no"
+                       elif test "x$withval" = "xyes"; then
+                         want_libevent="yes"
+                         ax_libevent_path=""
+                       else
+                         want_libevent="yes"
+                         ax_libevent_path="$withval"
+                       fi
+                       ],
+                       [ want_libevent="yes" ; ax_libevent_path="" ])
+
+
+          if test "$want_libevent" = "yes"; then
+            WANT_LIBEVENT_VERSION=ifelse([$1], ,1.2,$1)
+
+            AC_MSG_CHECKING(for libevent >= $WANT_LIBEVENT_VERSION)
+
+            # Run tests.
+            if test -n "$ax_libevent_path"; then
+              AX_LIB_EVENT_DO_CHECK
+            else
+              for ax_libevent_path in "" /usr /usr/local /opt /opt/libevent "$LIBEVENT_ROOT" ; do
+                AX_LIB_EVENT_DO_CHECK
+                if test "$success" = "yes"; then
+                  break;
+                fi
+              done
+            fi
+
+            if test "$success" != "yes" ; then
+              AC_MSG_RESULT(no)
+              LIBEVENT_CPPFLAGS=""
+              LIBEVENT_LDFLAGS=""
+              LIBEVENT_LIBS=""
+            else
+              AC_MSG_RESULT(yes)
+              AC_DEFINE(HAVE_LIBEVENT,,[define if libevent is available])
+            fi
+
+            ax_have_libevent="$success"
+
+            AC_SUBST(LIBEVENT_CPPFLAGS)
+            AC_SUBST(LIBEVENT_LDFLAGS)
+            AC_SUBST(LIBEVENT_LIBS)
+          fi
+
+          ])
diff --git a/lib/cpp/configure.ac b/lib/cpp/configure.ac
index 0208f1b..5e33617 100644
--- a/lib/cpp/configure.ac
+++ b/lib/cpp/configure.ac
@@ -68,7 +68,8 @@
 
 AX_BOOST_BASE([1.33.1])
 
-AX_EVENT_BASE([1.2.0])
+AX_LIB_EVENT([1.0])
+AM_CONDITIONAL([AMX_HAVE_LIBEVENT], [test "$success" = "yes"])
 
 AC_CHECK_LIB(pthread, pthread_create)