-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
154 lines (116 loc) · 5.55 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
142
143
144
145
146
147
148
149
150
151
152
153
154
PROJECT :=ocf-scheduler-cf-plugin
SHELL :=/bin/bash
GOOS :=$(shell go env GOOS)
GOARCH :=$(shell go env GOARCH)
GOMODULECMD :=main
RELEASE_ROOT ?=releases
DEV_TEST_BUILD =./$(PROJECT)
TARGETS ?=linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64
define is_not_number
$(shell echo ${1} | sed -e 's/[0123456789]//g')
endef
ifneq ($(VERSION),)
VERSION_SPLIT:=$(subst ., ,$(VERSION))
ifneq ($(words $(VERSION_SPLIT)),3)
$(error VERSION does not have 3 parts |$(words $(VERSION_SPLIT))|$(VERSION)|$(VERSION_SPLIT)|)
endif
else
VERSION_TAG:=$(shell (git describe --tags --abbrev=0 2>/dev/null || echo 0.0.0) | sed -e "s/^v//")
VERSION_SPLIT:=$(subst ., ,$(VERSION_TAG))
ifneq ($(words $(VERSION_SPLIT)),3)
$(error VERSION_TAG does not have 3 parts |$(words $(VERSION_SPLIT))|$(VERSION_TAG)|$(VERSION_SPLIT)|)
endif
ifneq ($(words $(call is_not_number,$(word 3,$(VERSION_SPLIT)))), 0)
$(error The VERSION_TAG patch version string contain non-numeric characters)
endif
VERSION_SPLIT:=$(wordlist 1, 2, $(VERSION_SPLIT)) $(shell echo $$(($(word 3,$(VERSION_SPLIT))+1)))
endif
ifneq ($(words $(call is_not_number,$(VERSION_SPLIT))), 0)
$(error The version string contain non-numeric characters)
endif
SEMVER_MAJOR ?=$(word 1,$(VERSION_SPLIT))
SEMVER_MINOR ?=$(word 2,$(VERSION_SPLIT))
SEMVER_PATCH ?=$(word 3,$(VERSION_SPLIT))
SEMVER_PRERELEASE ?=
SEMVER_BUILDMETA ?=
BUILD_DATE :=$(shell date -u -Iseconds)
BUILD_VCS_URL :=$(shell git config --get remote.origin.url)
BUILD_VCS_ID :=$(shell git log -n 1 --date=iso-strict-local --format="%h")
BUILD_VCS_ID_DATE :=$(shell TZ=UTC0 git log -n 1 --date=iso-strict-local --format='%ad')
build: SEMVER_PRERELEASE := dev
GO_LDFLAGS = -X '$(GOMODULECMD).SemVerMajor=$(SEMVER_MAJOR)' \
-X '$(GOMODULECMD).SemVerMinor=$(SEMVER_MINOR)' \
-X '$(GOMODULECMD).SemVerPatch=$(SEMVER_PATCH)' \
-X '$(GOMODULECMD).SemVerPrerelease=$(SEMVER_PRERELEASE)' \
-X '$(GOMODULECMD).SemVerBuild=$(SEMVER_BUILDMETA)' \
-X '$(GOMODULECMD).BuildDate=$(BUILD_DATE)' \
-X '$(GOMODULECMD).BuildVcsUrl=$(BUILD_VCS_URL)' \
-X '$(GOMODULECMD).BuildVcsId=$(BUILD_VCS_ID)' \
-X '$(GOMODULECMD).BuildVcsIdDate=$(BUILD_VCS_ID_DATE)'
# The build meta data is added when the build is done
#
SEMVER_VERSION := $(if $(SEMVER_MAJOR),$(SEMVER_MAJOR),$(error Missing SEMVER_MAJOR))
SEMVER_VERSION := $(SEMVER_VERSION)$(if $(SEMVER_MINOR),.$(SEMVER_MINOR),$(error Missing SEMVER_MINOR))
SEMVER_VERSION := $(SEMVER_VERSION)$(if $(SEMVER_PATCH),.$(SEMVER_PATCH),$(error Missing SEMVER_PATCH))
SEMVER_VERSION := $(SEMVER_VERSION)$(if $(SEMVER_PRERELEASE),-$(SEMVER_PRERELEASE))
# GMake rules generally used for local development
.PHONY: build clean install acceptance-tests
build: BUILD_GO_LDFLAGS:=-ldflags="$(GO_LDFLAGS) -X '$(GOMODULECMD).GoOs=$(GOOS)' -X '$(GOMODULECMD).GoArch=$(GOARCH)'"
build: BUILD_RULE_CMD := CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) \
go build $(BUILD_GO_LDFLAGS) -o $(DEV_TEST_BUILD)
build: clean
@echo "Building $(DEV_TEST_BUILD)"
$(BUILD_RULE_CMD)
clean:
@rm -f $(DEV_TEST_BUILD) || true
install: build
cf install-plugin $(DEV_TEST_BUILD) -f || true
acceptance-tests:
go test -timeout 600s ./...
# GMake rules for release building are below
.PHONY: distbuild require-% release-% ci-release clean distclean show-releases
require-%:
@ if [ "${${*}}" = "" ]; then \
echo "Environment variable $* not set"; \
exit 1; \
fi
RELEASES := $(foreach target,$(TARGETS),release-$(target)-$(PROJECT))
show-releases:
@ls -lA $(RELEASE_ROOT)
@echo ""
ci-release: require-VERSION release-all
release-all: release-clean distbuild $(RELEASES) show-releases
distbuild:
@mkdir -p $(RELEASE_ROOT)
# Arguments os,arch,build
define build-target
release-$(1)/$(2)-$(PROJECT): RELEASE_GO_LDFLAGS:=-ldflags="$(GO_LDFLAGS) -X '$(GOMODULECMD).GoOs=$(1)' -X '$(GOMODULECMD).GoArch=$(2)'"
release-$(1)/$(2)-$(PROJECT): RELEASE_EXECUTABLE_BASE:=$(RELEASE_ROOT)/$(PROJECT)-$(SEMVER_VERSION)+$(1).$(2)$(if $(3),.$(3))
release-$(1)/$(2)-$(PROJECT): RELEASE_EXECUTABLE:=$$(RELEASE_EXECUTABLE_BASE)$(if $(patsubst windows,,$(1)),,.exe)
release-$(1)/$(2)-$(PROJECT): RELEASE_EXECUTABLE_SHA1:=$$(RELEASE_EXECUTABLE_BASE).sha1
release-$(1)/$(2)-$(PROJECT):
@echo "Building $$(PROJECT) version $$(SEMVER_VERSION) for $(1) $(2) ..."
@CGO_ENABLED=0 GOOS=$(1) GOARCH=$(2) go build -o $$(RELEASE_EXECUTABLE) $$(RELEASE_GO_LDFLAGS)
@openssl sha1 -r $$(RELEASE_EXECUTABLE) > $$(RELEASE_EXECUTABLE_SHA1)
endef
$(foreach target,$(TARGETS), $(eval $(call build-target,$(word 1, $(subst /, ,$(target))),$(word 2, $(subst /, ,$(target))),$(SEMVER_BUILDMETA))))
release-clean:
@rm -f $(RELEASE_ROOT)/$(PROJECT)-* || true
@[[ ! -d $(RELEASE_ROOT) ]] || rmdir -p $(RELEASE_ROOT)
distclean: clean release-clean
# REMOTE_HOST := $(shell [ -f .ssh-remote ] && cat .ssh-remote || echo '')
# REMOTE_FOLDER := ~/programs/ocf-scheduler-cf-plugin/ocf-scheduler-cf-plugin
# docker-build:
# docker build -t ocf-scheduler-cf-plugin .
#
# mkdir -p build-output
#
# docker container create --name build ocf-scheduler-cf-plugin
# docker container cp build:/bin/ocf-scheduler-cf-plugin ./build-output
# docker container rm build
# install-remote:
# scp build-output/ocf-scheduler-cf-plugin $(REMOTE_HOST):$(REMOTE_FOLDER)/ocf-scheduler-cf-plugin
# ssh $(REMOTE_HOST) "cd $(REMOTE_FOLDER); cf uninstall-plugin OCFScheduler || true; yes | cf install-plugin ocf-scheduler-cf-plugin"
# run-remote: docker-build install-remote
#
.DEFAULT_GOAL := ci-release