1#!/usr/bin/env bash 2 3# Run this script to regenerate descriptor.pb.{h,cc} after the protocol 4# compiler changes. Since these files are compiled into the protocol compiler 5# itself, they cannot be generated automatically by a make rule. "make check" 6# will fail if these files do not match what the protocol compiler would 7# generate. 8# 9# HINT: Flags passed to generate_descriptor_proto.sh will be passed directly 10# to bazel when building protoc. This is particularly useful for passing 11# -j4 to run 4 jobs simultaneously. 12 13if test ! -e src/google/protobuf/stubs/common.h; then 14 cat >&2 << __EOF__ 15Could not find source code. Make sure you are running this script from the 16root of the distribution tree. 17__EOF__ 18 exit 1 19fi 20 21cd src 22 23declare -a RUNTIME_PROTO_FILES=(\ 24 google/protobuf/any.proto \ 25 google/protobuf/api.proto \ 26 google/protobuf/cpp_features.proto \ 27 google/protobuf/descriptor.proto \ 28 google/protobuf/duration.proto \ 29 google/protobuf/empty.proto \ 30 google/protobuf/field_mask.proto \ 31 google/protobuf/source_context.proto \ 32 google/protobuf/struct.proto \ 33 google/protobuf/timestamp.proto \ 34 google/protobuf/type.proto \ 35 google/protobuf/wrappers.proto) 36 37declare -a COMPILER_PROTO_FILES=(\ 38 google/protobuf/compiler/plugin.proto) 39 40CORE_PROTO_IS_CORRECT=0 41PROCESS_ROUND=1 42BOOTSTRAP_PROTOC="" 43while [ $# -gt 0 ]; do 44 case $1 in 45 --bootstrap_protoc) 46 BOOTSTRAP_PROTOC=$2 47 shift 2 48 ;; 49 *) 50 break 51 ;; 52 esac 53 shift 54done 55TMP=$(mktemp -d) 56echo "Updating descriptor protos..." 57while [ $CORE_PROTO_IS_CORRECT -ne 1 ] 58do 59 echo "Round $PROCESS_ROUND" 60 CORE_PROTO_IS_CORRECT=1 61 62 if [ "$BOOTSTRAP_PROTOC" != "" ]; then 63 PROTOC=$BOOTSTRAP_PROTOC 64 BOOTSTRAP_PROTOC="" 65 else 66 ${BAZEL:-bazel} ${BAZEL_STARTUP_FLAGS:-} build $@ //:protoc ${BAZEL_FLAGS:-} 67 if test $? -ne 0; then 68 echo "Failed to build protoc." 69 exit 1 70 fi 71 PROTOC="../bazel-bin/protoc" 72 fi 73 74 $PROTOC --cpp_out=dllexport_decl=PROTOBUF_EXPORT:$TMP ${RUNTIME_PROTO_FILES[@]} && \ 75 $PROTOC --cpp_out=dllexport_decl=PROTOC_EXPORT:$TMP ${COMPILER_PROTO_FILES[@]} 76 77 for PROTO_FILE in ${RUNTIME_PROTO_FILES[@]} ${COMPILER_PROTO_FILES[@]}; do 78 BASE_NAME=${PROTO_FILE%.*} 79 diff ${BASE_NAME}.pb.h $TMP/${BASE_NAME}.pb.h > /dev/null 80 if test $? -ne 0; then 81 CORE_PROTO_IS_CORRECT=0 82 fi 83 diff ${BASE_NAME}.pb.cc $TMP/${BASE_NAME}.pb.cc > /dev/null 84 if test $? -ne 0; then 85 CORE_PROTO_IS_CORRECT=0 86 fi 87 done 88 89 # Only override the output if the files are different to avoid re-compilation 90 # of the protoc. 91 if [ $CORE_PROTO_IS_CORRECT -ne 1 ]; then 92 for PROTO_FILE in ${RUNTIME_PROTO_FILES[@]} ${COMPILER_PROTO_FILES[@]}; do 93 BASE_NAME=${PROTO_FILE%.*} 94 mv $TMP/${BASE_NAME}.pb.h ${BASE_NAME}.pb.h 95 mv $TMP/${BASE_NAME}.pb.cc ${BASE_NAME}.pb.cc 96 done 97 fi 98 99 PROCESS_ROUND=$((PROCESS_ROUND + 1)) 100done 101cd .. 102 103if test -x objectivec/generate_well_known_types.sh; then 104 echo "Generating messages for objc." 105 objectivec/generate_well_known_types.sh $@ 106fi 107 108if test -x csharp/generate_protos.sh; then 109 echo "Generating messages for C#." 110 csharp/generate_protos.sh $@ 111fi 112 113if test -x php/generate_descriptor_protos.sh; then 114 echo "Generating messages for PHP." 115 php/generate_descriptor_protos.sh $@ 116fi 117