generate.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_routeguide_v1_example {
  69. local proto="$here/examples/route_guide/route_guide.proto"
  70. local output="$root/Examples/v1/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_v1_example {
  75. local proto="$here/upstream/grpc/examples/helloworld.proto"
  76. local output="$root/Examples/v1/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/Examples/v1/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. #------------------------------------------------------------------------------
  139. # Examples
  140. generate_echo_v1_example
  141. generate_routeguide_v1_example
  142. generate_helloworld_v1_example
  143. generate_reflection_data_example
  144. # Reflection service and tests
  145. generate_reflection_service
  146. generate_reflection_client_for_tests
  147. generate_echo_reflection_data_for_tests
  148. # Misc. tests
  149. generate_normalization_for_tests