diff --git a/CMakeLists.txt b/CMakeLists.txt index 85881a2eec..66cc015783 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,7 +66,10 @@ endif() if(WITH_THRIFT) set(THRIFT_CPP_FLAG "-DENABLE_THRIFT_FRAMED_PROTOCOL") - set(THRIFT_LIB "thrift") + find_library(THRIFT_LIB NAMES thrift) + if (NOT THRIFT_LIB) + message(FATAL_ERROR "Fail to find Thrift") + endif() endif() set(WITH_RDMA_VAL "0") diff --git a/config_brpc.sh b/config_brpc.sh index eabfda3f20..a660171c4a 100755 --- a/config_brpc.sh +++ b/config_brpc.sh @@ -352,6 +352,16 @@ if [ $WITH_THRIFT != 0 ]; then else append_to_output "STATIC_LINKINGS+=-lthriftnb" fi + # get thrift version + thrift_version=$(thrift --version | awk '{print $3}') + major=$(echo "$thrift_version" | awk -F '.' '{print $1}') + minor=$(echo "$thrift_version" | awk -F '.' '{print $2}') + if [ $((major)) -eq 0 -a $((minor)) -lt 11 ]; then + CPPFLAGS="${CPPFLAGS} -D_THRIFT_VERSION_LOWER_THAN_0_11_0_" + echo "less" + else + echo "greater" + fi fi if [ $WITH_RDMA != 0 ]; then diff --git a/example/thrift_extension_c++/native_client.cpp b/example/thrift_extension_c++/native_client.cpp index f157a12ef1..29d9e5c0fd 100644 --- a/example/thrift_extension_c++/native_client.cpp +++ b/example/thrift_extension_c++/native_client.cpp @@ -27,12 +27,15 @@ #include // _THRIFT_STDCXX_H_ is defined by thrift/stdcxx.h which was added since thrift 0.11.0 +// but deprecated after 0.13.0 #ifndef THRIFT_STDCXX #if defined(_THRIFT_STDCXX_H_) # define THRIFT_STDCXX apache::thrift::stdcxx - #else + #elif defined(_THRIFT_VERSION_LOWER_THAN_0_11_0_) # define THRIFT_STDCXX boost # include + #else + # define THRIFT_STDCXX std #endif #endif diff --git a/example/thrift_extension_c++/native_server.cpp b/example/thrift_extension_c++/native_server.cpp index c501a4931f..1ba3d1d4fd 100755 --- a/example/thrift_extension_c++/native_server.cpp +++ b/example/thrift_extension_c++/native_server.cpp @@ -28,17 +28,23 @@ #include #include #include -#include // _THRIFT_STDCXX_H_ is defined by thrift/stdcxx.h which was added since thrift 0.11.0 +// but deprecated after 0.13.0, PosixThreadFactory was also deprecated in 0.13.0 #include // to include stdcxx.h if present #ifndef THRIFT_STDCXX #if defined(_THRIFT_STDCXX_H_) # define THRIFT_STDCXX apache::thrift::stdcxx #include - #else + #include + #elif defined(_THRIFT_VERSION_LOWER_THAN_0_11_0_) # define THRIFT_STDCXX boost - # include + #include + #include + #else + # define THRIFT_STDCXX std + #include + #include #endif #endif @@ -61,10 +67,17 @@ int main(int argc, char *argv[]) { google::ParseCommandLineFlags(&argc, &argv, true); THRIFT_STDCXX::shared_ptr handler(new EchoServiceHandler()); +#if THRIFT_STDCXX != std + // For thrift version less than 0.13.0 THRIFT_STDCXX::shared_ptr thread_factory( new apache::thrift::concurrency::PosixThreadFactory( apache::thrift::concurrency::PosixThreadFactory::ROUND_ROBIN, apache::thrift::concurrency::PosixThreadFactory::NORMAL, 1, false)); +#else + // For thrift version greater equal than 0.13.0 + THRIFT_STDCXX::shared_ptr thread_factory( + new apache::thrift::concurrency::ThreadFactory(false)); +#endif THRIFT_STDCXX::shared_ptr processor( new example::EchoServiceProcessor(handler)); @@ -79,7 +92,7 @@ int main(int argc, char *argv[]) { thread_mgr->start(); -#if defined(_THRIFT_STDCXX_H_) +#if defined(_THRIFT_STDCXX_H_) || !defined (_THRIFT_VERSION_LOWER_THAN_0_11_0_) THRIFT_STDCXX::shared_ptr server_transport = THRIFT_STDCXX::make_shared(FLAGS_port); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dc1d6fb9f7..1b4b233247 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -35,8 +35,33 @@ add_library(brpc-static STATIC $ $ $) +function(check_thrift_version target_arg) + #use thrift command to get version + execute_process( + COMMAND thrift --version + OUTPUT_VARIABLE THRIFT_VERSION_OUTPUT + ERROR_QUIET + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + + string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" THRIFT_VERSION ${THRIFT_VERSION_OUTPUT}) + string(REGEX REPLACE "\\." ";" THRIFT_VERSION_LIST ${THRIFT_VERSION}) + + list(GET THRIFT_VERSION_LIST 0 THRIFT_MAJOR_VERSION) + list(GET THRIFT_VERSION_LIST 1 THRIFT_MINOR_VERSION) + + if (THRIFT_MAJOR_VERSION EQUAL 0 AND THRIFT_MINOR_VERSION LESS 11) + message(STATUS "Thrift version is less than 0.11.0") + target_compile_definitions($(target_arg) PRIVATE _THRIFT_VERSION_LOWER_THAN_0_11_0_) + else() + message(STATUS "Thrift version is equal to or greater than 0.11.0") + endif() +endfunction() + + if(WITH_THRIFT) target_link_libraries(brpc-static ${THRIFT_LIB}) + check_thrift_version(brpc-static) endif() SET_TARGET_PROPERTIES(brpc-static PROPERTIES OUTPUT_NAME brpc CLEAN_DIRECT_OUTPUT 1) @@ -60,6 +85,7 @@ if(BUILD_SHARED_LIBS) endif() if(WITH_THRIFT) target_link_libraries(brpc-shared ${THRIFT_LIB}) + check_thrift_version(brpc-shared) endif() SET_TARGET_PROPERTIES(brpc-shared PROPERTIES OUTPUT_NAME brpc CLEAN_DIRECT_OUTPUT 1) diff --git a/src/brpc/policy/thrift_protocol.cpp b/src/brpc/policy/thrift_protocol.cpp index a746cb0acd..d53ec5e988 100755 --- a/src/brpc/policy/thrift_protocol.cpp +++ b/src/brpc/policy/thrift_protocol.cpp @@ -40,13 +40,16 @@ #include // _THRIFT_STDCXX_H_ is defined by thrift/stdcxx.h which was added since thrift 0.11.0 +// but deprecated after thrift 0.13.0 #include // to include stdcxx.h if present #ifndef THRIFT_STDCXX #if defined(_THRIFT_STDCXX_H_) # define THRIFT_STDCXX apache::thrift::stdcxx - #else + #elif defined(_THRIFT_VERSION_LOWER_THAN_0_11_0_) # define THRIFT_STDCXX boost # include + #else + # define THRIFT_STDCXX std #endif #endif