blob: 8a5ed48a2b8b6febdfd9240d98a63ec7226671e9 [file] [log] [blame]
Austin Schuh40c16522018-10-28 20:27:54 -07001#!/usr/bin/env bash
Brian Silverman9c614bc2016-02-15 20:20:02 -05002
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 make when building protoc. This is particularly useful for passing
11# -j4 to run 4 jobs simultaneously.
12
Brian Silverman9c614bc2016-02-15 20:20:02 -050013if 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
21if test ! -e src/Makefile; then
22 cat >&2 << __EOF__
23Could not find src/Makefile. You must run ./configure (and perhaps
24./autogen.sh) first.
25__EOF__
26 exit 1
27fi
28
29cd src
30
31declare -a RUNTIME_PROTO_FILES=(\
32 google/protobuf/any.proto \
33 google/protobuf/api.proto \
34 google/protobuf/descriptor.proto \
35 google/protobuf/duration.proto \
36 google/protobuf/empty.proto \
37 google/protobuf/field_mask.proto \
38 google/protobuf/source_context.proto \
39 google/protobuf/struct.proto \
40 google/protobuf/timestamp.proto \
41 google/protobuf/type.proto \
42 google/protobuf/wrappers.proto)
43
Austin Schuh40c16522018-10-28 20:27:54 -070044declare -a COMPILER_PROTO_FILES=(\
45 google/protobuf/compiler/plugin.proto)
46
Brian Silverman9c614bc2016-02-15 20:20:02 -050047CORE_PROTO_IS_CORRECT=0
48PROCESS_ROUND=1
Austin Schuh40c16522018-10-28 20:27:54 -070049BOOTSTRAP_PROTOC=""
50while [ $# -gt 0 ]; do
51 case $1 in
52 --bootstrap_protoc)
53 BOOTSTRAP_PROTOC=$2
54 shift
55 ;;
56 *)
57 break
58 ;;
59 esac
60 shift
61done
Brian Silverman9c614bc2016-02-15 20:20:02 -050062TMP=$(mktemp -d)
63echo "Updating descriptor protos..."
64while [ $CORE_PROTO_IS_CORRECT -ne 1 ]
65do
66 echo "Round $PROCESS_ROUND"
67 CORE_PROTO_IS_CORRECT=1
68
Austin Schuh40c16522018-10-28 20:27:54 -070069 if [ "$BOOTSTRAP_PROTOC" != "" ]; then
70 PROTOC=$BOOTSTRAP_PROTOC
71 BOOTSTRAP_PROTOC=""
72 else
73 make $@ protoc
74 if test $? -ne 0; then
75 echo "Failed to build protoc."
76 exit 1
77 fi
78 PROTOC="./protoc"
79 fi
Brian Silverman9c614bc2016-02-15 20:20:02 -050080
Austin Schuh40c16522018-10-28 20:27:54 -070081 $PROTOC --cpp_out=dllexport_decl=LIBPROTOBUF_EXPORT:$TMP ${RUNTIME_PROTO_FILES[@]} && \
82 $PROTOC --cpp_out=dllexport_decl=LIBPROTOC_EXPORT:$TMP ${COMPILER_PROTO_FILES[@]}
83
84 for PROTO_FILE in ${RUNTIME_PROTO_FILES[@]} ${COMPILER_PROTO_FILES[@]}; do
Brian Silverman9c614bc2016-02-15 20:20:02 -050085 BASE_NAME=${PROTO_FILE%.*}
86 diff ${BASE_NAME}.pb.h $TMP/${BASE_NAME}.pb.h > /dev/null
87 if test $? -ne 0; then
88 CORE_PROTO_IS_CORRECT=0
89 fi
90 diff ${BASE_NAME}.pb.cc $TMP/${BASE_NAME}.pb.cc > /dev/null
91 if test $? -ne 0; then
92 CORE_PROTO_IS_CORRECT=0
93 fi
94 done
95
Brian Silverman9c614bc2016-02-15 20:20:02 -050096 # Only override the output if the files are different to avoid re-compilation
97 # of the protoc.
98 if [ $CORE_PROTO_IS_CORRECT -ne 1 ]; then
Austin Schuh40c16522018-10-28 20:27:54 -070099 for PROTO_FILE in ${RUNTIME_PROTO_FILES[@]} ${COMPILER_PROTO_FILES[@]}; do
Brian Silverman9c614bc2016-02-15 20:20:02 -0500100 BASE_NAME=${PROTO_FILE%.*}
101 mv $TMP/${BASE_NAME}.pb.h ${BASE_NAME}.pb.h
102 mv $TMP/${BASE_NAME}.pb.cc ${BASE_NAME}.pb.cc
103 done
Brian Silverman9c614bc2016-02-15 20:20:02 -0500104 fi
105
106 PROCESS_ROUND=$((PROCESS_ROUND + 1))
107done
108cd ..
109
Austin Schuh40c16522018-10-28 20:27:54 -0700110if test -x objectivec/generate_well_known_types.sh; then
Brian Silverman9c614bc2016-02-15 20:20:02 -0500111 echo "Generating messages for objc."
Austin Schuh40c16522018-10-28 20:27:54 -0700112 objectivec/generate_well_known_types.sh $@
113fi
114
115if test -x csharp/generate_protos.sh; then
116 echo "Generating messages for C#."
117 csharp/generate_protos.sh $@
118fi
119
120if test -x php/generate_descriptor_protos.sh; then
121 echo "Generating messages for PHP."
122 php/generate_descriptor_protos.sh $@
Brian Silverman9c614bc2016-02-15 20:20:02 -0500123fi