CMakeLists.txt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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(redis_c++ C CXX)
  19. # Install dependencies:
  20. # With apt:
  21. # sudo apt-get install libreadline-dev
  22. # sudo apt-get install ncurses-dev
  23. # With yum:
  24. # sudo yum install readline-devel
  25. # sudo yum install ncurses-devel
  26. option(LINK_SO "Whether examples are linked dynamically" OFF)
  27. execute_process(
  28. COMMAND bash -c "find ${PROJECT_SOURCE_DIR}/../.. -type d -regex \".*output/include$\" | head -n1 | xargs dirname | tr -d '\n'"
  29. OUTPUT_VARIABLE OUTPUT_PATH
  30. )
  31. set(CMAKE_PREFIX_PATH ${OUTPUT_PATH})
  32. include(FindThreads)
  33. include(FindProtobuf)
  34. # Search for libthrift* by best effort. If it is not found and brpc is
  35. # compiled with thrift protocol enabled, a link error would be reported.
  36. find_library(THRIFT_LIB NAMES thrift)
  37. if (NOT THRIFT_LIB)
  38. set(THRIFT_LIB "")
  39. endif()
  40. find_library(THRIFTNB_LIB NAMES thriftnb)
  41. if (NOT THRIFTNB_LIB)
  42. set(THRIFTNB_LIB "")
  43. endif()
  44. find_path(BRPC_INCLUDE_PATH NAMES brpc/server.h)
  45. if(LINK_SO)
  46. find_library(BRPC_LIB NAMES brpc)
  47. else()
  48. find_library(BRPC_LIB NAMES libbrpc.a brpc)
  49. endif()
  50. if((NOT BRPC_INCLUDE_PATH) OR (NOT BRPC_LIB))
  51. message(FATAL_ERROR "Fail to find brpc")
  52. endif()
  53. include_directories(${BRPC_INCLUDE_PATH})
  54. find_path(GFLAGS_INCLUDE_PATH gflags/gflags.h)
  55. find_library(GFLAGS_LIBRARY NAMES gflags libgflags)
  56. if((NOT GFLAGS_INCLUDE_PATH) OR (NOT GFLAGS_LIBRARY))
  57. message(FATAL_ERROR "Fail to find gflags")
  58. endif()
  59. include_directories(${GFLAGS_INCLUDE_PATH})
  60. execute_process(
  61. COMMAND bash -c "grep \"namespace [_A-Za-z0-9]\\+ {\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $2}' | tr -d '\n'"
  62. OUTPUT_VARIABLE GFLAGS_NS
  63. )
  64. if(${GFLAGS_NS} STREQUAL "GFLAGS_NAMESPACE")
  65. execute_process(
  66. 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'"
  67. OUTPUT_VARIABLE GFLAGS_NS
  68. )
  69. endif()
  70. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  71. include(CheckFunctionExists)
  72. CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME)
  73. if(NOT HAVE_CLOCK_GETTIME)
  74. set(DEFINE_CLOCK_GETTIME "-DNO_CLOCK_GETTIME_IN_MAC")
  75. endif()
  76. endif()
  77. set(CMAKE_CPP_FLAGS "${DEFINE_CLOCK_GETTIME} -DGFLAGS_NS=${GFLAGS_NS}")
  78. set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -DNDEBUG -O2 -D__const__= -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer")
  79. if(CMAKE_VERSION VERSION_LESS "3.1.3")
  80. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  81. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  82. endif()
  83. if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  84. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  85. endif()
  86. else()
  87. set(CMAKE_CXX_STANDARD 11)
  88. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  89. endif()
  90. find_path(LEVELDB_INCLUDE_PATH NAMES leveldb/db.h)
  91. find_library(LEVELDB_LIB NAMES leveldb)
  92. if ((NOT LEVELDB_INCLUDE_PATH) OR (NOT LEVELDB_LIB))
  93. message(FATAL_ERROR "Fail to find leveldb")
  94. endif()
  95. include_directories(${LEVELDB_INCLUDE_PATH})
  96. find_library(SSL_LIB NAMES ssl)
  97. if (NOT SSL_LIB)
  98. message(FATAL_ERROR "Fail to find ssl")
  99. endif()
  100. find_library(CRYPTO_LIB NAMES crypto)
  101. if (NOT CRYPTO_LIB)
  102. message(FATAL_ERROR "Fail to find crypto")
  103. endif()
  104. set(DYNAMIC_LIB
  105. ${CMAKE_THREAD_LIBS_INIT}
  106. ${GFLAGS_LIBRARY}
  107. ${PROTOBUF_LIBRARIES}
  108. ${LEVELDB_LIB}
  109. ${SSL_LIB}
  110. ${CRYPTO_LIB}
  111. ${THRIFT_LIB}
  112. ${THRIFTNB_LIB}
  113. dl
  114. )
  115. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  116. set(DYNAMIC_LIB ${DYNAMIC_LIB}
  117. pthread
  118. "-framework CoreFoundation"
  119. "-framework CoreGraphics"
  120. "-framework CoreData"
  121. "-framework CoreText"
  122. "-framework Security"
  123. "-framework Foundation"
  124. "-Wl,-U,_MallocExtension_ReleaseFreeMemory"
  125. "-Wl,-U,_ProfilerStart"
  126. "-Wl,-U,_ProfilerStop")
  127. endif()
  128. add_executable(redis_cli redis_cli.cpp)
  129. add_executable(redis_press redis_press.cpp)
  130. set(AUX_LIB readline ncurses)
  131. target_link_libraries(redis_cli ${BRPC_LIB} ${DYNAMIC_LIB} ${AUX_LIB})
  132. target_link_libraries(redis_press ${BRPC_LIB} ${DYNAMIC_LIB} ${AUX_LIB})