asio-grpc v3.1.0
Asynchronous gRPC with Asio/unified executors
CMake protobuf generate

In the same directory that called find_package(asio-grpc) the following CMake function will be made available. It can be used to generate Protobuf/gRPC source files from .proto schemas.
If you are using cmake-format then you can copy the asio_grpc_protobuf_generate section from cmake-format.yaml to get proper formatting.

asio_grpc_protobuf_generate(PROTOS <proto_file1> [<proto_file2>...]
[OUT_DIR <output_directory>]
[OUT_VAR <output_variable>]
[TARGET <target>]
[USAGE_REQUIREMENT PRIVATE|PUBLIC|INTERFACE]
[IMPORT_DIRS <directories>...]
[EXTRA_ARGS <arguments>...]
[GENERATE_GRPC]
[GENERATE_DESCRIPTORS]
[GENERATE_MOCK_CODE])
PROTOS: Input .proto schema files.

OUT_DIR: Generated files output directory. Default: CMAKE_CURRENT_BINARY_DIR.

OUT_VAR: Variable to define with generated source files.

TARGET: Add generated source files to target.

USAGE_REQUIREMENT: How to add sources to <target>: PRIVATE, PUBLIC, INTERFACE. Default: PRIVATE.

IMPORT_DIRS: Import directories to be added to the protoc command line. If unspecified then the directory of each .proto file will be used.

EXTRA_ARGS: Additional protoc command line arguments.

GENERATE_GRPC: Generate gRPC files (.grpc.pb.h and .grpc.pb.cc).

GENERATE_DESCRIPTORS: Generate descriptor files named <proto_file_base_name>.desc.

GENERATE_MOCK_CODE: Generate gRPC client stub mock files named _mock.grpc.pb.h.

Example

Given a CMake target called target-option:

asio_grpc_protobuf_generate(
GENERATE_GRPC GENERATE_MOCK_CODE
TARGET target-option
OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/target"
PROTOS "${CMAKE_CURRENT_SOURCE_DIR}/proto/target.proto")

Compiling target-option will cause the generation and compilation of:

  • ${CMAKE_CURRENT_BINARY_DIR}/target/target.pb.h
  • ${CMAKE_CURRENT_BINARY_DIR}/target/target.pb.cc
  • ${CMAKE_CURRENT_BINARY_DIR}/target/target.grpc.pb.h
  • ${CMAKE_CURRENT_BINARY_DIR}/target/target.grpc.pb.cc
  • ${CMAKE_CURRENT_BINARY_DIR}/target/target_mock.grpc.pb.h

whenever ${CMAKE_CURRENT_SOURCE_DIR}/proto/target.proto has been modified.