generate_well_known_types.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. # Run this script to regenerate *.pbobjc.{h,m} for the well known types after
  3. # the protocol compiler changes.
  4. # HINT: Flags passed to generate_well_known_types.sh will be passed directly
  5. # to make when building protoc. This is particularly useful for passing
  6. # -j4 to run 4 jobs simultaneously.
  7. set -eu
  8. readonly ScriptDir=$(dirname "$(echo $0 | sed -e "s,^\([^/]\),$(pwd)/\1,")")
  9. readonly ObjCDir="${ScriptDir}"
  10. readonly ProtoRootDir="${ObjCDir}/.."
  11. # Flag for continuous integration to check that everything is current.
  12. CHECK_ONLY=0
  13. if [[ $# -ge 1 && ( "$1" == "--check-only" ) ]] ; then
  14. CHECK_ONLY=1
  15. shift
  16. fi
  17. cd "${ProtoRootDir}"
  18. if [[ ! -e src/google/protobuf/stubs/common.h ]]; then
  19. cat >&2 << __EOF__
  20. Could not find source code. Make sure you are running this script from the
  21. root of the distribution tree.
  22. __EOF__
  23. exit 1
  24. fi
  25. if [[ ! -e src/Makefile ]]; then
  26. cat >&2 << __EOF__
  27. Could not find src/Makefile. You must run ./configure (and perhaps
  28. ./autogen.sh) first.
  29. __EOF__
  30. exit 1
  31. fi
  32. # Make sure the compiler is current.
  33. cd src
  34. make $@ protoc
  35. declare -a RUNTIME_PROTO_FILES=( \
  36. google/protobuf/any.proto \
  37. google/protobuf/api.proto \
  38. google/protobuf/duration.proto \
  39. google/protobuf/empty.proto \
  40. google/protobuf/field_mask.proto \
  41. google/protobuf/source_context.proto \
  42. google/protobuf/struct.proto \
  43. google/protobuf/timestamp.proto \
  44. google/protobuf/type.proto \
  45. google/protobuf/wrappers.proto)
  46. declare -a OBJC_EXTENSIONS=( .pbobjc.h .pbobjc.m )
  47. # Generate to a temp directory to see if they match.
  48. TMP_DIR=$(mktemp -d)
  49. trap "rm -rf ${TMP_DIR}" EXIT
  50. ./protoc --objc_out="${TMP_DIR}" ${RUNTIME_PROTO_FILES[@]}
  51. DID_COPY=0
  52. for PROTO_FILE in "${RUNTIME_PROTO_FILES[@]}"; do
  53. DIR=${PROTO_FILE%/*}
  54. BASE_NAME=${PROTO_FILE##*/}
  55. # Drop the extension
  56. BASE_NAME=${BASE_NAME%.*}
  57. OBJC_NAME=$(echo "${BASE_NAME}" | awk -F _ '{for(i=1; i<=NF; i++) printf "%s", toupper(substr($i,1,1)) substr($i,2);}')
  58. for EXT in "${OBJC_EXTENSIONS[@]}"; do
  59. if ! diff "${ObjCDir}/GPB${OBJC_NAME}${EXT}" "${TMP_DIR}/${DIR}/${OBJC_NAME}${EXT}" > /dev/null 2>&1 ; then
  60. if [[ "${CHECK_ONLY}" == 1 ]] ; then
  61. echo "ERROR: The WKTs need to be regenerated! Run $0"
  62. exit 1
  63. fi
  64. echo "INFO: Updating GPB${OBJC_NAME}${EXT}"
  65. cp "${TMP_DIR}/${DIR}/${OBJC_NAME}${EXT}" "${ObjCDir}/GPB${OBJC_NAME}${EXT}"
  66. DID_COPY=1
  67. fi
  68. done
  69. done
  70. if [[ "${DID_COPY}" == 0 ]]; then
  71. echo "INFO: Generated source for WellKnownTypes is current."
  72. exit 0
  73. fi