genproto.sh 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env bash
  2. #
  3. # Generate all etcd protobuf bindings.
  4. # Run from repository root.
  5. #
  6. set -e
  7. if ! [[ "$0" =~ "scripts/genproto.sh" ]]; then
  8. echo "must be run from repository root"
  9. exit 255
  10. fi
  11. # for now, be conservative about what version of protoc we expect
  12. if ! [[ $(protoc --version) =~ "3.0.0" ]]; then
  13. echo "could not find protoc 3.0.0, is it installed + in PATH?"
  14. exit 255
  15. fi
  16. # directories containing protos to be built
  17. DIRS="./wal/walpb ./etcdserver/etcdserverpb ./snap/snappb ./raft/raftpb ./storage/storagepb ./lease/leasepb ./auth/authpb"
  18. # exact version of protoc-gen-gogo to build
  19. SHA="c57e439bad574c2e0877ff18d514badcfced004d"
  20. # set up self-contained GOPATH for building
  21. export GOPATH=${PWD}/gopath
  22. export GOBIN=${PWD}/bin
  23. export PATH="${GOBIN}:${PATH}"
  24. COREOS_ROOT="${GOPATH}/src/github.com/coreos"
  25. ETCD_ROOT="${COREOS_ROOT}/etcd"
  26. GOGOPROTO_ROOT="${GOPATH}/src/github.com/gogo/protobuf"
  27. GOGOPROTO_PATH="${GOGOPROTO_ROOT}:${GOGOPROTO_ROOT}/protobuf"
  28. rm -f "${ETCD_ROOT}"
  29. mkdir -p "${COREOS_ROOT}"
  30. ln -s "${PWD}" "${ETCD_ROOT}"
  31. # Ensure we have the right version of protoc-gen-gogo by building it every time.
  32. # TODO(jonboulle): vendor this instead of `go get`ting it.
  33. go get github.com/gogo/protobuf/{proto,protoc-gen-gogo,gogoproto}
  34. go get golang.org/x/tools/cmd/goimports
  35. pushd "${GOGOPROTO_ROOT}"
  36. git reset --hard "${SHA}"
  37. make install
  38. popd
  39. for dir in ${DIRS}; do
  40. pushd ${dir}
  41. protoc --gogofast_out=plugins=grpc,import_prefix=github.com/coreos/:. -I=.:"${GOGOPROTO_PATH}":"${COREOS_ROOT}" *.proto
  42. sed -i.bak -E "s/github\.com\/coreos\/(gogoproto|github\.com|golang\.org|google\.golang\.org)/\1/g" *.pb.go
  43. sed -i.bak -E 's/github\.com\/coreos\/(errors|fmt|io)/\1/g' *.pb.go
  44. sed -i.bak -E 's/import _ \"gogoproto\"//g' *.pb.go
  45. sed -i.bak -E 's/import fmt \"fmt\"//g' *.pb.go
  46. rm -f *.bak
  47. goimports -w *.pb.go
  48. popd
  49. done
  50. # install protodoc
  51. # go get -v -u github.com/coreos/protodoc
  52. #
  53. # by default, do not run this option.
  54. # only run when './scripts/genproto.sh -g'
  55. #
  56. if [ "$1" = "-g" ]; then
  57. echo "protodoc is auto-generating Protocol Buffer documentation..."
  58. go get -v -u github.com/coreos/protodoc
  59. SHA_PROTODOC="4cd8db83c5595ac514169fda607d1ccb5eef669b"
  60. PROTODOC_PATH="${GOPATH}/src/github.com/coreos/protodoc"
  61. pushd "${PROTODOC_PATH}"
  62. git reset --hard "${SHA_PROTODOC}"
  63. go install
  64. echo "protodoc is updated"
  65. popd
  66. protodoc --directories="etcdserver/etcdserverpb=service_message,storage/storagepb=service_message,lease/leasepb=service_message,auth/authpb=service_message" \
  67. --title="etcd API Reference" \
  68. --output="Documentation/api_reference_v3.md" \
  69. --message-only-from-this-file="etcdserver/etcdserverpb/rpc.proto"
  70. echo "protodoc is finished..."
  71. else
  72. echo "skipping Protocol Buffer document auto-generation..."
  73. fi