From d2562f9c5e4c1ac04658183a4e41200534392bf0 Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 15 May 2024 16:34:36 +0800 Subject: [PATCH] =?UTF-8?q?2024.5.14=20=E6=B5=8B=E8=AF=95Findgrpc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin_test_grpc/CMakeLists.txt | 45 ++++----- patch/grpc/FindgRPC.cmake | 167 +++++++++++++--------------------- patch/grpc/Findprotobuf.cmake | 78 +++------------- 3 files changed, 94 insertions(+), 196 deletions(-) diff --git a/bin_test_grpc/CMakeLists.txt b/bin_test_grpc/CMakeLists.txt index 9cfb7a0..f12d806 100644 --- a/bin_test_grpc/CMakeLists.txt +++ b/bin_test_grpc/CMakeLists.txt @@ -6,24 +6,20 @@ string(ASCII 27 Esc) set(ColourReset "${Esc}[m") set(BoldGreen "${Esc}[1;32m") set(BoldRed "${Esc}[31m") -option(CLEAN_CMAKE_CACHE "更换Build目录运行需要清空缓存" ON) set(LIB_BASE_DIR /root/CXX_ENVS/wenet_runtime) -#清空缓存 -if(CLEAN_CMAKE_CACHE) - message(STATUS "Cleaning CMakeCache.txt") - execute_process( - COMMAND rm -rf *.pb.cc *.pb.h - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - ) - execute_process( - COMMAND rm -rf !(*.cc|*.proto|CMakeLists.txt) - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - ) -endif () # GRPC and Protocol Buffers libraries location -list(APPEND CMAKE_PREFIX_PATH "${LIB_BASE_DIR}/grpc-build" "${LIB_BASE_DIR}/protobuf-build") -find_package(protobuf REQUIRED) -find_package(gRPC REQUIRED) +set(GRPC_ROOT ${LIB_BASE_DIR}/grpc-build) +# 运行参数 +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pthread -fPIC") +list(APPEND CMAKE_PREFIX_PATH "${GRPC_ROOT}") +find_package(Threads REQUIRED) +message("${BoldGreen} ${CMAKE_PREFIX_PATH} ${ColourReset}") +find_package(gRPC CONFIG REQUIRED) + +message(${BoldGreen} + GRPC_LIBRARY_DIR:${GRPC_LIBRARY_DIR} \n + grpc_cpp_plugin:${grpc_BINARY_DIR}/grpc_cpp_plugin${ColourReset}) + set(PROTO_DIR ${CMAKE_CURRENT_SOURCE_DIR}) add_custom_command( @@ -31,25 +27,24 @@ add_custom_command( ${PROTO_DIR}/helloworld.pb.h ${PROTO_DIR}/helloworld.grpc.pb.cc ${PROTO_DIR}/helloworld.grpc.pb.h - COMMAND protobuf::protoc + COMMAND ${GRPC_ROOT}/bin/protoc ARGS --grpc_out "${PROTO_DIR}" --cpp_out "${PROTO_DIR}" - --plugin=protoc-gen-grpc=gRPC::grpc_cpp_plugin - ${PROTO_DIR}/helloworld.proto) - -message("${BoldGreen}add files at ${PROTO_DIR} successfully!${ColourReset}") - -add_library(test2_grpc STATIC + -I "${PROTO_DIR}" + --plugin=protoc-gen-grpc=${GRPC_ROOT}/bin/grpc_cpp_plugin + helloworld.proto) +add_library(hello_proto STATIC ${PROTO_DIR}/helloworld.pb.cc ${PROTO_DIR}/helloworld.pb.h ${PROTO_DIR}/helloworld.grpc.pb.cc ${PROTO_DIR}/helloworld.grpc.pb.h) -target_link_libraries(wenet_proto PUBLIC ${third_party_libraries}) +target_include_directories(hello_proto PUBLIC ${GRPC_INCLUDE_DIR} ${PROTOBUF_INCLUDE_DIR}) +target_link_libraries(hello_proto ${GRPC_CLIENT_LIBS}) foreach(_target greeter_client greeter_server greeter_async_client greeter_async_client2 greeter_async_server) add_executable(${_target} "${_target}.cc") - target_link_libraries(${_target} test2_grpc ${third_party_libraries}) + target_link_libraries(${_target} hello_proto ${GRPC_CLIENT_LIBS}) endforeach() \ No newline at end of file diff --git a/patch/grpc/FindgRPC.cmake b/patch/grpc/FindgRPC.cmake index e67c739..9d7e5e0 100644 --- a/patch/grpc/FindgRPC.cmake +++ b/patch/grpc/FindgRPC.cmake @@ -7,120 +7,75 @@ # gRPC::grpc++ - gRPC C++ library # gRPC::grpc++_reflection - gRPC C++ reflection library # gRPC::grpc_cpp_plugin - C++ generator plugin for Protocol Buffers -# - # # Generates C++ sources from the .proto files # -# grpc_generate_cpp ( [...]) -# -# SRCS - variable to define with autogenerated source files -# HDRS - variable to define with autogenerated header files +# grpc_generate_cpp ( [...]) +# PLIB - Proto Static Library generated by .proto file +# HDRS - variable to define with autogenerated source files and header files # DEST - directory where the source files will be created # ARGN - .proto files -# -function(GRPC_GENERATE_CPP SRCS HDRS DEST) - if(NOT ARGN) - message(SEND_ERROR "Error: GRPC_GENERATE_CPP() called without any proto files") - return() - endif() - - if(GRPC_GENERATE_CPP_APPEND_PATH) - # Create an include path for each file specified - foreach(FIL ${ARGN}) - get_filename_component(ABS_FIL ${FIL} ABSOLUTE) - get_filename_component(ABS_PATH ${ABS_FIL} PATH) - list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) - if(${_contains_already} EQUAL -1) - list(APPEND _protobuf_include_path -I ${ABS_PATH}) - endif() - endforeach() - else() - set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR}) - endif() - - if(DEFINED PROTOBUF_IMPORT_DIRS) - foreach(DIR ${PROTOBUF_IMPORT_DIRS}) - get_filename_component(ABS_PATH ${DIR} ABSOLUTE) - list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) - if(${_contains_already} EQUAL -1) - list(APPEND _protobuf_include_path -I ${ABS_PATH}) - endif() - endforeach() - endif() +function(GRPC_GENERATE_CPP PLIB HDRS DEST) + add_custom_command( + OUTPUT ${DEST}/${HDRS}.pb.cc + ${DEST}/${HDRS}.pb.h + ${DEST}/${HDRS}.grpc.pb.cc + ${DEST}/${HDRS}.grpc.pb.h + COMMAND ${GRPC_BINARY_DIR}/protoc + ARGS --grpc_out "${DEST}" + --cpp_out "${DEST}" + -I "${DEST}" + --plugin=protoc-gen-grpc=${GRPC_BINARY_DIR}/grpc_cpp_plugin + ${ARGN}) - set(${SRCS}) - set(${HDRS}) - foreach(FIL ${ARGN}) - get_filename_component(ABS_FIL ${FIL} ABSOLUTE) - get_filename_component(FIL_WE ${FIL} NAME_WE) - - list(APPEND ${SRCS} "${DEST}/${FIL_WE}.grpc.pb.cc") - list(APPEND ${HDRS} "${DEST}/${FIL_WE}.grpc.pb.h") - - add_custom_command( - OUTPUT "${DEST}/${FIL_WE}.grpc.pb.cc" - "${DEST}/${FIL_WE}.grpc.pb.h" - COMMAND protobuf::protoc - ARGS --grpc_out ${DEST} ${_protobuf_include_path} --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN} ${ABS_FIL} - DEPENDS ${ABS_FIL} protobuf::protoc gRPC::grpc_cpp_plugin - COMMENT "Running C++ gRPC compiler on ${FIL}" - VERBATIM ) - endforeach() - - set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE) - set(${SRCS} ${${SRCS}} PARENT_SCOPE) - set(${HDRS} ${${HDRS}} PARENT_SCOPE) + add_library(${PLIB} + ${DEST}/${HDRS}.pb.cc + ${DEST}/${HDRS}.pb.h + ${DEST}/${HDRS}.grpc.pb.cc + ${DEST}/${HDRS}.grpc.pb.h) + set_target_properties(${PLIB} PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY ${DEST}) + target_link_libraries(${PLIB} + ${GRPC_CLIENT_LIBS}) + if (EXISTS ${DEST}/${PLIB}) + message("${BoldGreen} Added ${PLIB} files at ${DEST} successfully!${ColourReset}") + else () + message(FATAL_ERROR "Add ${PLIB} code failed!") + endif () endfunction() -# By default have GRPC_GENERATE_CPP macro pass -I to protoc -# for each directory where a proto file is referenced. -if(NOT DEFINED GRPC_GENERATE_CPP_APPEND_PATH) - set(GRPC_GENERATE_CPP_APPEND_PATH TRUE) -endif() - -# Find gRPC include directory -find_path(GRPC_INCLUDE_DIR grpc/grpc.h) -mark_as_advanced(GRPC_INCLUDE_DIR) - -# Find gRPC library -find_library(GRPC_LIBRARY NAMES grpc) -mark_as_advanced(GRPC_LIBRARY) -add_library(gRPC::grpc UNKNOWN IMPORTED) -set_target_properties(gRPC::grpc PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR} - INTERFACE_LINK_LIBRARIES "-lpthread;-ldl" - IMPORTED_LOCATION ${GRPC_LIBRARY} -) +if(NOT DEFINED GRPC_ROOT) + message(FATAL_ERROR "GRPC_ROOT not set in FindgRPC.cmake") +else() + set (_GRPC_LIBS_DIR ${GRPC_ROOT}/lib) + set (_GRPC_INCLUDE_DIR ${GRPC_ROOT}/include) + set(gRPC_VERSION "1.37.1") + # Find gRPC library + find_path(PROTOBUF_INCLUDE_DIR google/protobuf/service.h PATHS ${_GRPC_INCLUDE_DIR} ) + # Find gRPC include directory + find_path(GRPC_INCLUDE_DIR grpc/grpc.h PATHS ${_GRPC_INCLUDE_DIR} ) + mark_as_advanced(GRPC_INCLUDE_DIR) + message("${BoldGreen}GRPC_INCLUDE_DIR is ${GRPC_INCLUDE_DIR}${ColourReset}") + set(GRPC_BINARY_DIR ${GRPC_ROOT}/bin) + set(GRPC_LIBRARY_DIR ${GRPC_ROOT}/lib) +endif () +# set libs dependencies for my repo +set (_GRPC_CLIENT_ORDERED_DEPS libgrpc++_reflection.a libgrpc++.a libgrpc_unsecure.a libgpr.a libaddress_sorting.a libprotobuf.a libupb.a libcares.a libz.a + libabsl_synchronization.a libabsl_statusor.a libabsl_status.a + libabsl_raw_logging_internal.a libabsl_str_format_internal.a libabsl_strings.a libabsl_strings_internal.a + libabsl_graphcycles_internal.a libabsl_spinlock_wait.a libabsl_malloc_internal.a libabsl_cord.a libabsl_time.a + libabsl_time_zone.a libabsl_stacktrace.a libabsl_base.a libabsl_throw_delegate.a libabsl_int128.a libabsl_symbolize.a + libabsl_debugging_internal.a libabsl_demangle_internal.a ) -# Find gRPC C++ library -find_library(GRPC_GRPC++_LIBRARY NAMES grpc++) -mark_as_advanced(GRPC_GRPC++_LIBRARY) -add_library(gRPC::grpc++ UNKNOWN IMPORTED) -set_target_properties(gRPC::grpc++ PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR} - INTERFACE_LINK_LIBRARIES gRPC::grpc - IMPORTED_LOCATION ${GRPC_GRPC++_LIBRARY} -) - -# Find gRPC C++ reflection library -find_library(GRPC_GRPC++_REFLECTION_LIBRARY NAMES grpc++_reflection) -mark_as_advanced(GRPC_GRPC++_REFLECTION_LIBRARY) -add_library(gRPC::grpc++_reflection UNKNOWN IMPORTED) -set_target_properties(gRPC::grpc++_reflection PROPERTIES - INTERFACE_INCLUDE_DIRECTORIES ${GRPC_INCLUDE_DIR} - INTERFACE_LINK_LIBRARIES gRPC::grpc++ - IMPORTED_LOCATION ${GRPC_GRPC++_REFLECTION_LIBRARY} -) - -# Find gRPC CPP generator -find_program(GRPC_CPP_PLUGIN NAMES grpc_cpp_plugin) -mark_as_advanced(GRPC_CPP_PLUGIN) -add_executable(gRPC::grpc_cpp_plugin IMPORTED) -set_target_properties(gRPC::grpc_cpp_plugin PROPERTIES - IMPORTED_LOCATION ${GRPC_CPP_PLUGIN} -) +set (GRPC_CLIENT_LIBS) +foreach (DEP ${_GRPC_CLIENT_ORDERED_DEPS}) + if (EXISTS ${_GRPC_LIBS_DIR}/${DEP}) + list (APPEND GRPC_CLIENT_LIBS ${_GRPC_LIBS_DIR}/${DEP} ) + else() + message(SEND_ERROR "Missing gRPC dependency ${DEP} " ) + endif() +endforeach() include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake) -FIND_PACKAGE_HANDLE_STANDARD_ARGS(gRPC DEFAULT_MSG - GRPC_LIBRARY GRPC_INCLUDE_DIR GRPC_GRPC++_REFLECTION_LIBRARY GRPC_CPP_PLUGIN) \ No newline at end of file +FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf DEFAULT_MSG + ${GRPC_CLIENT_LIBS}) \ No newline at end of file diff --git a/patch/grpc/Findprotobuf.cmake b/patch/grpc/Findprotobuf.cmake index 5e06d55..54bd441 100644 --- a/patch/grpc/Findprotobuf.cmake +++ b/patch/grpc/Findprotobuf.cmake @@ -19,70 +19,18 @@ # DEST - directory where the source files will be created # ARGN - .proto files # -function(PROTOBUF_GENERATE_CPP SRCS HDRS DEST) - if(NOT ARGN) - message(SEND_ERROR "Error: PROTOBUF_GENERATE_CPP() called without any proto files") - return() - endif() - - if(PROTOBUF_GENERATE_CPP_APPEND_PATH) - # Create an include path for each file specified - foreach(FIL ${ARGN}) - get_filename_component(ABS_FIL ${FIL} ABSOLUTE) - get_filename_component(ABS_PATH ${ABS_FIL} PATH) - list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) - if(${_contains_already} EQUAL -1) - list(APPEND _protobuf_include_path -I ${ABS_PATH}) - endif() - endforeach() - else() - set(_protobuf_include_path -I ${CMAKE_CURRENT_SOURCE_DIR}) - endif() - - if(DEFINED PROTOBUF_IMPORT_DIRS) - foreach(DIR ${PROTOBUF_IMPORT_DIRS}) - get_filename_component(ABS_PATH ${DIR} ABSOLUTE) - list(FIND _protobuf_include_path ${ABS_PATH} _contains_already) - if(${_contains_already} EQUAL -1) - list(APPEND _protobuf_include_path -I ${ABS_PATH}) - endif() - endforeach() - endif() - - set(${SRCS}) - set(${HDRS}) - foreach(FIL ${ARGN}) - get_filename_component(ABS_FIL ${FIL} ABSOLUTE) - get_filename_component(FIL_WE ${FIL} NAME_WE) - - list(APPEND ${SRCS} "${DEST}/${FIL_WE}.pb.cc") - list(APPEND ${HDRS} "${DEST}/${FIL_WE}.pb.h") - - add_custom_command( - OUTPUT "${DEST}/${FIL_WE}.pb.cc" - "${DEST}/${FIL_WE}.pb.h" - COMMAND protobuf::protoc - ARGS --cpp_out ${DEST} ${_protobuf_include_path} ${ABS_FIL} - DEPENDS ${ABS_FIL} protobuf::protoc - COMMENT "Running C++ protocol buffer compiler on ${FIL}" - VERBATIM ) - endforeach() - - set_source_files_properties(${${SRCS}} ${${HDRS}} PROPERTIES GENERATED TRUE) - set(${SRCS} ${${SRCS}} PARENT_SCOPE) - set(${HDRS} ${${HDRS}} PARENT_SCOPE) -endfunction() - -# By default have PROTOBUF_GENERATE_CPP macro pass -I to protoc -# for each directory where a proto file is referenced. -if(NOT DEFINED PROTOBUF_GENERATE_CPP_APPEND_PATH) - set(PROTOBUF_GENERATE_CPP_APPEND_PATH TRUE) -endif() - -# Find the include directory -find_path(PROTOBUF_INCLUDE_DIR google/protobuf/service.h) -mark_as_advanced(PROTOBUF_INCLUDE_DIR) +if(NOT DEFINED PROTOBUF_ROOT) + message(FATAL_ERROR "GRPC_ROOT not set in FindgRPC.cmake") +else() + # Find gRPC include directory + find_path(PROTOBUF_INCLUDE_DIR google/protobuf/service.h ${PROTOBUF_ROOT}/include) + mark_as_advanced(PROTOBUF_INCLUDE_DIR) + message("${BoldGreen}PROTOBUF_INCLUDE_DIR is ${PROTOBUF_INCLUDE_DIR}${ColourReset}") + set(PROTOBUF_BINARY_DIR ${PROTOBUF_ROOT}/bin) + set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} ${PROTOBUF_ROOT}/lib) + message("${BoldGreen}PROTOBUF_BINARY_DIR is ${PROTOBUF_BINARY_DIR}${ColourReset}") +endif () # The Protobuf library find_library(PROTOBUF_LIBRARY NAMES protobuf) mark_as_advanced(PROTOBUF_LIBRARY) @@ -114,13 +62,13 @@ set_target_properties(protobuf::libprotoc PROPERTIES ) # Find the protoc Executable -find_program(PROTOBUF_PROTOC_EXECUTABLE NAMES protoc) +find_program(PROTOBUF_PROTOC_EXECUTABLE protoc ${PROTOBUF_BINARY_DIR}) mark_as_advanced(PROTOBUF_PROTOC_EXECUTABLE) +message("${BoldGreen}PROTOBUF_PROTOC_EXECUTABLE is ${PROTOBUF_PROTOC_EXECUTABLE}${ColourReset}") add_executable(protobuf::protoc IMPORTED) set_target_properties(protobuf::protoc PROPERTIES IMPORTED_LOCATION ${PROTOBUF_PROTOC_EXECUTABLE} ) - include(${CMAKE_ROOT}/Modules/FindPackageHandleStandardArgs.cmake) FIND_PACKAGE_HANDLE_STANDARD_ARGS(Protobuf DEFAULT_MSG PROTOBUF_LIBRARY PROTOBUF_INCLUDE_DIR PROTOBUF_PROTOC_EXECUTABLE) \ No newline at end of file