compile_testing_protos.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/bin/bash -eu
  2. # Invoked by the Xcode projects to build the protos needed for the unittests.
  3. readonly OUTPUT_DIR="${PROJECT_DERIVED_FILE_DIR}/protos"
  4. # -----------------------------------------------------------------------------
  5. # Helper for bailing.
  6. die() {
  7. echo "Error: $1"
  8. exit 2
  9. }
  10. # -----------------------------------------------------------------------------
  11. # What to do.
  12. case "${ACTION}" in
  13. "")
  14. # Build, fall thru
  15. ;;
  16. "clean")
  17. rm -rf "${OUTPUT_DIR}"
  18. exit 0
  19. ;;
  20. *)
  21. die "Unknown action requested: ${ACTION}"
  22. ;;
  23. esac
  24. # -----------------------------------------------------------------------------
  25. # Reusing a bunch of the protos from the protocolbuffers/protobuf tree, this
  26. # can include some extras as there is no harm in ensuring work for C++
  27. # generation.
  28. CORE_PROTO_FILES=(
  29. src/google/protobuf/any_test.proto
  30. src/google/protobuf/unittest_arena.proto
  31. src/google/protobuf/unittest_custom_options.proto
  32. src/google/protobuf/unittest_enormous_descriptor.proto
  33. src/google/protobuf/unittest_embed_optimize_for.proto
  34. src/google/protobuf/unittest_empty.proto
  35. src/google/protobuf/unittest_import.proto
  36. src/google/protobuf/unittest_import_lite.proto
  37. src/google/protobuf/unittest_lite.proto
  38. src/google/protobuf/unittest_mset.proto
  39. src/google/protobuf/unittest_mset_wire_format.proto
  40. src/google/protobuf/unittest_no_generic_services.proto
  41. src/google/protobuf/unittest_optimize_for.proto
  42. src/google/protobuf/unittest.proto
  43. src/google/protobuf/unittest_import_public.proto
  44. src/google/protobuf/unittest_import_public_lite.proto
  45. src/google/protobuf/unittest_drop_unknown_fields.proto
  46. src/google/protobuf/unittest_preserve_unknown_enum.proto
  47. src/google/protobuf/map_lite_unittest.proto
  48. src/google/protobuf/map_proto2_unittest.proto
  49. src/google/protobuf/map_unittest.proto
  50. # The unittest_custom_options.proto extends the messages in descriptor.proto
  51. # so we build it in to test extending in general. The library doesn't provide
  52. # a descriptor as it doesn't use the classes/enums.
  53. src/google/protobuf/descriptor.proto
  54. )
  55. # -----------------------------------------------------------------------------
  56. # The objc unittest specific proto files.
  57. OBJC_TEST_PROTO_FILES=(
  58. objectivec/Tests/unittest_cycle.proto
  59. objectivec/Tests/unittest_deprecated.proto
  60. objectivec/Tests/unittest_deprecated_file.proto
  61. objectivec/Tests/unittest_extension_chain_a.proto
  62. objectivec/Tests/unittest_extension_chain_b.proto
  63. objectivec/Tests/unittest_extension_chain_c.proto
  64. objectivec/Tests/unittest_extension_chain_d.proto
  65. objectivec/Tests/unittest_extension_chain_e.proto
  66. objectivec/Tests/unittest_extension_chain_f.proto
  67. objectivec/Tests/unittest_extension_chain_g.proto
  68. objectivec/Tests/unittest_objc.proto
  69. objectivec/Tests/unittest_objc_startup.proto
  70. objectivec/Tests/unittest_objc_options.proto
  71. objectivec/Tests/unittest_runtime_proto2.proto
  72. objectivec/Tests/unittest_runtime_proto3.proto
  73. )
  74. OBJC_EXTENSIONS=( .pbobjc.h .pbobjc.m )
  75. # -----------------------------------------------------------------------------
  76. # Ensure the output dir exists
  77. mkdir -p "${OUTPUT_DIR}/google/protobuf"
  78. # -----------------------------------------------------------------------------
  79. # Move to the top of the protobuf directories and ensure there is a protoc
  80. # binary to use.
  81. cd "${SRCROOT}/.."
  82. [[ -x src/protoc ]] || \
  83. die "Could not find the protoc binary; make sure you have built it (objectivec/DevTools/full_mac_build.sh -h)."
  84. # -----------------------------------------------------------------------------
  85. RUN_PROTOC=no
  86. # Check to if all the output files exist (in case a new one got added).
  87. for PROTO_FILE in "${CORE_PROTO_FILES[@]}" "${OBJC_TEST_PROTO_FILES[@]}"; do
  88. DIR=${PROTO_FILE%/*}
  89. BASE_NAME=${PROTO_FILE##*/}
  90. # Drop the extension
  91. BASE_NAME=${BASE_NAME%.*}
  92. OBJC_NAME=$(echo "${BASE_NAME}" | awk -F _ '{for(i=1; i<=NF; i++) printf "%s", toupper(substr($i,1,1)) substr($i,2);}')
  93. for EXT in "${OBJC_EXTENSIONS[@]}"; do
  94. if [[ ! -f "${OUTPUT_DIR}/google/protobuf/${OBJC_NAME}${EXT}" ]]; then
  95. RUN_PROTOC=yes
  96. fi
  97. done
  98. done
  99. # If we haven't decided to run protoc because of a missing file, check to see if
  100. # an input has changed.
  101. if [[ "${RUN_PROTOC}" != "yes" ]] ; then
  102. # Find the newest input file (protos, compiler, and this script).
  103. # (these patterns catch some extra stuff, but better to over sample than
  104. # under)
  105. readonly NewestInput=$(find \
  106. src/google/protobuf/*.proto \
  107. objectivec/Tests/*.proto \
  108. src/.libs src/*.la src/protoc \
  109. objectivec/DevTools/compile_testing_protos.sh \
  110. -type f -print0 \
  111. | xargs -0 stat -f "%m %N" \
  112. | sort -n | tail -n1 | cut -f2- -d" ")
  113. # Find the oldest output file.
  114. readonly OldestOutput=$(find \
  115. "${OUTPUT_DIR}" \
  116. -type f -name "*.pbobjc.[hm]" -print0 \
  117. | xargs -0 stat -f "%m %N" \
  118. | sort -n -r | tail -n1 | cut -f2- -d" ")
  119. # If the newest input is newer than the oldest output, regenerate.
  120. if [[ "${NewestInput}" -nt "${OldestOutput}" ]] ; then
  121. RUN_PROTOC=yes
  122. fi
  123. fi
  124. if [[ "${RUN_PROTOC}" != "yes" ]] ; then
  125. # Up to date.
  126. exit 0
  127. fi
  128. # -----------------------------------------------------------------------------
  129. # Prune out all the files from previous generations to ensure we only have
  130. # current ones.
  131. find "${OUTPUT_DIR}" \
  132. -type f -name "*.pbobjc.[hm]" -print0 \
  133. | xargs -0 rm -rf
  134. # -----------------------------------------------------------------------------
  135. # Helper to invoke protoc
  136. compile_protos() {
  137. src/protoc \
  138. --objc_out="${OUTPUT_DIR}/google/protobuf" \
  139. --proto_path=src/google/protobuf/ \
  140. --proto_path=src \
  141. --experimental_allow_proto3_optional \
  142. "$@"
  143. }
  144. # -----------------------------------------------------------------------------
  145. # Generate most of the proto files that exist in the C++ src tree.
  146. # Note: there is overlap in package.Message names between some of the test
  147. # files, so they can't be generated all at once. This works because the overlap
  148. # isn't linked into a single binary.
  149. for a_proto in "${CORE_PROTO_FILES[@]}" ; do
  150. compile_protos "${a_proto}"
  151. done
  152. # -----------------------------------------------------------------------------
  153. # Generate the Objective C specific testing protos.
  154. compile_protos \
  155. --proto_path="objectivec/Tests" \
  156. "${OBJC_TEST_PROTO_FILES[@]}"