-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMakefile
More file actions
199 lines (169 loc) · 7.16 KB
/
Copy pathMakefile
File metadata and controls
199 lines (169 loc) · 7.16 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
# OpenBackup build. Everything a contributor needs is one `make` away, and the
# same targets run in CI so a green pipeline means a working build locally too.
SHELL := /bin/sh
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)
# Version metadata is injected at link time, so the binary can report exactly
# what it is without a generated source file in the tree.
LDFLAGS := -s -w \
-X github.com/foisalislambd/openbackup/internal/version.Version=$(VERSION) \
-X github.com/foisalislambd/openbackup/internal/version.Commit=$(COMMIT) \
-X github.com/foisalislambd/openbackup/internal/version.Date=$(DATE)
BIN := bin
WEB_OUT := web/dist
WEB_DIST := internal/server/web/dist
# PKGS is spelled out rather than ./... because the dashboard's node_modules can
# contain Go files (some npm packages ship them), and those are not ours to
# build, vet or test.
PKGS := ./cmd/... ./internal/...
.PHONY: all
all: web build
.PHONY: help
help:
@echo "make build Build the server and agent for this machine"
@echo "make web Build the dashboard and embed it in the server"
@echo "make desktop Build the desktop app for this machine (needs wails)"
@echo "make desktop-dev Live-reload desktop window"
@echo "make desktop-linux-package Linux release binary + .desktop + icon"
@echo "make test Run every test"
@echo "make check Format check, vet and test (what CI runs)"
@echo "make run-server Run the server against ./data"
@echo "make dev Run the server and the dashboard dev server together"
@echo "make release Cross-compile release binaries for all platforms"
@echo "make docker Build the container image"
@echo "make clean Remove build output"
# ---------------------------------------------------------------------------
# Go
# ---------------------------------------------------------------------------
.PHONY: build
build:
go build -trimpath -ldflags '$(LDFLAGS)' -o $(BIN)/ ./cmd/...
.PHONY: test
test:
go test $(PKGS)
.PHONY: test-race
test-race:
go test -race $(PKGS)
.PHONY: vet
vet:
go vet $(PKGS)
.PHONY: fmt
fmt:
gofmt -w .
.PHONY: check
check:
@test -z "$$(gofmt -l . | tee /dev/stderr)" || (echo "run 'make fmt'" && exit 1)
go vet $(PKGS)
go test $(PKGS)
.PHONY: tidy
tidy:
go mod tidy
# Cross-compilation matters here: a user installs the agent on whatever they own,
# and every target must build from one machine with no cgo.
.PHONY: cross
cross:
@for target in linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64 windows/arm64; do \
echo "checking $$target"; \
GOOS=$${target%/*} GOARCH=$${target#*/} CGO_ENABLED=0 go build $(PKGS) || exit 1; \
done
# ---------------------------------------------------------------------------
# Dashboard
# ---------------------------------------------------------------------------
# The dashboard is a Vite React SPA copied into the package that embeds it, so
# `go build` alone produces a server with a working UI.
.PHONY: web
web:
cd web && npm ci --no-audit --no-fund
cd web && npm run build
rm -rf $(WEB_DIST)
mkdir -p $(WEB_DIST)
cp -r $(WEB_OUT)/. $(WEB_DIST)/
@git checkout -- $(WEB_DIST)/.gitkeep 2>/dev/null || true
@echo "dashboard embedded in $(WEB_DIST)"
.PHONY: web-check
web-check:
cd web && npm run typecheck && npm run lint
# ---------------------------------------------------------------------------
# Desktop app
# ---------------------------------------------------------------------------
# The desktop app is a separate module because Wails needs native webview
# headers, and the server and agent must stay pure-Go cross-compiles. It also
# only builds for the machine it runs on: there is no cross-compiling a native
# webview. Windows installers are produced by the release workflow.
.PHONY: desktop
desktop:
@command -v wails >/dev/null || \
(echo "install the Wails CLI: go install github.com/wailsapp/wails/v2/cmd/wails@latest" && exit 1)
@pv=$$(printf '%s' "$(VERSION)" | sed 's/^v//'); \
node -e "const fs=require('fs');const p='desktop/wails.json';const j=JSON.parse(fs.readFileSync(p,'utf8'));j.info=j.info||{};j.info.productVersion=process.argv[1];fs.writeFileSync(p,JSON.stringify(j,null,2)+'\n');" "$$pv"
@if [ "$$(uname -s)" = Linux ]; then \
cd desktop && wails build -trimpath -ldflags '$(LDFLAGS)' -tags webkit2_41; \
else \
cd desktop && wails build -trimpath -ldflags '$(LDFLAGS)'; \
fi
@echo "desktop app in desktop/build/bin"
.PHONY: desktop-dev
desktop-dev:
@command -v wails >/dev/null || \
(echo "install the Wails CLI: go install github.com/wailsapp/wails/v2/cmd/wails@latest" && exit 1)
@if [ "$$(uname -s)" = Linux ]; then \
cd desktop && wails dev -tags webkit2_41; \
else \
cd desktop && wails dev; \
fi
.PHONY: desktop-check
desktop-check:
cd desktop && go vet ./...
cd desktop/frontend && npm run typecheck
# After `make desktop` on Linux: rename the binary for Releases and copy the
# .desktop template next to it for packaging.
.PHONY: desktop-linux-package
desktop-linux-package: desktop
@test "$$(uname -s)" = Linux || (echo "desktop-linux-package only runs on Linux" && exit 1)
@arch=$$(uname -m); \
case "$$arch" in x86_64|amd64) goarch=amd64 ;; aarch64|arm64) goarch=arm64 ;; *) echo "unsupported arch $$arch"; exit 1 ;; esac; \
src=desktop/build/bin/openbackup-desktop; \
dst=desktop/build/bin/openbackup-desktop-linux-$$goarch; \
cp "$$src" "$$dst"; \
cp desktop/build/linux/openbackup-desktop.desktop desktop/build/bin/; \
cp desktop/build/appicon.png desktop/build/bin/openbackup.png; \
echo "packaged $$dst (+ .desktop + icon)"
.PHONY: dev
dev:
@echo "server on :18200, dashboard on :5173 (proxying /api to the server)"
@( go run ./cmd/openbackup-server & cd web && npm run dev; kill %1 )
.PHONY: run-server
run-server:
OPENBACKUP_DATA_DIR=./data go run ./cmd/openbackup-server
# ---------------------------------------------------------------------------
# Release
# ---------------------------------------------------------------------------
.PHONY: release
release: web
@mkdir -p dist
@for target in linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64; do \
os=$${target%/*}; arch=$${target#*/}; ext=""; \
if [ "$$os" = "windows" ]; then ext=".exe"; fi; \
echo "building openbackup-$$os-$$arch"; \
GOOS=$$os GOARCH=$$arch CGO_ENABLED=0 go build -trimpath -ldflags '$(LDFLAGS)' \
-o dist/openbackup-$$os-$$arch$$ext ./cmd/openbackup || exit 1; \
GOOS=$$os GOARCH=$$arch CGO_ENABLED=0 go build -trimpath -ldflags '$(LDFLAGS)' \
-o dist/openbackup-server-$$os-$$arch$$ext ./cmd/openbackup-server || exit 1; \
done
@cd dist && sha256sum * > SHA256SUMS 2>/dev/null || shasum -a 256 * > SHA256SUMS
@echo "release binaries in ./dist"
.PHONY: docker
docker:
docker build \
--build-arg VERSION=$(VERSION) \
--build-arg COMMIT=$(COMMIT) \
--build-arg DATE=$(DATE) \
-t foisalislambd/openbackup:$(VERSION) -t foisalislambd/openbackup:latest .
.PHONY: clean
clean:
rm -rf $(BIN) dist web/dist web/out web/.next
rm -rf desktop/build/bin desktop/frontend/dist
rm -rf $(WEB_DIST)
mkdir -p $(WEB_DIST)
git checkout -- $(WEB_DIST)/.gitkeep 2>/dev/null || true