-
Notifications
You must be signed in to change notification settings - Fork 67
/
builder.Makefile
95 lines (79 loc) · 3.43 KB
/
builder.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
# Copyright 2020 Docker Hub Tool authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
include vars.mk
NULL:=/dev/null
ifeq ($(COMMIT),)
COMMIT:=$(shell git rev-parse HEAD 2> $(NULL))
endif
ifeq ($(TAG_NAME),)
TAG_NAME:=$(shell git describe --tags --match "v[0-9]*" 2> $(NULL))
endif
PKG_NAME:=github.com/docker/hub-tool
STATIC_FLAGS:=CGO_ENABLED=0
LDFLAGS:="-s -w \
-X $(PKG_NAME)/internal.GitCommit=$(COMMIT) \
-X $(PKG_NAME)/internal.Version=$(TAG_NAME)"
GO_BUILD:=go build -trimpath -ldflags=$(LDFLAGS)
VARS:=BINARY_NAME=${BINARY_NAME} \
BINARY=${BINARY}
ifneq ($(strip $(E2E_TEST_NAME)),)
RUN_TEST=-test.run $(E2E_TEST_NAME)
endif
TAR_TRANSFORM:=--transform s/packaging/${BINARY_NAME}/ --transform s/bin/${BINARY_NAME}/ --transform s/${PLATFORM_BINARY}/${BINARY_NAME}/
ifneq ($(findstring bsd,$(shell tar --version)),)
TAR_TRANSFORM=-s /packaging/${BINARY_NAME}/ -s /bin/${BINARY_NAME}/ -s /${PLATFORM_BINARY}/${BINARY_NAME}/
endif
TMPDIR_WIN_PKG:=$(shell mktemp -d)
.PHONY: lint
lint:
$(STATIC_FLAGS) golangci-lint run --timeout 10m0s ./...
.PHONY: e2e
e2e:
# TODO: gotestsum doesn't forward ldflags to go test with golang 1.15.0, so moving back to go test temporarily
$(VARS) go test ./e2e $(RUN_TEST) -ldflags=$(LDFLAGS)
.PHONY: test-unit
test-unit:
$(STATIC_FLAGS) gotestsum $(shell go list ./... | grep -vE '/e2e')
.PHONY:cross
cross:
GOOS=linux GOARCH=amd64 $(STATIC_FLAGS) $(GO_BUILD) -o bin/$(BINARY_NAME)_linux_amd64 .
GOOS=linux GOARCH=arm64 $(STATIC_FLAGS) $(GO_BUILD) -o bin/$(BINARY_NAME)_linux_arm64 .
GOOS=linux GOARCH=arm $(STATIC_FLAGS) $(GO_BUILD) -o bin/$(BINARY_NAME)_linux_arm .
GOOS=darwin GOARCH=amd64 $(STATIC_FLAGS) $(GO_BUILD) -o bin/$(BINARY_NAME)_darwin_amd64 .
GOOS=darwin GOARCH=arm64 $(STATIC_FLAGS) $(GO_BUILD) -o bin/$(BINARY_NAME)_darwin_arm64 .
GOOS=windows GOARCH=amd64 $(STATIC_FLAGS) $(GO_BUILD) -o bin/$(BINARY_NAME)_windows_amd64.exe .
GOOS=windows GOARCH=arm64 $(STATIC_FLAGS) $(GO_BUILD) -o bin/$(BINARY_NAME)_windows_arm64.exe .
# Note we're building statically for now to simplify releases. We can
# investigate dynamic builds later.
.PHONY: build
build:
mkdir -p bin
$(STATIC_FLAGS) $(GO_BUILD) -o bin/$(PLATFORM_BINARY) .
cp bin/$(PLATFORM_BINARY) bin/$(BINARY)
.PHONY: package
package: build
mkdir -p dist
ifeq ($(GOOS),windows)
cp bin/$(PLATFORM_BINARY) $(TMPDIR_WIN_PKG)/$(BINARY_NAME).exe
cp packaging/LICENSE $(TMPDIR_WIN_PKG)/LICENSE
rm -f dist/$(BINARY_NAME)-windows-$(GOARCH).zip && 7z a dist/$(BINARY_NAME)-windows-$(GOARCH).zip $(TMPDIR_WIN_PKG)/*
rm -r $(TMPDIR_WIN_PKG)
else
tar -czf dist/$(BINARY_NAME)-$(GOOS)-$(GOARCH).tar.gz $(TAR_TRANSFORM) packaging/LICENSE bin/$(PLATFORM_BINARY)
endif
.PHONY: ci-extract
ci-extract:
mkdir -p bin
ifeq ($(GOOS),windows)
7z e -obin/ dist/$(BINARY_NAME)-windows-amd64.zip $(BINARY_NAME).exe
else
tar xzf dist/$(BINARY_NAME)-$(GOOS)-$(GOARCH).tar.gz --strip-components 1 --directory bin/ $(BINARY_NAME)/$(BINARY_NAME)
endif