generate_descriptor_proto.sh 3.3 KB

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