CMakeLists.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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(parallel_echo_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(GPERFTOOLS_INCLUDE_DIR NAMES gperftools/heap-profiler.h)
  41. find_library(GPERFTOOLS_LIBRARIES NAMES tcmalloc_and_profiler)
  42. include_directories(${GPERFTOOLS_INCLUDE_DIR})
  43. find_path(BRPC_INCLUDE_PATH NAMES brpc/server.h)
  44. if(LINK_SO)
  45. find_library(BRPC_LIB NAMES brpc)
  46. else()
  47. find_library(BRPC_LIB NAMES libbrpc.a brpc)
  48. endif()
  49. if((NOT BRPC_INCLUDE_PATH) OR (NOT BRPC_LIB))
  50. message(FATAL_ERROR "Fail to find brpc")
  51. endif()
  52. include_directories(${BRPC_INCLUDE_PATH})
  53. find_path(GFLAGS_INCLUDE_PATH gflags/gflags.h)
  54. find_library(GFLAGS_LIBRARY NAMES gflags libgflags)
  55. if((NOT GFLAGS_INCLUDE_PATH) OR (NOT GFLAGS_LIBRARY))
  56. message(FATAL_ERROR "Fail to find gflags")
  57. endif()
  58. include_directories(${GFLAGS_INCLUDE_PATH})
  59. execute_process(
  60. COMMAND bash -c "grep \"namespace [_A-Za-z0-9]\\+ {\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $2}' | tr -d '\n'"
  61. OUTPUT_VARIABLE GFLAGS_NS
  62. )
  63. if(${GFLAGS_NS} STREQUAL "GFLAGS_NAMESPACE")
  64. execute_process(
  65. 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'"
  66. OUTPUT_VARIABLE GFLAGS_NS
  67. )
  68. endif()
  69. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  70. include(CheckFunctionExists)
  71. CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME)
  72. if(NOT HAVE_CLOCK_GETTIME)
  73. set(DEFINE_CLOCK_GETTIME "-DNO_CLOCK_GETTIME_IN_MAC")
  74. endif()
  75. endif()
  76. set(CMAKE_CPP_FLAGS "${DEFINE_CLOCK_GETTIME} -DGFLAGS_NS=${GFLAGS_NS}")
  77. set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -DNDEBUG -O2 -D__const__= -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer")
  78. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBRPC_ENABLE_CPU_PROFILER")
  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. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  97. set(OPENSSL_ROOT_DIR
  98. "/usr/local/opt/openssl" # Homebrew installed OpenSSL
  99. )
  100. endif()
  101. find_package(OpenSSL)
  102. include_directories(${OPENSSL_INCLUDE_DIR})
  103. set(DYNAMIC_LIB
  104. ${CMAKE_THREAD_LIBS_INIT}
  105. ${GFLAGS_LIBRARY}
  106. ${PROTOBUF_LIBRARIES}
  107. ${LEVELDB_LIB}
  108. ${OPENSSL_CRYPTO_LIBRARY}
  109. ${OPENSSL_SSL_LIBRARY}
  110. ${THRIFT_LIB}
  111. ${THRIFTNB_LIB}
  112. dl
  113. )
  114. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  115. set(DYNAMIC_LIB ${DYNAMIC_LIB}
  116. pthread
  117. "-framework CoreFoundation"
  118. "-framework CoreGraphics"
  119. "-framework CoreData"
  120. "-framework CoreText"
  121. "-framework Security"
  122. "-framework Foundation"
  123. "-Wl,-U,_MallocExtension_ReleaseFreeMemory"
  124. "-Wl,-U,_ProfilerStart"
  125. "-Wl,-U,_ProfilerStop")
  126. endif()
  127. add_executable(parallel_echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER})
  128. add_executable(parallel_echo_server server.cpp ${PROTO_SRC} ${PROTO_HEADER})
  129. target_link_libraries(parallel_echo_client ${BRPC_LIB} ${DYNAMIC_LIB} ${GPERFTOOLS_LIBRARIES})
  130. target_link_libraries(parallel_echo_server ${BRPC_LIB} ${DYNAMIC_LIB} ${GPERFTOOLS_LIBRARIES})