protoc.sh 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #!/usr/bin/env bash
  2. # Copyright 2017 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. # The root of the build/dist directory
  19. KUBE_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)"
  20. source "${KUBE_ROOT}/hack/lib/init.sh"
  21. # Generates $1/api.pb.go from the protobuf file $1/api.proto
  22. # and formats it correctly
  23. # $1: Full path to the directory where the api.proto file is
  24. function kube::protoc::generate_proto() {
  25. kube::golang::setup_env
  26. local bins=(
  27. vendor/k8s.io/code-generator/cmd/go-to-protobuf/protoc-gen-gogo
  28. )
  29. make -C "${KUBE_ROOT}" WHAT="${bins[*]}"
  30. kube::protoc::check_protoc
  31. local package=${1}
  32. kube::protoc::protoc "${package}"
  33. kube::protoc::format "${package}"
  34. }
  35. # Checks that the current protoc version is at least version 3.0.0-beta1
  36. # exit 1 if it's not the case
  37. function kube::protoc::check_protoc() {
  38. if [[ -z "$(which protoc)" || "$(protoc --version)" != "libprotoc 3."* ]]; then
  39. echo "Generating protobuf requires protoc 3.0.0-beta1 or newer. Please download and"
  40. echo "install the platform appropriate Protobuf package for your OS: "
  41. echo
  42. echo " https://github.com/google/protobuf/releases"
  43. echo
  44. echo "WARNING: Protobuf changes are not being validated"
  45. exit 1
  46. fi
  47. }
  48. # Generates $1/api.pb.go from the protobuf file $1/api.proto
  49. # $1: Full path to the directory where the api.proto file is
  50. function kube::protoc::protoc() {
  51. local package=${1}
  52. gogopath=$(dirname "$(kube::util::find-binary "protoc-gen-gogo")")
  53. PATH="${gogopath}:${PATH}" protoc \
  54. --proto_path="${package}" \
  55. --proto_path="${KUBE_ROOT}/vendor" \
  56. --gogo_out=plugins=grpc:"${package}" "${package}/api.proto"
  57. }
  58. # Formats $1/api.pb.go, adds the boilerplate comments and run gofmt on it
  59. # $1: Full path to the directory where the api.proto file is
  60. function kube::protoc::format() {
  61. local package=${1}
  62. # Update boilerplate for the generated file.
  63. cat hack/boilerplate/boilerplate.generatego.txt "${package}/api.pb.go" > tmpfile && mv tmpfile "${package}/api.pb.go"
  64. # Run gofmt to clean up the generated code.
  65. kube::golang::verify_go_version
  66. gofmt -l -s -w "${package}/api.pb.go"
  67. }
  68. # Compares the contents of $1 and $2
  69. # Echo's $3 in case of error and exits 1
  70. function kube::protoc::diff() {
  71. local ret=0
  72. diff -I "gzipped FileDescriptorProto" -I "0x" -Naupr "${1}" "${2}" || ret=$?
  73. if [[ ${ret} -ne 0 ]]; then
  74. echo "${3}"
  75. exit 1
  76. fi
  77. }