-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
167 lines (134 loc) · 5.91 KB
/
Copy pathMakefile
File metadata and controls
167 lines (134 loc) · 5.91 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
CC ?= cc
HOSTCC ?= $(CC)
CFLAGS ?= -O2 -Wall -Wextra -Werror -Wpedantic -Wshadow -std=c11 -Isrc
VERSION ?= $(shell git describe --tags --abbrev --dirty 2>/dev/null || echo unknown)
override CFLAGS += -DENVCTL_VERSION='"$(VERSION)"'
# -MMD -MP: emit .d dependency files so .h edits rebuild the right .o files.
DEPFLAGS ?= -MMD -MP
PREFIX ?= $(HOME)/.local
DIST ?= dist
SRCS := src/util.c src/agent.c src/cli.c src/complete.c src/module.c src/help.c src/lines.c src/entropy.c \
src/redact.c src/mask.c src/filter.c src/envsrc.c src/diff.c \
src/fileio.c src/main.c
OBJS := $(SRCS:.c=.o)
DEPS := $(OBJS:.o=.d)
# git describe reports a new version without any source file changing.
STAMP := .version
# All release artifacts are built natively on the matching runner.
LINUX_AMD64_CC ?= $(CC)
LINUX_ARM64_CC ?= $(CC)
WINDOWS_AMD64_CC ?= gcc
WINDOWS_ARM64_CC ?= gcc
DARWIN_CC ?= $(CC)
ifeq ($(OS),Windows_NT)
EXE := .exe
# Windows has no unprivileged symlinks, so `install` copies the binary; Unix
# links it so rebuilds propagate without re-installing.
INSTALL_LINK := cp -f
else
EXE :=
INSTALL_LINK := ln -sf
endif
BIN := envctl$(EXE)
MANGEN := gen/man$(EXE)
MAN1 := man/envctl.1
# The housekeeping recipes below shell out to coreutils (mkdir/cp/rm). GNU make
# finds an sh.exe on PATH even when launched from PowerShell/cmd, but only routes
# a recipe through it when the line holds a shell metacharacter — otherwise it
# tries a bare CreateProcess, which can't run mkdir/cp/rm (they aren't .exe
# files). So quote every path (the quote forces the shell) and normalise make's
# backslashy $(HOME) to forward slashes (a bare '\' is an escape to sh). No-op
# on Unix, where paths already use '/'.
slash = $(subst \,/,$1)
ART_LINUX_AMD64 := $(DIST)/envctl-linux-amd64
ART_LINUX_ARM64 := $(DIST)/envctl-linux-arm64
ART_DARWIN_AMD64 := $(DIST)/envctl-darwin-amd64
ART_DARWIN_ARM64 := $(DIST)/envctl-darwin-arm64
ART_WINDOWS_AMD64 := $(DIST)/envctl-windows-amd64.exe
ART_WINDOWS_ARM64 := $(DIST)/envctl-windows-arm64.exe
NPROCS := $(shell getconf _NPROCESSORS_ONLN 2>/dev/null || getconf NPROCESSORS_ONLN 2>/dev/null || sysctl -n hw.logicalcpu 2>/dev/null || nproc 2>/dev/null || echo 2)
MAKEFLAGS += -j$(NPROCS)
.PHONY: all clean install fmt format test man FORCE
.PHONY: dist-linux-amd64 dist-linux-arm64 dist-darwin-amd64 dist-darwin-arm64
.PHONY: dist-windows-amd64 dist-windows-arm64 checksums
all: $(BIN)
$(BIN): $(OBJS)
$(CC) $(CFLAGS) -o $@ $(OBJS)
src/%.o: src/%.c
$(CC) $(CFLAGS) $(DEPFLAGS) -c -o $@ $<
$(OBJS): $(STAMP)
$(STAMP): FORCE
@printf '%s' '$(VERSION)' > "$@.tmp"
@if cmp -s "$@.tmp" "$@"; then rm -f "$@.tmp"; else mv -f "$@.tmp" "$@"; fi
FORCE:
# Pull in header deps generated by $(DEPFLAGS); missing files are fine on first build.
-include $(DEPS)
# ---------------------------------------------------------------------------
# man page (generated from the CLI registry in src/cli.c)
# ---------------------------------------------------------------------------
man: $(MAN1)
$(MANGEN): gen/man.c src/cli.c src/cli.h
$(HOSTCC) $(CFLAGS) -o $@ gen/man.c src/cli.c
$(MAN1): $(MANGEN)
mkdir -p "man"
"./$(MANGEN)" > "$(MAN1)"
# ---------------------------------------------------------------------------
# test
# ---------------------------------------------------------------------------
test: $(BIN) $(MANGEN)
@"./$(MANGEN)" > "$(MAN1).gen"
@cmp -s "$(MAN1)" "$(MAN1).gen" || { \
echo "$(MAN1) is stale: regenerate with 'make man' and commit it" >&2; \
diff -u "$(MAN1)" "$(MAN1).gen" >&2 || true; \
rm -f "$(MAN1).gen"; \
exit 1; \
}
@rm -f "$(MAN1).gen"
@bash tests/run.sh "$(CURDIR)/$(BIN)"
@bash tests/completions.sh "$(CURDIR)/$(BIN)"
# ---------------------------------------------------------------------------
# release artifacts — one compile of all sources per platform triple
# ---------------------------------------------------------------------------
$(DIST):
mkdir -p "$(call slash,$(DIST))"
dist-linux-amd64: $(ART_LINUX_AMD64)
$(ART_LINUX_AMD64): $(SRCS) $(STAMP) | $(DIST)
$(LINUX_AMD64_CC) $(CFLAGS) -static -s -o $@ $(SRCS)
dist-linux-arm64: $(ART_LINUX_ARM64)
$(ART_LINUX_ARM64): $(SRCS) $(STAMP) | $(DIST)
$(LINUX_ARM64_CC) $(CFLAGS) -static -s -o $@ $(SRCS)
dist-darwin-amd64: $(ART_DARWIN_AMD64)
$(ART_DARWIN_AMD64): $(SRCS) $(STAMP) | $(DIST)
$(DARWIN_CC) $(CFLAGS) -o $@ $(SRCS)
dist-darwin-arm64: $(ART_DARWIN_ARM64)
$(ART_DARWIN_ARM64): $(SRCS) $(STAMP) | $(DIST)
$(DARWIN_CC) $(CFLAGS) -o $@ $(SRCS)
dist-windows-amd64: $(ART_WINDOWS_AMD64)
$(ART_WINDOWS_AMD64): $(SRCS) $(STAMP) | $(DIST)
$(WINDOWS_AMD64_CC) $(CFLAGS) -static -s -o $@ $(SRCS)
dist-windows-arm64: $(ART_WINDOWS_ARM64)
$(ART_WINDOWS_ARM64): $(SRCS) $(STAMP) | $(DIST)
$(WINDOWS_ARM64_CC) $(CFLAGS) -static -s -o $@ $(SRCS)
checksums:
@cd $(DIST) && sha256sum envctl-* > SHA256SUMS && cat SHA256SUMS
install: $(BIN)
mkdir -p "$(call slash,$(PREFIX))/bin"
$(INSTALL_LINK) "$(call slash,$(CURDIR))/$(BIN)" "$(call slash,$(PREFIX))/bin/$(BIN)"
mkdir -p "$(call slash,$(PREFIX))/share/man/man1"
cp -f "$(call slash,$(CURDIR))/$(MAN1)" "$(call slash,$(PREFIX))/share/man/man1/envctl.1"
mkdir -p "$(call slash,$(PREFIX))/share/bash-completion/completions"
"./$(BIN)" completions bash > "$(call slash,$(PREFIX))/share/bash-completion/completions/envctl"
mkdir -p "$(call slash,$(PREFIX))/share/zsh/site-functions"
"./$(BIN)" completions zsh > "$(call slash,$(PREFIX))/share/zsh/site-functions/_envctl"
mkdir -p "$(call slash,$(PREFIX))/share/fish/vendor_completions.d"
"./$(BIN)" completions fish > "$(call slash,$(PREFIX))/share/fish/vendor_completions.d/envctl.fish"
@case "$(PREFIX)" in \
/usr | /usr/* | /usr/local | /usr/local/*) ;; \
*) echo "note: add $(PREFIX)/share/zsh/site-functions to \$$fpath before compinit" ;; \
esac
fmt format:
dprint fmt
clean:
rm -f "envctl" "envctl.exe" $(OBJS) $(DEPS)
rm -f "gen/man" "gen/man.exe" "$(MAN1).gen" "$(STAMP)" "$(STAMP).tmp"
rm -rf "$(call slash,$(DIST))"