Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
83 lines (67 loc) · 2.34 KB
/
Copy pathMakefile
File metadata and controls
83 lines (67 loc) · 2.34 KB
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
MODULE := github.com/frankcruz/tasklin
BINARY := tasklin
BIN_DIR := bin
SAMPLE_DIR := sample
# Injected at link time
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
DATE ?= $(shell date -u +%Y-%m-%dT%H:%M:%SZ)
LDFLAGS := -s -w \
-X main.version=$(VERSION) \
-X main.commit=$(COMMIT) \
-X main.buildDate=$(DATE)
GO := go
GOFLAGS := -trimpath
# Cross-compile targets: OS/ARCH pairs
PLATFORMS := \
linux/amd64 \
linux/arm64 \
darwin/amd64 \
darwin/arm64 \
windows/amd64
.PHONY: all build build-all run run-sample sample test install clean help
## all: build for the current architecture (default)
all: build
## build: compile for the current OS/ARCH into bin/
build:
@mkdir -p $(BIN_DIR)
$(GO) build $(GOFLAGS) -ldflags "$(LDFLAGS)" -o $(BIN_DIR)/$(BINARY) .
## build-all: cross-compile for all target platforms into bin/
build-all:
@mkdir -p $(BIN_DIR)
@$(foreach PLATFORM,$(PLATFORMS), \
$(eval OS := $(word 1,$(subst /, ,$(PLATFORM)))) \
$(eval ARCH := $(word 2,$(subst /, ,$(PLATFORM)))) \
$(eval EXT := $(if $(filter windows,$(OS)),.exe,)) \
$(eval OUT := $(BIN_DIR)/$(BINARY)-$(OS)-$(ARCH)$(EXT)) \
echo " building $(OUT) ..."; \
GOOS=$(OS) GOARCH=$(ARCH) $(GO) build $(GOFLAGS) -ldflags "$(LDFLAGS)" -o $(OUT) . || exit 1; \
)
@echo "All builds complete. Artifacts in $(BIN_DIR)/"
## run: build and run the application
run: build
./$(BIN_DIR)/$(BINARY)
## sample: generate sample data (pass CLEAN=1 to regenerate from scratch)
sample:
@bash resources/gen-sample.sh $(if $(CLEAN),--clean,)
## run-sample: build and run tasklin inside the sample/ directory
run-sample: build sample
@cd $(SAMPLE_DIR) && ../$(BIN_DIR)/$(BINARY)
## test: run all unit tests with race detector and coverage
test:
$(GO) test -race -cover ./...
## test-ci: run tests in CI mode (no colour, junit-friendly output)
test-ci:
$(GO) test -race -coverprofile=coverage.out ./...
$(GO) tool cover -func=coverage.out
## install: install the binary to GOPATH/bin (or GOBIN if set)
install:
$(GO) install $(GOFLAGS) -ldflags "$(LDFLAGS)" .
## clean: remove build artefacts
clean:
@rm -rf $(BIN_DIR) coverage.out
## help: list available targets
help:
@echo "Usage: make [target]"
@echo ""
@grep -E '^## ' $(MAKEFILE_LIST) | sed 's/^## / /'