• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# Copyright 2016 gRPC authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17# Example usage:
18#   tools/codegen/core/gen_nano_proto.sh \
19#     src/proto/grpc/lb/v1/load_balancer.proto \
20#     $PWD/src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1 \
21#     src/core/ext/filters/client_channel/lb_policy/grpclb/proto/grpc/lb/v1
22#
23# Exit statuses:
24# 1: Incorrect number of arguments
25# 2: Input proto file (1st argument) doesn't exist or is not a regular file.
26# 3: Options file for nanopb not found in same dir as the input proto file.
27# 4: Output dir not an absolute path.
28# 5: Couldn't create output directory (2nd argument).
29
30set -ex
31if [ $# -lt 2 ] || [ $# -gt 3 ]; then
32  echo "Usage: $0 <input.proto> <absolute path to output dir> [grpc path]"
33  exit 1
34fi
35
36readonly GRPC_ROOT="$PWD"
37readonly INPUT_PROTO="$1"
38readonly OUTPUT_DIR="$2"
39readonly GRPC_OUTPUT_DIR="${3:-$OUTPUT_DIR}"
40readonly EXPECTED_OPTIONS_FILE_PATH="${1%.*}.options"
41
42if [[ ! -f "$INPUT_PROTO" ]]; then
43  echo "Input proto file '$INPUT_PROTO' doesn't exist."
44  exit 2
45fi
46
47if [[ ! -f "${EXPECTED_OPTIONS_FILE_PATH}" ]]; then
48  echo "Input proto file may need .options file to be correctly compiled."
49fi
50
51if [[ "${OUTPUT_DIR:0:1}" != '/' ]]; then
52  echo "The output directory must be an absolute path. Got '$OUTPUT_DIR'"
53  exit 4
54fi
55
56mkdir -p "$OUTPUT_DIR"
57if [ $? != 0 ]; then
58  echo "Error creating output directory $OUTPUT_DIR"
59  exit 5
60fi
61
62readonly VENV_DIR=$(mktemp -d)
63readonly VENV_NAME="nanopb-$(date '+%Y%m%d_%H%M%S_%N')"
64pushd $VENV_DIR
65virtualenv $VENV_NAME
66. $VENV_NAME/bin/activate
67popd
68
69# this should be the same version as the submodule we compile against
70# ideally we'd update this as a template to ensure that
71pip install protobuf==3.6.0
72
73pushd "$(dirname $INPUT_PROTO)" > /dev/null
74
75protoc \
76--plugin=protoc-gen-nanopb="$GRPC_ROOT/third_party/nanopb/generator/protoc-gen-nanopb" \
77--nanopb_out='-T -Q#include\ \"'"${GRPC_OUTPUT_DIR}"'/%s\" -L#include\ \"pb.h\"'":$OUTPUT_DIR" \
78"$(basename $INPUT_PROTO)"
79
80deactivate
81rm -rf $VENV_DIR
82
83popd > /dev/null
84