generate_descriptor_proto.sh 3.1 KB

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