generate.sh 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/bin/bash
  2. #
  3. # Copyright 2024, gRPC Authors All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. set -eu
  17. here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  18. root="$here/.."
  19. protoc=$(which protoc)
  20. # Build the protoc plugins.
  21. swift build -c release --product protoc-gen-swift
  22. swift build -c release --product protoc-gen-grpc-swift
  23. # Grab the plugin paths.
  24. bin_path=$(swift build -c release --show-bin-path)
  25. protoc_gen_swift="$bin_path/protoc-gen-swift"
  26. protoc_generate_grpc_swift="$bin_path/protoc-gen-grpc-swift"
  27. # Genreates gRPC by invoking protoc with the gRPC Swift plugin.
  28. # Parameters:
  29. # - $1: .proto file
  30. # - $2: proto path
  31. # - $3: output path
  32. # - $4 onwards: options to forward to the plugin
  33. function generate_grpc {
  34. local proto=$1
  35. local args=("--plugin=$protoc_generate_grpc_swift" "--proto_path=${2}" "--grpc-swift_out=${3}")
  36. for option in "${@:4}"; do
  37. args+=("--grpc-swift_opt=$option")
  38. done
  39. invoke_protoc "${args[@]}" "$proto"
  40. }
  41. # Genreates messages by invoking protoc with the Swift plugin.
  42. # Parameters:
  43. # - $1: .proto file
  44. # - $2: proto path
  45. # - $3: output path
  46. # - $4 onwards: options to forward to the plugin
  47. function generate_message {
  48. local proto=$1
  49. local args=("--plugin=$protoc_gen_swift" "--proto_path=$2" "--swift_out=$3")
  50. for option in "${@:4}"; do
  51. args+=("--swift_opt=$option")
  52. done
  53. invoke_protoc "${args[@]}" "$proto"
  54. }
  55. function invoke_protoc {
  56. # Setting -x when running the script produces a lot of output, instead boil
  57. # just echo out the protoc invocations.
  58. echo "$protoc" "$@"
  59. "$protoc" "$@"
  60. }
  61. #------------------------------------------------------------------------------
  62. function generate_echo_example {
  63. local proto="$here/examples/echo/echo.proto"
  64. local output="$root/Sources/Examples/Echo/Model"
  65. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Public"
  66. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Visibility=Public" "TestClient=true"
  67. }
  68. function generate_routeguide_example {
  69. local proto="$here/examples/route_guide/route_guide.proto"
  70. local output="$root/Sources/Examples/RouteGuide/Model"
  71. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Public"
  72. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Visibility=Public"
  73. }
  74. function generate_helloworld_example {
  75. local proto="$here/upstream/grpc/examples/helloworld.proto"
  76. local output="$root/Sources/Examples/HelloWorld/Model"
  77. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Public"
  78. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Visibility=Public"
  79. }
  80. function generate_reflection_service {
  81. local proto_v1="$here/upstream/grpc/reflection/v1/reflection.proto"
  82. local output_v1="$root/Sources/GRPCReflectionService/v1"
  83. # Messages were accidentally leaked into public API, they shouldn't be but we
  84. # can't undo that change until the next major version.
  85. generate_message "$proto_v1" "$(dirname "$proto_v1")" "$output_v1" "Visibility=Public"
  86. generate_grpc "$proto_v1" "$(dirname "$proto_v1")" "$output_v1" "Visibility=Internal" "Client=false"
  87. # Both protos have the same name so will generate Swift files with the same
  88. # name. SwiftPM can't handle this so rename them.
  89. mv "$output_v1/reflection.pb.swift" "$output_v1/reflection-v1.pb.swift"
  90. mv "$output_v1/reflection.grpc.swift" "$output_v1/reflection-v1.grpc.swift"
  91. local proto_v1alpha="$here/upstream/grpc/reflection/v1alpha/reflection.proto"
  92. local output_v1alpha="$root/Sources/GRPCReflectionService/v1alpha"
  93. # Messages were accidentally leaked into public API, they shouldn't be but we
  94. # can't undo that change until the next major version.
  95. generate_message "$proto_v1alpha" "$(dirname "$proto_v1alpha")" "$output_v1alpha" "Visibility=Public"
  96. generate_grpc "$proto_v1alpha" "$(dirname "$proto_v1alpha")" "$output_v1alpha" "Visibility=Internal" "Client=false"
  97. # Both protos have the same name so will generate Swift files with the same
  98. # name. SwiftPM can't handle this so rename them.
  99. mv "$output_v1alpha/reflection.pb.swift" "$output_v1alpha/reflection-v1alpha.pb.swift"
  100. mv "$output_v1alpha/reflection.grpc.swift" "$output_v1alpha/reflection-v1alpha.grpc.swift"
  101. }
  102. function generate_reflection_client_for_tests {
  103. local proto_v1="$here/upstream/grpc/reflection/v1/reflection.proto"
  104. local output_v1="$root/Tests/GRPCTests/GRPCReflectionServiceTests/Generated/v1"
  105. generate_message "$proto_v1" "$(dirname "$proto_v1")" "$output_v1" "Visibility=Internal"
  106. generate_grpc "$proto_v1" "$(dirname "$proto_v1")" "$output_v1" "Visibility=Internal" "Server=false"
  107. # Both protos have the same name so will generate Swift files with the same
  108. # name. SwiftPM can't handle this so rename them.
  109. mv "$output_v1/reflection.pb.swift" "$output_v1/reflection-v1.pb.swift"
  110. mv "$output_v1/reflection.grpc.swift" "$output_v1/reflection-v1.grpc.swift"
  111. local proto_v1alpha="$here/upstream/grpc/reflection/v1alpha/reflection.proto"
  112. local output_v1alpha="$root/Tests/GRPCTests/GRPCReflectionServiceTests/Generated/v1Alpha"
  113. generate_message "$proto_v1alpha" "$(dirname "$proto_v1alpha")" "$output_v1alpha" "Visibility=Internal"
  114. generate_grpc "$proto_v1alpha" "$(dirname "$proto_v1alpha")" "$output_v1alpha" "Visibility=Internal" "Server=false"
  115. # Both protos have the same name so will generate Swift files with the same
  116. # name. SwiftPM can't handle this so rename them.
  117. mv "$output_v1alpha/reflection.pb.swift" "$output_v1alpha/reflection-v1alpha.pb.swift"
  118. mv "$output_v1alpha/reflection.grpc.swift" "$output_v1alpha/reflection-v1alpha.grpc.swift"
  119. }
  120. function generate_normalization_for_tests {
  121. local proto="$here/tests/normalization/normalization.proto"
  122. local output="$root/Tests/GRPCTests/Codegen/Normalization"
  123. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal"
  124. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal" "KeepMethodCasing=true"
  125. }
  126. function generate_echo_reflection_data_for_tests {
  127. local proto="$here/examples/echo/echo.proto"
  128. local output="$root/Tests/GRPCTests/Codegen/Serialization"
  129. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Client=false" "Server=false" "ReflectionData=true"
  130. }
  131. function generate_reflection_data_example {
  132. local protos=("$here/examples/echo/echo.proto" "$here/upstream/grpc/examples/helloworld.proto")
  133. local output="$root/Sources/Examples/ReflectionService/Generated"
  134. for proto in "${protos[@]}"; do
  135. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Client=false" "Server=false" "ReflectionData=true"
  136. done
  137. }
  138. function generate_rpc_code_for_tests {
  139. local protos=(
  140. "$here/upstream/grpc/service_config/service_config.proto"
  141. "$here/upstream/grpc/lookup/v1/rls.proto"
  142. "$here/upstream/grpc/lookup/v1/rls_config.proto"
  143. "$here/upstream/google/rpc/code.proto"
  144. )
  145. local output="$root/Tests/GRPCCoreTests/Configuration/Generated"
  146. for proto in "${protos[@]}"; do
  147. generate_message "$proto" "$here/upstream" "$output" "Visibility=Internal" "FileNaming=DropPath"
  148. done
  149. }
  150. function generate_service_messages_interop_tests {
  151. local protos=(
  152. "$here/tests/interoperability/src/proto/grpc/testing/empty_service.proto"
  153. "$here/tests/interoperability/src/proto/grpc/testing/empty.proto"
  154. "$here/tests/interoperability/src/proto/grpc/testing/messages.proto"
  155. "$here/tests/interoperability/src/proto/grpc/testing/test.proto"
  156. )
  157. local output="$root/Sources/InteroperabilityTests/Generated"
  158. for proto in "${protos[@]}"; do
  159. generate_message "$proto" "$here/tests/interoperability" "$output" "Visibility=Public" "FileNaming=DropPath"
  160. generate_grpc "$proto" "$here/tests/interoperability" "$output" "Visibility=Public" "Server=true" "_V2=true" "FileNaming=DropPath"
  161. done
  162. }
  163. function generate_worker_service {
  164. local protos=(
  165. "$here/upstream/grpc/testing/payloads.proto"
  166. "$here/upstream/grpc/testing/control.proto"
  167. "$here/upstream/grpc/testing/messages.proto"
  168. "$here/upstream/grpc/testing/stats.proto"
  169. "$here/upstream/grpc/testing/benchmark_service.proto"
  170. "$here/upstream/grpc/testing/worker_service.proto"
  171. )
  172. local output="$root/Sources/performance-worker/Generated"
  173. generate_message "$here/upstream/grpc/core/stats.proto" "$here/upstream" "$output" "Visibility=Internal" "FileNaming=PathToUnderscores"
  174. for proto in "${protos[@]}"; do
  175. generate_message "$proto" "$here/upstream" "$output" "Visibility=Internal" "FileNaming=PathToUnderscores"
  176. if [ "$proto" == "$here/upstream/grpc/testing/worker_service.proto" ]; then
  177. generate_grpc "$proto" "$here/upstream" "$output" "Visibility=Internal" "Client=false" "_V2=true" "FileNaming=PathToUnderscores"
  178. else
  179. generate_grpc "$proto" "$here/upstream" "$output" "Visibility=Internal" "_V2=true" "FileNaming=PathToUnderscores"
  180. fi
  181. done
  182. }
  183. #------------------------------------------------------------------------------
  184. # Examples
  185. generate_echo_example
  186. generate_routeguide_example
  187. generate_helloworld_example
  188. generate_reflection_data_example
  189. # Reflection service and tests
  190. generate_reflection_service
  191. generate_reflection_client_for_tests
  192. generate_echo_reflection_data_for_tests
  193. # Interoperability tests
  194. generate_service_messages_interop_tests
  195. # Misc. tests
  196. generate_normalization_for_tests
  197. generate_rpc_code_for_tests
  198. # Performance worker service
  199. generate_worker_service