| 1 | # todo-swift: see README.md. |
| 2 | # |
| 3 | # Everything SwiftPM fetches or compiles stays under .cache/ (sources in |
| 4 | # .cache/swiftpm and .cache/build/checkouts, compiled output in |
| 5 | # .cache/build/<triple>), so `make clean-deps` can remove the compiled |
| 6 | # part without touching any other project. |
| 7 | |
| 8 | SHELL := bash |
| 9 | ROOT := $(abspath ../..) |
| 10 | SWIFT := source $(ROOT)/scripts/env.sh && swift |
| 11 | SWIFTPM := --scratch-path .cache/build --cache-path .cache/swiftpm |
| 12 | ARM64_SDK := aarch64-swift-linux-musl |
| 13 | DIRTY := $(shell git status --porcelain -- . 2>/dev/null | grep -q . && echo -dirty) |
| 14 | VERSION ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo dev)$(DIRTY) |
| 15 | MSG ?= todo-swift $(VERSION) |
| 16 | |
| 17 | .PHONY: build build-arm64 test clean clean-deps bump unbump deploy generate run |
| 18 | |
| 19 | # Embeds the CSS and JS into the binary and stamps the build version. |
| 20 | # Each file is only rewritten when its content changes. |
| 21 | generate: |
| 22 | @tools/embed.sh $(ROOT)/shared static Sources/TodoApp/Assets.swift |
| 23 | @mkdir -p .cache |
| 24 | @printf '// GENERATED by make. The build version /api/version reports.\npublic let buildVersion = "%s"\n' '$(VERSION)' > .cache/Version.swift.new |
| 25 | @if cmp -s .cache/Version.swift.new Sources/TodoApp/Version.swift; then rm .cache/Version.swift.new; \ |
| 26 | else mv .cache/Version.swift.new Sources/TodoApp/Version.swift; fi |
| 27 | |
| 28 | build: generate |
| 29 | $(SWIFT) build -c release $(SWIFTPM) |
| 30 | @mkdir -p dist |
| 31 | cp .cache/build/release/todo dist/app.new && mv dist/app.new dist/app |
| 32 | |
| 33 | build-arm64: generate |
| 34 | $(SWIFT) build -c release $(SWIFTPM) --swift-sdk $(ARM64_SDK) -Xlinker --strip-all |
| 35 | @mkdir -p dist |
| 36 | cp .cache/build/$(ARM64_SDK)/release/todo dist/app-linux-arm64.new && mv dist/app-linux-arm64.new dist/app-linux-arm64 |
| 37 | echo '$(VERSION)' > dist/app-linux-arm64.version |
| 38 | @file dist/app-linux-arm64 | grep -q 'ARM aarch64.*statically linked' \ |
| 39 | || { echo "dist/app-linux-arm64 is not a static arm64 binary"; file dist/app-linux-arm64; exit 1; } |
| 40 | |
| 41 | test: generate |
| 42 | $(SWIFT) test $(SWIFTPM) |
| 43 | |
| 44 | # Runs the local build on 127.0.0.1 for the shared suite. |
| 45 | PORT ?= 8080 |
| 46 | run: build |
| 47 | dist/app --listen 127.0.0.1:$(PORT) --db $${TMPDIR:-/tmp}/todo-swift.db |
| 48 | |
| 49 | clean: |
| 50 | rm -rf dist |
| 51 | |
| 52 | # Removes every compiled artifact, ours and the dependencies', and keeps the |
| 53 | # downloaded sources (.cache/swiftpm, .cache/build/checkouts and |
| 54 | # .cache/build/repositories), so the next build compiles everything again. |
| 55 | clean-deps: clean |
| 56 | @[ ! -d .cache/build ] || find .cache/build -mindepth 1 -maxdepth 1 \ |
| 57 | ! -name checkouts ! -name repositories ! -name artifacts ! -name workspace-state.json \ |
| 58 | -exec rm -rf {} + |
| 59 | |
| 60 | bump: |
| 61 | sed -i 's/^let bumpNote = "unbumped"$$/let bumpNote = "bumped"/' Sources/TodoApp/Routes.swift |
| 62 | grep -q '^let bumpNote = "bumped"$$' Sources/TodoApp/Routes.swift |
| 63 | |
| 64 | unbump: |
| 65 | sed -i 's/^let bumpNote = "bumped"$$/let bumpNote = "unbumped"/' Sources/TodoApp/Routes.swift |
| 66 | grep -q '^let bumpNote = "unbumped"$$' Sources/TodoApp/Routes.swift |
| 67 | |
| 68 | # Ships dist/app-linux-arm64. Build it from a clean commit first: the |
| 69 | # version it reports is that commit's short SHA. |
| 70 | deploy: |
| 71 | @[ -f dist/app-linux-arm64 ] || { echo "run make build-arm64 first"; exit 1; } |
| 72 | @case "$(VERSION)" in *-dirty) echo "commit first: the tree has changes"; exit 1;; esac |
| 73 | @[ "$$(cat dist/app-linux-arm64.version)" = '$(VERSION)' ] \ |
| 74 | || { echo "dist/app-linux-arm64 wasn't built at $(VERSION); run make build-arm64"; exit 1; } |
| 75 | carlos deploy --account bfj --app todo-swift --target linux-arm64 --message "$(MSG)" dist/app-linux-arm64 |
| 76 | |