github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/scripts/generate-protobuf.sh (about)

     1  #!/usr/bin/env bash
     2  # Copyright 2022 PingCAP, Inc.
     3  #
     4  # Licensed under the Apache License, Version 2.0 (the "License");
     5  # you may not use this file except in compliance with the License.
     6  # You may obtain a copy of the License at
     7  #
     8  #     http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  # Unless required by applicable law or agreed to in writing, software
    11  # distributed under the License is distributed on an "AS IS" BASIS,
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  set -eu
    16  
    17  TOOLS_BIN_DIR="tools/bin"
    18  TOOLS_INCLUDE_DIR="tools/include"
    19  PROTO_DIR="proto"
    20  DM_PROTO_DIR="dm/proto"
    21  TiCDC_SOURCE_DIR="cdc"
    22  INCLUDE="-I $PROTO_DIR \
    23  	-I $TOOLS_INCLUDE_DIR \
    24  	-I $TiCDC_SOURCE_DIR \
    25  	-I $DM_PROTO_DIR"
    26  
    27  PROTOC="$TOOLS_BIN_DIR/protoc"
    28  GO="$TOOLS_BIN_DIR/protoc-gen-go"
    29  GO_GRPC="$TOOLS_BIN_DIR/protoc-gen-go-grpc"
    30  GOGO_FASTER=$TOOLS_BIN_DIR/protoc-gen-gogofaster
    31  GRPC_GATEWAY=$TOOLS_BIN_DIR/protoc-gen-grpc-gateway
    32  GRPC_GATEWAY_V2=$TOOLS_BIN_DIR/protoc-gen-grpc-gateway-v2
    33  OPENAPIV2=tools/bin/protoc-gen-openapiv2
    34  
    35  function generate() {
    36  	local out_dir=$1
    37  	local proto_file=$2
    38  	local gogo_option=
    39  	if [ $# -eq 3 ]; then
    40  		gogo_option=$3
    41  	fi
    42  
    43  	echo "generate $proto_file..."
    44  	$PROTOC $INCLUDE \
    45  		--plugin=protoc-gen-gogofaster="$GOGO_FASTER" \
    46  		--gogofaster_out=$gogo_option:$out_dir $proto_file
    47  }
    48  
    49  for tool in $PROTOC $GO $GO_GRPC $GOGO_FASTER $GRPC_GATEWAY $GRPC_GATEWAY_V2 $OPENAPIV2; do
    50  	if [ ! -x $tool ]; then
    51  		echo "$tool does not exist, please run 'make $tool' first."
    52  		exit 1
    53  	fi
    54  done
    55  
    56  generate ./proto/canal ./proto/EntryProtocol.proto
    57  generate ./proto/canal ./proto/CanalProtocol.proto
    58  generate ./proto/benchmark ./proto/CraftBenchmark.proto
    59  generate ./proto/p2p ./proto/CDCPeerToPeer.proto plugins=grpc
    60  generate ./dm/pb ./dm/proto/dmworker.proto plugins=grpc,protoc-gen-grpc-gateway="$GRPC_GATEWAY"
    61  generate ./dm/pb ./dm/proto/dmmaster.proto plugins=grpc,protoc-gen-grpc-gateway="$GRPC_GATEWAY"
    62  
    63  for pb in $(find cdc -name '*.proto'); do
    64  	# Output generated go files next to protobuf files.
    65  	generate ./cdc $pb paths="source_relative"
    66  done
    67  
    68  echo "generate enginepb..."
    69  mkdir -p ./engine/enginepb
    70  $PROTOC -I. -I"$TOOLS_INCLUDE_DIR" \
    71  	--plugin=protoc-gen-go="$GO" \
    72  	--plugin=protoc-gen-go-grpc="$GO_GRPC" \
    73  	--plugin=protoc-gen-grpc-gateway-v2="$GRPC_GATEWAY_V2" \
    74  	--plugin=protoc-gen-openapiv2="$OPENAPIV2" \
    75  	--go_out=. --go_opt=module=github.com/pingcap/tiflow \
    76  	--go-grpc_out=. --go-grpc_opt=module=github.com/pingcap/tiflow,require_unimplemented_servers=false \
    77  	--grpc-gateway-v2_out=. --grpc-gateway-v2_opt=module=github.com/pingcap/tiflow \
    78  	--openapiv2_out=engine/pkg/openapi \
    79  	--openapiv2_opt=allow_merge=true,merge_file_name="apiv1",omit_enum_default_value=true,json_names_for_fields=false \
    80  	engine/proto/*.proto