genproto.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. PREFIX="github.com/coreos/etcd/Godeps/_workspace/src"
  17. ESCAPED_PREFIX=$(echo $PREFIX | sed -e 's/[\/&]/\\&/g')
  18. # directories containing protos to be built
  19. DIRS="./wal/walpb ./etcdserver/etcdserverpb ./snap/snappb ./raft/raftpb ./storage/storagepb ./lease/leasepb"
  20. # exact version of protoc-gen-gogo to build
  21. SHA="c57e439bad574c2e0877ff18d514badcfced004d"
  22. # set up self-contained GOPATH for building
  23. export GOPATH=${PWD}/gopath
  24. export GOBIN=${PWD}/bin
  25. export PATH="${GOBIN}:${PATH}"
  26. COREOS_ROOT="${GOPATH}/src/github.com/coreos"
  27. ETCD_ROOT="${COREOS_ROOT}/etcd"
  28. GOGOPROTO_ROOT="${GOPATH}/src/github.com/gogo/protobuf"
  29. GOGOPROTO_PATH="${GOGOPROTO_ROOT}:${GOGOPROTO_ROOT}/protobuf"
  30. rm -f "${ETCD_ROOT}"
  31. mkdir -p "${COREOS_ROOT}"
  32. ln -s "${PWD}" "${ETCD_ROOT}"
  33. # Ensure we have the right version of protoc-gen-gogo by building it every time.
  34. # TODO(jonboulle): vendor this instead of `go get`ting it.
  35. go get github.com/gogo/protobuf/{proto,protoc-gen-gogo,gogoproto}
  36. go get golang.org/x/tools/cmd/goimports
  37. pushd "${GOGOPROTO_ROOT}"
  38. git reset --hard "${SHA}"
  39. make install
  40. popd
  41. for dir in ${DIRS}; do
  42. pushd ${dir}
  43. protoc --gogofast_out=plugins=grpc,import_prefix=github.com/coreos/:. -I=.:"${GOGOPROTO_PATH}":"${COREOS_ROOT}" *.proto
  44. sed -i.bak -E "s/github\.com\/coreos\/(gogoproto|github\.com|golang\.org|google\.golang\.org)/${ESCAPED_PREFIX}\/\1/g" *.pb.go
  45. sed -i.bak -E 's/github\.com\/coreos\/(errors|fmt|io)/\1/g' *.pb.go
  46. sed -i.bak -E 's/import _ \"github\.com\/coreos\/etcd\/Godeps\/\_workspace\/src\/gogoproto\"//g' *.pb.go
  47. sed -i.bak -E 's/import fmt \"fmt\"//g' *.pb.go
  48. rm -f *.bak
  49. goimports -w *.pb.go
  50. popd
  51. done