Skip to content

Commit

Permalink
Merge pull request #22 from spiffe/dirt-build5
Browse files Browse the repository at this point in the history
update build.sh, fix protobuf build
  • Loading branch information
drrt committed Jul 31, 2017
2 parents 4b9f871 + c1bfa07 commit b3ab6ee
Showing 1 changed file with 55 additions and 42 deletions.
97 changes: 55 additions & 42 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
#!/bin/bash

set -ex
set -o errexit
[[ $DEBUG ]] && set -o xtrace

PROTOBUF_VERSION=${PROTOBUF_VERSION:-3.3.0}
GO_VERSION=${GO_VERSION:-1.8.3}
GLIDE_VERSION=${GLIDE_VERSION:-0.12.3}
BUILD_DIR=${BUILD_DIR:-$PWD/.build}
BUILD_CACHE=${BUILD_CACHE:-$PWD/.cache}
declare -r BINARY_DIRS="node_agent $(find plugins/*/* -maxdepth 1 -type d -not -name 'proto')"
declare -r PROTO_FILES="$(find plugins api -name '*.proto')"

declare -r GO_VERSION=${GO_VERSION:-1.8.3}
declare -r GO_URL="https://storage.googleapis.com/golang"
declare -r GO_TGZ="go${GO_VERSION}.linux-amd64.tar.gz"
declare -r PROTOBUF_VERSION=${PROTOBUF_VERSION:-3.3.0}
declare -r PROTOBUF_URL="https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}"
declare -r PROTOBUF_TGZ="protoc-${PROTOBUF_VERSION}-linux-x86_64.zip"
declare -r GLIDE_VERSION=${GLIDE_VERSION:-0.12.3}
declare -r GLIDE_URL="https://github.com/Masterminds/glide/releases/download/v${GLIDE_VERSION}"
declare -r GLIDE_TGZ="glide-v${GLIDE_VERSION}-linux-amd64.tar.gz"

declare -r BUILD_DIR=${BUILD_DIR:-$PWD/.build}
declare -r BUILD_CACHE=${BUILD_CACHE:-$PWD/.cache}

[[ $CIRCLECI ]] && unset GOPATH
declare -xr GOPATH=${GOPATH:-$HOME/go}
declare -xr GOROOT=${BUILD_DIR}/golang
declare -xr PATH=${GOROOT}/bin:${GOPATH}/bin:${BUILD_DIR}/protobuf/bin:${BUILD_DIR}/glide/bin:${PATH}

export GOPATH=${HOME}/go
export GOROOT=${BUILD_DIR}/golang
export PATH=${GOROOT}/bin:${GOPATH}/bin:${BUILD_DIR}/protobuf/bin:${BUILD_DIR}/glide/bin:${PATH}

_fetch_url() {
if [[ ! -r ${BUILD_CACHE}/${2} ]]; then
_log_info "downloading \"${1}/${2}\""
curl --output ${BUILD_CACHE}/${2} --location --silent ${1}/${2}
else
_log_info "\"${2}\" found in ${BUILD_CACHE}"
fi
}

build_setup() {
go_url="https://storage.googleapis.com/golang"
go_tgz="go${GO_VERSION}.linux-amd64.tar.gz"
pb_url="https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}"
pb_tgz="protoc-${PROTOBUF_VERSION}-linux-x86_64.zip"
glide_url="https://github.com/Masterminds/glide/releases/download/v${GLIDE_VERSION}"
glide_tgz="glide-v${GLIDE_VERSION}-linux-amd64.tar.gz"
_exit_error() { echo "ERROR: $*" 1>2; exit 1; }
_log_info() { echo "INFO: $*"; }

build_setup() {
mkdir -p ${BUILD_CACHE} ${BUILD_DIR}
_fetch_url ${pb_url} ${pb_tgz}
_fetch_url ${go_url} ${go_tgz}
_fetch_url ${glide_url} ${glide_tgz}


rm -rf ${GOROOT}
mkdir -p ${GOROOT}
tar --directory ${GOROOT} --transform 's|^go|.|' -xf ${BUILD_CACHE}/${go_tgz}
_fetch_url ${GO_URL} ${GO_TGZ}
tar --directory ${GOROOT} --transform 's|^go/|./|' -xf ${BUILD_CACHE}/${GO_TGZ}

rm -rf ${BUILD_DIR}/protobuf
mkdir -p ${BUILD_DIR}/protobuf
unzip -qod ${BUILD_DIR}/protobuf ${BUILD_CACHE}/${pb_tgz}
_fetch_url ${PROTOBUF_URL} ${PROTOBUF_TGZ}
unzip -qod ${BUILD_DIR}/protobuf ${BUILD_CACHE}/${PROTOBUF_TGZ}

rm -rf ${BUILD_DIR}/glide
tar --directory ${BUILD_DIR} --transform 's|^linux-amd64|glide/bin|' -xf ${BUILD_CACHE}/${glide_tgz}
_fetch_url ${GLIDE_URL} ${GLIDE_TGZ}
tar --directory ${BUILD_DIR} --transform 's|^linux-amd64|glide/bin|' -xf ${BUILD_CACHE}/${GLIDE_TGZ}

go get github.com/golang/protobuf/protoc-gen-go
go get github.com/jstemmer/go-junit-report
Expand All @@ -49,34 +61,35 @@ build_setup() {
}

build_deps() {
glide --home .${BUILD_CACHE} install 2>&1 | tee /tmp/glide.out
if grep -q WARN /tmp/glide.out; then
echo "[ERROR] glide.lock file may be out of date"
return 1
glide --home ${BUILD_CACHE} install 2>&1 | tee /tmp/glide.out
if grep -q "Lock file may be out of date" /tmp/glide.out; then
_exit_error "glide.lock file may be out of date"
fi
}



build_protobuf() {
local _n
for _n in $(find plugins api -name '*.proto'); do
protoc --go_out=plugins=grpc:. ${_n}
local _n _dir
for _n in ${PROTO_FILES}; do
_log_info "running protoc on \"${_n}\""
_dir="$(dirname ${_n})"
protoc --proto_path=${_dir} --proto_path=${GOPATH}/src --go_out=plugins=grpc:${_dir} ${_n}
done
}

build_binaries() {
local _n
for _n in node_agent $(find plugins/*/* -maxdepth 1 -type d -not -name 'proto'); do
( cd $_n; go build )
local _n _dirs="$@"
for _n in ${_dirs}; do
_log_info "building in directory \"${_n}\""
( cd $_n; go build ${DEBUG+-v} )
done
}

build_test() {
test_path=$(go list ./... | egrep -v '(/vendor|/proto$$)')
if [[ $CI ]]; then
test_path=$(go list ./... | grep -v -e'/vendor' -e'/proto$')
if [[ ${CI} ]]; then
mkdir -p .test_results/junit .test_results/coverage
go test -v ${test_path} | go-junit-report > .test_results/junit/report.xml
if [[ $COVERALLS_TOKEN ]]; then
if [[ ${COVERALLS_TOKEN} ]]; then
gocovermerge -coverprofile=.test_results/coverage/cover.out test -covermode=count ${test_path}
goveralls -coverprofile=.test_results/coverage/cover.out -service=circle-ci -repotoken=${COVERALLS_TOKEN}
fi
Expand All @@ -86,21 +99,21 @@ build_test() {
}

build_clean() {
rm -f ${BINARIES} ${PROTOBUF_GO}
rm -f ${BINARIES}
}

build_distclean() {
rm -rf ${BUILD_CACHE} ${BUILD_DIR}
rm -rf ${BUILD_CACHE} ${BUILD_DIR}
}

case $1 in
setup) build_setup ;;
deps) build_deps ;;
protobuf) build_protobuf ;;
binaries|bin) build_binaries ;;
binaries|bin) build_binaries ${2:-$BINARY_DIRS} ;;
test) build_test ;;
clean) build_clean ;;
distclean) build_clean; build_distclean ;;
all) build_deps; build_binaries ;;
all) build_deps; build_binaries ${2:-$BINARY_DIRS} ;;
esac

0 comments on commit b3ab6ee

Please sign in to comment.