Debian Codegraph @

Makefile

raw
1 # Make does not offer a recursive wildcard function, so here's one:
2 rwildcard=$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
3
4 SHELL := /bin/bash
5 NAME := docker-credential-acr-env
6 BUILD_TARGET = build
7 MAIN_SRC_FILE=./main.go
8 GO := GO111MODULE=on go
9 GO_NOMOD :=GO111MODULE=off go
10 REV := $(shell git rev-parse --short HEAD 2> /dev/null || echo 'unknown')
11 ORG := chrismellard
12 ORG_REPO := $(ORG)/$(NAME)
13 RELEASE_ORG_REPO := $(ORG_REPO)
14 ROOT_PACKAGE := github.com/$(ORG_REPO)
15 GO_VERSION := $(shell $(GO) version | sed -e 's/^[^0-9.]*\([0-9.]*\).*/\1/')
16 GO_DEPENDENCIES := $(call rwildcard,pkg/,*.go) $(call rwildcard,cmd/,*.go)
17
18 BRANCH := $(shell git rev-parse --abbrev-ref HEAD 2> /dev/null || echo 'unknown')
19 BUILD_DATE := $(shell date +%Y%m%d-%H:%M:%S)
20 CGO_ENABLED = 0
21
22 REPORTS_DIR=$(BUILD_TARGET)/reports
23
24 GOTEST := $(GO) test
25
26 # set dev version unless VERSION is explicitly set via environment
27 VERSION ?= $(shell echo "$$(git for-each-ref refs/tags/ --count=1 --sort=-version:refname --format='%(refname:short)' 2>/dev/null)-dev+$(REV)" | sed 's/^v//')
28
29 # Build flags for setting build-specific configuration at build time - defaults to empty
30 #BUILD_TIME_CONFIG_FLAGS ?= ""
31
32 # Full build flags used when building binaries. Not used for test compilation/execution.
33 BUILDFLAGS := -ldflags \
34 " -X $(ROOT_PACKAGE)/pkg/cmd/version.Version=$(VERSION)\
35 -X github.com/chrismellard/docker-credential-acr-env/pkg/cmd/version.Version=$(VERSION)\
36 -X $(ROOT_PACKAGE)/pkg/cmd/version.Revision='$(REV)'\
37 -X $(ROOT_PACKAGE)/pkg/cmd/version.Branch='$(BRANCH)'\
38 -X $(ROOT_PACKAGE)/pkg/cmd/version.BuildDate='$(BUILD_DATE)'\
39 -X $(ROOT_PACKAGE)/pkg/cmd/version.GoVersion='$(GO_VERSION)'\
40 $(BUILD_TIME_CONFIG_FLAGS)"
41
42 # Some tests expect default values for version.*, so just use the config package values there.
43 TEST_BUILDFLAGS := -ldflags "$(BUILD_TIME_CONFIG_FLAGS)"
44
45 ifdef DEBUG
46 BUILDFLAGS := -gcflags "all=-N -l" $(BUILDFLAGS)
47 endif
48
49 ifdef PARALLEL_BUILDS
50 BUILDFLAGS += -p $(PARALLEL_BUILDS)
51 GOTEST += -p $(PARALLEL_BUILDS)
52 else
53 # -p 4 seems to work well for people
54 GOTEST += -p 4
55 endif
56
57 ifdef DISABLE_TEST_CACHING
58 GOTEST += -count=1
59 endif
60
61 TEST_PACKAGE ?= ./...
62 COVER_OUT:=$(REPORTS_DIR)/cover.out
63 COVERFLAGS=-coverprofile=$(COVER_OUT) --covermode=count --coverpkg=./...
64
65 .PHONY: list
66 list: ## List all make targets
67 @$(MAKE) -pRrn : -f $(MAKEFILE_LIST) 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | sort
68
69 .PHONY: help
70 .DEFAULT_GOAL := help
71 help:
72 @grep -h -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
73
74 full: check ## Build and run the tests
75 check: build test ## Build and run the tests
76 get-test-deps: ## Install test dependencies
77 $(GO_NOMOD) get github.com/axw/gocov/gocov
78 $(GO_NOMOD) get -u gopkg.in/matm/v1/gocov-html
79
80 print-version: ## Print version
81 @echo $(VERSION)
82
83 build: $(GO_DEPENDENCIES) clean ## Build binary for current OS
84 go mod download
85 CGO_ENABLED=$(CGO_ENABLED) GOARCH=$(GOARCH) GOOS=$(GOOS) $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/$(NAME) $(MAIN_SRC_FILE)
86
87 build-all: $(GO_DEPENDENCIES) build make-reports-dir ## Build all files - runtime, all tests etc.
88 CGO_ENABLED=$(CGO_ENABLED) GOARCH=$(GOARCH) GOOS=$(GOOS) $(GOTEST) -run=nope -tags=integration -failfast -short ./... $(BUILDFLAGS)
89
90 tidy-deps: ## Cleans up dependencies
91 $(GO) mod tidy
92 # mod tidy only takes compile dependencies into account, let's make sure we capture tooling dependencies as well
93 @$(MAKE) install-generate-deps
94
95 .PHONY: make-reports-dir
96 make-reports-dir:
97 mkdir -p $(REPORTS_DIR)
98
99 test: ## Run tests with the "unit" build tag
100 CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) --tags=unit -failfast -short ./... $(TEST_BUILDFLAGS)
101
102 test-coverage : make-reports-dir ## Run tests and coverage for all tests with the "unit" build tag
103 CGO_ENABLED=$(CGO_ENABLED) $(GOTEST) --tags=unit $(COVERFLAGS) -failfast -short ./... $(TEST_BUILDFLAGS)
104
105 test-report: make-reports-dir get-test-deps test-coverage ## Create the test report
106 @gocov convert $(COVER_OUT) | gocov report
107
108 test-report-html: make-reports-dir get-test-deps test-coverage ## Create the test report in HTML format
109 @gocov convert $(COVER_OUT) | gocov-html > $(REPORTS_DIR)/cover.html && open $(REPORTS_DIR)/cover.html
110
111 install: $(GO_DEPENDENCIES) ## Install the binary
112 GOBIN=${GOPATH}/bin $(GO) install $(BUILDFLAGS) $(MAIN_SRC_FILE)
113
114 linux: ## Build for Linux
115 CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/linux/$(NAME) $(MAIN_SRC_FILE)
116 chmod +x build/linux/$(NAME)
117
118 arm: ## Build for ARM
119 CGO_ENABLED=$(CGO_ENABLED) GOOS=linux GOARCH=arm $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/arm/$(NAME) $(MAIN_SRC_FILE)
120 chmod +x build/arm/$(NAME)
121
122 win: ## Build for Windows
123 CGO_ENABLED=$(CGO_ENABLED) GOOS=windows GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/win/$(NAME)-windows-amd64.exe $(MAIN_SRC_FILE)
124
125 darwin: ## Build for OSX
126 CGO_ENABLED=$(CGO_ENABLED) GOOS=darwin GOARCH=amd64 $(GO) $(BUILD_TARGET) $(BUILDFLAGS) -o build/darwin/$(NAME) $(MAIN_SRC_FILE)
127 chmod +x build/darwin/$(NAME)
128
129 .PHONY: release
130 release: clean linux test
131
132 release-all: release linux win darwin
133
134 promoter:
135 cd promote && go build main.go
136
137 .PHONY: goreleaser
138 goreleaser:
139 step-go-releaser --organisation=$(ORG) --revision=$(REV) --branch=$(BRANCH) --build-date=$(BUILD_DATE) --go-version=$(GO_VERSION) --root-package=$(ROOT_PACKAGE) --version=$(VERSION) --timeout 200m
140
141 .PHONY: clean
142 clean: ## Clean the generated artifacts
143 rm -rf build release dist
144
145 get-fmt-deps: ## Install test dependencies
146 $(GO_NOMOD) get golang.org/x/tools/cmd/goimports
147
148 .PHONY: fmt
149 fmt: importfmt ## Format the code
150 $(eval FORMATTED = $(shell $(GO) fmt ./...))
151 @if [ "$(FORMATTED)" == "" ]; \
152 then \
153 echo "All Go files properly formatted"; \
154 else \
155 echo "Fixed formatting for: $(FORMATTED)"; \
156 fi
157
158 .PHONY: importfmt
159 importfmt: get-fmt-deps
160 @echo "Formatting the imports..."
161 goimports -w $(GO_DEPENDENCIES)
162
163 .PHONY: lint
164 lint: ## Lint the code
165 ./hack/gofmt.sh
166 ./hack/linter.sh
167 ./hack/generate.sh
168
169 .PHONY: all
170 all: fmt build test lint generate-refdocs
171
172 install-refdocs:
173 $(GO) get github.com/jenkins-x/gen-crd-api-reference-docs
174
175 generate-refdocs: install-refdocs
176 gen-crd-api-reference-docs -config "hack/configdocs/config.json" \
177 -template-dir hack/configdocs/templates \
178 -api-dir "./pkg/apis/gitops/v1alpha1" \
179 -out-file docs/config.md
180
181 bin/docs:
182 go build $(LDFLAGS) -v -o bin/docs cmd/docs/*.go
183
184 .PHONY: docs
185 docs: bin/docs generate-refdocs ## update docs
186 @echo "Generating docs"
187 @./bin/docs --target=./docs/cmd
188 @./bin/docs --target=./docs/man/man1 --kind=man
189 @rm -f ./bin/docs