-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMakefile
More file actions
238 lines (203 loc) · 7.07 KB
/
Makefile
File metadata and controls
238 lines (203 loc) · 7.07 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# SPDX-FileCopyrightText: 2025 OpenCHAMI a Series of LF Projects, LLC
#
# SPDX-License-Identifier: MIT
# Set path to commands
GO ?= $(shell command -v go 2>/dev/null)
GOLANGCI_LINT ?= $(shell command -v golangci-lint 2>/dev/null)
GORELEASER ?= $(shell command -v goreleaser 2>/dev/null)
GIT ?= $(shell command -v git 2>/dev/null)
AWK ?= $(shell command -v awk 2>/dev/null)
REUSE ?= $(shell command -v reuse 2>/dev/null)
# Use HOSTCMD to not conflict with Make's $(HOSTNAME)
HOSTCMD ?= $(shell command -v hostname 2>/dev/null)
INSTALL ?= $(shell command -v install 2>/dev/null)
SCDOC ?= $(shell command -v scdoc 2>/dev/null)
SHELL ?= /bin/sh
INSTALL_PROGRAM ?= $(INSTALL) -Dm755
INSTALL_DATA ?= $(INSTALL) -Dm644
# Check that commands are present
ifeq ($(GIT),)
$(error git command not found.)
endif
ifeq ($(HOSTCMD),)
$(error hostname command not found.)
endif
ifeq ($(SHELL),)
$(error '$(SHELL)' undefined.)
endif
# Recursive wildcard function, obtained from https://stackoverflow.com/a/18258352
#
# Arg 1: Space-separated list of directories to recurse into
# Arg 2: Space-separated list of patterns to match
rwildcard = $(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d))
NAME ?= ochami
IMPORT := github.com/OpenCHAMI/$(NAME)/
VERSION ?= $(shell $(GIT) describe --tags --always --dirty --broken --abbrev=0)
TAG ?= $(shell $(GIT) describe --tags --always --abbrev=0)
BRANCH ?= $(shell $(GIT) branch --show-current)
BUILD ?= $(shell $(GIT) rev-parse HEAD)
GOVER := $(shell $(GO) env GOVERSION)
GITSTATE := $(shell if output=$($(GIT) status --porcelain) && [ -n "$output" ]; then echo dirty; else echo clean; fi)
BUILDHOST := $(shell $(HOSTCMD))
BUILDUSER := $(shell whoami)
LDFLAGS := -s \
-X '$(IMPORT)internal/version.Version=$(VERSION)' \
-X '$(IMPORT)internal/version.Tag=$(TAG)' \
-X '$(IMPORT)internal/version.Branch=$(BRANCH)' \
-X '$(IMPORT)internal/version.Commit=$(BUILD)' \
-X '$(IMPORT)internal/version.Date=$(shell date -Iseconds)' \
-X '$(IMPORT)internal/version.GoVersion=$(GOVER)' \
-X '$(IMPORT)internal/version.GitState=$(GITSTATE)' \
-X '$(IMPORT)internal/version.BuildHost=$(BUILDHOST)' \
-X '$(IMPORT)internal/version.BuildUser=$(BUILDUSER)'
CMD := $(call rwildcard,cmd,*.go)
INTERNAL := $(call rwildcard,internal,*.go)
PKG := $(call rwildcard,pkg,*.go)
MANSRC := $(wildcard man/*.sc)
MANBIN := $(subst .sc,,$(MANSRC))
MAN1BIN := $(filter %.1,$(MANBIN))
MAN5BIN := $(filter %.5,$(MANBIN))
HELPERS := extras/scripts/ochami-discovery-old2new.py
prefix ?= /usr/local
exec_prefix ?= $(prefix)
bindir ?= $(exec_prefix)/bin
mandir ?= $(exec_prefix)/man
libexecdir ?= $(prefix)/libexec/$(NAME)
sharedir ?= $(prefix)/share
.PHONY: all
all: binaries ## Build everything
.PHONY: binaries
binaries: $(NAME) ## Build binaries
.PHONY: help
help: ## Show this help
ifeq ($(AWK),)
$(error awk command not found.)
endif
@$(AWK) 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m[VAR=val]... <target>\033[0m\n\nTargets:\n"} \
/^[a-zA-Z0-9_\/.-]+:.*##/ { \
printf " \033[36m%-22s\033[0m %s\n", $$1, $$2 \
}' $(MAKEFILE_LIST)
.PHONY: goreleaser-build
goreleaser-build: ## Run `goreleaser build` (accepts GORELEASER_OPTS)
ifeq ($(GO),)
$(error go command not found.)
endif
ifeq ($(GORELEASER),)
$(error goreleaser command not found.)
endif
env \
GOVERSION=$(GOVER) \
BUILD_HOST=$(BUILDHOST) \
BUILD_USER=$(BUILDUSER) \
$(GORELEASER) build $(GORELEASER_OPTS)
.PHONY: goreleaser-release
goreleaser-release: ## Run `goreleaser release` (accepts GORELEASER_OPTS)
ifeq ($(GO),)
$(error go command not found.)
endif
ifeq ($(GORELEASER),)
$(error goreleaser command not found.)
endif
env \
GOVERSION=$(GOVER) \
BUILD_HOST=$(BUILDHOST) \
BUILD_USER=$(BUILDUSER) \
$(GORELEASER) release $(GORELEASER_OPTS)
.PHONY: goreleaser-clean
goreleaser-clean: ## Clean Goreleaser files (remove dist/)
$(RM) -rf dist/
.PHONY: check-reuse
check-reuse:
ifeq ($(REUSE),)
$(error reuse command not found)
endif
reuse lint
.PHONY: lint
lint:
ifeq ($(GOLANGCI_LINT),)
$(error golangci-lint command not found)
endif
$(GOLANGCI_LINT) run
.PHONY: test
test: unit-test ## Run all tests
.PHONY: unit-test
unit-test: ## Run unit tests only
ifeq ($(GO),)
$(error go command not found.)
endif
$(GO) test -cover -v ./...
.PHONY: clean
clean: ## Clean Go build artifacts
ifeq ($(GO),)
$(error go command not found.)
endif
$(GO) clean -i -x
.PHONY: clean-man
clean-man: ## Clean generated manual pages
rm -f $(MANBIN)
.PHONY: clean-completions
clean-completions: ## Clean generated shell completions
rm -rf completions/
completions: $(NAME) ## Generate shell completions
./scripts/completions.sh
.PHONY: distclean
distclean: clean clean-completions clean-man ## Clean everything (prepare for distribution)
.PHONY: install
install: install-prog install-helper install-completions install-man ## Install everything
.PHONY: install-prog
install-prog: $(NAME) ## Install program
ifeq ($(INSTALL),)
$(error install command not found.)
endif
$(INSTALL_PROGRAM) $(NAME) $(DESTDIR)$(bindir)/$(NAME)
.PHONY: install-helper
install-helper: $(HELPERS) ## Install helper scripts
ifeq ($(INSTALL),)
$(error install command not found.)
endif
for h in $(HELPERS); do \
$(INSTALL_PROGRAM) "$$h" "$(DESTDIR)$(libexecdir)/$$(basename $$h)"; \
done
.PHONY: install-completions
install-completions: completions ## Install shell completions
ifeq ($(INSTALL),)
$(error install command not found.)
endif
$(INSTALL_DATA) ./completions/ochami.bash $(DESTDIR)$(sharedir)/bash-completion/completions/ochami
$(INSTALL_DATA) ./completions/ochami.fish $(DESTDIR)$(sharedir)/fish/vendor_completions.d/ochami.fish
$(INSTALL_DATA) ./completions/ochami.zsh $(DESTDIR)$(sharedir)/zsh/site-functions/_ochami
.PHONY: install-man
install-man: $(MANBIN) ## Install manual pages
ifeq ($(INSTALL),)
$(error install command not found.)
endif
mkdir -p $(DESTDIR)$(mandir)/man1
mkdir -p $(DESTDIR)$(mandir)/man5
$(INSTALL_DATA) $(MAN1BIN) $(DESTDIR)$(mandir)/man1/
$(INSTALL_DATA) $(MAN5BIN) $(DESTDIR)$(mandir)/man5/
.PHONY: man
man: $(MANBIN) ## Generate manual pages
man/%: man/%.sc
ifeq ($(SCDOC),)
$(error scdoc command not found.)
endif
$(SCDOC) < $< > $@
.PHONY: uninstall
uninstall: uninstall-prog uninstall-completions uninstall-man ## Uninstall everything
.PHONY: uninstall-prog
uninstall-prog: ## Uninstall program
rm -f $(DESTDIR)$(bindir)/$(NAME)
.PHONY: uninstall-completions
uninstall-completions: ## Uninstall shell completions
rm -f $(DESTDIR)/usr/share/bash-completion/completions/ochami
rm -f $(DESTDIR)/usr/share/fish/vendor_completions.d/ochami.fish
rm -f $(DESTDIR)/usr/share/zsh/site-functions/_ochami
.PHONY: uninstall-man
uninstall-man: ## Uninstall manual pages
rm -f $(foreach man1page,$(subst man/,,$(MAN1BIN)),$(DESTDIR)$(mandir)/man1/$(man1page))
rm -f $(foreach man5page,$(subst man/,,$(MAN5BIN)),$(DESTDIR)$(mandir)/man5/$(man5page))
$(NAME): *.go $(CMD) $(INTERNAL) $(PKG)
ifeq ($(GO),)
$(error go command not found.)
endif
$(GO) build -v -ldflags="$(LDFLAGS)"