From bba0266a2fc2c1efc57d54858ada98ee1da72f24 Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Thu, 31 Mar 2016 22:10:01 -0700 Subject: [PATCH 1/4] Generalized nanopb generator script --- ...d_balancing_proto.sh => gen_nano_proto.sh} | 51 +++++++++---------- tools/distrib/check_nanopb_output.sh | 2 +- 2 files changed, 26 insertions(+), 27 deletions(-) rename tools/codegen/core/{gen_load_balancing_proto.sh => gen_nano_proto.sh} (79%) diff --git a/tools/codegen/core/gen_load_balancing_proto.sh b/tools/codegen/core/gen_nano_proto.sh similarity index 79% rename from tools/codegen/core/gen_load_balancing_proto.sh rename to tools/codegen/core/gen_nano_proto.sh index 339da0a7339..ad3c920ee0b 100755 --- a/tools/codegen/core/gen_load_balancing_proto.sh +++ b/tools/codegen/core/gen_nano_proto.sh @@ -31,8 +31,9 @@ # # Example usage: -# tools/codegen/core/gen_load_balancing_proto.sh \ +# tools/codegen/core/gen_nano_proto.sh \ # src/proto/grpc/lb/v0/load_balancer.proto +# src/core/ext/lb_policy/grpclb/proto/grpc/lb/v0 read -r -d '' COPYRIGHT <<'EOF' /* @@ -75,27 +76,19 @@ COPYRIGHT_FILE=$(mktemp) echo "${COPYRIGHT//$CURRENT_YEAR}" > $COPYRIGHT_FILE set -ex -if [ $# -eq 0 ]; then - echo "Usage: $0 [output dir]" +if [ $# -ne 2 ]; then + echo "Usage: $0 " exit 1 fi readonly GRPC_ROOT=$PWD - -OUTPUT_DIR="$GRPC_ROOT/src/core/ext/lb_policy/grpclb/proto/grpc/lb/v0" -if [ $# -eq 2 ]; then - mkdir -p "$2" - if [ $? != 0 ]; then - echo "Error creating output directory $2" - exit 2 - fi - OUTPUT_DIR="$2" -fi - +readonly INPUT_PROTO="$1" +readonly REL_OUTPUT_DIR="$2" +readonly ABS_OUTPUT_DIR="$GRPC_ROOT/$2" readonly EXPECTED_OPTIONS_FILE_PATH="${1%.*}.options" -if [[ ! -f "$1" ]]; then - echo "Input proto file '$1' doesn't exist." +if [[ ! -f "$INPUT_PROTO" ]]; then + echo "Input proto file '$INPUT_PROTO' doesn't exist." exit 3 fi if [[ ! -f "${EXPECTED_OPTIONS_FILE_PATH}" ]]; then @@ -103,6 +96,12 @@ if [[ ! -f "${EXPECTED_OPTIONS_FILE_PATH}" ]]; then exit 4 fi +mkdir -p "ABS_OUTPUT_DIR" +if [ $? != 0 ]; then + echo "Error creating output directory $ABS_OUTPUT_DIR" + exit 2 +fi + readonly VENV_DIR=$(mktemp -d) readonly VENV_NAME="nanopb-$(date '+%Y%m%d_%H%M%S_%N')" pushd $VENV_DIR @@ -114,23 +113,23 @@ popd # ideally we'd update this as a template to ensure that pip install protobuf==3.0.0b2 -pushd "$(dirname $1)" > /dev/null +pushd "$(dirname $INPUT_PROTO)" > /dev/null protoc \ --plugin=protoc-gen-nanopb="$GRPC_ROOT/third_party/nanopb/generator/protoc-gen-nanopb" \ ---nanopb_out='-T -L#include\ \"third_party/nanopb/pb.h\"'":$OUTPUT_DIR" \ -"$(basename $1)" +--nanopb_out='-T -L#include\ \"third_party/nanopb/pb.h\"'":$ABS_OUTPUT_DIR" \ +"$(basename $INPUT_PROTO)" -readonly PROTO_BASENAME=$(basename $1 .proto) -sed -i "s:$PROTO_BASENAME.pb.h:src/core/ext/lb_policy/grpclb/proto/grpc/lb/v0/$PROTO_BASENAME.pb.h:g" \ - "$OUTPUT_DIR/$PROTO_BASENAME.pb.c" +readonly PROTO_BASENAME=$(basename $INPUT_PROTO .proto) +sed -i "s:$PROTO_BASENAME.pb.h:$REL_OUTPUT_DIR/$PROTO_BASENAME.pb.h:g" \ + "$ABS_OUTPUT_DIR/$PROTO_BASENAME.pb.c" # prepend copyright TMPFILE=$(mktemp) -cat $COPYRIGHT_FILE "$OUTPUT_DIR/$PROTO_BASENAME.pb.c" > $TMPFILE -mv -v $TMPFILE "$OUTPUT_DIR/$PROTO_BASENAME.pb.c" -cat $COPYRIGHT_FILE "$OUTPUT_DIR/$PROTO_BASENAME.pb.h" > $TMPFILE -mv -v $TMPFILE "$OUTPUT_DIR/$PROTO_BASENAME.pb.h" +cat $COPYRIGHT_FILE "$ABS_OUTPUT_DIR/$PROTO_BASENAME.pb.c" > $TMPFILE +mv -v $TMPFILE "$ABS_OUTPUT_DIR/$PROTO_BASENAME.pb.c" +cat $COPYRIGHT_FILE "$ABS_OUTPUT_DIR/$PROTO_BASENAME.pb.h" > $TMPFILE +mv -v $TMPFILE "$ABS_OUTPUT_DIR/$PROTO_BASENAME.pb.h" deactivate rm -rf $VENV_DIR diff --git a/tools/distrib/check_nanopb_output.sh b/tools/distrib/check_nanopb_output.sh index 4f68e4c596f..f299d74638b 100755 --- a/tools/distrib/check_nanopb_output.sh +++ b/tools/distrib/check_nanopb_output.sh @@ -55,7 +55,7 @@ popd # nanopb-compile the proto to a temp location -PATH="$PROTOC_PATH:$PATH" ./tools/codegen/core/gen_load_balancing_proto.sh \ +PATH="$PROTOC_PATH:$PATH" ./tools/codegen/core/gen_nano_proto.sh \ src/proto/grpc/lb/v0/load_balancer.proto \ $NANOPB_TMP_OUTPUT From 4875ce606b4147c0e11baef9e8cc51bc29e84eff Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Fri, 1 Apr 2016 14:39:35 -0700 Subject: [PATCH 2/4] fixed typo --- tools/codegen/core/gen_nano_proto.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/codegen/core/gen_nano_proto.sh b/tools/codegen/core/gen_nano_proto.sh index ad3c920ee0b..81b91229709 100755 --- a/tools/codegen/core/gen_nano_proto.sh +++ b/tools/codegen/core/gen_nano_proto.sh @@ -96,7 +96,7 @@ if [[ ! -f "${EXPECTED_OPTIONS_FILE_PATH}" ]]; then exit 4 fi -mkdir -p "ABS_OUTPUT_DIR" +mkdir -p "$ABS_OUTPUT_DIR" if [ $? != 0 ]; then echo "Error creating output directory $ABS_OUTPUT_DIR" exit 2 From d6b4628f42d7a9889c488d7d11d1c2a936f6583d Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Fri, 1 Apr 2016 19:06:44 -0700 Subject: [PATCH 3/4] Further improvements --- tools/codegen/core/gen_nano_proto.sh | 35 ++++++++++++++++------------ tools/distrib/check_nanopb_output.sh | 28 +++++++++++++--------- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/tools/codegen/core/gen_nano_proto.sh b/tools/codegen/core/gen_nano_proto.sh index 81b91229709..db69d28ae78 100755 --- a/tools/codegen/core/gen_nano_proto.sh +++ b/tools/codegen/core/gen_nano_proto.sh @@ -33,7 +33,7 @@ # Example usage: # tools/codegen/core/gen_nano_proto.sh \ # src/proto/grpc/lb/v0/load_balancer.proto -# src/core/ext/lb_policy/grpclb/proto/grpc/lb/v0 +# $PWD/src/core/ext/lb_policy/grpclb/proto/grpc/lb/v0 read -r -d '' COPYRIGHT <<'EOF' /* @@ -76,15 +76,15 @@ COPYRIGHT_FILE=$(mktemp) echo "${COPYRIGHT//$CURRENT_YEAR}" > $COPYRIGHT_FILE set -ex -if [ $# -ne 2 ]; then - echo "Usage: $0 " +if [ $# -lt 2 ]; then + echo "Usage: $0 [grpc path]" exit 1 fi -readonly GRPC_ROOT=$PWD +readonly GRPC_ROOT="$PWD" readonly INPUT_PROTO="$1" -readonly REL_OUTPUT_DIR="$2" -readonly ABS_OUTPUT_DIR="$GRPC_ROOT/$2" +readonly OUTPUT_DIR="$2" +readonly GRPC_OUTPUT_DIR="${3:-$OUTPUT_DIR}" readonly EXPECTED_OPTIONS_FILE_PATH="${1%.*}.options" if [[ ! -f "$INPUT_PROTO" ]]; then @@ -96,9 +96,14 @@ if [[ ! -f "${EXPECTED_OPTIONS_FILE_PATH}" ]]; then exit 4 fi -mkdir -p "$ABS_OUTPUT_DIR" +if [[ "${OUTPUT_DIR:0:1}" != '/' ]]; then + echo "The output directory must be an absolute path. Got '$OUTPUT_DIR'" + exit 5 +fi + +mkdir -p "$OUTPUT_DIR" if [ $? != 0 ]; then - echo "Error creating output directory $ABS_OUTPUT_DIR" + echo "Error creating output directory $OUTPUT_DIR" exit 2 fi @@ -117,19 +122,19 @@ pushd "$(dirname $INPUT_PROTO)" > /dev/null protoc \ --plugin=protoc-gen-nanopb="$GRPC_ROOT/third_party/nanopb/generator/protoc-gen-nanopb" \ ---nanopb_out='-T -L#include\ \"third_party/nanopb/pb.h\"'":$ABS_OUTPUT_DIR" \ +--nanopb_out='-T -L#include\ \"third_party/nanopb/pb.h\"'":$OUTPUT_DIR" \ "$(basename $INPUT_PROTO)" readonly PROTO_BASENAME=$(basename $INPUT_PROTO .proto) -sed -i "s:$PROTO_BASENAME.pb.h:$REL_OUTPUT_DIR/$PROTO_BASENAME.pb.h:g" \ - "$ABS_OUTPUT_DIR/$PROTO_BASENAME.pb.c" +sed -i "s:$PROTO_BASENAME.pb.h:${GRPC_OUTPUT_DIR}/$PROTO_BASENAME.pb.h:g" \ + "$OUTPUT_DIR/$PROTO_BASENAME.pb.c" # prepend copyright TMPFILE=$(mktemp) -cat $COPYRIGHT_FILE "$ABS_OUTPUT_DIR/$PROTO_BASENAME.pb.c" > $TMPFILE -mv -v $TMPFILE "$ABS_OUTPUT_DIR/$PROTO_BASENAME.pb.c" -cat $COPYRIGHT_FILE "$ABS_OUTPUT_DIR/$PROTO_BASENAME.pb.h" > $TMPFILE -mv -v $TMPFILE "$ABS_OUTPUT_DIR/$PROTO_BASENAME.pb.h" +cat $COPYRIGHT_FILE "$OUTPUT_DIR/$PROTO_BASENAME.pb.c" > $TMPFILE +mv -v $TMPFILE "$OUTPUT_DIR/$PROTO_BASENAME.pb.c" +cat $COPYRIGHT_FILE "$OUTPUT_DIR/$PROTO_BASENAME.pb.h" > $TMPFILE +mv -v $TMPFILE "$OUTPUT_DIR/$PROTO_BASENAME.pb.h" deactivate rm -rf $VENV_DIR diff --git a/tools/distrib/check_nanopb_output.sh b/tools/distrib/check_nanopb_output.sh index f299d74638b..92cb8ecbb49 100755 --- a/tools/distrib/check_nanopb_output.sh +++ b/tools/distrib/check_nanopb_output.sh @@ -31,36 +31,42 @@ set -ex readonly NANOPB_TMP_OUTPUT="$(mktemp -d)" +readonly PROTOBUF_INSTALL_PREFIX="$(mktemp -d)" # install protoc version 3 pushd third_party/protobuf ./autogen.sh -./configure +./configure --prefix="$PROTOBUF_INSTALL_PREFIX" make make install -ldconfig +#ldconfig popd -if [ ! -x "/usr/local/bin/protoc" ]; then - echo "Error: protoc not found in path" +readonly PROTOC_BIN_PATH="$PROTOBUF_INSTALL_PREFIX/bin" +if [ ! -x "$PROTOBUF_INSTALL_PREFIX/bin/protoc" ]; then + echo "Error: protoc not found in temp install dir '$PROTOBUF_INSTALL_PREFIX'" exit 1 fi -readonly PROTOC_PATH='/usr/local/bin' + # stack up and change to nanopb's proto generator directory pushd third_party/nanopb/generator/proto -PATH="$PROTOC_PATH:$PATH" make - +export PATH="$PROTOC_BIN_PATH:$PATH" +make # back to the root directory popd - +# +# Checks for load_balancer.proto +# +readonly LOAD_BALANCER_GRPC_OUTPUT_PATH='src/core/ext/lb_policy/grpclb/proto/grpc/lb/v0' # nanopb-compile the proto to a temp location -PATH="$PROTOC_PATH:$PATH" ./tools/codegen/core/gen_nano_proto.sh \ +./tools/codegen/core/gen_nano_proto.sh \ src/proto/grpc/lb/v0/load_balancer.proto \ - $NANOPB_TMP_OUTPUT + "$NANOPB_TMP_OUTPUT" \ + "$LOAD_BALANCER_GRPC_OUTPUT_PATH" # compare outputs to checked compiled code if ! diff -r $NANOPB_TMP_OUTPUT src/core/ext/lb_policy/grpclb/proto/grpc/lb/v0; then - echo "Outputs differ: $NANOPB_TMP_OUTPUT vs src/core/ext/lb_policy/grpclb/proto/grpc/lb/v0" + echo "Outputs differ: $NANOPB_TMP_OUTPUT vs $LOAD_BALANCER_GRPC_OUTPUT_PATH" exit 2 fi From f7d9cbe65861fb796698d1d741d0ed798ffa8a53 Mon Sep 17 00:00:00 2001 From: David Garcia Quintas Date: Tue, 19 Apr 2016 14:10:18 -0700 Subject: [PATCH 4/4] PR comments --- tools/codegen/core/gen_nano_proto.sh | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tools/codegen/core/gen_nano_proto.sh b/tools/codegen/core/gen_nano_proto.sh index db69d28ae78..e2d2f672e93 100755 --- a/tools/codegen/core/gen_nano_proto.sh +++ b/tools/codegen/core/gen_nano_proto.sh @@ -34,6 +34,13 @@ # tools/codegen/core/gen_nano_proto.sh \ # src/proto/grpc/lb/v0/load_balancer.proto # $PWD/src/core/ext/lb_policy/grpclb/proto/grpc/lb/v0 +# +# Exit statuses: +# 1: Incorrect number of arguments +# 2: Input proto file (1st argument) doesn't exist or is not a regular file. +# 3: Options file for nanopb not found in same dir as the input proto file. +# 4: Output dir not an absolute path. +# 5: Couldn't create output directory (2nd argument). read -r -d '' COPYRIGHT <<'EOF' /* @@ -76,7 +83,7 @@ COPYRIGHT_FILE=$(mktemp) echo "${COPYRIGHT//$CURRENT_YEAR}" > $COPYRIGHT_FILE set -ex -if [ $# -lt 2 ]; then +if [ $# -lt 2 ] || [ $# -gt 3 ]; then echo "Usage: $0 [grpc path]" exit 1 fi @@ -89,22 +96,22 @@ readonly EXPECTED_OPTIONS_FILE_PATH="${1%.*}.options" if [[ ! -f "$INPUT_PROTO" ]]; then echo "Input proto file '$INPUT_PROTO' doesn't exist." - exit 3 + exit 2 fi if [[ ! -f "${EXPECTED_OPTIONS_FILE_PATH}" ]]; then echo "Expected nanopb options file '${EXPECTED_OPTIONS_FILE_PATH}' missing" - exit 4 + exit 3 fi if [[ "${OUTPUT_DIR:0:1}" != '/' ]]; then echo "The output directory must be an absolute path. Got '$OUTPUT_DIR'" - exit 5 + exit 4 fi mkdir -p "$OUTPUT_DIR" if [ $? != 0 ]; then echo "Error creating output directory $OUTPUT_DIR" - exit 2 + exit 5 fi readonly VENV_DIR=$(mktemp -d)