From 71f4e25043ec3f6cac2e6da4be49fa0cd9df4ada Mon Sep 17 00:00:00 2001 From: Michael Dai Date: Thu, 24 Jan 2019 18:10:43 +0800 Subject: [PATCH] Fixed make proto (#747) * Fixed make proto Signed-off-by: jojohappy * Address review comments Signed-off-by: jojohappy * Change the download destination of protoc package Signed-off-by: jojohappy * Rollback && simplify the code Signed-off-by: jojohappy * Fixed temporary dir doesn't exist Signed-off-by: jojohappy * To install protoc command only suuportting linux or macosx Signed-off-by: jojohappy * Update docs Signed-off-by: jojohappy --- CONTRIBUTING.md | 6 +++- Makefile | 14 ++++++++-- scripts/genproto.sh | 9 ++++-- scripts/installprotoc.sh | 59 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 6 deletions(-) create mode 100755 scripts/installprotoc.sh diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index d8118e696a..de315fcd88 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -21,10 +21,14 @@ The philosophy of Thanos and our community is borrowing much from UNIX philosoph Adding large new features and components to Thanos should be done by first creating a [proposal](docs/proposals) document outlining the design decisions of the change, motivations for the change, and any alternatives that might have been considered. +## Prerequisites + +* It is strongly recommended that you use OSX or popular Linux distributions systems e.g. Ubuntu, Redhat, or OpenSUSE for development. + ## Pull Request Process 1. Read [getting started docs](docs/getting_started.md) and prepare Thanos. -2. Familarize yourself with [Makefile](Makefile) commands like `format`, `build`, `proto` and `test`. +2. Familiarize yourself with [Makefile](Makefile) commands like `format`, `build`, `proto` and `test`. 3. Fork improbable-eng/thanos.git and start development from your own fork. Here are sample steps to setup your development environment: ```console $ mkdir -p $GOPATH/src/github.com/improbable-eng diff --git a/Makefile b/Makefile index 9bd2cd077a..4d0e27aab2 100644 --- a/Makefile +++ b/Makefile @@ -31,6 +31,8 @@ GOIMPORTS_VERSION ?= 1c3d964395ce8f04f3b03b30aaed0b096c08c3c6 PROMU ?= $(BIN_DIR)/promu-$(PROMU_VERSION) # v0.2.0 PROMU_VERSION ?= 264dc36af9ea3103255063497636bd5713e3e9c1 +PROTOC ?= $(BIN_DIR)/protoc-$(PROTOC_VERSION) +PROTOC_VERSION ?= 3.4.0 # E2e test deps. # Referenced by github.com/improbable-eng/thanos/blob/master/docs/getting_started.md#prometheus @@ -129,9 +131,9 @@ format: $(GOIMPORTS) # proto generates golang files from Thanos proto files. .PHONY: proto -proto: deps +proto: deps $(GOIMPORTS) $(PROTOC) @go install ./vendor/github.com/gogo/protobuf/protoc-gen-gogofast - @./scripts/genproto.sh + @GOIMPORTS_BIN="$(GOIMPORTS)" PROTOC_BIN="$(PROTOC)" scripts/genproto.sh .PHONY: promu promu: $(PROMU) @@ -200,3 +202,11 @@ $(LICHE): $(PROMU): $(call fetch_go_bin_version,github.com/prometheus/promu,$(PROMU_VERSION)) + +$(PROTOC): + @mkdir -p $(TMP_GOPATH) + @echo ">> fetching protoc@${PROTOC_VERSION}" + @PROTOC_VERSION="$(PROTOC_VERSION)" TMP_GOPATH="$(TMP_GOPATH)" scripts/installprotoc.sh + @echo ">> installing protoc@${PROTOC_VERSION}" + @mv -- "$(TMP_GOPATH)/bin/protoc" "$(BIN_DIR)/protoc-$(PROTOC_VERSION)" + @echo ">> produced $(BIN_DIR)/protoc-$(PROTOC_VERSION)" diff --git a/scripts/genproto.sh b/scripts/genproto.sh index 48dfaae879..fa2933831f 100755 --- a/scripts/genproto.sh +++ b/scripts/genproto.sh @@ -5,12 +5,15 @@ set -e set -u +PROTOC_BIN=${PROTOC_BIN:-protoc} +GOIMPORTS_BIN=${GOIMPORTS_BIN:-goimports} + if ! [[ "$0" =~ "scripts/genproto.sh" ]]; then echo "must be run from repository root" exit 255 fi -if ! [[ $(protoc --version) =~ "3.4.0" ]]; then +if ! [[ $(${PROTOC_BIN} --version) =~ "3.4.0" ]]; then echo "could not find protoc 3.4.0, is it installed + in PATH?" exit 255 fi @@ -24,7 +27,7 @@ DIRS="pkg/store/storepb pkg/store/prompb" for dir in ${DIRS}; do pushd ${dir} - protoc --gogofast_out=plugins=grpc:. -I=. \ + ${PROTOC_BIN} --gogofast_out=plugins=grpc:. -I=. \ -I="${GOGOPROTO_PATH}" \ -I="${PROM_PATH}" \ *.proto @@ -32,6 +35,6 @@ for dir in ${DIRS}; do sed -i.bak -E 's/import _ \"gogoproto\"//g' *.pb.go sed -i.bak -E 's/import _ \"google\/protobuf\"//g' *.pb.go rm -f *.bak - goimports -w *.pb.go + ${GOIMPORTS_BIN} -w *.pb.go popd done diff --git a/scripts/installprotoc.sh b/scripts/installprotoc.sh new file mode 100755 index 0000000000..1ca843c6cc --- /dev/null +++ b/scripts/installprotoc.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +# +# Install the standard protocol buffer implementation - protoc. +set -e +set -u + +PROTOC_VERSION=${PROTOC_VERSION:-3.4.0} +TMP_GOPATH=${TMP_GOPATH:-/tmp/thanos-go} +PROTOC_DOWNLOAD_URL="https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}" + +OS=$(go env GOOS) +ARCH=$(go env GOARCH) +PLATFORM="${OS}/${ARCH}" + +is_supported_platform() { + platform=$1 + found=1 + case "$platform" in + darwin/amd64) found=0 ;; + darwin/i386) found=0 ;; + linux/amd64) found=0 ;; + linux/i386) found=0 ;; + linux/arm64) found=0 ;; + esac + return $found +} + +adjust_os() { + case ${OS} in + darwin) OS=osx ;; + esac + true +} + +adjust_arch() { + case ${ARCH} in + amd64) ARCH=x86_64 ;; + i386) ARCH=x86_32 ;; + arm64) ARCH=aarch_64 ;; + esac + true +} + +mkdir -p ${TMP_GOPATH} + +is_supported_platform "$PLATFORM" +if [[ $? -eq 1 ]]; then + echo "platform $PLATFORM is not supported. See https://github.com/protocolbuffers/protobuf/releases for details" + exit 1 +fi + +adjust_os + +adjust_arch + +PACKAGE="protoc-${PROTOC_VERSION}-${OS}-${ARCH}.zip" +PACKAGE_DOWNLOAD_URL="${PROTOC_DOWNLOAD_URL}/${PACKAGE}" +curl -LSs ${PACKAGE_DOWNLOAD_URL} -o ${TMP_GOPATH}/${PACKAGE} +unzip -qqj ${TMP_GOPATH}/${PACKAGE} "bin/protoc" -d "${TMP_GOPATH}/bin/"