|
1 | | -SHELL := /bin/bash |
| 1 | +SHELL :=$(shell which bash) |
| 2 | +.SHELLFLAGS =-c |
| 3 | + |
| 4 | +ifndef DEBUG |
| 5 | +.SILENT: ; |
| 6 | +endif |
| 7 | +.EXPORT_ALL_VARIABLES: ; |
| 8 | + |
| 9 | +WORKDIR =$(patsubst %/,%,$(dir $(realpath $(lastword $(MAKEFILE_LIST))))) |
| 10 | +PROJECT =$(notdir $(WORKDIR)) |
| 11 | + |
2 | 12 | DIR :=$(shell pwd) |
3 | 13 | TIME :=$(shell date '+%Y-%m-%dT%T%z') |
| 14 | +GPG_KEY ?=$(shell git config user.signingkey) |
4 | 15 |
|
5 | 16 | SRC ?=$(shell go list ./...) |
6 | | -SRCFILES ?=$(shell find . -type f -name '*.go' -not -path './vendor/*') |
| 17 | +SRCFILES ?=$(shell find . -type f -name '*.go') |
7 | 18 |
|
8 | 19 | LDFLAGS ?=-s -w -extld ld -extldflags -static |
9 | 20 | FLAGS ?=-a -installsuffix cgo -ldflags "$(LDFLAGS)" |
10 | | -GOOS ?=darwin windows linux |
11 | | -GOARCH ?=amd64 |
12 | 21 |
|
13 | | -PROJECT ?=packer-post-processor-ami-copy |
| 22 | +GOVERS =$(shell go version) |
| 23 | +GOOS =$(word 1,$(subst /, ,$(lastword $(GOVERS)))) |
| 24 | +GOARCH =$(word 2,$(subst /, ,$(lastword $(GOVERS)))) |
| 25 | +GOOSES =darwin linux windows |
| 26 | +GOARCHES =amd64 |
14 | 27 |
|
15 | 28 | .DEFAULT_GOAL := build |
16 | 29 |
|
17 | | -.PHONY: clean fix fmt generate test build help |
| 30 | +.PHONY: clean fix fmt generate test build tag help |
18 | 31 |
|
19 | | -clean:: ## Removes binary and generated files |
20 | | - @echo ">> cleaning" |
21 | | - @rm -rf $(PROJECT)* |
| 32 | +clean: ## Removes binary and generated files |
| 33 | + echo >&2 ">> cleaning" |
| 34 | + rm -rf $(PROJECT)* |
22 | 35 |
|
23 | | -fix:: ## Runs the Golang fix tool |
24 | | - @echo ">> fixing" |
25 | | - @go fix $(SRC) |
| 36 | +fix: ## Runs the Golang fix tool |
| 37 | + echo >&2 ">> fixing" |
| 38 | + go fix $(SRC) |
26 | 39 |
|
27 | | -fmt:: ## Formats source code according to the Go standard |
28 | | - @echo ">> formatting" |
29 | | - @gofmt -w -s -l $(SRCFILES) |
| 40 | +fmt: ## Formats source code according to the Go standard |
| 41 | + echo >&2 ">> formatting" |
| 42 | + gofmt -w -s -l $(SRCFILES) |
30 | 43 |
|
31 | | -generate:: ## Runs the Golang generate tool |
32 | | - @echo ">> generating" |
33 | | - @go install github.com/hashicorp/packer/cmd/mapstructure-to-hcl2 |
34 | | - @go generate ./... |
| 44 | +generate: ## Runs the Golang generate tool |
| 45 | + echo >&2 ">> generating" |
| 46 | + go install github.com/hashicorp/packer/cmd/mapstructure-to-hcl2 |
| 47 | + go generate ./... |
35 | 48 |
|
36 | 49 | test: clean fix fmt generate |
37 | 50 |
|
38 | | -build:: test ## Builds for all arch ($GOARCH) and OS ($GOOS) |
39 | | - @echo ">> building" |
40 | | - @for arch in ${GOARCH}; do \ |
41 | | - for os in ${GOOS}; do \ |
42 | | - echo ">>>> $${os}/$${arch}"; \ |
43 | | - env GOOS=$${os} GOARCH=$${arch} \ |
44 | | - CGO_ENABLED=0 go build $(FLAGS) \ |
45 | | - -o $(PROJECT)-$${os}-$${arch}; \ |
46 | | - done; \ |
47 | | - done |
| 51 | +# Create a cross-compile target for every os/arch pairing. This will generate a |
| 52 | +# non-phony make target for each os/arch pair as well as a phony meta target |
| 53 | +# (build) for compiling everything. |
| 54 | +_build: |
| 55 | + echo >&2 ">> building" |
| 56 | +.PHONY: _build |
| 57 | +define build-target |
| 58 | + $(PROJECT)-$(1)-$(2)$(3): |
| 59 | + ifeq (,$(findstring $(1)-$(2),$(NOARCHES))) |
| 60 | + echo >&2 ">>>> $$@" |
| 61 | + env GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 go build $(FLAGS) -o $$@ |
| 62 | + endif |
| 63 | + |
| 64 | + build: _build $(PROJECT)-$(1)-$(2)$(3) |
| 65 | +endef |
| 66 | +$(foreach goarch,$(GOARCHES), \ |
| 67 | + $(foreach goos,$(GOOSES), \ |
| 68 | + $(eval \ |
| 69 | + $(call build-target,$(goos),$(goarch),$(if \ |
| 70 | + $(findstring windows,$(goos)),.exe,)\ |
| 71 | + ) \ |
| 72 | + ) \ |
| 73 | + ) \ |
| 74 | +) |
| 75 | +build: ## Build for every supported OS and arch combination |
| 76 | +.PHONY: build |
| 77 | + |
| 78 | +tag: ## Create a signed commit and tag |
| 79 | + echo >&2 ">> tagging" |
| 80 | + if [[ ! $(VERSION) =~ ^[0-9]+[.][0-9]+([.][0.9]*)?$ ]]; then \ |
| 81 | + echo >&2 "ERROR: VERSION ($(VERSION)) is not a semantic version"; \ |
| 82 | + exit 1; \ |
| 83 | + fi |
| 84 | + echo >&2 ">>>> v$(VERSION)" |
| 85 | + git commit \ |
| 86 | + --allow-empty \ |
| 87 | + --gpg-sign="$(GPG_KEY)" \ |
| 88 | + --message "Release v$(VERSION)" \ |
| 89 | + --quiet \ |
| 90 | + --signoff |
| 91 | + git tag \ |
| 92 | + --annotate \ |
| 93 | + --create-reflog \ |
| 94 | + --local-user "$(GPG_KEY)" \ |
| 95 | + --message "Version $(VERSION)" \ |
| 96 | + --sign \ |
| 97 | + "v$(VERSION)" master |
| 98 | +.PHONY: tag |
48 | 99 |
|
49 | 100 | # A help target including self-documenting targets (see the awk statement) |
| 101 | +help: FORMAT="\033[36m%-30s\033[0m %s\n" |
50 | 102 | help: ## This help target |
51 | | - @awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / \ |
52 | | - {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) |
| 103 | + awk 'BEGIN {FS = ":.*?## "} /^[%a-zA-Z_-]+:.*?## / \ |
| 104 | + {printf $(FORMAT), $$1, $$2}' $(MAKEFILE_LIST) |
| 105 | + printf $(FORMAT) $(PROJECT)-%-% \ |
| 106 | + "Build for a specific OS and arch (where '%' = OS, arch)" |
| 107 | +.PHONY: help |
0 commit comments