| 1 | # TodoMVC in Nim. See README.md. |
| 2 | SHELL := bash |
| 3 | REPO := $(abspath ../..) |
| 4 | ZIGCC := $(REPO)/scripts/zigcc-aarch64-musl |
| 5 | CACHE := $(CURDIR)/.cache |
| 6 | |
| 7 | VERSION ?= $(shell git rev-parse --short HEAD) |
| 8 | SQLITE := sqlite-amalgamation-3540100 |
| 9 | SQLITE_URL := https://sqlite.org/2026/$(SQLITE).zip |
| 10 | SQLITE_SHA256 := 0a9c8e34352dad46006aa82836d5383bff9dc6bc9ce5a60553c05aa30e6ad1f9 |
| 11 | SQLITE_C := $(CACHE)/sqlite/sqlite3.c |
| 12 | NIMBLE_DIR := $(CACHE)/nimble |
| 13 | DEPS_STAMP := $(NIMBLE_DIR)/.installed |
| 14 | |
| 15 | # zig cc (the arm64 C compiler) keeps compiled musl, compiler-rt and every |
| 16 | # object it builds in its caches; keep them here so clean-deps reaches them. |
| 17 | export ZIG_GLOBAL_CACHE_DIR := $(CACHE)/zig-global |
| 18 | export ZIG_LOCAL_CACHE_DIR := $(CACHE)/zig-local |
| 19 | |
| 20 | # Every library is compiled from source into a nimcache under .cache/. |
| 21 | NIMFLAGS := --hints:off -d:release --opt:speed \ |
| 22 | --noNimblePath $$(for p in $(NIMBLE_DIR)/pkgs2/*/; do echo --path:$$p; done) \ |
| 23 | --dynlibOverride:sqlite3 -d:sqliteSource=$(SQLITE_C) \ |
| 24 | -d:appVersion=$(VERSION) |
| 25 | ARM64FLAGS := --cpu:arm64 --os:linux --cc:clang \ |
| 26 | --clang.exe=$(ZIGCC) --clang.linkerexe=$(ZIGCC) --passL:-static --passL:-s |
| 27 | |
| 28 | SOURCES := $(wildcard src/*.nim src/*.nimf static/*) $(REPO)/shared/base.css $(REPO)/shared/index.css |
| 29 | |
| 30 | .PHONY: build build-arm64 test clean clean-deps deps bump unbump deploy |
| 31 | |
| 32 | build: dist/app |
| 33 | build-arm64: dist/app-linux-arm64 |
| 34 | deps: $(DEPS_STAMP) $(SQLITE_C) |
| 35 | |
| 36 | dist/app: $(SOURCES) deps .FORCE |
| 37 | nim c $(NIMFLAGS) --nimcache:$(CACHE)/nimcache-host -o:$@ src/app.nim |
| 38 | |
| 39 | dist/app-linux-arm64: $(SOURCES) deps .FORCE |
| 40 | nim c $(NIMFLAGS) $(ARM64FLAGS) --nimcache:$(CACHE)/nimcache-arm64 -o:$@ src/app.nim |
| 41 | |
| 42 | test: deps |
| 43 | nim c -r $(NIMFLAGS) -d:testing --nimcache:$(CACHE)/nimcache-test -o:$(CACHE)/test_app tests/test_app.nim |
| 44 | |
| 45 | $(DEPS_STAMP): todo.nimble |
| 46 | source $(REPO)/scripts/env.sh && nimble --nimbleDir:$(NIMBLE_DIR) install --depsOnly -y |
| 47 | touch $@ |
| 48 | |
| 49 | $(SQLITE_C): |
| 50 | mkdir -p $(CACHE)/dl $(CACHE)/sqlite |
| 51 | [ -f $(CACHE)/dl/$(SQLITE).zip ] || curl -sSfL -o $(CACHE)/dl/$(SQLITE).zip $(SQLITE_URL) |
| 52 | echo "$(SQLITE_SHA256) $(CACHE)/dl/$(SQLITE).zip" | sha256sum -c --quiet |
| 53 | unzip -q -o -j $(CACHE)/dl/$(SQLITE).zip '$(SQLITE)/sqlite3.c' '$(SQLITE)/sqlite3.h' -d $(CACHE)/sqlite |
| 54 | touch $@ |
| 55 | |
| 56 | clean: |
| 57 | rm -rf dist |
| 58 | |
| 59 | # Compiled output only: downloaded sources (.cache/nimble, .cache/dl, |
| 60 | # .cache/sqlite) stay, so the next build compiles every library again, |
| 61 | # musl and compiler-rt included. |
| 62 | clean-deps: clean |
| 63 | rm -rf $(CACHE)/nimcache-host $(CACHE)/nimcache-arm64 $(CACHE)/nimcache-test $(CACHE)/test_app \ |
| 64 | $(CACHE)/zig-global $(CACHE)/zig-local |
| 65 | |
| 66 | bump: |
| 67 | sed -i 's/serverName = "todo-nim"/serverName = "todo-nim bumped"/' src/app.nim |
| 68 | |
| 69 | unbump: |
| 70 | sed -i 's/serverName = "todo-nim bumped"/serverName = "todo-nim"/' src/app.nim |
| 71 | |
| 72 | deploy: |
| 73 | source $(REPO)/scripts/env.sh && carlos deploy --account bfj --app todo-nim \ |
| 74 | --target linux-arm64 --no-prompt --message "$${MESSAGE:-Deploy todo-nim $(VERSION)}" \ |
| 75 | dist/app-linux-arm64 |
| 76 | |
| 77 | .FORCE: |
| 78 | |