|
1 | | -# This is how we want to name the binary output |
2 | | -APP_NAME ?= ocf-scheduler-cf-plugin |
3 | | -VERSION ?= `./scripts/genver`-dev |
| 1 | +PROJECT :=ocf-scheduler-cf-plugin |
| 2 | +SHELL :=/bin/bash |
| 3 | +GOOS :=$(shell go env GOOS) |
| 4 | +GOARCH :=$(shell go env GOARCH) |
| 5 | +GOMODULECMD :=main |
| 6 | +RELEASE_ROOT ?=releases |
| 7 | +DEV_TEST_BUILD =./$(PROJECT) |
| 8 | +TARGETS ?=linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64 |
| 9 | + |
| 10 | +define is_not_number |
| 11 | +$(shell echo ${1} | sed -e 's/[0123456789]//g') |
| 12 | +endef |
| 13 | + |
| 14 | +ifneq ($(VERSION),) |
| 15 | +VERSION_SPLIT:=$(subst ., ,$(VERSION)) |
| 16 | + ifneq ($(words $(VERSION_SPLIT)),3) |
| 17 | + $(error VERSION does not have 3 parts |$(words $(VERSION_SPLIT))|$(VERSION)|$(VERSION_SPLIT)|) |
| 18 | + endif |
| 19 | +else |
| 20 | +VERSION_TAG:=$(shell (git describe --tags --abbrev=0 2>/dev/null || echo 0.0.0) | sed -e "s/^v//") |
| 21 | +VERSION_SPLIT:=$(subst ., ,$(VERSION_TAG)) |
| 22 | + ifneq ($(words $(VERSION_SPLIT)),3) |
| 23 | + $(error VERSION_TAG does not have 3 parts |$(words $(VERSION_SPLIT))|$(VERSION_TAG)|$(VERSION_SPLIT)|) |
| 24 | + endif |
| 25 | + |
| 26 | + ifneq ($(words $(call is_not_number,$(word 3,$(VERSION_SPLIT)))), 0) |
| 27 | + $(error The VERSION_TAG patch version string contain non-numeric characters) |
| 28 | + endif |
| 29 | + |
| 30 | + VERSION_SPLIT:=$(wordlist 1, 2, $(VERSION_SPLIT)) $(shell echo $$(($(word 3,$(VERSION_SPLIT))+1))) |
| 31 | +endif |
| 32 | + |
| 33 | +ifneq ($(words $(call is_not_number,$(VERSION_SPLIT))), 0) |
| 34 | + $(error The version string contain non-numeric characters) |
| 35 | +endif |
| 36 | + |
| 37 | +SEMVER_MAJOR ?=$(word 1,$(VERSION_SPLIT)) |
| 38 | +SEMVER_MINOR ?=$(word 2,$(VERSION_SPLIT)) |
| 39 | +SEMVER_PATCH ?=$(word 3,$(VERSION_SPLIT)) |
| 40 | +SEMVER_PRERELEASE ?= |
| 41 | +SEMVER_BUILDMETA ?= |
| 42 | +BUILD_DATE :=$(shell date -u -Iseconds) |
| 43 | +BUILD_VCS_URL :=$(shell git config --get remote.origin.url) |
| 44 | +BUILD_VCS_ID :=$(shell git log -n 1 --date=iso-strict-local --format="%h") |
| 45 | +BUILD_VCS_ID_DATE :=$(shell TZ=UTC0 git log -n 1 --date=iso-strict-local --format='%ad') |
| 46 | + |
| 47 | +build: SEMVER_PRERELEASE := dev |
| 48 | + |
| 49 | +GO_LDFLAGS = -X '$(GOMODULECMD).SemVerMajor=$(SEMVER_MAJOR)' \ |
| 50 | + -X '$(GOMODULECMD).SemVerMinor=$(SEMVER_MINOR)' \ |
| 51 | + -X '$(GOMODULECMD).SemVerPatch=$(SEMVER_PATCH)' \ |
| 52 | + -X '$(GOMODULECMD).SemVerPrerelease=$(SEMVER_PRERELEASE)' \ |
| 53 | + -X '$(GOMODULECMD).SemVerBuild=$(SEMVER_BUILDMETA)' \ |
| 54 | + -X '$(GOMODULECMD).BuildDate=$(BUILD_DATE)' \ |
| 55 | + -X '$(GOMODULECMD).BuildVcsUrl=$(BUILD_VCS_URL)' \ |
| 56 | + -X '$(GOMODULECMD).BuildVcsId=$(BUILD_VCS_ID)' \ |
| 57 | + -X '$(GOMODULECMD).BuildVcsIdDate=$(BUILD_VCS_ID_DATE)' |
| 58 | + |
| 59 | +# The build meta data is added when the build is done |
| 60 | +# |
| 61 | +SEMVER_VERSION := $(if $(SEMVER_MAJOR),$(SEMVER_MAJOR),$(error Missing SEMVER_MAJOR)) |
| 62 | +SEMVER_VERSION := $(SEMVER_VERSION)$(if $(SEMVER_MINOR),.$(SEMVER_MINOR),$(error Missing SEMVER_MINOR)) |
| 63 | +SEMVER_VERSION := $(SEMVER_VERSION)$(if $(SEMVER_PATCH),.$(SEMVER_PATCH),$(error Missing SEMVER_PATCH)) |
| 64 | +SEMVER_VERSION := $(SEMVER_VERSION)$(if $(SEMVER_PRERELEASE),-$(SEMVER_PRERELEASE)) |
| 65 | + |
| 66 | +# GMake rules generally used for local development |
| 67 | + |
| 68 | +.PHONY: build clean install acceptance-tests |
| 69 | + |
| 70 | +build: BUILD_GO_LDFLAGS:=-ldflags="$(GO_LDFLAGS) -X '$(GOMODULECMD).GoOs=$(GOOS)' -X '$(GOMODULECMD).GoArch=$(GOARCH)'" |
| 71 | + |
| 72 | +build: BUILD_RULE_CMD := CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) \ |
| 73 | + go build $(BUILD_GO_LDFLAGS) -o $(DEV_TEST_BUILD) |
4 | 74 |
|
5 | | -MODULE ?= github.com/cloudfoundry-community/ocf-scheduler-cf-plugin |
6 | | -CMD_PATH ?= . |
7 | | -CGO_ENABLED ?= 0 |
8 | | -BUILD_PATH=builds |
9 | | -BUILD=${APP_NAME}-${VERSION} |
10 | | -PACKAGE=${MODULE}/${CMD_PATH} |
| 75 | +build: clean |
| 76 | + @echo "Building $(DEV_TEST_BUILD)" |
| 77 | + $(BUILD_RULE_CMD) |
11 | 78 |
|
12 | | -SHELL:=/bin/bash |
13 | | -GO_LDFLAGS := $(shell echo "-ldflags='-X main.Version=$(VERSION)'" | sed -e 's/-rc./-rc/') #plugins can't have periods after rc in version |
| 79 | +clean: |
| 80 | + @rm -f $(DEV_TEST_BUILD) || true |
14 | 81 |
|
15 | | -REMOTE_HOST := $(shell [ -f .ssh-remote ] && cat .ssh-remote || echo '') |
16 | | -REMOTE_FOLDER := ~/programs/ocf-scheduler-cf-plugin/ocf-scheduler-cf-plugin |
| 82 | +install: build |
| 83 | + cf install-plugin $(DEV_TEST_BUILD) -f || true |
17 | 84 |
|
18 | | -build: clean |
19 | | - go build $(GO_LDFLAGS) . |
| 85 | +acceptance-tests: |
| 86 | + go test -timeout 600s ./... |
20 | 87 |
|
21 | | -# Cleans our project: deletes binaries |
22 | | -clean: |
23 | | - rm -rf ${APP_NAME} |
| 88 | +# GMake rules for release building are below |
| 89 | + |
| 90 | +.PHONY: distbuild require-% release-% ci-release clean distclean show-releases |
| 91 | + |
| 92 | +require-%: |
| 93 | + @ if [ "${${*}}" = "" ]; then \ |
| 94 | + echo "Environment variable $* not set"; \ |
| 95 | + exit 1; \ |
| 96 | + fi |
24 | 97 |
|
25 | | -# Releases |
26 | | -release: distclean distbuild linux darwin windows |
| 98 | +RELEASES := $(foreach target,$(TARGETS),release-$(target)-$(PROJECT)) |
| 99 | + |
| 100 | +show-releases: |
| 101 | + @ls -lA $(RELEASE_ROOT) |
| 102 | + @echo "" |
| 103 | + |
| 104 | +ci-release: require-VERSION release-all |
| 105 | + |
| 106 | +release-all: release-clean distbuild $(RELEASES) show-releases |
27 | 107 |
|
28 | 108 | distbuild: |
29 | | - mkdir -p ${BUILD_PATH} |
| 109 | + @mkdir -p $(RELEASE_ROOT) |
30 | 110 |
|
31 | | -distclean: |
32 | | - rm -rf ${BUILD_PATH} |
| 111 | +# Arguments os,arch,build |
| 112 | +define build-target |
| 113 | +release-$(1)/$(2)-$(PROJECT): RELEASE_GO_LDFLAGS:=-ldflags="$(GO_LDFLAGS) -X '$(GOMODULECMD).GoOs=$(1)' -X '$(GOMODULECMD).GoArch=$(2)'" |
33 | 114 |
|
34 | | -linux: |
35 | | - CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ${GO_LDFLAGS} -o ${BUILD_PATH}/${BUILD}-linux-amd64 ${PACKAGE} |
36 | | - CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build ${GO_LDFLAGS} -o ${BUILD_PATH}/${BUILD}-linux-arm64 ${PACKAGE} |
| 115 | +release-$(1)/$(2)-$(PROJECT): RELEASE_EXECUTABLE_BASE:=$(RELEASE_ROOT)/$(PROJECT)-$(SEMVER_VERSION)+$(1).$(2)$(if $(3),.$(3)) |
37 | 116 |
|
38 | | -darwin: |
39 | | - CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build ${GO_LDFLAGS} -o ${BUILD_PATH}/${BUILD}-darwin-amd64 ${PACKAGE} |
40 | | - CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build ${GO_LDFLAGS} -o ${BUILD_PATH}/${BUILD}-darwin-arm64 ${PACKAGE} |
41 | | - |
42 | | -windows: |
43 | | - CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build ${GO_LDFLAGS} -o ${BUILD_PATH}/${BUILD}-windows-amd64.exe ${PACKAGE} |
| 117 | +release-$(1)/$(2)-$(PROJECT): RELEASE_EXECUTABLE:=$$(RELEASE_EXECUTABLE_BASE)$(if $(patsubst windows,,$(1)),,.exe) |
44 | 118 |
|
45 | | -docker-build: |
46 | | - docker build -t ocf-scheduler-cf-plugin . |
| 119 | +release-$(1)/$(2)-$(PROJECT): RELEASE_EXECUTABLE_SHA1:=$$(RELEASE_EXECUTABLE_BASE).sha1 |
47 | 120 |
|
48 | | - mkdir -p build-output |
| 121 | +release-$(1)/$(2)-$(PROJECT): |
| 122 | + @echo "Building $$(PROJECT) version $$(SEMVER_VERSION) for $(1) $(2) ..." |
| 123 | + @CGO_ENABLED=0 GOOS=$(1) GOARCH=$(2) go build -o $$(RELEASE_EXECUTABLE) $$(RELEASE_GO_LDFLAGS) |
| 124 | + @openssl sha1 -r $$(RELEASE_EXECUTABLE) > $$(RELEASE_EXECUTABLE_SHA1) |
| 125 | +endef |
49 | 126 |
|
50 | | - docker container create --name build ocf-scheduler-cf-plugin |
51 | | - docker container cp build:/bin/ocf-scheduler-cf-plugin ./build-output |
52 | | - docker container rm build |
| 127 | +$(foreach target,$(TARGETS), $(eval $(call build-target,$(word 1, $(subst /, ,$(target))),$(word 2, $(subst /, ,$(target))),$(SEMVER_BUILDMETA)))) |
53 | 128 |
|
54 | | -install-remote: |
55 | | - scp build-output/ocf-scheduler-cf-plugin $(REMOTE_HOST):$(REMOTE_FOLDER)/ocf-scheduler-cf-plugin |
56 | | - ssh $(REMOTE_HOST) "cd $(REMOTE_FOLDER); cf uninstall-plugin OCFScheduler || true; yes | cf install-plugin ocf-scheduler-cf-plugin" |
| 129 | +release-clean: |
| 130 | + @rm -f $(RELEASE_ROOT)/$(PROJECT)-* || true |
| 131 | + @[[ ! -d $(RELEASE_ROOT) ]] || rmdir -p $(RELEASE_ROOT) |
57 | 132 |
|
58 | | -install: |
59 | | - cf uninstall-plugin OCFScheduler || true |
60 | | - yes | cf install-plugin ocf-scheduler-cf-plugin |
| 133 | +distclean: clean release-clean |
61 | 134 |
|
62 | | -acceptance-tests: |
63 | | - go test -timeout 600s ./... |
| 135 | +# REMOTE_HOST := $(shell [ -f .ssh-remote ] && cat .ssh-remote || echo '') |
| 136 | +# REMOTE_FOLDER := ~/programs/ocf-scheduler-cf-plugin/ocf-scheduler-cf-plugin |
| 137 | + |
| 138 | +# docker-build: |
| 139 | +# docker build -t ocf-scheduler-cf-plugin . |
| 140 | +# |
| 141 | +# mkdir -p build-output |
| 142 | +# |
| 143 | +# docker container create --name build ocf-scheduler-cf-plugin |
| 144 | +# docker container cp build:/bin/ocf-scheduler-cf-plugin ./build-output |
| 145 | +# docker container rm build |
| 146 | + |
| 147 | +# install-remote: |
| 148 | +# scp build-output/ocf-scheduler-cf-plugin $(REMOTE_HOST):$(REMOTE_FOLDER)/ocf-scheduler-cf-plugin |
| 149 | +# ssh $(REMOTE_HOST) "cd $(REMOTE_FOLDER); cf uninstall-plugin OCFScheduler || true; yes | cf install-plugin ocf-scheduler-cf-plugin" |
64 | 150 |
|
65 | | -all: build install |
| 151 | +# run-remote: docker-build install-remote |
| 152 | +# |
66 | 153 |
|
67 | | -run-remote: docker-build install-remote |
| 154 | +.DEFAULT_GOAL := ci-release |
0 commit comments