generate.sh 12 KB

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