generate_descriptor_proto.sh 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/sh
  2. # Run this script to regenerate descriptor.pb.{h,cc} after the protocol
  3. # compiler changes. Since these files are compiled into the protocol compiler
  4. # itself, they cannot be generated automatically by a make rule. "make check"
  5. # will fail if these files do not match what the protocol compiler would
  6. # generate.
  7. #
  8. # HINT: Flags passed to generate_descriptor_proto.sh will be passed directly
  9. # to make when building protoc. This is particularly useful for passing
  10. # -j4 to run 4 jobs simultaneously.
  11. if test ! -e src/google/protobuf/stubs/common.h; then
  12. cat >&2 << __EOF__
  13. Could not find source code. Make sure you are running this script from the
  14. root of the distribution tree.
  15. __EOF__
  16. exit 1
  17. fi
  18. if test ! -e src/Makefile; then
  19. cat >&2 << __EOF__
  20. Could not find src/Makefile. You must run ./configure (and perhaps
  21. ./autogen.sh) first.
  22. __EOF__
  23. exit 1
  24. fi
  25. cd src
  26. declare -a RUNTIME_PROTO_FILES=(\
  27. google/protobuf/any.proto \
  28. google/protobuf/api.proto \
  29. google/protobuf/descriptor.proto \
  30. google/protobuf/duration.proto \
  31. google/protobuf/empty.proto \
  32. google/protobuf/field_mask.proto \
  33. google/protobuf/source_context.proto \
  34. google/protobuf/struct.proto \
  35. google/protobuf/timestamp.proto \
  36. google/protobuf/type.proto \
  37. google/protobuf/wrappers.proto)
  38. CORE_PROTO_IS_CORRECT=0
  39. PROCESS_ROUND=1
  40. TMP=$(mktemp -d)
  41. echo "Updating descriptor protos..."
  42. while [ $CORE_PROTO_IS_CORRECT -ne 1 ]
  43. do
  44. echo "Round $PROCESS_ROUND"
  45. CORE_PROTO_IS_CORRECT=1
  46. make $@ protoc
  47. if test $? -ne 0; then
  48. echo "Failed to build protoc."
  49. exit 1
  50. fi
  51. ./protoc --cpp_out=dllexport_decl=LIBPROTOBUF_EXPORT:$TMP ${RUNTIME_PROTO_FILES[@]} && \
  52. ./protoc --cpp_out=dllexport_decl=LIBPROTOC_EXPORT:$TMP google/protobuf/compiler/plugin.proto
  53. for PROTO_FILE in ${RUNTIME_PROTO_FILES[@]}; do
  54. BASE_NAME=${PROTO_FILE%.*}
  55. diff ${BASE_NAME}.pb.h $TMP/${BASE_NAME}.pb.h > /dev/null
  56. if test $? -ne 0; then
  57. CORE_PROTO_IS_CORRECT=0
  58. fi
  59. diff ${BASE_NAME}.pb.cc $TMP/${BASE_NAME}.pb.cc > /dev/null
  60. if test $? -ne 0; then
  61. CORE_PROTO_IS_CORRECT=0
  62. fi
  63. done
  64. diff google/protobuf/compiler/plugin.pb.h $TMP/google/protobuf/compiler/plugin.pb.h > /dev/null
  65. if test $? -ne 0; then
  66. CORE_PROTO_IS_CORRECT=0
  67. fi
  68. diff google/protobuf/compiler/plugin.pb.cc $TMP/google/protobuf/compiler/plugin.pb.cc > /dev/null
  69. if test $? -ne 0; then
  70. CORE_PROTO_IS_CORRECT=0
  71. fi
  72. # Only override the output if the files are different to avoid re-compilation
  73. # of the protoc.
  74. if [ $CORE_PROTO_IS_CORRECT -ne 1 ]; then
  75. for PROTO_FILE in ${RUNTIME_PROTO_FILES[@]}; do
  76. BASE_NAME=${PROTO_FILE%.*}
  77. mv $TMP/${BASE_NAME}.pb.h ${BASE_NAME}.pb.h
  78. mv $TMP/${BASE_NAME}.pb.cc ${BASE_NAME}.pb.cc
  79. done
  80. mv $TMP/google/protobuf/compiler/plugin.pb.* google/protobuf/compiler/
  81. fi
  82. PROCESS_ROUND=$((PROCESS_ROUND + 1))
  83. done
  84. cd ..