From f2bafea2fe805ef619494639ba7fbc328de1a59a Mon Sep 17 00:00:00 2001 From: Dongsheng He Date: Tue, 30 Apr 2024 23:30:58 +0800 Subject: [PATCH] build: refactor example cmake (#12) details: https://github.com/ehds/mbraft/pull/12 --- .github/workflows/build.yml | 10 ++- CMakeLists.txt | 27 ++++-- example/CMakeLists.txt | 3 + example/README.md | 12 ++- example/atomic/CMakeLists.txt | 158 ++++----------------------------- example/atomic/run_client.sh | 2 +- example/atomic/run_server.sh | 2 +- example/block/CMakeLists.txt | 143 ++++------------------------- example/block/run_client.sh | 2 +- example/block/run_server.sh | 2 +- example/counter/CMakeLists.txt | 141 +++-------------------------- example/counter/run_client.sh | 2 +- example/counter/run_server.sh | 2 +- test/CMakeLists.txt | 9 -- tools/CMakeLists.txt | 18 +--- 15 files changed, 99 insertions(+), 434 deletions(-) create mode 100644 example/CMakeLists.txt diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bfdc737..d76c795 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,5 +1,11 @@ name: ci -on: [push, pull_request] +on: + push: + branches: + - main + pull_request: + branches: + - main permissions: contents: read @@ -66,7 +72,7 @@ jobs: - name: Build with cmake if: ${{ matrix.build_tool == 'cmake' }} run: | - cmake -S "${{ github.workspace }}" -B "${{ env.CMAKE_BUILD_DIR }}" -DWITH_TESTS=ON -DWITH_COVERAGE=ON + cmake -S "${{ github.workspace }}" -B "${{ env.CMAKE_BUILD_DIR }}" -DWITH_TESTS=ON -DWITH_COVERAGE=ON -DWITH_EXAMPLES=ON cmake --build bld --parallel 16 -- brpc-static cmake --build "${{ env.CMAKE_BUILD_DIR }}" --parallel 16 diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a16b35..16ab8b2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,17 @@ option(WITH_DEBUG_SYMBOLS "With debug symbols" ON) # https://cmake.org/cmake/help/latest/policy/CMP0058.html # suppress this CMP0058 warning cmake_policy(SET CMP0058 OLD) + +option(WITH_EXAMPLES "Whether to build examples" OFF) option(WITH_TESTS "Whether to build unit tests" OFF) option(WITH_COVERAGE "Whether build with coverage report" OFF) +set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) + +include(FindThreads) +include(FindProtobuf) +include(thirdparty/brpc/SetupBrpc) + set(WITH_GLOG_VAL "0") if(BRPC_WITH_GLOG) set(WITH_GLOG_VAL "1") @@ -24,11 +32,13 @@ if(WITH_DEBUG_SYMBOLS) set(DEBUG_SYMBOL "-g") endif() -set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) - -include(FindThreads) -include(FindProtobuf) -include(thirdparty/brpc/SetupBrpc) +if (WITH_COVERAGE) + if(CMAKE_C_COMPILER_ID STREQUAL "GNU") + link_libraries(gcov) + else() + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage") + endif() +endif() if ((NOT BRPC_INCLUDE_PATH) OR (NOT BRPC_LIB)) message(FATAL_ERROR "Fail to find brpc") @@ -233,7 +243,12 @@ add_subdirectory(src) if(WITH_TESTS) add_subdirectory(test) endif() -# add_subdirectory(tools) + +if(WITH_EXAMPLES) + add_subdirectory(example) +endif() + +add_subdirectory(tools) file(COPY ${CMAKE_CURRENT_BINARY_DIR}/braft/ DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/output/include/braft/ diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt new file mode 100644 index 0000000..218819f --- /dev/null +++ b/example/CMakeLists.txt @@ -0,0 +1,3 @@ +add_subdirectory(atomic) +add_subdirectory(block) +add_subdirectory(counter) \ No newline at end of file diff --git a/example/README.md b/example/README.md index 1a242e7..c592140 100644 --- a/example/README.md +++ b/example/README.md @@ -7,13 +7,16 @@ Brief introduction of examples: # Build steps ```shell -example=counter|atomic|block -cd $example && cmake . && make +cmake -B build -DWITH_EXAMPLES=ON -GNinja +ninja -C build all ``` # Run Server ```sh +# example = atomic/block/server. +export example=atomic +cd build/example/${example}/ bash run_server.sh ``` @@ -30,3 +33,8 @@ bash run_client.sh * If server_num of run_server.sh has been changed, specify run_client to make it consistent. * Add `--log_each_request` if you want detailed information of each request. +# Stop + +```sh +bash stop.sh +``` \ No newline at end of file diff --git a/example/atomic/CMakeLists.txt b/example/atomic/CMakeLists.txt index dbbb96d..867c482 100644 --- a/example/atomic/CMakeLists.txt +++ b/example/atomic/CMakeLists.txt @@ -1,148 +1,28 @@ -cmake_minimum_required(VERSION 2.8.10) -project(atomic C CXX) - -option(EXAMPLE_LINK_SO "Whether examples are linked dynamically" OFF) -option(LINK_TCMALLOC "Link tcmalloc if possible" ON) - -execute_process( - COMMAND bash -c "find ${CMAKE_SOURCE_DIR}/../.. -type d -path \"*output/include/braft\" | xargs dirname | xargs dirname | tr -d '\n'" - OUTPUT_VARIABLE OUTPUT_PATH -) - -set(CMAKE_PREFIX_PATH ${OUTPUT_PATH}) - -include(FindThreads) -include(FindProtobuf) - -if (NOT PROTOBUF_PROTOC_EXECUTABLE) - get_filename_component(PROTO_LIB_DIR ${PROTOBUF_LIBRARY} DIRECTORY) - set (PROTOBUF_PROTOC_EXECUTABLE "${PROTO_LIB_DIR}/../bin/protoc") -endif() protobuf_generate_cpp(PROTO_SRC PROTO_HEADER atomic.proto) -# include PROTO_HEADER include_directories(${CMAKE_CURRENT_BINARY_DIR}) -find_path(BRPC_INCLUDE_PATH NAMES brpc/server.h) -if(EXAMPLE_LINK_SO) - find_library(BRPC_LIB NAMES brpc) - find_library(BRAFT_LIB NAMES braft) -else() - find_library(BRPC_LIB NAMES libbrpc.a brpc) - find_library(BRAFT_LIB NAMES libbraft.a braft) -endif() - -if((NOT BRPC_INCLUDE_PATH) OR (NOT BRPC_LIB)) - message(FATAL_ERROR "Fail to find brpc") -endif() -include_directories(${BRPC_INCLUDE_PATH}) - -find_path(BRAFT_INCLUDE_PATH NAMES braft/raft.h) -if ((NOT BRAFT_INCLUDE_PATH) OR (NOT BRAFT_LIB)) - message (FATAL_ERROR "Fail to find braft") -endif() -include_directories(${BRAFT_INCLUDE_PATH}) - -find_path(GFLAGS_INCLUDE_PATH gflags/gflags.h) -find_library(GFLAGS_LIBRARY NAMES gflags libgflags) -if((NOT GFLAGS_INCLUDE_PATH) OR (NOT GFLAGS_LIBRARY)) - message(FATAL_ERROR "Fail to find gflags") -endif() -include_directories(${GFLAGS_INCLUDE_PATH}) - -find_path(GLOG_INCLUDE_PATH glog/logging.h) -find_library(GLOG_LIBRARY NAMES glog libglog) -if((NOT GLOG_INCLUDE_PATH) OR (NOT GLOG_LIBRARY)) - message(FATAL_ERROR "Fail to find glog") -endif() -include_directories(${GLOG_INCLUDE_PATH}) - -execute_process( - COMMAND bash -c "grep \"namespace [_A-Za-z0-9]\\+ {\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $2}' | tr -d '\n'" - OUTPUT_VARIABLE GFLAGS_NS -) -if(${GFLAGS_NS} STREQUAL "GFLAGS_NAMESPACE") - execute_process( - 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'" - OUTPUT_VARIABLE GFLAGS_NS - ) -endif() - -if (LINK_TCMALLOC) - find_path(GPERFTOOLS_INCLUDE_DIR NAMES gperftools/heap-profiler.h) - find_library(GPERFTOOLS_LIBRARIES NAMES tcmalloc_and_profiler) - if (GPERFTOOLS_INCLUDE_DIR AND GPERFTOOLS_LIBRARIES) - set(CMAKE_CXX_FLAGS "-DBRPC_ENABLE_CPU_PROFILER") - include_directories(${GPERFTOOLS_INCLUDE_DIR}) - else () - set (GPERFTOOLS_LIBRARIES "") - endif () -endif () - -set(CMAKE_CPP_FLAGS "-DGFLAGS_NS=${GFLAGS_NS}") -set(CMAKE_CXX_FLAGS "${CMAKE_CPP_FLAGS} ${CMAKE_CXX_FLAGS} -DNDEBUG -O2 -D__const__=__unused__ -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer") -if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - # require at least gcc 4.8 - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8) - message(FATAL_ERROR "GCC is too old, please install a newer version supporting C++11") - endif() -elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - # require at least clang 3.3 - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.3) - message(FATAL_ERROR "Clang is too old, please install a newer version supporting C++11") - endif() -else() - message(WARNING "You are using an unsupported compiler! Compilation has only been tested with Clang and GCC.") -endif() - -if(CMAKE_VERSION VERSION_LESS "3.1.3") - if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - endif() - if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - endif() -else() - set(CMAKE_CXX_STANDARD 11) - set(CMAKE_CXX_STANDARD_REQUIRED ON) -endif() - -find_path(LEVELDB_INCLUDE_PATH NAMES leveldb/db.h) -find_library(LEVELDB_LIB NAMES leveldb) -if ((NOT LEVELDB_INCLUDE_PATH) OR (NOT LEVELDB_LIB)) - message(FATAL_ERROR "Fail to find leveldb") -endif() -include_directories(${LEVELDB_INCLUDE_PATH}) - add_executable(atomic_client client.cpp ${PROTO_SRC} ${PROTO_HEADER}) add_executable(atomic_server server.cpp ${PROTO_SRC} ${PROTO_HEADER}) add_executable(atomic_test test.cpp ${PROTO_SRC} ${PROTO_HEADER}) -set(DYNAMIC_LIB - ${CMAKE_THREAD_LIBS_INIT} - ${GFLAGS_LIBRARY} - ${GLOG_LIBRARY} - ${PROTOBUF_LIBRARY} - ${GPERFTOOLS_LIBRARIES} - ${LEVELDB_LIB} - ${BRAFT_LIB} - ${BRPC_LIB} - rt - ssl - crypto - dl - z - ) +target_link_libraries(atomic_client braft-static) +target_link_libraries(atomic_server braft-static) +target_link_libraries(atomic_test braft-static) + +message("--- ${CMAKE_CURRENT_BINARY_DIR}") +message("--- ${CMAKE_CURRENT_SOURCE_DIR}") +message("--- ${CMAKE_CURRENT_LIST_DIR}") + +# Copy start/stop scripts +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/ + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ + FILES_MATCHING + PATTERN "run_client.sh" + PATTERN "run_server.sh" + PATTERN "stop.sh" +) -target_link_libraries(atomic_client - "-Xlinker \"-(\"" - ${DYNAMIC_LIB} - "-Xlinker \"-)\"") -target_link_libraries(atomic_server - "-Xlinker \"-(\"" - ${DYNAMIC_LIB} - "-Xlinker \"-)\"") -target_link_libraries(atomic_test - "-Xlinker \"-(\"" - ${DYNAMIC_LIB} - "-Xlinker \"-)\"") +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../shflags + DESTINATION ${CMAKE_CURRENT_BINARY_DIR} +) \ No newline at end of file diff --git a/example/atomic/run_client.sh b/example/atomic/run_client.sh index 3f266cb..cdc6987 100755 --- a/example/atomic/run_client.sh +++ b/example/atomic/run_client.sh @@ -17,7 +17,7 @@ # source shflags from current directory mydir="${BASH_SOURCE%/*}" if [[ ! -d "$mydir" ]]; then mydir="$PWD"; fi -. $mydir/../shflags +. $mydir/shflags # define command-line flags diff --git a/example/atomic/run_server.sh b/example/atomic/run_server.sh index baa2511..0eb5229 100755 --- a/example/atomic/run_server.sh +++ b/example/atomic/run_server.sh @@ -17,7 +17,7 @@ # source shflags from current directory mydir="${BASH_SOURCE%/*}" if [[ ! -d "$mydir" ]]; then mydir="$PWD"; fi -. $mydir/../shflags +. $mydir/shflags # define command-line flags DEFINE_string crash_on_fatal 'true' 'Crash on fatal log' diff --git a/example/block/CMakeLists.txt b/example/block/CMakeLists.txt index f71e166..d79aecc 100644 --- a/example/block/CMakeLists.txt +++ b/example/block/CMakeLists.txt @@ -1,135 +1,22 @@ -cmake_minimum_required(VERSION 2.8.10) -project(block C CXX) - -option(EXAMPLE_LINK_SO "Whether examples are linked dynamically" OFF) -option(LINK_TCMALLOC "Link tcmalloc if possible" ON) - -execute_process( - COMMAND bash -c "find ${CMAKE_SOURCE_DIR}/../.. -type d -path \"*output/include/braft\" | xargs dirname | xargs dirname | tr -d '\n'" - OUTPUT_VARIABLE OUTPUT_PATH -) - -set(CMAKE_PREFIX_PATH ${OUTPUT_PATH}) - -include(FindThreads) -include(FindProtobuf) - -if (NOT PROTOBUF_PROTOC_EXECUTABLE) - get_filename_component(PROTO_LIB_DIR ${PROTOBUF_LIBRARY} DIRECTORY) - set (PROTOBUF_PROTOC_EXECUTABLE "${PROTO_LIB_DIR}/../bin/protoc") -endif() - protobuf_generate_cpp(PROTO_SRC PROTO_HEADER block.proto) -# include PROTO_HEADER include_directories(${CMAKE_CURRENT_BINARY_DIR}) -find_path(BRPC_INCLUDE_PATH NAMES brpc/server.h) -if(EXAMPLE_LINK_SO) - find_library(BRPC_LIB NAMES brpc) - find_library(BRAFT_LIB NAMES braft) -else() - find_library(BRPC_LIB NAMES libbrpc.a brpc) - find_library(BRAFT_LIB NAMES libbraft.a braft) -endif() - -if((NOT BRPC_INCLUDE_PATH) OR (NOT BRPC_LIB)) - message(FATAL_ERROR "Fail to find brpc") -endif() -include_directories(${BRPC_INCLUDE_PATH}) - -find_path(BRAFT_INCLUDE_PATH NAMES braft/raft.h) -if ((NOT BRAFT_INCLUDE_PATH) OR (NOT BRAFT_LIB)) - message (FATAL_ERROR "Fail to find braft") -endif() -include_directories(${BRAFT_INCLUDE_PATH}) - -find_path(GFLAGS_INCLUDE_PATH gflags/gflags.h) -find_library(GFLAGS_LIBRARY NAMES gflags libgflags) -if((NOT GFLAGS_INCLUDE_PATH) OR (NOT GFLAGS_LIBRARY)) - message(FATAL_ERROR "Fail to find gflags") -endif() -include_directories(${GFLAGS_INCLUDE_PATH}) - -execute_process( - COMMAND bash -c "grep \"namespace [_A-Za-z0-9]\\+ {\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $2}' | tr -d '\n'" - OUTPUT_VARIABLE GFLAGS_NS -) -if(${GFLAGS_NS} STREQUAL "GFLAGS_NAMESPACE") - execute_process( - 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'" - OUTPUT_VARIABLE GFLAGS_NS - ) -endif() - -if (LINK_TCMALLOC) - find_path(GPERFTOOLS_INCLUDE_DIR NAMES gperftools/heap-profiler.h) - find_library(GPERFTOOLS_LIBRARIES NAMES tcmalloc_and_profiler) - if (GPERFTOOLS_INCLUDE_DIR AND GPERFTOOLS_LIBRARIES) - set(CMAKE_CXX_FLAGS "-DBRPC_ENABLE_CPU_PROFILER") - include_directories(${GPERFTOOLS_INCLUDE_DIR}) - else () - set (GPERFTOOLS_LIBRARIES "") - endif () -endif () - -set(CMAKE_CPP_FLAGS "-DGFLAGS_NS=${GFLAGS_NS}") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CPP_FLAGS} -DNDEBUG -O2 -D__const__=__unused__ -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer") -if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - # require at least gcc 4.8 - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8) - message(FATAL_ERROR "GCC is too old, please install a newer version supporting C++11") - endif() -elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - # require at least clang 3.3 - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.3) - message(FATAL_ERROR "Clang is too old, please install a newer version supporting C++11") - endif() -else() - message(WARNING "You are using an unsupported compiler! Compilation has only been tested with Clang and GCC.") -endif() - -if(CMAKE_VERSION VERSION_LESS "3.1.3") - if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - endif() - if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - endif() -else() - set(CMAKE_CXX_STANDARD 11) - set(CMAKE_CXX_STANDARD_REQUIRED ON) -endif() - -find_path(LEVELDB_INCLUDE_PATH NAMES leveldb/db.h) -find_library(LEVELDB_LIB NAMES leveldb) -if ((NOT LEVELDB_INCLUDE_PATH) OR (NOT LEVELDB_LIB)) - message(FATAL_ERROR "Fail to find leveldb") -endif() -include_directories(${LEVELDB_INCLUDE_PATH}) - add_executable(block_client client.cpp ${PROTO_SRC} ${PROTO_HEADER}) add_executable(block_server server.cpp ${PROTO_SRC} ${PROTO_HEADER}) -set(DYNAMIC_LIB - ${CMAKE_THREAD_LIBS_INIT} - ${GFLAGS_LIBRARY} - ${PROTOBUF_LIBRARY} - ${GPERFTOOLS_LIBRARIES} - ${LEVELDB_LIB} - ${BRAFT_LIB} - ${BRPC_LIB} - rt - ssl - crypto - dl - z - ) +target_link_libraries(block_server braft-static) +target_link_libraries(block_client braft-static) + +# Copy start/stop scripts +# Copy start/stop scripts +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/ + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ + FILES_MATCHING + PATTERN "run_client.sh" + PATTERN "run_server.sh" + PATTERN "stop.sh" +) -target_link_libraries(block_client - "-Xlinker \"-(\"" - ${DYNAMIC_LIB} - "-Xlinker \"-)\"") -target_link_libraries(block_server - "-Xlinker \"-(\"" - ${DYNAMIC_LIB} - "-Xlinker \"-)\"") +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../shflags + DESTINATION ${CMAKE_CURRENT_BINARY_DIR} +) \ No newline at end of file diff --git a/example/block/run_client.sh b/example/block/run_client.sh index e6e1ff7..4f00b9e 100644 --- a/example/block/run_client.sh +++ b/example/block/run_client.sh @@ -17,7 +17,7 @@ # source shflags from current directory mydir="${BASH_SOURCE%/*}" if [[ ! -d "$mydir" ]]; then mydir="$PWD"; fi -. $mydir/../shflags +. $mydir/shflags # define command-line flags diff --git a/example/block/run_server.sh b/example/block/run_server.sh index d2cfa58..4d073e8 100644 --- a/example/block/run_server.sh +++ b/example/block/run_server.sh @@ -17,7 +17,7 @@ # source shflags from current directory mydir="${BASH_SOURCE%/*}" if [[ ! -d "$mydir" ]]; then mydir="$PWD"; fi -. $mydir/../shflags +. $mydir/shflags # define command-line flags DEFINE_string crash_on_fatal 'true' 'Crash on fatal log' diff --git a/example/counter/CMakeLists.txt b/example/counter/CMakeLists.txt index 42785c8..e95a906 100644 --- a/example/counter/CMakeLists.txt +++ b/example/counter/CMakeLists.txt @@ -1,134 +1,21 @@ -cmake_minimum_required(VERSION 2.8.10) -project(counter C CXX) - -option(EXAMPLE_LINK_SO "Whether examples are linked dynamically" OFF) -option(LINK_TCMALLOC "Link tcmalloc if possible" ON) - -execute_process( - COMMAND bash -c "find ${CMAKE_SOURCE_DIR}/../.. -type d -path \"*output/include/braft\" | xargs dirname | xargs dirname | tr -d '\n'" - OUTPUT_VARIABLE OUTPUT_PATH -) - -set(CMAKE_PREFIX_PATH ${OUTPUT_PATH}) - -include(FindThreads) -include(FindProtobuf) - -if (NOT PROTOBUF_PROTOC_EXECUTABLE) - get_filename_component(PROTO_LIB_DIR ${PROTOBUF_LIBRARY} DIRECTORY) - set (PROTOBUF_PROTOC_EXECUTABLE "${PROTO_LIB_DIR}/../bin/protoc") -endif() - protobuf_generate_cpp(PROTO_SRC PROTO_HEADER counter.proto) -# include PROTO_HEADER include_directories(${CMAKE_CURRENT_BINARY_DIR}) -find_path(BRPC_INCLUDE_PATH NAMES brpc/server.h) -if(EXAMPLE_LINK_SO) - find_library(BRPC_LIB NAMES brpc) - find_library(BRAFT_LIB NAMES braft) -else() - find_library(BRPC_LIB NAMES libbrpc.a brpc) - find_library(BRAFT_LIB NAMES libbraft.a braft) -endif() - -if((NOT BRPC_INCLUDE_PATH) OR (NOT BRPC_LIB)) - message(FATAL_ERROR "Fail to find brpc") -endif() -include_directories(${BRPC_INCLUDE_PATH}) - -find_path(BRAFT_INCLUDE_PATH NAMES braft/raft.h) -if ((NOT BRAFT_INCLUDE_PATH) OR (NOT BRAFT_LIB)) - message (FATAL_ERROR "Fail to find braft") -endif() -include_directories(${BRAFT_INCLUDE_PATH}) - -find_path(GFLAGS_INCLUDE_PATH gflags/gflags.h) -find_library(GFLAGS_LIBRARY NAMES gflags libgflags) -if((NOT GFLAGS_INCLUDE_PATH) OR (NOT GFLAGS_LIBRARY)) - message(FATAL_ERROR "Fail to find gflags") -endif() -include_directories(${GFLAGS_INCLUDE_PATH}) - -execute_process( - COMMAND bash -c "grep \"namespace [_A-Za-z0-9]\\+ {\" ${GFLAGS_INCLUDE_PATH}/gflags/gflags_declare.h | head -1 | awk '{print $2}' | tr -d '\n'" - OUTPUT_VARIABLE GFLAGS_NS -) -if(${GFLAGS_NS} STREQUAL "GFLAGS_NAMESPACE") - execute_process( - 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'" - OUTPUT_VARIABLE GFLAGS_NS - ) -endif() - -if (LINK_TCMALLOC) - find_path(GPERFTOOLS_INCLUDE_DIR NAMES gperftools/heap-profiler.h) - find_library(GPERFTOOLS_LIBRARIES NAMES tcmalloc_and_profiler) - if (GPERFTOOLS_INCLUDE_DIR AND GPERFTOOLS_LIBRARIES) - set(CMAKE_CXX_FLAGS "-DBRPC_ENABLE_CPU_PROFILER") - include_directories(${GPERFTOOLS_INCLUDE_DIR}) - else () - set (GPERFTOOLS_LIBRARIES "") - endif () -endif () - -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CPP_FLAGS} -DGFLAGS_NS=${GFLAGS_NS} -DNDEBUG -O2 -D__const__=__unused__ -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer") -if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - # require at least gcc 4.8 - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8) - message(FATAL_ERROR "GCC is too old, please install a newer version supporting C++11") - endif() -elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - # require at least clang 3.3 - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.3) - message(FATAL_ERROR "Clang is too old, please install a newer version supporting C++11") - endif() -else() - message(WARNING "You are using an unsupported compiler! Compilation has only been tested with Clang and GCC.") -endif() - -if(CMAKE_VERSION VERSION_LESS "3.1.3") - if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - endif() - if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") - endif() -else() - set(CMAKE_CXX_STANDARD 11) - set(CMAKE_CXX_STANDARD_REQUIRED ON) -endif() - -find_path(LEVELDB_INCLUDE_PATH NAMES leveldb/db.h) -find_library(LEVELDB_LIB NAMES leveldb) -if ((NOT LEVELDB_INCLUDE_PATH) OR (NOT LEVELDB_LIB)) - message(FATAL_ERROR "Fail to find leveldb") -endif() -include_directories(${LEVELDB_INCLUDE_PATH}) - add_executable(counter_client client.cpp ${PROTO_SRC} ${PROTO_HEADER}) add_executable(counter_server server.cpp ${PROTO_SRC} ${PROTO_HEADER}) -set(DYNAMIC_LIB - ${CMAKE_THREAD_LIBS_INIT} - ${GFLAGS_LIBRARY} - ${PROTOBUF_LIBRARY} - ${GPERFTOOLS_LIBRARIES} - ${LEVELDB_LIB} - ${BRAFT_LIB} - ${BRPC_LIB} - rt - ssl - crypto - dl - z - ) +target_link_libraries(counter_server braft-static) +target_link_libraries(counter_client braft-static) + +# Copy start/stop scripts +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/ + DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/ + FILES_MATCHING + PATTERN "run_client.sh" + PATTERN "run_server.sh" + PATTERN "stop.sh" +) -target_link_libraries(counter_client - "-Xlinker \"-(\"" - ${DYNAMIC_LIB} - "-Xlinker \"-)\"") -target_link_libraries(counter_server - "-Xlinker \"-(\"" - ${DYNAMIC_LIB} - "-Xlinker \"-)\"") +file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../shflags + DESTINATION ${CMAKE_CURRENT_BINARY_DIR} +) \ No newline at end of file diff --git a/example/counter/run_client.sh b/example/counter/run_client.sh index 9646c8d..b3bf814 100644 --- a/example/counter/run_client.sh +++ b/example/counter/run_client.sh @@ -17,7 +17,7 @@ # source shflags from current directory mydir="${BASH_SOURCE%/*}" if [[ ! -d "$mydir" ]]; then mydir="$PWD"; fi -. $mydir/../shflags +. $mydir/shflags # define command-line flags diff --git a/example/counter/run_server.sh b/example/counter/run_server.sh index 9a5d815..7e58df2 100644 --- a/example/counter/run_server.sh +++ b/example/counter/run_server.sh @@ -17,7 +17,7 @@ # source shflags from current directory mydir="${BASH_SOURCE%/*}" if [[ ! -d "$mydir" ]]; then mydir="$PWD"; fi -. $mydir/../shflags +. $mydir/shflags # define command-line flags DEFINE_string crash_on_fatal 'true' 'Crash on fatal log' diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 244ff95..27ca024 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -9,15 +9,6 @@ set(CMAKE_CPP_FLAGS "-DGFLAGS_NS=${GFLAGS_NS}") set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -D__const__=__unused__ -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DUNIT_TEST -g -Dprivate=public -Dprotected=public -D__STRICT_ANSI__ -include sstream_workaround.h") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CPP_FLAGS} -pipe -Wall -W -fPIC -fstrict-aliasing -Wno-invalid-offsetof -Wno-unused-parameter -fno-omit-frame-pointer -Wno-unused-result") -if (WITH_COVERAGE) - if(CMAKE_C_COMPILER_ID STREQUAL "GNU") - link_libraries(gcov) - else() - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage") - endif() - -endif() - # bthread_* functions are used in logging.cc, and they need to be marked as # weak symbols explicitly in Darwin system. if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 89aa7e4..438330d 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,19 +1,7 @@ -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG -O2 -D__const__=__unused__ -pipe -W -Wall -Wno-unused-parameter -fPIC -fno-omit-frame-pointer") - set(EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/output/bin) include_directories(${CMAKE_CURRENT_BINARY_DIR}) + add_executable(braft_cli braft_cli.cpp) -if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") -target_link_libraries(braft_cli - braft-static - ${DYNAMIC_LIB} - ) -else() -target_link_libraries(braft_cli - "-Xlinker \"-(\"" - braft-static - ${DYNAMIC_LIB} - "-Xlinker \"-)\"" - ) -endif() \ No newline at end of file +target_link_libraries(braft_cli braft-static) + \ No newline at end of file