CMakeLists.txt 4.7 KB

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