CMakeLists.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. find_path(GPERFTOOLS_INCLUDE_DIR NAMES gperftools/heap-profiler.h)
  35. find_library(GPERFTOOLS_LIBRARIES NAMES tcmalloc_and_profiler)
  36. include_directories(${GPERFTOOLS_INCLUDE_DIR})
  37. # Search for libthrift* by best effort. If it is not found and brpc is
  38. # compiled with thrift protocol enabled, a link error would be reported.
  39. find_library(THRIFT_LIB NAMES thrift)
  40. if (NOT THRIFT_LIB)
  41. set(THRIFT_LIB "")
  42. endif()
  43. find_library(THRIFTNB_LIB NAMES thriftnb)
  44. if (NOT THRIFTNB_LIB)
  45. set(THRIFTNB_LIB "")
  46. endif()
  47. find_path(BRPC_INCLUDE_PATH NAMES brpc/server.h)
  48. if(LINK_SO)
  49. find_library(BRPC_LIB NAMES brpc)
  50. else()
  51. find_library(BRPC_LIB NAMES libbrpc.a brpc)
  52. endif()
  53. if((NOT BRPC_INCLUDE_PATH) OR (NOT BRPC_LIB))
  54. message(FATAL_ERROR "Fail to find brpc")
  55. endif()
  56. include_directories(${BRPC_INCLUDE_PATH})
  57. find_path(GFLAGS_INCLUDE_PATH gflags/gflags.h)
  58. find_library(GFLAGS_LIBRARY NAMES gflags libgflags)
  59. if((NOT GFLAGS_INCLUDE_PATH) OR (NOT GFLAGS_LIBRARY))
  60. message(FATAL_ERROR "Fail to find gflags")
  61. endif()
  62. include_directories(${GFLAGS_INCLUDE_PATH})
  63. execute_process(
  64. COMMAND bash -c "grep \"namespace [_A-Za-z0-9]\\+ {\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $2}' | tr -d '\n'"
  65. OUTPUT_VARIABLE GFLAGS_NS
  66. )
  67. if(${GFLAGS_NS} STREQUAL "GFLAGS_NAMESPACE")
  68. execute_process(
  69. 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'"
  70. OUTPUT_VARIABLE GFLAGS_NS
  71. )
  72. endif()
  73. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  74. include(CheckFunctionExists)
  75. CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME)
  76. if(NOT HAVE_CLOCK_GETTIME)
  77. set(DEFINE_CLOCK_GETTIME "-DNO_CLOCK_GETTIME_IN_MAC")
  78. endif()
  79. endif()
  80. set(CMAKE_CPP_FLAGS "${DEFINE_CLOCK_GETTIME} -DGFLAGS_NS=${GFLAGS_NS}")
  81. set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -DNDEBUG -O2 -D__const__= -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer")
  82. if(CMAKE_VERSION VERSION_LESS "3.1.3")
  83. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  84. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  85. endif()
  86. if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  87. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  88. endif()
  89. else()
  90. set(CMAKE_CXX_STANDARD 11)
  91. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  92. endif()
  93. find_path(LEVELDB_INCLUDE_PATH NAMES leveldb/db.h)
  94. find_library(LEVELDB_LIB NAMES leveldb)
  95. if ((NOT LEVELDB_INCLUDE_PATH) OR (NOT LEVELDB_LIB))
  96. message(FATAL_ERROR "Fail to find leveldb")
  97. endif()
  98. include_directories(${LEVELDB_INCLUDE_PATH})
  99. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  100. set(OPENSSL_ROOT_DIR
  101. "/usr/local/opt/openssl" # Homebrew installed OpenSSL
  102. )
  103. endif()
  104. find_package(OpenSSL)
  105. include_directories(${OPENSSL_INCLUDE_DIR})
  106. set(DYNAMIC_LIB
  107. ${CMAKE_THREAD_LIBS_INIT}
  108. ${GFLAGS_LIBRARY}
  109. ${PROTOBUF_LIBRARIES}
  110. ${LEVELDB_LIB}
  111. ${OPENSSL_CRYPTO_LIBRARY}
  112. ${OPENSSL_SSL_LIBRARY}
  113. ${THRIFT_LIB}
  114. ${THRIFTNB_LIB}
  115. ${GPERFTOOLS_LIBRARIES}
  116. dl
  117. )
  118. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  119. set(DYNAMIC_LIB ${DYNAMIC_LIB}
  120. pthread
  121. "-framework CoreFoundation"
  122. "-framework CoreGraphics"
  123. "-framework CoreData"
  124. "-framework CoreText"
  125. "-framework Security"
  126. "-framework Foundation"
  127. "-Wl,-U,_MallocExtension_ReleaseFreeMemory"
  128. "-Wl,-U,_ProfilerStart"
  129. "-Wl,-U,_ProfilerStop")
  130. endif()
  131. add_executable(redis_cli redis_cli.cpp)
  132. add_executable(redis_press redis_press.cpp)
  133. add_executable(redis_server redis_server.cpp)
  134. set(AUX_LIB readline ncurses)
  135. target_link_libraries(redis_cli ${BRPC_LIB} ${DYNAMIC_LIB} ${AUX_LIB})
  136. target_link_libraries(redis_press ${BRPC_LIB} ${DYNAMIC_LIB})
  137. target_link_libraries(redis_server ${BRPC_LIB} ${DYNAMIC_LIB})