|
| 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 | + |
| 18 | +cmake_minimum_required(VERSION 2.8.10) |
| 19 | +project(rpcz_echo_c++ C CXX) |
| 20 | + |
| 21 | +option(LINK_SO "Whether examples are linked dynamically" OFF) |
| 22 | + |
| 23 | +execute_process( |
| 24 | + COMMAND bash -c "find ${PROJECT_SOURCE_DIR}/../.. -type d -regex \".*output/include$\" | head -n1 | xargs dirname | tr -d '\n'" |
| 25 | + OUTPUT_VARIABLE OUTPUT_PATH |
| 26 | +) |
| 27 | + |
| 28 | +set(CMAKE_PREFIX_PATH ${OUTPUT_PATH}) |
| 29 | + |
| 30 | +include(FindThreads) |
| 31 | +include(FindProtobuf) |
| 32 | +protobuf_generate_cpp(PROTO_SRC PROTO_HEADER echo.proto) |
| 33 | +# include PROTO_HEADER |
| 34 | +include_directories(${CMAKE_CURRENT_BINARY_DIR}) |
| 35 | + |
| 36 | +# Search for libthrift* by best effort. If it is not found and brpc is |
| 37 | +# compiled with thrift protocol enabled, a link error would be reported. |
| 38 | +find_library(THRIFT_LIB NAMES thrift) |
| 39 | +if (NOT THRIFT_LIB) |
| 40 | + set(THRIFT_LIB "") |
| 41 | +endif() |
| 42 | + |
| 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 | + |
| 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 | + |
| 61 | +execute_process( |
| 62 | + COMMAND bash -c "grep \"namespace [_A-Za-z0-9]\\+ {\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $2}' | tr -d '\n'" |
| 63 | + OUTPUT_VARIABLE GFLAGS_NS |
| 64 | +) |
| 65 | +if(${GFLAGS_NS} STREQUAL "GFLAGS_NAMESPACE") |
| 66 | + execute_process( |
| 67 | + 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'" |
| 68 | + OUTPUT_VARIABLE GFLAGS_NS |
| 69 | + ) |
| 70 | +endif() |
| 71 | +if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") |
| 72 | + include(CheckFunctionExists) |
| 73 | + CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME) |
| 74 | + if(NOT HAVE_CLOCK_GETTIME) |
| 75 | + set(DEFINE_CLOCK_GETTIME "-DNO_CLOCK_GETTIME_IN_MAC") |
| 76 | + endif() |
| 77 | +endif() |
| 78 | + |
| 79 | +set(CMAKE_CPP_FLAGS "${DEFINE_CLOCK_GETTIME} -DGFLAGS_NS=${GFLAGS_NS}") |
| 80 | +set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} -DNDEBUG -O2 -D__const__=__unused__ -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer") |
| 81 | + |
| 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 | + |
| 94 | +find_path(LEVELDB_INCLUDE_PATH NAMES leveldb/db.h) |
| 95 | +find_library(LEVELDB_LIB NAMES leveldb) |
| 96 | +if ((NOT LEVELDB_INCLUDE_PATH) OR (NOT LEVELDB_LIB)) |
| 97 | + message(FATAL_ERROR "Fail to find leveldb") |
| 98 | +endif() |
| 99 | +include_directories(${LEVELDB_INCLUDE_PATH}) |
| 100 | + |
| 101 | +if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") |
| 102 | + set(OPENSSL_ROOT_DIR |
| 103 | + "/usr/local/opt/openssl" # Homebrew installed OpenSSL |
| 104 | + ) |
| 105 | +endif() |
| 106 | + |
| 107 | +find_package(OpenSSL) |
| 108 | +include_directories(${OPENSSL_INCLUDE_DIR}) |
| 109 | + |
| 110 | +set(DYNAMIC_LIB |
| 111 | + ${CMAKE_THREAD_LIBS_INIT} |
| 112 | + ${GFLAGS_LIBRARY} |
| 113 | + ${PROTOBUF_LIBRARIES} |
| 114 | + ${LEVELDB_LIB} |
| 115 | + ${OPENSSL_CRYPTO_LIBRARY} |
| 116 | + ${OPENSSL_SSL_LIBRARY} |
| 117 | + ${THRIFT_LIB} |
| 118 | + dl |
| 119 | + ) |
| 120 | + |
| 121 | +if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") |
| 122 | + set(DYNAMIC_LIB ${DYNAMIC_LIB} |
| 123 | + pthread |
| 124 | + "-framework CoreFoundation" |
| 125 | + "-framework CoreGraphics" |
| 126 | + "-framework CoreData" |
| 127 | + "-framework CoreText" |
| 128 | + "-framework Security" |
| 129 | + "-framework Foundation" |
| 130 | + "-Wl,-U,_MallocExtension_ReleaseFreeMemory" |
| 131 | + "-Wl,-U,_ProfilerStart" |
| 132 | + "-Wl,-U,_ProfilerStop" |
| 133 | + "-Wl,-U,__Z13GetStackTracePPvii") |
| 134 | +endif() |
| 135 | + |
| 136 | +add_executable(rpcz_echo_client client.cpp ${PROTO_SRC} ${PROTO_HEADER}) |
| 137 | +add_executable(rpcz_echo_server server.cpp ${PROTO_SRC} ${PROTO_HEADER}) |
| 138 | + |
| 139 | +target_link_libraries(rpcz_echo_client ${BRPC_LIB} ${DYNAMIC_LIB}) |
| 140 | +target_link_libraries(rpcz_echo_server ${BRPC_LIB} ${DYNAMIC_LIB}) |
| 141 | + |
0 commit comments