• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 make 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/descriptor.proto \
27  google/protobuf/duration.proto \
28  google/protobuf/empty.proto \
29  google/protobuf/field_mask.proto \
30  google/protobuf/source_context.proto \
31  google/protobuf/struct.proto \
32  google/protobuf/timestamp.proto \
33  google/protobuf/type.proto \
34  google/protobuf/wrappers.proto)
35
36declare -a COMPILER_PROTO_FILES=(\
37  google/protobuf/compiler/plugin.proto)
38
39CORE_PROTO_IS_CORRECT=0
40PROCESS_ROUND=1
41BOOTSTRAP_PROTOC=""
42while [ $# -gt 0 ]; do
43  case $1 in
44    --bootstrap_protoc)
45      BOOTSTRAP_PROTOC=$2
46      shift 2
47      ;;
48    *)
49      break
50      ;;
51  esac
52  shift
53done
54TMP=$(mktemp -d)
55echo "Updating descriptor protos..."
56while [ $CORE_PROTO_IS_CORRECT -ne 1 ]
57do
58  echo "Round $PROCESS_ROUND"
59  CORE_PROTO_IS_CORRECT=1
60
61  if [ "$BOOTSTRAP_PROTOC" != "" ]; then
62    PROTOC=$BOOTSTRAP_PROTOC
63    BOOTSTRAP_PROTOC=""
64  else
65    make -j$(nproc) $@ protoc
66    if test $? -ne 0; then
67      echo "Failed to build protoc."
68      exit 1
69    fi
70    PROTOC="./protoc"
71  fi
72
73  $PROTOC --cpp_out=dllexport_decl=PROTOBUF_EXPORT:$TMP ${RUNTIME_PROTO_FILES[@]} && \
74  $PROTOC --cpp_out=dllexport_decl=PROTOC_EXPORT:$TMP ${COMPILER_PROTO_FILES[@]}
75
76  for PROTO_FILE in ${RUNTIME_PROTO_FILES[@]} ${COMPILER_PROTO_FILES[@]}; do
77    BASE_NAME=${PROTO_FILE%.*}
78    diff ${BASE_NAME}.pb.h $TMP/${BASE_NAME}.pb.h > /dev/null
79    if test $? -ne 0; then
80      CORE_PROTO_IS_CORRECT=0
81    fi
82    diff ${BASE_NAME}.pb.cc $TMP/${BASE_NAME}.pb.cc > /dev/null
83    if test $? -ne 0; then
84      CORE_PROTO_IS_CORRECT=0
85    fi
86  done
87
88  # Only override the output if the files are different to avoid re-compilation
89  # of the protoc.
90  if [ $CORE_PROTO_IS_CORRECT -ne 1 ]; then
91    for PROTO_FILE in ${RUNTIME_PROTO_FILES[@]} ${COMPILER_PROTO_FILES[@]}; do
92      BASE_NAME=${PROTO_FILE%.*}
93      mv $TMP/${BASE_NAME}.pb.h ${BASE_NAME}.pb.h
94      mv $TMP/${BASE_NAME}.pb.cc ${BASE_NAME}.pb.cc
95    done
96  fi
97
98  PROCESS_ROUND=$((PROCESS_ROUND + 1))
99done
100cd ..
101
102if test -x objectivec/generate_well_known_types.sh; then
103  echo "Generating messages for objc."
104  objectivec/generate_well_known_types.sh $@
105fi
106
107if test -x csharp/generate_protos.sh; then
108  echo "Generating messages for C#."
109  csharp/generate_protos.sh $@
110fi
111
112if test -x php/generate_descriptor_protos.sh; then
113  echo "Generating messages for PHP."
114  php/generate_descriptor_protos.sh $@
115fi
116