|
| 1 | +# Run `make help` to display help |
| 2 | +.DEFAULT_GOAL := help |
| 3 | + |
| 4 | +# --- Global ------------------------------------------------------------------- |
| 5 | +O = out |
| 6 | +COVERAGE = 0 |
| 7 | +VERSION ?= $(shell git describe --tags --dirty --always) |
| 8 | + |
| 9 | +## Build and lint |
| 10 | +all: build lint |
| 11 | + @if [ -e .git/rebase-merge ]; then git --no-pager log -1 --pretty='%h %s'; fi |
| 12 | + @echo '$(COLOUR_GREEN)Success$(COLOUR_NORMAL)' |
| 13 | + |
| 14 | +## Full clean build and up-to-date checks as run on CI |
| 15 | +ci: clean check-uptodate all |
| 16 | + |
| 17 | +# GENERATED_FILES is apended at targets that generate or modify files that are |
| 18 | +# required to be up-to-date. |
| 19 | +GENERATED_FILES := |
| 20 | +check-uptodate: tidy godoc |
| 21 | + test -z "$$(git status --porcelain -- $(GENERATED_FILES))" || { git status; false; } |
| 22 | + |
| 23 | +## Remove generated files |
| 24 | +clean:: |
| 25 | + -rm -rf $(O) |
| 26 | + |
| 27 | +.PHONY: all check-uptodate ci clean |
| 28 | + |
| 29 | +# --- Build -------------------------------------------------------------------- |
| 30 | +BIN_NAME = flapjak |
| 31 | +GO_TAGS = |
| 32 | +GO_LDFLAGS = -X main.version=$(VERSION) |
| 33 | +GO_FLAGS += $(if $(GO_TAGS),-tags='$(GO_TAGS)') |
| 34 | +GO_FLAGS += $(if $(GO_LDFLAGS),-ldflags='$(GO_LDFLAGS)') |
| 35 | +GO_BIN_SUFFIX = $(if $(GOOS),_$(GOOS))$(if $(GOARCH),_$(GOARCH)) |
| 36 | +GO_BIN_NAME = $(BIN_NAME)$(GO_BIN_SUFFIX) |
| 37 | + |
| 38 | +## Build flapjak binary |
| 39 | +build: | $(O) |
| 40 | + go build -o $(O)/$(GO_BIN_NAME) $(GO_FLAGS) . |
| 41 | + |
| 42 | +GENERATED_FILES += go.mod go.sum |
| 43 | +## Tidy go modules with "go mod tidy" |
| 44 | +tidy: |
| 45 | + go mod tidy |
| 46 | + |
| 47 | +.PHONY: build tidy |
| 48 | + |
| 49 | +# --- Lint --------------------------------------------------------------------- |
| 50 | +## Lint go source code |
| 51 | +lint: |
| 52 | + golangci-lint run |
| 53 | + |
| 54 | +.PHONY: lint |
| 55 | + |
| 56 | +# --- Docs --------------------------------------------------------------------- |
| 57 | + |
| 58 | +GENERATED_FILES += main.go |
| 59 | +## Generate Go doc comment for command with usage. |
| 60 | +godoc: build |
| 61 | + ./bin/gengodoc.awk main.go > $(O)/out.go |
| 62 | + mv $(O)/out.go main.go |
| 63 | + |
| 64 | +.PHONY: godoc |
| 65 | + |
| 66 | +# --- Release ------------------------------------------------------------------ |
| 67 | +RELEASE_DIR = $(O)/release |
| 68 | + |
| 69 | +## Tag and release binaries for different OS on GitHub release |
| 70 | +release: tag-release .WAIT build-release .WAIT publish-release |
| 71 | + |
| 72 | +tag-release: nexttag |
| 73 | + git tag $(RELEASE_TAG) |
| 74 | + git push origin $(RELEASE_TAG) |
| 75 | + |
| 76 | +build-release: |
| 77 | + $(MAKE) build GOOS=linux GOARCH=amd64 O=$(RELEASE_DIR) |
| 78 | + $(MAKE) build GOOS=linux GOARCH=arm64 O=$(RELEASE_DIR) |
| 79 | + $(MAKE) build GOOS=darwin GOARCH=amd64 O=$(RELEASE_DIR) |
| 80 | + $(MAKE) build GOOS=darwin GOARCH=arm64 O=$(RELEASE_DIR) |
| 81 | + |
| 82 | +publish-release: |
| 83 | + gh release create $(RELEASE_TAG) --generate-notes $(RELEASE_DIR)/* |
| 84 | + |
| 85 | +nexttag: |
| 86 | + $(if $(RELEASE_TAG),,$(eval RELEASE_TAG := $(shell $(NEXTTAG_CMD)))) |
| 87 | + |
| 88 | +.PHONY: build-release nexttag publish-release release tag-release |
| 89 | + |
| 90 | +define NEXTTAG_CMD |
| 91 | +{ git tag --list --merged HEAD --sort=-v:refname; echo v0.0.0; } |
| 92 | +| grep -E "^v?[0-9]+.[0-9]+.[0-9]+$$" |
| 93 | +| head -n1 |
| 94 | +| awk -F . '{ print $$1 "." $$2 "." $$3 + 1 }' |
| 95 | +endef |
| 96 | + |
| 97 | +# --- Utilities ---------------------------------------------------------------- |
| 98 | +COLOUR_NORMAL = $(shell tput sgr0 2>/dev/null) |
| 99 | +COLOUR_RED = $(shell tput setaf 1 2>/dev/null) |
| 100 | +COLOUR_GREEN = $(shell tput setaf 2 2>/dev/null) |
| 101 | +COLOUR_WHITE = $(shell tput setaf 7 2>/dev/null) |
| 102 | + |
| 103 | +help: |
| 104 | + $(eval export HELP_AWK) |
| 105 | + @awk "$${HELP_AWK}" $(MAKEFILE_LIST) | sort | column -s "$$(printf \\t)" -t |
| 106 | + |
| 107 | +$(O): |
| 108 | + @mkdir -p $@ |
| 109 | + |
| 110 | +.PHONY: help |
| 111 | + |
| 112 | +# Awk script to extract and print target descriptions for `make help`. |
| 113 | +define HELP_AWK |
| 114 | +/^## / { desc = desc substr($$0, 3) } |
| 115 | +/^[A-Za-z0-9%_-]+:/ && desc { |
| 116 | + sub(/::?$$/, "", $$1) |
| 117 | + printf "$(COLOUR_WHITE)%s$(COLOUR_NORMAL)\t%s\n", $$1, desc |
| 118 | + desc = "" |
| 119 | +} |
| 120 | +endef |
| 121 | + |
| 122 | +define nl |
| 123 | + |
| 124 | + |
| 125 | +endef |
| 126 | +ifndef ACTIVE_HERMIT |
| 127 | +$(eval $(subst \n,$(nl),$(shell bin/hermit env -r | sed 's/^\(.*\)$$/export \1\\n/'))) |
| 128 | +endif |
| 129 | + |
| 130 | +# Ensure make version is gnu make 4.4 or higher (for .WAIT target) |
| 131 | +ifeq ($(filter shell-export,$(value .FEATURES)),) |
| 132 | +$(error Unsupported Make version. \ |
| 133 | + $(nl)Use GNU Make 4.4 or higher (current: $(MAKE_VERSION)). \ |
| 134 | + $(nl)Activate 🐚 hermit with `. bin/activate-hermit` and run again \ |
| 135 | + $(nl)or use `bin/make`) |
| 136 | +endif |
0 commit comments