Makefile 12 KB

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