-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
71 lines (55 loc) · 1.62 KB
/
Copy pathMakefile
File metadata and controls
71 lines (55 loc) · 1.62 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
.DEFAULT_GOAL := all
# Config
DEST = out
PREFIX = /usr/local
VERVAR = main.version
SRCS = Go Embed
# Commands
GIT = git
GO = go
GOBUILD = $(GO) build
GOLIST = $(GO) list
GOLF = $(GOLIST) -f
GOTEST = $(GO) test
INSTALL = install
H2M = help2man
# Main packages
PKGS != $(GOLF) '{{if eq .Name "main"}}{{.ImportPath}}{{end}}' ./...
BINS = $(addprefix $(DEST)/,$(notdir $(PKGS)))
MANS = $(addsuffix .1,$(BINS))
# Build/test flags
VERSION != $(GIT) tag --sort=-v:refname 2>/dev/null | head -n1
LDFLAGS = $(if $(VERSION),-X '$(VERVAR)=$(VERSION)')
BUILDFLAGS = -trimpath $(if $(LDFLAGS),-ldflags "$(LDFLAGS)")
TESTFLAGS = -v
# Functions
getrange = {{range .$(1)Files}}{{$$.Dir}}/{{.}} {{end}}
filesfmt = $(foreach t,$(SRCS),$(call getrange,$(t)))
getpreqs = $(shell $(GOLF) '$(filesfmt)' $(1))
getdir = $(shell $(GOLF) '{{.Dir}}' $(1))
getusage = $(shell test -f $(1) && head -n1 $(1) \
| sed -e 's/^[A-Z]/\L&/' -e 's/\.$$//')
# Rule generator
define mkpkg =
$(1)_USAGE = $(call getusage,$(call getdir,$(2))/usage.txt)
$$(DEST)/$(1): $(call getpreqs,$(2))
@mkdir -p $$(@D)
$$(GOBUILD) $$(BUILDFLAGS) -o $$@ $(2)
$$(DEST)/$(1).1: USAGE ?= $$($(1)_USAGE)
$$(DEST)/$(1).1: $(call getpreqs,$(2))
@mkdir -p $$(@D)
$$(H2M) --output=$$@ --name='$$(USAGE)' $$(DEST)/$(1)
endef
-include config.mk
all: $(BINS) $(MANS)
test:
$(GOTEST) $(TESTFLAGS) ./...
install: $(BINS) $(MANS)
$(INSTALL) -Dm755 $(BINS) $(PREFIX)/bin/
$(INSTALL) -Dm644 $(MANS) $(PREFIX)/share/man/man1/
clean:
-rm $(BINS)
$(foreach p,$(PKGS),$(eval $(call mkpkg,$(notdir $(p)),$(p))))
show-%:
@echo '$* = $($*)'
.PHONY: all test install clean