|
| 1 | +all: build |
| 2 | + |
| 3 | +######################################################################## |
| 4 | +## GOLANG ## |
| 5 | +######################################################################## |
| 6 | + |
| 7 | +# If GOPATH isn't defined then set its default location. |
| 8 | +ifeq (,$(strip $(GOPATH))) |
| 9 | +GOPATH := $(HOME)/go |
| 10 | +else |
| 11 | +# If GOPATH is already set then update GOPATH to be its own |
| 12 | +# first element. |
| 13 | +GOPATH := $(word 1,$(subst :, ,$(GOPATH))) |
| 14 | +endif |
| 15 | +export GOPATH |
| 16 | + |
| 17 | + |
| 18 | +######################################################################## |
| 19 | +## PROTOC ## |
| 20 | +######################################################################## |
| 21 | + |
| 22 | +# Only set PROTOC_VER if it has an empty value. |
| 23 | +ifeq (,$(strip $(PROTOC_VER))) |
| 24 | +PROTOC_VER := 3.9.1 |
| 25 | +endif |
| 26 | + |
| 27 | +PROTOC_OS := $(shell uname -s) |
| 28 | +ifeq (Darwin,$(PROTOC_OS)) |
| 29 | +PROTOC_OS := osx |
| 30 | +endif |
| 31 | + |
| 32 | +PROTOC_ARCH := $(shell uname -m) |
| 33 | +ifeq (i386,$(PROTOC_ARCH)) |
| 34 | +PROTOC_ARCH := x86_32 |
| 35 | +endif |
| 36 | + |
| 37 | +PROTOC := ./protoc |
| 38 | +PROTOC_ZIP := protoc-$(PROTOC_VER)-$(PROTOC_OS)-$(PROTOC_ARCH).zip |
| 39 | +PROTOC_URL := https://github.com/google/protobuf/releases/download/v$(PROTOC_VER)/$(PROTOC_ZIP) |
| 40 | +PROTOC_TMP_DIR := .protoc |
| 41 | +PROTOC_TMP_BIN := $(PROTOC_TMP_DIR)/bin/protoc |
| 42 | + |
| 43 | +$(PROTOC): |
| 44 | + -mkdir -p "$(PROTOC_TMP_DIR)" && \ |
| 45 | + curl -L $(PROTOC_URL) -o "$(PROTOC_TMP_DIR)/$(PROTOC_ZIP)" && \ |
| 46 | + unzip "$(PROTOC_TMP_DIR)/$(PROTOC_ZIP)" -d "$(PROTOC_TMP_DIR)" && \ |
| 47 | + chmod 0755 "$(PROTOC_TMP_BIN)" && \ |
| 48 | + cp -f "$(PROTOC_TMP_BIN)" "$@" |
| 49 | + stat "$@" > /dev/null 2>&1 |
| 50 | + |
| 51 | + |
| 52 | +######################################################################## |
| 53 | +## PROTOC-GEN-GO ## |
| 54 | +######################################################################## |
| 55 | + |
| 56 | +# This is the recipe for getting and installing the go plug-in |
| 57 | +# for protoc |
| 58 | +PROTOC_GEN_GO_PKG := github.com/golang/protobuf/protoc-gen-go |
| 59 | +PROTOC_GEN_GO := protoc-gen-go |
| 60 | +$(PROTOC_GEN_GO): PROTOBUF_PKG := $(dir $(PROTOC_GEN_GO_PKG)) |
| 61 | +$(PROTOC_GEN_GO): PROTOBUF_VERSION := v1.3.2 |
| 62 | +$(PROTOC_GEN_GO): |
| 63 | + mkdir -p $(dir $(GOPATH)/src/$(PROTOBUF_PKG)) |
| 64 | + test -d $(GOPATH)/src/$(PROTOBUF_PKG)/.git || git clone https://$(PROTOBUF_PKG) $(GOPATH)/src/$(PROTOBUF_PKG) |
| 65 | + (cd $(GOPATH)/src/$(PROTOBUF_PKG) && \ |
| 66 | + (test "$$(git describe --tags | head -1)" = "$(PROTOBUF_VERSION)" || \ |
| 67 | + (git fetch && git checkout tags/$(PROTOBUF_VERSION)))) |
| 68 | + (cd $(GOPATH)/src/$(PROTOBUF_PKG) && go get -v -d $$(go list -f '{{ .ImportPath }}' ./...)) && \ |
| 69 | + go build -o "$@" $(PROTOC_GEN_GO_PKG) |
| 70 | + |
| 71 | + |
| 72 | +######################################################################## |
| 73 | +## GEN-PROTO-GO ## |
| 74 | +######################################################################## |
| 75 | + |
| 76 | +# This is the recipe for getting and installing the gen-proto pkg |
| 77 | +# This is a dependency of grpc-go and must be installed before |
| 78 | +# installing grpc-go. |
| 79 | +GENPROTO_GO_SRC := github.com/googleapis/go-genproto |
| 80 | +GENPROTO_GO_PKG := google.golang.org/genproto |
| 81 | +GENPROTO_BUILD_GO := genproto-build-go |
| 82 | +$(GENPROTO_BUILD_GO): GENPROTO_VERSION := 24fa4b261c55da65468f2abfdae2b024eef27dfb |
| 83 | +$(GENPROTO_BUILD_GO): |
| 84 | + mkdir -p $(dir $(GOPATH)/src/$(GENPROTO_GO_PKG)) |
| 85 | + test -d $(GOPATH)/src/$(GENPROTO_GO_PKG)/.git || git clone https://$(GENPROTO_GO_SRC) $(GOPATH)/src/$(GENPROTO_GO_PKG) |
| 86 | + (cd $(GOPATH)/src/$(GENPROTO_GO_PKG) && \ |
| 87 | + (git fetch && git checkout $(GENPROTO_VERSION))) |
| 88 | + (cd $(GOPATH)/src/$(GENPROTO_GO_PKG) && go get -v -d $$(go list -f '{{ .ImportPath }}' ./...)) |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +######################################################################## |
| 93 | +## GRPC-GO ## |
| 94 | +######################################################################## |
| 95 | + |
| 96 | +# This is the recipe for getting and installing the grpc go |
| 97 | +GRPC_GO_SRC := github.com/grpc/grpc-go |
| 98 | +GRPC_GO_PKG := google.golang.org/grpc |
| 99 | +GRPC_BUILD_GO := grpc-build-go |
| 100 | +$(GRPC_BUILD_GO): GRPC_VERSION := v1.26.0 |
| 101 | +$(GRPC_BUILD_GO): |
| 102 | + mkdir -p $(dir $(GOPATH)/src/$(GRPC_GO_PKG)) |
| 103 | + test -d $(GOPATH)/src/$(GRPC_GO_PKG)/.git || git clone https://$(GRPC_GO_SRC) $(GOPATH)/src/$(GRPC_GO_PKG) |
| 104 | + (cd $(GOPATH)/src/$(GRPC_GO_PKG) && \ |
| 105 | + (test "$$(git describe --tags | head -1)" = "$(GRPC_VERSION)" || \ |
| 106 | + (git fetch && git checkout tags/$(GRPC_VERSION)))) |
| 107 | + (cd $(GOPATH)/src/$(GRPC_GO_PKG) && go get -v -d $$(go list -f '{{ .ImportPath }}' ./...) && \ |
| 108 | + go build -o "$@" $(GRPC_GO_PKG)) |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | +######################################################################## |
| 113 | +## PATH ## |
| 114 | +######################################################################## |
| 115 | + |
| 116 | +# Update PATH with the current directory. This enables the protoc |
| 117 | +# binary to discover the protoc-gen-go binary, built inside this |
| 118 | +# directory. |
| 119 | +export PATH := $(shell pwd):$(PATH) |
| 120 | + |
| 121 | + |
| 122 | +######################################################################## |
| 123 | +## BUILD ## |
| 124 | +######################################################################## |
| 125 | +COSI_PROTO := ./cosi.proto |
| 126 | +COSI_PKG_ROOT := github.com/kubernetes-sigs/container-object-storage-interface-spec |
| 127 | +COSI_PKG_SUB := . |
| 128 | +COSI_BUILD := $(COSI_PKG_SUB)/.build |
| 129 | +COSI_GO := $(COSI_PKG_SUB)/cosi.pb.go |
| 130 | +COSI_A := cosi.a |
| 131 | +COSI_GO_TMP := $(COSI_BUILD)/$(COSI_PKG_ROOT)/cosi.pb.go |
| 132 | + |
| 133 | +# This recipe generates the go language bindings to a temp area. |
| 134 | +$(COSI_GO_TMP): HERE := $(shell pwd) |
| 135 | +$(COSI_GO_TMP): PTYPES_PKG := github.com/golang/protobuf/ptypes |
| 136 | +$(COSI_GO_TMP): GO_OUT := plugins=grpc |
| 137 | +$(COSI_GO_TMP): GO_OUT := $(GO_OUT),Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor |
| 138 | +$(COSI_GO_TMP): GO_OUT := $(GO_OUT),Mgoogle/protobuf/wrappers.proto=$(PTYPES_PKG)/wrappers |
| 139 | +$(COSI_GO_TMP): GO_OUT := $(GO_OUT):"$(HERE)/$(COSI_BUILD)" |
| 140 | +$(COSI_GO_TMP): INCLUDE := -I$(GOPATH)/src -I$(HERE)/$(PROTOC_TMP_DIR)/include |
| 141 | +$(COSI_GO_TMP): $(COSI_PROTO) | $(PROTOC) $(PROTOC_GEN_GO) |
| 142 | + @mkdir -p "$(@D)" |
| 143 | + (cd "$(GOPATH)/src" && \ |
| 144 | + $(HERE)/$(PROTOC) $(INCLUDE) --go_out=$(GO_OUT) "$(COSI_PKG_ROOT)/$(<F)") |
| 145 | + |
| 146 | +# The temp language bindings are compared to the ones that are |
| 147 | +# versioned. If they are different then it means the language |
| 148 | +# bindings were not updated prior to being committed. |
| 149 | +$(COSI_GO): $(COSI_GO_TMP) |
| 150 | +ifeq (true,$(TRAVIS)) |
| 151 | + diff "$@" "$?" |
| 152 | +else |
| 153 | + @mkdir -p "$(@D)" |
| 154 | + diff "$@" "$?" > /dev/null 2>&1 || cp -f "$?" "$@" |
| 155 | +endif |
| 156 | + |
| 157 | +# This recipe builds the Go archive from the sources in three steps: |
| 158 | +# |
| 159 | +# 1. Go get any missing dependencies. |
| 160 | +# 2. Cache the packages. |
| 161 | +# 3. Build the archive file. |
| 162 | +$(COSI_A): $(COSI_GO) $(GENPROTO_BUILD_GO) $(GRPC_BUILD_GO) |
| 163 | + go get -v -d ./... |
| 164 | + go install ./$(COSI_PKG_SUB) |
| 165 | + go build -o "$@" ./$(COSI_PKG_SUB) |
| 166 | + |
| 167 | +build: $(COSI_A) |
| 168 | + |
| 169 | +clean: |
| 170 | + go clean -i ./... |
| 171 | + rm -rf "$(COSI_A)" "$(COSI_GO)" "$(COSI_BUILD)" |
| 172 | + |
| 173 | +clobber: clean |
| 174 | + rm -fr "$(PROTOC)" "$(PROTOC_GEN_GO)" "$(COSI_PKG_SUB)" |
| 175 | + |
| 176 | +.PHONY: clean clobber $(GRPC_BUILD_GO) $(GENPROTO_BUILD_GO) |
0 commit comments