Makefile 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. MAKEFLAGS := --jobs=1
  2. VERSION := $(shell git describe --tag)
  3. .PHONY:
  4. help:
  5. @echo "Typical commands (more see below):"
  6. @echo " make build - Build web app, documentation and server/client (sloowwww)"
  7. @echo " make cli-linux-amd64 - Build server/client binary (amd64, no web app or docs)"
  8. @echo " make install-linux-amd64 - Install ntfy binary to /usr/bin/ntfy (amd64)"
  9. @echo " make web - Build the web app"
  10. @echo " make docs - Build the documentation"
  11. @echo " make check - Run all tests, vetting/formatting checks and linters"
  12. @echo
  13. @echo "Build everything:"
  14. @echo " make build - Build web app, documentation and server/client"
  15. @echo " make clean - Clean build/dist folders"
  16. @echo
  17. @echo "Build server & client (using GoReleaser, not release version):"
  18. @echo " make cli - Build server & client (all architectures)"
  19. @echo " make cli-linux-amd64 - Build server & client (Linux, amd64 only)"
  20. @echo " make cli-linux-armv6 - Build server & client (Linux, armv6 only)"
  21. @echo " make cli-linux-armv7 - Build server & client (Linux, armv7 only)"
  22. @echo " make cli-linux-arm64 - Build server & client (Linux, arm64 only)"
  23. @echo " make cli-windows-amd64 - Build client (Windows, amd64 only)"
  24. @echo " make cli-darwin-all - Build client (macOS, arm64+amd64 universal binary)"
  25. @echo
  26. @echo "Build server & client (without GoReleaser):"
  27. @echo " make cli-linux-server - Build client & server (no GoReleaser, current arch, Linux)"
  28. @echo " make cli-darwin-server - Build client & server (no GoReleaser, current arch, macOS)"
  29. @echo " make cli-client - Build client only (no GoReleaser, current arch, Linux/macOS/Windows)"
  30. @echo
  31. @echo "Build web app:"
  32. @echo " make web - Build the web app"
  33. @echo " make web-deps - Install web app dependencies (npm install the universe)"
  34. @echo " make web-build - Actually build the web app"
  35. @echo
  36. @echo "Build documentation:"
  37. @echo " make docs - Build the documentation"
  38. @echo " make docs-deps - Install Python dependencies (pip3 install)"
  39. @echo " make docs-build - Actually build the documentation"
  40. @echo
  41. @echo "Test/check:"
  42. @echo " make test - Run tests"
  43. @echo " make race - Run tests with -race flag"
  44. @echo " make coverage - Run tests and show coverage"
  45. @echo " make coverage-html - Run tests and show coverage (as HTML)"
  46. @echo " make coverage-upload - Upload coverage results to codecov.io"
  47. @echo
  48. @echo "Lint/format:"
  49. @echo " make fmt - Run 'go fmt'"
  50. @echo " make fmt-check - Run 'go fmt', but don't change anything"
  51. @echo " make vet - Run 'go vet'"
  52. @echo " make lint - Run 'golint'"
  53. @echo " make staticcheck - Run 'staticcheck'"
  54. @echo
  55. @echo "Releasing:"
  56. @echo " make release - Create a release"
  57. @echo " make release-snapshot - Create a test release"
  58. @echo
  59. @echo "Install locally (requires sudo):"
  60. @echo " make install-linux-amd64 - Copy amd64 binary from dist/ to /usr/bin/ntfy"
  61. @echo " make install-linux-armv6 - Copy armv6 binary from dist/ to /usr/bin/ntfy"
  62. @echo " make install-linux-armv7 - Copy armv7 binary from dist/ to /usr/bin/ntfy"
  63. @echo " make install-linux-arm64 - Copy arm64 binary from dist/ to /usr/bin/ntfy"
  64. @echo " make install-linux-deb-amd64 - Install .deb from dist/ (amd64 only)"
  65. @echo " make install-linux-deb-armv6 - Install .deb from dist/ (armv6 only)"
  66. @echo " make install-linux-deb-armv7 - Install .deb from dist/ (armv7 only)"
  67. @echo " make install-linux-deb-arm64 - Install .deb from dist/ (arm64 only)"
  68. # Building everything
  69. clean: .PHONY
  70. rm -rf dist build server/docs server/site
  71. build: web docs cli
  72. update: web-deps-update cli-deps-update docs-deps-update
  73. docker pull alpine
  74. # Ubuntu-specific
  75. build-deps-ubuntu:
  76. sudo apt update
  77. sudo apt install -y \
  78. curl \
  79. gcc-aarch64-linux-gnu \
  80. gcc-arm-linux-gnueabi \
  81. upx \
  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 --rm-dist
  122. cli-linux-amd64: cli-deps-static-sites
  123. goreleaser build --snapshot --rm-dist --id ntfy_linux_amd64
  124. cli-linux-armv6: cli-deps-static-sites cli-deps-gcc-armv6-armv7
  125. goreleaser build --snapshot --rm-dist --id ntfy_linux_armv6
  126. cli-linux-armv7: cli-deps-static-sites cli-deps-gcc-armv6-armv7
  127. goreleaser build --snapshot --rm-dist --id ntfy_linux_armv7
  128. cli-linux-arm64: cli-deps-static-sites cli-deps-gcc-arm64
  129. goreleaser build --snapshot --rm-dist --id ntfy_linux_arm64
  130. cli-windows-amd64: cli-deps-static-sites
  131. goreleaser build --snapshot --rm-dist --id ntfy_windows_amd64
  132. cli-darwin-all: cli-deps-static-sites
  133. goreleaser build --snapshot --rm-dist --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=$(shell git rev-parse --short HEAD) -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=$(shell git rev-parse --short HEAD) -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=$(shell git rev-parse --short HEAD) -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. which upx || { echo "ERROR: upx not installed. On Ubuntu, run: apt install upx"; exit 1; }
  168. go install github.com/goreleaser/goreleaser@latest
  169. cli-deps-gcc-armv6-armv7:
  170. which arm-linux-gnueabi-gcc || { echo "ERROR: ARMv6/ARMv7 cross compiler not installed. On Ubuntu, run: apt install gcc-arm-linux-gnueabi"; exit 1; }
  171. cli-deps-gcc-arm64:
  172. which aarch64-linux-gnu-gcc || { echo "ERROR: ARM64 cross compiler not installed. On Ubuntu, run: apt install gcc-aarch64-linux-gnu"; exit 1; }
  173. cli-deps-update:
  174. go get -u
  175. go install honnef.co/go/tools/cmd/staticcheck@latest
  176. go install golang.org/x/lint/golint@latest
  177. go install github.com/goreleaser/goreleaser@latest
  178. cli-build-results:
  179. cat dist/config.yaml
  180. [ -f dist/artifacts.json ] && cat dist/artifacts.json | jq . || true
  181. [ -f dist/metadata.json ] && cat dist/metadata.json | jq . || true
  182. [ -f dist/checksums.txt ] && cat dist/checksums.txt || true
  183. find dist -maxdepth 2 -type f \
  184. \( -name '*.deb' -or -name '*.rpm' -or -name '*.zip' -or -name '*.tar.gz' -or -name 'ntfy' \) \
  185. -and -not -path 'dist/goreleaserdocker*' \
  186. -exec sha256sum {} \;
  187. # Test/check targets
  188. check: test fmt-check vet lint staticcheck
  189. test: .PHONY
  190. go test -v $(shell go list ./... | grep -vE 'ntfy/(test|examples|tools)')
  191. race: .PHONY
  192. go test -race $(shell go list ./... | grep -vE 'ntfy/(test|examples|tools)')
  193. coverage:
  194. mkdir -p build/coverage
  195. go test -race -coverprofile=build/coverage/coverage.txt -covermode=atomic $(shell go list ./... | grep -vE 'ntfy/(test|examples|tools)')
  196. go tool cover -func build/coverage/coverage.txt
  197. coverage-html:
  198. mkdir -p build/coverage
  199. go test -race -coverprofile=build/coverage/coverage.txt -covermode=atomic $(shell go list ./... | grep -vE 'ntfy/(test|examples|tools)')
  200. go tool cover -html build/coverage/coverage.txt
  201. coverage-upload:
  202. cd build/coverage && (curl -s https://codecov.io/bash | bash)
  203. # Lint/formatting targets
  204. fmt:
  205. gofmt -s -w .
  206. fmt-check:
  207. test -z $(shell gofmt -l .)
  208. vet:
  209. go vet ./...
  210. lint:
  211. which golint || go install golang.org/x/lint/golint@latest
  212. go list ./... | grep -v /vendor/ | xargs -L1 golint -set_exit_status
  213. staticcheck: .PHONY
  214. rm -rf build/staticcheck
  215. which staticcheck || go install honnef.co/go/tools/cmd/staticcheck@latest
  216. mkdir -p build/staticcheck
  217. ln -s "go" build/staticcheck/go
  218. PATH="$(PWD)/build/staticcheck:$(PATH)" staticcheck ./...
  219. rm -rf build/staticcheck
  220. # Releasing targets
  221. release: clean update cli-deps release-checks docs web check
  222. goreleaser release --rm-dist
  223. release-snapshot: clean update cli-deps docs web check
  224. goreleaser release --snapshot --skip-publish --rm-dist
  225. release-checks:
  226. $(eval LATEST_TAG := $(shell git describe --abbrev=0 --tags | cut -c2-))
  227. if ! grep -q $(LATEST_TAG) docs/install.md; then\
  228. echo "ERROR: Must update docs/install.md with latest tag first.";\
  229. exit 1;\
  230. fi
  231. if ! grep -q $(LATEST_TAG) docs/releases.md; then\
  232. echo "ERROR: Must update docs/releases.md with latest tag first.";\
  233. exit 1;\
  234. fi
  235. if [ -n "$(shell git status -s)" ]; then\
  236. echo "ERROR: Git repository is in an unclean state.";\
  237. exit 1;\
  238. fi
  239. # Installing targets
  240. install-linux-amd64: remove-binary
  241. sudo cp -a dist/ntfy_linux_amd64_linux_amd64_v1/ntfy /usr/bin/ntfy
  242. install-linux-armv6: remove-binary
  243. sudo cp -a dist/ntfy_linux_armv6_linux_arm_6/ntfy /usr/bin/ntfy
  244. install-linux-armv7: remove-binary
  245. sudo cp -a dist/ntfy_linux_armv7_linux_arm_7/ntfy /usr/bin/ntfy
  246. install-linux-arm64: remove-binary
  247. sudo cp -a dist/ntfy_linux_arm64_linux_arm64/ntfy /usr/bin/ntfy
  248. remove-binary:
  249. sudo rm -f /usr/bin/ntfy
  250. install-linux-amd64-deb: purge-package
  251. sudo dpkg -i dist/ntfy_*_linux_amd64.deb
  252. install-linux-armv6-deb: purge-package
  253. sudo dpkg -i dist/ntfy_*_linux_armv6.deb
  254. install-linux-armv7-deb: purge-package
  255. sudo dpkg -i dist/ntfy_*_linux_armv7.deb
  256. install-linux-arm64-deb: purge-package
  257. sudo dpkg -i dist/ntfy_*_linux_arm64.deb
  258. purge-package:
  259. sudo systemctl stop ntfy || true
  260. sudo apt-get purge ntfy || true