cmake 常用指令,变量

 

源码目录: ${CMAKE_SOURCE_DIR}

This is the full path to the top level of the current CMake source tree. For an in-source build, this would be the same as CMAKE_BINARY_DIR.

 

grpc helloword 改造

cmake_minimum_required(VERSION 3.20.1)

project(HelloWorld C CXX)

add_definitions( -D_WIN32_WINNT=0x0600 )
add_definitions( -DGRPC_CALLBACK_API_NONEXPERIMENTAL )

set(protobuf_MODULE_COMPATIBLE TRUE)
find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${Protobuf_VERSION}")

set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
set(_REFLECTION gRPC::grpc++_reflection)
if(CMAKE_CROSSCOMPILING)
  find_program(_PROTOBUF_PROTOC protoc)
else()
  set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
endif()

 # Find gRPC installation
 # Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
find_package(gRPC CONFIG REQUIRED)
message(STATUS "Using gRPC ${gRPC_VERSION}")

set(_GRPC_GRPCPP gRPC::grpc++)
if(CMAKE_CROSSCOMPILING)
  find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
else()
  set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
endif()

get_filename_component(proto_dir "./protos" ABSOLUTE)
get_filename_component(gen_dir "./protos_generated" ABSOLUTE)
# 获取所有proto文件
file(GLOB proto_src CONFIGURE_DEPENDS "${proto_dir}/*.proto")
# 设置生成文件列表
set(gen_files "")
# 拼接生成文件
foreach(_proto ${proto_src})
  get_filename_component(gen_file_name  ${_proto} NAME_WE)
  get_filename_component(gen_file_pb_h "${gen_dir}/${gen_file_name}.pb.h" ABSOLUTE)
  get_filename_component(gen_file_pb_cc "${gen_dir}/${gen_file_name}.pb.cc" ABSOLUTE)
  get_filename_component(gen_file_grpc_pb_h "${gen_dir}/${gen_file_name}.grpc.pb.h" ABSOLUTE)
  get_filename_component(gen_file_grpc_pb_cc "${gen_dir}/${gen_file_name}.grpc.pb.cc" ABSOLUTE)
  list(APPEND gen_files ${gen_file_pb_h} ${gen_file_pb_cc} ${gen_file_grpc_pb_h} ${gen_file_grpc_pb_cc})
endforeach()


add_custom_command(
      OUTPUT ${gen_files}
      COMMAND ${_PROTOBUF_PROTOC}
      ARGS --grpc_out "${gen_dir}"
        --cpp_out "${gen_dir}"
        -I "${proto_dir}"
        --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
        ${proto_src}
      DEPENDS ${proto_src})

# Include generated *.pb.h files
include_directories("${gen_dir}")



# hw_grpc_proto
add_library(hw_grpc_proto
  ${gen_files})
target_link_libraries(hw_grpc_proto
  ${_REFLECTION}
  ${_GRPC_GRPCPP}
  ${_PROTOBUF_LIBPROTOBUF})

# Targets greeter_[async_](client|server)
foreach(_target
  greeter_client greeter_server 
   greeter_callback_client greeter_callback_server 
  greeter_async_client greeter_async_client2 greeter_async_server
 )
  add_executable(${_target} "${_target}.cc")
  target_link_libraries(${_target}
    hw_grpc_proto
    ${_REFLECTION}
    ${_GRPC_GRPCPP}
    ${_PROTOBUF_LIBPROTOBUF})
endforeach()

 

posted @ 2021-08-14 11:00  wolbo  阅读(43)  评论(0编辑  收藏  举报