forked from TheThingsArchive/gateway-connector-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
141 lines (98 loc) · 4.04 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
SHELL = bash
# Environment
GIT_BRANCH = $(or $(CI_BUILD_REF_NAME) ,`git rev-parse --abbrev-ref HEAD 2>/dev/null`)
GIT_COMMIT = $(or $(CI_BUILD_REF), `git rev-parse HEAD 2>/dev/null`)
GIT_TAG = $(shell git describe --abbrev=0 --tags 2>/dev/null)
BUILD_DATE = $(or $(CI_BUILD_DATE), `date -u +%Y-%m-%dT%H:%M:%SZ`)
GO_PATH = `echo $(GOPATH) | awk -F':' '{print $$1}'`
GO_SRC = `pwd | xargs dirname | xargs dirname | xargs dirname`
ifeq ($(GIT_BRANCH), $(GIT_TAG))
TTN_VERSION = $(GIT_TAG)
TAGS += prod
else
TTN_VERSION = $(GIT_TAG)-dev
TAGS += dev
endif
# All
.PHONY: all build-deps deps dev-deps protos-clean protos test cover-clean cover-deps cover coveralls fmt vet build dev link docs clean docker
all: deps build
# Deps
build-deps:
@command -v dep > /dev/null || go get "github.com/golang/dep/cmd/dep"
deps: build-deps
dep ensure -v -vendor-only
dev-deps: deps
@command -v protoc-gen-gofast > /dev/null || go get github.com/gogo/protobuf/protoc-gen-gofast
@command -v golint > /dev/null || go get github.com/golang/lint/golint
@command -v forego > /dev/null || go get github.com/ddollar/forego
# Protobuf
PROTOC = protoc \
-I/usr/local/include \
-I$(GO_PATH)/src \
--gofast_out=plugins=grpc:$(GO_SRC) \
`pwd`/
protos-clean:
rm -f types/types.pb.go status/status.pb.go
protos: types/types.pb.go status/status.pb.go
%.pb.go: %.proto
$(PROTOC)$<
# Go Test
GO_FILES = $(shell find . -name "*.go" | grep -vE ".git|.env|vendor")
GO_PACKAGES = $(shell find . -name "*.go" | grep -vE ".git|.env|vendor" | sed 's:/[^/]*$$::' | sort | uniq)
GO_TEST_PACKAGES = $(shell find . -name "*_test.go" | grep -vE ".git|.env|vendor" | sed 's:/[^/]*$$::' | sort | uniq)
GO_COVER_FILE ?= coverage.out
GO_COVER_DIR ?= .cover
GO_COVER_FILES = $(patsubst ./%, $(GO_COVER_DIR)/%.out, $(shell echo "$(GO_TEST_PACKAGES)"))
test: $(GO_FILES)
go test -v $(GO_TEST_PACKAGES)
cover-clean:
rm -rf $(GO_COVER_DIR) $(GO_COVER_FILE)
cover-deps:
@command -v goveralls > /dev/null || go get github.com/mattn/goveralls
cover: $(GO_COVER_FILE)
$(GO_COVER_FILE): cover-clean $(GO_COVER_FILES)
echo "mode: set" > $(GO_COVER_FILE)
cat $(GO_COVER_FILES) | grep -vE "mode: set" | sort >> $(GO_COVER_FILE)
$(GO_COVER_DIR)/%.out: %
@mkdir -p "$(GO_COVER_DIR)/$<"
go test -cover -coverprofile="$@" "./$<"
coveralls: cover-deps $(GO_COVER_FILE)
goveralls -coverprofile=$(GO_COVER_FILE) -service=travis-ci -repotoken $$COVERALLS_TOKEN
fmt:
[[ -z "`echo "$(GO_PACKAGES)" | xargs go fmt | tee -a /dev/stderr`" ]]
vet:
echo $(GO_PACKAGES) | xargs go vet
lint:
for pkg in `echo $(GO_PACKAGES)`; do golint $$pkg; done
# Go Build
RELEASE_DIR ?= release
GOOS ?= $(shell go env GOOS)
GOARCH ?= $(shell go env GOARCH)
GOEXE = $(shell GOOS=$(GOOS) GOARCH=$(GOARCH) go env GOEXE)
CGO_ENABLED ?= 0
DIST_FLAGS ?= -a -installsuffix cgo
splitfilename = $(subst ., ,$(subst -, ,$(subst $(RELEASE_DIR)/,,$1)))
GOOSfromfilename = $(word 4, $(call splitfilename, $1))
GOARCHfromfilename = $(word 5, $(call splitfilename, $1))
LDFLAGS = -ldflags "-w -X main.gitBranch=${GIT_BRANCH} -X main.gitCommit=${GIT_COMMIT} -X main.buildDate=${BUILD_DATE}"
GOBUILD = CGO_ENABLED=$(CGO_ENABLED) GOOS=$(call GOOSfromfilename, $@) GOARCH=$(call GOARCHfromfilename, $@) go build $(DIST_FLAGS) ${LDFLAGS} -tags "${TAGS}" -o "$@"
build: $(RELEASE_DIR)/gateway-connector-bridge-$(GOOS)-$(GOARCH)$(GOEXE)
$(RELEASE_DIR)/gateway-connector-bridge-%: $(GO_FILES)
$(GOBUILD) ./main.go
gateway-connector-bridge-dev: DIST_FLAGS=
gateway-connector-bridge-dev: CGO_ENABLED=1
gateway-connector-bridge-dev: $(RELEASE_DIR)/gateway-connector-bridge-$(GOOS)-$(GOARCH)$(GOEXE)
install:
go install -v
dev: install gateway-connector-bridge-dev
GOBIN ?= $(GO_PATH)/bin
link: build
ln -sf $(PWD)/$(RELEASE_DIR)/gateway-connector-bridge-$(GOOS)-$(GOARCH)$(GOEXE) $(GOBIN)/gateway-connector-bridge
# Clean
clean:
[ -d $(RELEASE_DIR) ] && rm -rf $(RELEASE_DIR) || [ ! -d $(RELEASE_DIR) ]
# Docker
docker: GOOS=linux
docker: GOARCH=amd64
docker: $(RELEASE_DIR)/gateway-connector-bridge-linux-amd64
docker build -t thethingsnetwork/gateway-connector-bridge -f Dockerfile .