-
Notifications
You must be signed in to change notification settings - Fork 5
/
Makefile
136 lines (111 loc) · 3.37 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
VERBOSE_ORIGINS := "command line" "environment"
ifdef V
ifeq ($(filter $(VERBOSE_ORIGINS),$(origin V)),)
BUILD_VERBOSE := $(V)
endif
endif
ifndef BUILD_VERBOSE
BUILD_VERBOSE := 0
endif
ifeq ($(BUILD_VERBOSE),1)
Q :=
else
Q := @
endif
PHONY += all test clean docker docker-push dockers dockers-push
CURDIR := $(shell pwd)
BUILD_DIR ?= $(CURDIR)/build
GOBIN_DIR := $(BUILD_DIR)/bin
HOST_DIR := $(BUILD_DIR)/host
HOSTBIN_DIR := $(HOST_DIR)/bin
GOTOOLSBIN_DIR := $(HOSTBIN_DIR)
GOTOOLS_DIR := $(CURDIR)/tools
TMP_DIR := $(BUILD_DIR)/tmp
DIRS := \
$(GOBIN_DIR) \
$(HOST_DIR) \
$(HOSTBIN_DIR) \
$(TMP_DIR)
HOST_OS := $(shell uname -s)
# Define your docker repository
DOCKER_REPOSITORY ?= quay.io/alan/$(notdir $(CURDIR))
REV ?= $(shell git rev-parse --short HEAD 2> /dev/null)
GOPATH ?= $(shell go env GOPATH)
export PATH:=$(GOTOOLS_DIR):$(HOSTBIN_DIR):$(PATH)
export REV
define app-docker-image-name
$(if $(filter-out all,$(1)), \
$(DOCKER_REPOSITORY)-$(1):$(REV), \
$(DOCKER_REPOSITORY):$(REV)\
)
endef
define find-subdir
$(shell find $(1) -maxdepth 1 -mindepth 1 -type d -o -type l)
endef
APPS := $(sort $(notdir $(call find-subdir,cmd)))
PHONY += $(APPS)
all: $(APPS)
.SECONDEXPANSION:
$(APPS): $(addprefix $(GOBIN_DIR)/,$$@)
$(DIRS) :
$(Q)mkdir -p $@
$(GOBIN_DIR)/%: $(GOBIN_DIR) FORCE
$(Q)go build -o $@ ./cmd/$(notdir $@)
@echo "Done building."
@echo "Run \"$(subst $(CURDIR),.,$@)\" to launch $(notdir $@)."
include $(wildcard build/*.mk)
CODEGEN_DEPS := \
$(GOTOOLS_DIR)/mockgen \
$(GOTOOLS_DIR)/protoc-gen-go \
$(PROTOC)
.PHONY: gen
gen: $(CODEGEN_DEPS)
$(Q)go generate -x ./...
dockers: $(addsuffix -docker,$(APPS))
%-docker:
$(eval APP=$(subst -docker,,$@))
$(Q)docker build --build-arg APP=$(APP) -t $(call app-docker-image-name,$(APP)) .
docker:
$(Q)docker build -t $(DOCKER_REPOSITORY):$(REV) .
dockers-push: $(addsuffix -docker-push,$(APPS))
%-docker-push:
$(eval APP=$(subst -docker,,$@))
$(Q)docker push $(call app-docker-image-name,$(APP))
docker-push:
$(Q)docker push $(DOCKER_REPOSITORY):$(REV)
test:
$(Q)go test -v ./...
clean:
$(Q)rm -fr $(GOBIN_DIR) $(HOST_DIR)
.PHONY: help
help:
@echo 'Generic targets:'
@echo ' all - Build all targets marked with [*]'
@for app in $(APPS); do \
printf "* %s\n" $$app; done
@echo ''
@echo 'Code generation targets:'
@echo ' gen - Generate API code from .proto files and mocks'
@echo ''
@echo 'Docker targets:'
@echo ' dockers - Build docker images marked with [*]'
@for app in $(APPS); do \
printf "* %-20s - Build %s\n" $$app-docker $(call app-docker-image-name,$$app); done
@echo ' docker - Build single docker image which includes all executables'
@echo ''
@echo ' dockers-push - Push docker images marked with [*]'
@for app in $(APPS); do \
printf "* %-20s - Push %s\n" $$app-docker-push $(call app-docker-image-name,$$app); done
@echo ' docker-push - Push $(DOCKER_REPOSITORY):$(REV)'
@echo ''
@echo 'Test targets:'
@echo ' test - Run all tests'
@echo ''
@echo 'Cleaning targets:'
@echo ' clean - Remove built executables'
@echo ''
@echo 'Execute "make" or "make all" to build all targets marked with [*] '
@echo 'For further info see the ./README.md file'
.PHONY: $(PHONY)
.PHONY: FORCE
FORCE: