Makefile 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. MAKEFLAGS := --jobs=1
  2. PYTHON := python3
  3. PIP := pip3
  4. VERSION := $(shell git describe --tag)
  5. COMMIT := $(shell git rev-parse --short HEAD)
  6. .PHONY:
  7. help:
  8. @echo "Typical commands (more see below):"
  9. @echo " make build - Build web app, documentation and server/client (sloowwww)"
  10. @echo " make cli-linux-amd64 - Build server/client binary (amd64, no web app or docs)"
  11. @echo " make install-linux-amd64 - Install ntfy binary to /usr/bin/ntfy (amd64)"
  12. @echo " make web - Build the web app"
  13. @echo " make docs - Build the documentation"
  14. @echo " make check - Run all tests, vetting/formatting checks and linters"
  15. @echo
  16. @echo "Build everything:"
  17. @echo " make build - Build web app, documentation and server/client"
  18. @echo " make clean - Clean build/dist folders"
  19. @echo
  20. @echo "Build server & client (using GoReleaser, not release version):"
  21. @echo " make cli - Build server & client (all architectures)"
  22. @echo " make cli-linux-amd64 - Build server & client (Linux, amd64 only)"
  23. @echo " make cli-linux-armv6 - Build server & client (Linux, armv6 only)"
  24. @echo " make cli-linux-armv7 - Build server & client (Linux, armv7 only)"
  25. @echo " make cli-linux-arm64 - Build server & client (Linux, arm64 only)"
  26. @echo " make cli-windows-amd64 - Build client (Windows, amd64 only)"
  27. @echo " make cli-darwin-all - Build client (macOS, arm64+amd64 universal binary)"
  28. @echo
  29. @echo "Build server & client (without GoReleaser):"
  30. @echo " make cli-linux-server - Build client & server (no GoReleaser, current arch, Linux)"
  31. @echo " make cli-darwin-server - Build client & server (no GoReleaser, current arch, macOS)"
  32. @echo " make cli-windows-server - Build client & server (no GoReleaser, amd64 only, Windows)"
  33. @echo " make cli-client - Build client only (no GoReleaser, current arch, Linux/macOS/Windows)"
  34. @echo
  35. @echo "Build dev Docker:"
  36. @echo " make docker-dev - Build client & server for current architecture using Docker only"
  37. @echo
  38. @echo "Build web app:"
  39. @echo " make web - Build the web app"
  40. @echo " make web-deps - Install web app dependencies (npm install the universe)"
  41. @echo " make web-build - Actually build the web app"
  42. @echo " make web-lint - Run eslint on the web app"
  43. @echo " make web-fmt - Run prettier on the web app"
  44. @echo " make web-fmt-check - Run prettier on the web app, but don't change anything"
  45. @echo
  46. @echo "Build documentation:"
  47. @echo " make docs - Build the documentation"
  48. @echo " make docs-deps - Install Python dependencies (pip3 install)"
  49. @echo " make docs-build - Actually build the documentation"
  50. @echo
  51. @echo "Test/check:"
  52. @echo " make test - Run tests"
  53. @echo " make race - Run tests with -race flag"
  54. @echo " make coverage - Run tests and show coverage"
  55. @echo " make coverage-html - Run tests and show coverage (as HTML)"
  56. @echo " make coverage-upload - Upload coverage results to codecov.io"
  57. @echo
  58. @echo "Lint/format:"
  59. @echo " make fmt - Run 'go fmt'"
  60. @echo " make fmt-check - Run 'go fmt', but don't change anything"
  61. @echo " make vet - Run 'go vet'"
  62. @echo " make lint - Run 'golint'"
  63. @echo " make staticcheck - Run 'staticcheck'"
  64. @echo
  65. @echo "Releasing:"
  66. @echo " make release - Create a release"
  67. @echo " make release-snapshot - Create a test release"
  68. @echo
  69. @echo "Install locally (requires sudo):"
  70. @echo " make install-linux-amd64 - Copy amd64 binary from dist/ to /usr/bin/ntfy"
  71. @echo " make install-linux-armv6 - Copy armv6 binary from dist/ to /usr/bin/ntfy"
  72. @echo " make install-linux-armv7 - Copy armv7 binary from dist/ to /usr/bin/ntfy"
  73. @echo " make install-linux-arm64 - Copy arm64 binary from dist/ to /usr/bin/ntfy"
  74. @echo " make install-linux-deb-amd64 - Install .deb from dist/ (amd64 only)"
  75. @echo " make install-linux-deb-armv6 - Install .deb from dist/ (armv6 only)"
  76. @echo " make install-linux-deb-armv7 - Install .deb from dist/ (armv7 only)"
  77. @echo " make install-linux-deb-arm64 - Install .deb from dist/ (arm64 only)"
  78. # Building everything
  79. clean: .PHONY
  80. rm -rf dist build server/docs server/site
  81. build: web docs cli
  82. update: web-deps-update cli-deps-update docs-deps-update
  83. docker pull alpine
  84. docker-dev:
  85. docker build \
  86. --file ./Dockerfile-build \
  87. --tag binwiederhier/ntfy:$(VERSION) \
  88. --tag binwiederhier/ntfy:dev \
  89. --build-arg VERSION=$(VERSION) \
  90. --build-arg COMMIT=$(COMMIT) \
  91. ./
  92. # Ubuntu-specific
  93. build-deps-ubuntu:
  94. sudo apt-get update
  95. sudo apt-get install -y \
  96. curl \
  97. gcc-aarch64-linux-gnu \
  98. gcc-arm-linux-gnueabi \
  99. gcc-mingw-w64-x86-64 \
  100. python3 \
  101. python3-venv \
  102. jq
  103. which pip3 || sudo apt-get install -y python3-pip
  104. # Documentation
  105. docs: docs-deps docs-build
  106. docs-venv: .PHONY
  107. $(PYTHON) -m venv ./venv
  108. docs-build: docs-venv
  109. (. venv/bin/activate && $(PYTHON) -m mkdocs build)
  110. docs-deps: docs-venv
  111. (. venv/bin/activate && $(PIP) install -r requirements.txt)
  112. docs-deps-update: .PHONY
  113. (. venv/bin/activate && $(PIP) install -r requirements.txt --upgrade)
  114. # Web app
  115. web: web-deps web-build
  116. web-build:
  117. cd web \
  118. && npm run build \
  119. && mv build/index.html build/app.html \
  120. && rm -rf ../server/site \
  121. && mv build ../server/site \
  122. && rm \
  123. ../server/site/config.js
  124. web-deps:
  125. cd web && npm install
  126. # If this fails for .svg files, optimize them with svgo
  127. web-deps-update:
  128. cd web && npm update
  129. web-fmt:
  130. cd web && npm run format
  131. web-fmt-check:
  132. cd web && npm run format:check
  133. web-lint:
  134. cd web && npm run lint
  135. # Main server/client build
  136. cli: cli-deps
  137. goreleaser build --snapshot --clean
  138. cli-linux-amd64: cli-deps-static-sites
  139. goreleaser build --snapshot --clean --id ntfy_linux_amd64
  140. cli-linux-armv6: cli-deps-static-sites cli-deps-gcc-armv6-armv7
  141. goreleaser build --snapshot --clean --id ntfy_linux_armv6
  142. cli-linux-armv7: cli-deps-static-sites cli-deps-gcc-armv6-armv7
  143. goreleaser build --snapshot --clean --id ntfy_linux_armv7
  144. cli-linux-arm64: cli-deps-static-sites cli-deps-gcc-arm64
  145. goreleaser build --snapshot --clean --id ntfy_linux_arm64
  146. cli-windows-amd64: cli-deps-static-sites
  147. goreleaser build --snapshot --clean --id ntfy_windows_amd64
  148. cli-darwin-all: cli-deps-static-sites
  149. goreleaser build --snapshot --clean --id ntfy_darwin_all
  150. cli-linux-server: cli-deps-static-sites
  151. # This is a target to build the CLI (including the server) manually.
  152. # Use this for development, if you really don't want to install GoReleaser ...
  153. mkdir -p dist/ntfy_linux_server server/docs
  154. CGO_ENABLED=1 go build \
  155. -o dist/ntfy_linux_server/ntfy \
  156. -tags sqlite_omit_load_extension,osusergo,netgo \
  157. -ldflags \
  158. "-linkmode=external -extldflags=-static -s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(shell date +%s)"
  159. cli-darwin-server: cli-deps-static-sites
  160. # This is a target to build the CLI (including the server) manually.
  161. # Use this for macOS/iOS development, so you have a local server to test with.
  162. mkdir -p dist/ntfy_darwin_server server/docs
  163. CGO_ENABLED=1 go build \
  164. -o dist/ntfy_darwin_server/ntfy \
  165. -tags sqlite_omit_load_extension,osusergo,netgo \
  166. -ldflags \
  167. "-linkmode=external -s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(shell date +%s)"
  168. cli-windows-server: cli-deps-static-sites
  169. # This is a target to build the CLI (including the server) for Windows.
  170. # Use this for Windows development, if you really don't want to install GoReleaser ...
  171. mkdir -p dist/ntfy_windows_server server/docs
  172. CC=x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 CGO_ENABLED=1 go build \
  173. -o dist/ntfy_windows_server/ntfy.exe \
  174. -tags sqlite_omit_load_extension,osusergo,netgo \
  175. -ldflags \
  176. "-s -w -X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(shell date +%s)"
  177. cli-client: cli-deps-static-sites
  178. # This is a target to build the CLI (excluding the server) manually. This should work on Linux/macOS/Windows.
  179. # Use this for development, if you really don't want to install GoReleaser ...
  180. mkdir -p dist/ntfy_client server/docs
  181. CGO_ENABLED=0 go build \
  182. -o dist/ntfy_client/ntfy \
  183. -tags noserver \
  184. -ldflags \
  185. "-X main.version=$(VERSION) -X main.commit=$(COMMIT) -X main.date=$(shell date +%s)"
  186. cli-deps: cli-deps-static-sites cli-deps-all cli-deps-gcc
  187. cli-deps-gcc: cli-deps-gcc-armv6-armv7 cli-deps-gcc-arm64 cli-deps-gcc-windows
  188. cli-deps-static-sites:
  189. mkdir -p server/docs server/site
  190. touch server/docs/index.html server/site/app.html
  191. cli-deps-all:
  192. go install github.com/goreleaser/goreleaser/v2@latest
  193. cli-deps-gcc-armv6-armv7:
  194. which arm-linux-gnueabi-gcc || { echo "ERROR: ARMv6/ARMv7 cross compiler not installed. On Ubuntu, run: apt install gcc-arm-linux-gnueabi"; exit 1; }
  195. cli-deps-gcc-arm64:
  196. which aarch64-linux-gnu-gcc || { echo "ERROR: ARM64 cross compiler not installed. On Ubuntu, run: apt install gcc-aarch64-linux-gnu"; exit 1; }
  197. cli-deps-gcc-windows:
  198. which x86_64-w64-mingw32-gcc || { echo "ERROR: Windows cross compiler not installed. On Ubuntu, run: apt install gcc-mingw-w64-x86-64"; exit 1; }
  199. cli-deps-update:
  200. go get -u
  201. go mod tidy
  202. go install honnef.co/go/tools/cmd/staticcheck@latest
  203. go install golang.org/x/lint/golint@latest
  204. go install github.com/goreleaser/goreleaser/v2@latest
  205. cli-build-results:
  206. cat dist/config.yaml
  207. [ -f dist/artifacts.json ] && cat dist/artifacts.json | jq . || true
  208. [ -f dist/metadata.json ] && cat dist/metadata.json | jq . || true
  209. [ -f dist/checksums.txt ] && cat dist/checksums.txt || true
  210. find dist -maxdepth 2 -type f \
  211. \( -name '*.deb' -or -name '*.rpm' -or -name '*.zip' -or -name '*.tar.gz' -or -name 'ntfy' \) \
  212. -and -not -path 'dist/goreleaserdocker*' \
  213. -exec sha256sum {} \;
  214. # Test/check targets
  215. check: test web-fmt-check fmt-check vet web-lint lint staticcheck
  216. test: .PHONY
  217. go test $(shell go list ./... | grep -vE 'ntfy/(test|examples|tools)')
  218. testv: .PHONY
  219. go test -v $(shell go list ./... | grep -vE 'ntfy/(test|examples|tools)')
  220. race: .PHONY
  221. go test -v -race $(shell go list ./... | grep -vE 'ntfy/(test|examples|tools)')
  222. coverage:
  223. mkdir -p build/coverage
  224. go test -v -race -coverprofile=build/coverage/coverage.txt -covermode=atomic $(shell go list ./... | grep -vE 'ntfy/(test|examples|tools)')
  225. go tool cover -func build/coverage/coverage.txt
  226. coverage-html:
  227. mkdir -p build/coverage
  228. go test -race -coverprofile=build/coverage/coverage.txt -covermode=atomic $(shell go list ./... | grep -vE 'ntfy/(test|examples|tools)')
  229. go tool cover -html build/coverage/coverage.txt
  230. coverage-upload:
  231. cd build/coverage && (curl -s https://codecov.io/bash | bash)
  232. # Lint/formatting targets
  233. fmt: web-fmt
  234. gofmt -s -w .
  235. fmt-check:
  236. test -z $(shell gofmt -l .)
  237. vet:
  238. go vet ./...
  239. lint:
  240. which golint || go install golang.org/x/lint/golint@latest
  241. go list ./... | grep -v /vendor/ | xargs -L1 golint -set_exit_status
  242. staticcheck: .PHONY
  243. rm -rf build/staticcheck
  244. which staticcheck || go install honnef.co/go/tools/cmd/staticcheck@latest
  245. mkdir -p build/staticcheck
  246. ln -s "go" build/staticcheck/go
  247. PATH="$(PWD)/build/staticcheck:$(PATH)" staticcheck ./...
  248. rm -rf build/staticcheck
  249. # Releasing targets
  250. release: clean cli-deps release-checks docs web check
  251. goreleaser release --clean
  252. release-snapshot: clean cli-deps docs web check
  253. goreleaser release --snapshot --clean
  254. release-checks:
  255. $(eval LATEST_TAG := $(shell git describe --abbrev=0 --tags | cut -c2-))
  256. if ! grep -q $(LATEST_TAG) docs/install.md; then\
  257. echo "ERROR: Must update docs/install.md with latest tag first.";\
  258. exit 1;\
  259. fi
  260. if ! grep -q $(LATEST_TAG) docs/releases.md; then\
  261. echo "ERROR: Must update docs/releases.md with latest tag first.";\
  262. exit 1;\
  263. fi
  264. if [ -n "$(shell git status -s)" ]; then\
  265. echo "ERROR: Git repository is in an unclean state.";\
  266. exit 1;\
  267. fi
  268. # Installing targets
  269. install-linux-amd64: remove-binary
  270. sudo cp -a dist/ntfy_linux_amd64_linux_amd64_v1/ntfy /usr/bin/ntfy
  271. install-linux-armv6: remove-binary
  272. sudo cp -a dist/ntfy_linux_armv6_linux_arm_6/ntfy /usr/bin/ntfy
  273. install-linux-armv7: remove-binary
  274. sudo cp -a dist/ntfy_linux_armv7_linux_arm_7/ntfy /usr/bin/ntfy
  275. install-linux-arm64: remove-binary
  276. sudo cp -a dist/ntfy_linux_arm64_linux_arm64/ntfy /usr/bin/ntfy
  277. remove-binary:
  278. sudo rm -f /usr/bin/ntfy
  279. install-linux-amd64-deb: purge-package
  280. sudo dpkg -i dist/ntfy_*_linux_amd64.deb
  281. install-linux-armv6-deb: purge-package
  282. sudo dpkg -i dist/ntfy_*_linux_armv6.deb
  283. install-linux-armv7-deb: purge-package
  284. sudo dpkg -i dist/ntfy_*_linux_armv7.deb
  285. install-linux-arm64-deb: purge-package
  286. sudo dpkg -i dist/ntfy_*_linux_arm64.deb
  287. purge-package:
  288. sudo systemctl stop ntfy || true
  289. sudo apt-get purge ntfy || true