CMakeLists.txt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. cmake_minimum_required(VERSION 2.8.10)
  18. project(memcache_c++ C CXX)
  19. option(LINK_SO "Whether examples are linked dynamically" OFF)
  20. execute_process(
  21. COMMAND bash -c "find ${PROJECT_SOURCE_DIR}/../.. -type d -regex \".*output/include$\" | head -n1 | xargs dirname | tr -d '\n'"
  22. OUTPUT_VARIABLE OUTPUT_PATH
  23. )
  24. set(CMAKE_PREFIX_PATH ${OUTPUT_PATH})
  25. include(FindThreads)
  26. include(FindProtobuf)
  27. protobuf_generate_cpp(PROTO_SRC PROTO_HEADER echo.proto)
  28. # include PROTO_HEADER
  29. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  30. # Search for libthrift* by best effort. If it is not found and brpc is
  31. # compiled with thrift protocol enabled, a link error would be reported.
  32. find_library(THRIFT_LIB NAMES thrift)
  33. if (NOT THRIFT_LIB)
  34. set(THRIFT_LIB "")
  35. endif()
  36. find_library(THRIFTNB_LIB NAMES thriftnb)
  37. if (NOT THRIFTNB_LIB)
  38. set(THRIFTNB_LIB "")
  39. endif()
  40. find_path(BRPC_INCLUDE_PATH NAMES brpc/server.h)
  41. if(LINK_SO)
  42. find_library(BRPC_LIB NAMES brpc)
  43. else()
  44. find_library(BRPC_LIB NAMES libbrpc.a brpc)
  45. endif()
  46. if((NOT BRPC_INCLUDE_PATH) OR (NOT BRPC_LIB))
  47. message(FATAL_ERROR "Fail to find brpc")
  48. endif()
  49. include_directories(${BRPC_INCLUDE_PATH})
  50. find_path(GFLAGS_INCLUDE_PATH gflags/gflags.h)
  51. find_library(GFLAGS_LIBRARY NAMES gflags libgflags)
  52. if((NOT GFLAGS_INCLUDE_PATH) OR (NOT GFLAGS_LIBRARY))
  53. message(FATAL_ERROR "Fail to find gflags")
  54. endif()
  55. include_directories(${GFLAGS_INCLUDE_PATH})
  56. execute_process(
  57. COMMAND bash -c "grep \"namespace [_A-Za-z0-9]\\+ {\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $2}' | tr -d '\n'"
  58. OUTPUT_VARIABLE GFLAGS_NS
  59. )
  60. if(${GFLAGS_NS} STREQUAL "GFLAGS_NAMESPACE")
  61. execute_process(
  62. COMMAND bash -c "grep \"#define GFLAGS_NAMESPACE [_A-Za-z0-9]\\+\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $3}' | tr -d '\n'"
  63. OUTPUT_VARIABLE GFLAGS_NS
  64. )
  65. endif()
  66. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  67. include(CheckFunctionExists)
  68. CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME)
  69. if(NOT HAVE_CLOCK_GETTIME)
  70. set(DEFINE_CLOCK_GETTIME "-DNO_CLOCK_GETTIME_IN_MAC")
  71. endif()
  72. endif()
  73. set(CMAKE_CPP_FLAGS "${DEFINE_CLOCK_GETTIME} -DGFLAGS_NS=${GFLAGS_NS}")
  74. set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -DNDEBUG -O2 -D__const__= -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer")
  75. if(CMAKE_VERSION VERSION_LESS "3.1.3")
  76. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  77. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  78. endif()
  79. if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  80. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  81. endif()
  82. else()
  83. set(CMAKE_CXX_STANDARD 11)
  84. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  85. endif()
  86. find_path(LEVELDB_INCLUDE_PATH NAMES leveldb/db.h)
  87. find_library(LEVELDB_LIB NAMES leveldb)
  88. if ((NOT LEVELDB_INCLUDE_PATH) OR (NOT LEVELDB_LIB))
  89. message(FATAL_ERROR "Fail to find leveldb")
  90. endif()
  91. include_directories(${LEVELDB_INCLUDE_PATH})
  92. find_library(SSL_LIB NAMES ssl)
  93. if (NOT SSL_LIB)
  94. message(FATAL_ERROR "Fail to find ssl")
  95. endif()
  96. find_library(CRYPTO_LIB NAMES crypto)
  97. if (NOT CRYPTO_LIB)
  98. message(FATAL_ERROR "Fail to find crypto")
  99. endif()
  100. set(DYNAMIC_LIB
  101. ${CMAKE_THREAD_LIBS_INIT}
  102. ${GFLAGS_LIBRARY}
  103. ${PROTOBUF_LIBRARIES}
  104. ${LEVELDB_LIB}
  105. ${SSL_LIB}
  106. ${CRYPTO_LIB}
  107. ${THRIFT_LIB}
  108. ${THRIFTNB_LIB}
  109. dl
  110. )
  111. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  112. set(DYNAMIC_LIB ${DYNAMIC_LIB}
  113. pthread
  114. "-framework CoreFoundation"
  115. "-framework CoreGraphics"
  116. "-framework CoreData"
  117. "-framework CoreText"
  118. "-framework Security"
  119. "-framework Foundation"
  120. "-Wl,-U,_MallocExtension_ReleaseFreeMemory"
  121. "-Wl,-U,_ProfilerStart"
  122. "-Wl,-U,_ProfilerStop")
  123. endif()
  124. add_executable(memcache_client client.cpp)
  125. target_link_libraries(memcache_client ${BRPC_LIB} ${DYNAMIC_LIB})