Skip to content

Commit

Permalink
cmake modules
Browse files Browse the repository at this point in the history
  • Loading branch information
thesamet committed Dec 24, 2011
1 parent 89d7ab9 commit fef6402
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 17 deletions.
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
option(zrpc_build_tests "Build zrpc's tests." OFF)

find_package(ZeroMQ REQUIRED)
find_package(ProtobufPlugin REQUIRED)
find_package(Boost REQUIRED COMPONENTS thread)
include_directories(${PROTOBUF_INCLUDE_DIR})

find_package(Glog REQUIRED)
find_package(GFlags REQUIRED)
include_directories(${ZeroMQ_INCLUDE_DIRS})
include_directories(${PROTOBUF_INCLUDE_DIRS})
include_directories(${Boost_INCLUDE_DIRS})
# include_directories(${GLOG_INCLUDE_DIRS})
include_directories(${GFlags_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_BINARY_DIR}/src)

Expand Down
41 changes: 41 additions & 0 deletions cmake_modules/FindGFlags.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# - Try to find gflags
# Once done, this will define
#
# GFlags_FOUND - system has gflags
# GFlags_INCLUDE_DIRS - the gflags include directories
# GFlags_LIBRARIES - link these to use gflags

include(LibFindMacros)

IF (UNIX)
# Use pkg-config to get hints about paths
libfind_pkg_check_modules(GFlags_PKGCONF libgflags)

# Include dir
find_path(GFlags_INCLUDE_DIR
NAMES gflags/gflags.h
PATHS ${GFLAGS_ROOT}/include ${GFlags_PKGCONF_INCLUDE_DIRS}
)

# Finally the library itself
find_library(GFlags_LIBRARY
NAMES gflags
PATHS ${GFLAGS_ROOT}/lib ${GFlags_PKGCONF_LIBRARY_DIRS}
)
ELSEIF (WIN32)
find_path(GFlags_INCLUDE_DIR
NAMES gflags/gflags.h
PATHS ${GFLAGS_ROOT}/include ${CMAKE_INCLUDE_PATH}
)
# Finally the library itself
find_library(GFlags_LIBRARY
NAMES gflags
PATHS ${GFLAGS_ROOT}/lib ${CMAKE_LIB_PATH}
)
ENDIF()

# Set the include dir variables and the libraries and let libfind_process do the rest.
# NOTE: Singular variables for this library, plural for libraries this this lib depends on.
set(GFlags_PROCESS_INCLUDES GFlags_INCLUDE_DIR GFlags_INCLUDE_DIRS)
set(GFlags_PROCESS_LIBS GFlags_LIBRARY GFlags_LIBRARIES)
libfind_process(GFlags)
41 changes: 41 additions & 0 deletions cmake_modules/FindZeroMQ.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# - Try to find libzmq
# Once done, this will define
#
# ZeroMQ_FOUND - system has libzmq
# ZeroMQ_INCLUDE_DIRS - the libzmq include directories
# ZeroMQ_LIBRARIES - link these to use libzmq

include(LibFindMacros)

IF (UNIX)
# Use pkg-config to get hints about paths
libfind_pkg_check_modules(ZeroMQ_PKGCONF libzmq)

# Include dir
find_path(ZeroMQ_INCLUDE_DIR
NAMES zmq.hpp
PATHS ${ZEROMQ_ROOT}/include ${ZeroMQ_PKGCONF_INCLUDE_DIRS}
)

# Finally the library itself
find_library(ZeroMQ_LIBRARY
NAMES zmq
PATHS ${ZEROMQ_ROOT}/lib ${ZeroMQ_PKGCONF_LIBRARY_DIRS}
)
ELSEIF (WIN32)
find_path(ZeroMQ_INCLUDE_DIR
NAMES zmq.hpp
PATHS ${ZEROMQ_ROOT}/include ${CMAKE_INCLUDE_PATH}
)
# Finally the library itself
find_library(ZeroMQ_LIBRARY
NAMES libzmq
PATHS ${ZEROMQ_ROOT}/lib ${CMAKE_LIB_PATH}
)
ENDIF()

# Set the include dir variables and the libraries and let libfind_process do the rest.
# NOTE: Singular variables for this library, plural for libraries this this lib depends on.
set(ZeroMQ_PROCESS_INCLUDES ZeroMQ_INCLUDE_DIR ZeroMQ_INCLUDE_DIRS)
set(ZeroMQ_PROCESS_LIBS ZeroMQ_LIBRARY ZeroMQ_LIBRARIES)
libfind_process(ZeroMQ)
99 changes: 99 additions & 0 deletions cmake_modules/LibFindMacros.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Works the same as find_package, but forwards the "REQUIRED" and "QUIET" arguments
# used for the current package. For this to work, the first parameter must be the
# prefix of the current package, then the prefix of the new package etc, which are
# passed to find_package.
macro (libfind_package PREFIX)
set (LIBFIND_PACKAGE_ARGS ${ARGN})
if (${PREFIX}_FIND_QUIETLY)
set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} QUIET)
endif (${PREFIX}_FIND_QUIETLY)
if (${PREFIX}_FIND_REQUIRED)
set (LIBFIND_PACKAGE_ARGS ${LIBFIND_PACKAGE_ARGS} REQUIRED)
endif (${PREFIX}_FIND_REQUIRED)
find_package(${LIBFIND_PACKAGE_ARGS})
endmacro (libfind_package)

# CMake developers made the UsePkgConfig system deprecated in the same release (2.6)
# where they added pkg_check_modules. Consequently I need to support both in my scripts
# to avoid those deprecated warnings. Here's a helper that does just that.
# Works identically to pkg_check_modules, except that no checks are needed prior to use.
macro (libfind_pkg_check_modules PREFIX PKGNAME)
if (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
include(UsePkgConfig)
pkgconfig(${PKGNAME} ${PREFIX}_INCLUDE_DIRS ${PREFIX}_LIBRARY_DIRS ${PREFIX}_LDFLAGS ${PREFIX}_CFLAGS)
else (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
find_package(PkgConfig)
if (PKG_CONFIG_FOUND)
pkg_check_modules(${PREFIX} ${PKGNAME})
endif (PKG_CONFIG_FOUND)
endif (${CMAKE_MAJOR_VERSION} EQUAL 2 AND ${CMAKE_MINOR_VERSION} EQUAL 4)
endmacro (libfind_pkg_check_modules)

# Do the final processing once the paths have been detected.
# If include dirs are needed, ${PREFIX}_PROCESS_INCLUDES should be set to contain
# all the variables, each of which contain one include directory.
# Ditto for ${PREFIX}_PROCESS_LIBS and library files.
# Will set ${PREFIX}_FOUND, ${PREFIX}_INCLUDE_DIRS and ${PREFIX}_LIBRARIES.
# Also handles errors in case library detection was required, etc.
macro (libfind_process PREFIX)
# Skip processing if already processed during this run
if (NOT ${PREFIX}_FOUND)
# Start with the assumption that the library was found
set (${PREFIX}_FOUND TRUE)

# Process all includes and set _FOUND to false if any are missing
foreach (i ${${PREFIX}_PROCESS_INCLUDES})
if (${i})
set (${PREFIX}_INCLUDE_DIRS ${${PREFIX}_INCLUDE_DIRS} ${${i}})
mark_as_advanced(${i})
else (${i})
set (${PREFIX}_FOUND FALSE)
endif (${i})
endforeach (i)

# Process all libraries and set _FOUND to false if any are missing
foreach (i ${${PREFIX}_PROCESS_LIBS})
if (${i})
set (${PREFIX}_LIBRARIES ${${PREFIX}_LIBRARIES} ${${i}})
mark_as_advanced(${i})
else (${i})
set (${PREFIX}_FOUND FALSE)
endif (${i})
endforeach (i)

# Print message and/or exit on fatal error
if (${PREFIX}_FOUND)
if (NOT ${PREFIX}_FIND_QUIETLY)
message (STATUS "Found ${PREFIX} ${${PREFIX}_VERSION}")
endif (NOT ${PREFIX}_FIND_QUIETLY)
else (${PREFIX}_FOUND)
if (${PREFIX}_FIND_REQUIRED)
foreach (i ${${PREFIX}_PROCESS_INCLUDES} ${${PREFIX}_PROCESS_LIBS})
message("${i}=${${i}}")
endforeach (i)
message (FATAL_ERROR "Required library ${PREFIX} NOT FOUND.\nInstall the library (dev version) and try again. If the library is already installed, use ccmake to set the missing variables manually.")
endif (${PREFIX}_FIND_REQUIRED)
endif (${PREFIX}_FOUND)
endif (NOT ${PREFIX}_FOUND)
endmacro (libfind_process)

macro(libfind_library PREFIX basename)
set(TMP "")
if(MSVC80)
set(TMP -vc80)
endif(MSVC80)
if(MSVC90)
set(TMP -vc90)
endif(MSVC90)
set(${PREFIX}_LIBNAMES ${basename}${TMP})
if(${ARGC} GREATER 2)
set(${PREFIX}_LIBNAMES ${basename}${TMP}-${ARGV2})
string(REGEX REPLACE "\\." "_" TMP ${${PREFIX}_LIBNAMES})
set(${PREFIX}_LIBNAMES ${${PREFIX}_LIBNAMES} ${TMP})
endif(${ARGC} GREATER 2)
find_library(${PREFIX}_LIBRARY
NAMES ${${PREFIX}_LIBNAMES}
PATHS ${${PREFIX}_PKGCONF_LIBRARY_DIRS}
)
endmacro(libfind_library)

10 changes: 4 additions & 6 deletions src/zrpc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ add_library(zrpc clock.cc function_server.cc remote_response.cc
event_manager.cc
${PROTO_SOURCES})

# add_executable(zsendrpc zsendrpc.cc)
# target_link_libraries(zsendrpc zrpc zmq glog gflags ${PROTOBUF_LIBRARY}
# ${Boost_THREAD_LIBRARIES})

# add_executable(client_pool client_pool.cc connection_manager.cc zmq_utils.cc)
# target_link_libraries(client_pool zmq glog gflags protobuf)
add_executable(zsendrpc zsendrpc.cc)
target_link_libraries(zsendrpc zrpc ${ZeroMQ_LIBRARIES} ${GLOG_LIBRARIES}
${GFlags_LIBRARIES} ${PROTOBUF_LIBRARY}
${Boost_THREAD_LIBRARIES})
1 change: 0 additions & 1 deletion src/zrpc/zmq_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
// Author: [email protected] <Nadav Samet>

#include "zrpc/zmq_utils.h"
#include <i386/types.h>
#include <stddef.h>
#include <string.h>
#include <zmq.h>
Expand Down
2 changes: 1 addition & 1 deletion src/zrpc/zsendrpc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,6 @@ int main(int argc, char *argv[]) {
}

::google::protobuf::ShutdownProtobufLibrary();
::google::ShutdownGoogleLogging();
// ::google::ShutdownGoogleLogging();
return retval;
}
10 changes: 3 additions & 7 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,20 @@ include_directories(${GTEST_INCLUDE_DIRS})
include_directories(${PROJECT_BINARY_DIR}/test)
include_directories(${PROJECT_SOURCE_DIR}/src)
add_library(zrpc_gtest_main zrpc_gtest_main.cc)

function(zrpc_test TESTNAME)
CMAKE_PARSE_ARGUMENTS(OPTIONS "" "" "SRCS;LIBS" ${ARGN})
add_executable(${TESTNAME} ${OPTIONS_SRCS})
set(LIBS zrpc zmq glog gflags zrpc_gtest_main
${GTEST_LIBRARIES}
${PROTOBUF_LIBRARY}
${Boost_THREAD_LIBRARIES})
${Boost_THREAD_LIBRARIES}
${GLOG_LIBRARIES}
${GFlags_LIBRARY})
list(APPEND LIBS ${OPTIONS_LIBS})
target_link_libraries(${TESTNAME} ${LIBS})
add_test(${TESTNAME} ${TESTNAME} --logtostderr)
endfunction()

# add_executable(client_test client_test.cc)
# target_link_libraries(client_test
# search_pb zrpc glog zmq gflags)


zrpc_test(function_server_test SRCS function_server_test.cc)
zrpc_test(callback_test SRCS callback_test.cc)
zrpc_test(connection_manager_test SRCS connection_manager_test.cc)
Expand Down
2 changes: 2 additions & 0 deletions test/zrpc_gtest_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "glog/logging.h"
#include "google/protobuf/stubs/common.h"

DECLARE_bool(logtostderr);

int main(int argc, char** argv) {
::google::InstallFailureSignalHandler();
::google::ParseCommandLineFlags(&argc, &argv, true);
Expand Down

0 comments on commit fef6402

Please sign in to comment.