CMakeLists.txt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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(asynchronous_echo_c++ C CXX)
  19. option(LINK_SO "Whether examples are linked dynamically" OFF)
  20. execute_process(
  21. COMMAND bash -c "find ${CMAKE_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 cl_test.proto)
  28. # include PROTO_HEADER
  29. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  30. find_path(BRPC_INCLUDE_PATH NAMES brpc/server.h)
  31. if(LINK_SO)
  32. find_library(BRPC_LIB NAMES brpc)
  33. else()
  34. find_library(BRPC_LIB NAMES libbrpc.a brpc)
  35. endif()
  36. if((NOT BRPC_INCLUDE_PATH) OR (NOT BRPC_LIB))
  37. message(FATAL_ERROR "Fail to find brpc")
  38. endif()
  39. include_directories(${BRPC_INCLUDE_PATH})
  40. find_path(GFLAGS_INCLUDE_PATH gflags/gflags.h)
  41. find_library(GFLAGS_LIBRARY NAMES gflags libgflags)
  42. if((NOT GFLAGS_INCLUDE_PATH) OR (NOT GFLAGS_LIBRARY))
  43. message(FATAL_ERROR "Fail to find gflags")
  44. endif()
  45. include_directories(${GFLAGS_INCLUDE_PATH})
  46. execute_process(
  47. COMMAND bash -c "grep \"namespace [_A-Za-z0-9]\\+ {\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $2}' | tr -d '\n'"
  48. OUTPUT_VARIABLE GFLAGS_NS
  49. )
  50. if(${GFLAGS_NS} STREQUAL "GFLAGS_NAMESPACE")
  51. execute_process(
  52. 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'"
  53. OUTPUT_VARIABLE GFLAGS_NS
  54. )
  55. endif()
  56. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  57. include(CheckFunctionExists)
  58. CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME)
  59. if(NOT HAVE_CLOCK_GETTIME)
  60. set(DEFINE_CLOCK_GETTIME "-DNO_CLOCK_GETTIME_IN_MAC")
  61. endif()
  62. endif()
  63. set(CMAKE_CPP_FLAGS "${DEFINE_CLOCK_GETTIME} -DGFLAGS_NS=${GFLAGS_NS}")
  64. set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -DNDEBUG -O2 -D__const__= -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer")
  65. if(CMAKE_VERSION VERSION_LESS "3.1.3")
  66. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  67. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  68. endif()
  69. if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  70. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  71. endif()
  72. else()
  73. set(CMAKE_CXX_STANDARD 11)
  74. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  75. endif()
  76. find_path(LEVELDB_INCLUDE_PATH NAMES leveldb/db.h)
  77. find_library(LEVELDB_LIB NAMES leveldb)
  78. if ((NOT LEVELDB_INCLUDE_PATH) OR (NOT LEVELDB_LIB))
  79. message(FATAL_ERROR "Fail to find leveldb")
  80. endif()
  81. include_directories(${LEVELDB_INCLUDE_PATH})
  82. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  83. set(OPENSSL_ROOT_DIR
  84. "/usr/local/opt/openssl" # Homebrew installed OpenSSL
  85. )
  86. endif()
  87. find_package(OpenSSL)
  88. include_directories(${OPENSSL_INCLUDE_DIR})
  89. set(DYNAMIC_LIB
  90. ${CMAKE_THREAD_LIBS_INIT}
  91. ${GFLAGS_LIBRARY}
  92. ${PROTOBUF_LIBRARIES}
  93. ${LEVELDB_LIB}
  94. ${OPENSSL_CRYPTO_LIBRARY}
  95. ${OPENSSL_SSL_LIBRARY}
  96. ${THRIFT_LIB}
  97. ${THRIFTNB_LIB}
  98. dl
  99. )
  100. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
  101. set(DYNAMIC_LIB ${DYNAMIC_LIB}
  102. pthread
  103. "-framework CoreFoundation"
  104. "-framework CoreGraphics"
  105. "-framework CoreData"
  106. "-framework CoreText"
  107. "-framework Security"
  108. "-framework Foundation"
  109. "-Wl,-U,_MallocExtension_ReleaseFreeMemory"
  110. "-Wl,-U,_ProfilerStart"
  111. "-Wl,-U,_ProfilerStop"
  112. "-Wl,-U,_RegisterThriftProtocol")
  113. endif()
  114. add_executable(asynchronous_echo_client client.cpp ${PROTO_SRC})
  115. add_executable(asynchronous_echo_server server.cpp ${PROTO_SRC})
  116. target_link_libraries(asynchronous_echo_client ${BRPC_LIB} ${DYNAMIC_LIB})
  117. target_link_libraries(asynchronous_echo_server ${BRPC_LIB} ${DYNAMIC_LIB})