2
0

generate.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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_v1_example {
  63. local proto="$here/examples/echo/echo.proto"
  64. local output="$root/Sources/Examples/v1/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_echo_v2_example {
  69. local proto="$here/examples/echo/echo.proto"
  70. local output="$root/Sources/Examples/v2/Echo/Generated"
  71. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal"
  72. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal" "_V2=true"
  73. }
  74. function generate_routeguide_example {
  75. local proto="$here/examples/route_guide/route_guide.proto"
  76. local output="$root/Sources/Examples/v1/RouteGuide/Model"
  77. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Public"
  78. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Visibility=Public"
  79. }
  80. function generate_helloworld_example {
  81. local proto="$here/upstream/grpc/examples/helloworld.proto"
  82. local output="$root/Sources/Examples/v1/HelloWorld/Model"
  83. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Public"
  84. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Visibility=Public"
  85. }
  86. function generate_reflection_service {
  87. local proto_v1="$here/upstream/grpc/reflection/v1/reflection.proto"
  88. local output_v1="$root/Sources/GRPCReflectionService/v1"
  89. # Messages were accidentally leaked into public API, they shouldn't be but we
  90. # can't undo that change until the next major version.
  91. generate_message "$proto_v1" "$(dirname "$proto_v1")" "$output_v1" "Visibility=Public"
  92. generate_grpc "$proto_v1" "$(dirname "$proto_v1")" "$output_v1" "Visibility=Internal" "Client=false"
  93. # Both protos have the same name so will generate Swift files with the same
  94. # name. SwiftPM can't handle this so rename them.
  95. mv "$output_v1/reflection.pb.swift" "$output_v1/reflection-v1.pb.swift"
  96. mv "$output_v1/reflection.grpc.swift" "$output_v1/reflection-v1.grpc.swift"
  97. local proto_v1alpha="$here/upstream/grpc/reflection/v1alpha/reflection.proto"
  98. local output_v1alpha="$root/Sources/GRPCReflectionService/v1Alpha"
  99. # Messages were accidentally leaked into public API, they shouldn't be but we
  100. # can't undo that change until the next major version.
  101. generate_message "$proto_v1alpha" "$(dirname "$proto_v1alpha")" "$output_v1alpha" "Visibility=Public"
  102. generate_grpc "$proto_v1alpha" "$(dirname "$proto_v1alpha")" "$output_v1alpha" "Visibility=Internal" "Client=false"
  103. # Both protos have the same name so will generate Swift files with the same
  104. # name. SwiftPM can't handle this so rename them.
  105. mv "$output_v1alpha/reflection.pb.swift" "$output_v1alpha/reflection-v1alpha.pb.swift"
  106. mv "$output_v1alpha/reflection.grpc.swift" "$output_v1alpha/reflection-v1alpha.grpc.swift"
  107. }
  108. function generate_reflection_client_for_tests {
  109. local proto_v1="$here/upstream/grpc/reflection/v1/reflection.proto"
  110. local output_v1="$root/Tests/GRPCTests/GRPCReflectionServiceTests/Generated/v1"
  111. generate_message "$proto_v1" "$(dirname "$proto_v1")" "$output_v1" "Visibility=Internal"
  112. generate_grpc "$proto_v1" "$(dirname "$proto_v1")" "$output_v1" "Visibility=Internal" "Server=false"
  113. # Both protos have the same name so will generate Swift files with the same
  114. # name. SwiftPM can't handle this so rename them.
  115. mv "$output_v1/reflection.pb.swift" "$output_v1/reflection-v1.pb.swift"
  116. mv "$output_v1/reflection.grpc.swift" "$output_v1/reflection-v1.grpc.swift"
  117. local proto_v1alpha="$here/upstream/grpc/reflection/v1alpha/reflection.proto"
  118. local output_v1alpha="$root/Tests/GRPCTests/GRPCReflectionServiceTests/Generated/v1Alpha"
  119. generate_message "$proto_v1alpha" "$(dirname "$proto_v1alpha")" "$output_v1alpha" "Visibility=Internal"
  120. generate_grpc "$proto_v1alpha" "$(dirname "$proto_v1alpha")" "$output_v1alpha" "Visibility=Internal" "Server=false"
  121. # Both protos have the same name so will generate Swift files with the same
  122. # name. SwiftPM can't handle this so rename them.
  123. mv "$output_v1alpha/reflection.pb.swift" "$output_v1alpha/reflection-v1alpha.pb.swift"
  124. mv "$output_v1alpha/reflection.grpc.swift" "$output_v1alpha/reflection-v1alpha.grpc.swift"
  125. }
  126. function generate_normalization_for_tests {
  127. local proto="$here/tests/normalization/normalization.proto"
  128. local output="$root/Tests/GRPCTests/Codegen/Normalization"
  129. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal"
  130. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal" "KeepMethodCasing=true"
  131. }
  132. function generate_echo_reflection_data_for_tests {
  133. local proto="$here/examples/echo/echo.proto"
  134. local output="$root/Tests/GRPCTests/Codegen/Serialization"
  135. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Client=false" "Server=false" "ReflectionData=true"
  136. }
  137. function generate_reflection_data_example {
  138. local protos=("$here/examples/echo/echo.proto" "$here/upstream/grpc/examples/helloworld.proto")
  139. local output="$root/Sources/Examples/v1/ReflectionService/Generated"
  140. for proto in "${protos[@]}"; do
  141. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Client=false" "Server=false" "ReflectionData=true"
  142. done
  143. }
  144. function generate_rpc_code_for_tests {
  145. local protos=(
  146. "$here/upstream/grpc/service_config/service_config.proto"
  147. "$here/upstream/grpc/lookup/v1/rls.proto"
  148. "$here/upstream/grpc/lookup/v1/rls_config.proto"
  149. "$here/upstream/google/rpc/code.proto"
  150. )
  151. local output="$root/Tests/GRPCCoreTests/Configuration/Generated"
  152. for proto in "${protos[@]}"; do
  153. generate_message "$proto" "$here/upstream" "$output" "Visibility=Internal" "FileNaming=DropPath"
  154. done
  155. }
  156. function generate_http2_transport_tests_service {
  157. local proto="$here/tests/control/control.proto"
  158. local output="$root/Tests/GRPCHTTP2TransportTests/Generated"
  159. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal"
  160. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Visibility=Internal" "Client=true" "Server=true" "_V2=true"
  161. }
  162. function generate_service_messages_interop_tests {
  163. local protos=(
  164. "$here/tests/interoperability/src/proto/grpc/testing/empty_service.proto"
  165. "$here/tests/interoperability/src/proto/grpc/testing/empty.proto"
  166. "$here/tests/interoperability/src/proto/grpc/testing/messages.proto"
  167. "$here/tests/interoperability/src/proto/grpc/testing/test.proto"
  168. )
  169. local output="$root/Sources/InteroperabilityTests/Generated"
  170. for proto in "${protos[@]}"; do
  171. generate_message "$proto" "$here/tests/interoperability" "$output" "Visibility=Public" "FileNaming=DropPath"
  172. generate_grpc "$proto" "$here/tests/interoperability" "$output" "Visibility=Public" "Server=true" "_V2=true" "FileNaming=DropPath"
  173. done
  174. }
  175. function generate_worker_service {
  176. local protos=(
  177. "$here/upstream/grpc/testing/payloads.proto"
  178. "$here/upstream/grpc/testing/control.proto"
  179. "$here/upstream/grpc/testing/messages.proto"
  180. "$here/upstream/grpc/testing/stats.proto"
  181. "$here/upstream/grpc/testing/benchmark_service.proto"
  182. "$here/upstream/grpc/testing/worker_service.proto"
  183. )
  184. local output="$root/Sources/performance-worker/Generated"
  185. generate_message "$here/upstream/grpc/core/stats.proto" "$here/upstream" "$output" "Visibility=Internal" "FileNaming=PathToUnderscores"
  186. for proto in "${protos[@]}"; do
  187. generate_message "$proto" "$here/upstream" "$output" "Visibility=Internal" "FileNaming=PathToUnderscores"
  188. if [ "$proto" == "$here/upstream/grpc/testing/worker_service.proto" ]; then
  189. generate_grpc "$proto" "$here/upstream" "$output" "Visibility=Internal" "Client=false" "_V2=true" "FileNaming=PathToUnderscores"
  190. else
  191. generate_grpc "$proto" "$here/upstream" "$output" "Visibility=Internal" "_V2=true" "FileNaming=PathToUnderscores"
  192. fi
  193. done
  194. }
  195. function generate_health_service {
  196. local proto="$here/upstream/grpc/health/v1/health.proto"
  197. local output="$root/Sources/Services/Health/Generated"
  198. generate_message "$proto" "$(dirname "$proto")" "$output" "Visibility=Package"
  199. generate_grpc "$proto" "$(dirname "$proto")" "$output" "Visibility=Package" "Client=true" "Server=true" "_V2=true"
  200. }
  201. #------------------------------------------------------------------------------
  202. # Examples
  203. generate_echo_v1_example
  204. generate_echo_v2_example
  205. generate_routeguide_example
  206. generate_helloworld_example
  207. generate_reflection_data_example
  208. # Reflection service and tests
  209. generate_reflection_service
  210. generate_reflection_client_for_tests
  211. generate_echo_reflection_data_for_tests
  212. # Interoperability tests
  213. generate_service_messages_interop_tests
  214. # Misc. tests
  215. generate_normalization_for_tests
  216. generate_rpc_code_for_tests
  217. generate_http2_transport_tests_service
  218. # Performance worker service
  219. generate_worker_service
  220. # Health
  221. generate_health_service