AnyBot/Makefile

194 lines
9.7 KiB
Makefile
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# AnyBot — build and install from source. make help lists everything.
#
# git clone https://code.30cm.net/daniel.w/AnyBot.git && cd AnyBot
# make deps everything the build needs (system packages, Rust) — once
# make install build and install: ~/Applications/AnyBot.app on a Mac, ~/.local/bin on Linux
# make run open it without installing
#
# The one-step version of all of this is ./install.sh (README.md).
# Rust installed by rustup lives in ~/.cargo/bin, which a fresh shell may not have on PATH yet.
export PATH := $(HOME)/.cargo/bin:$(PATH)
CARGO ?= cargo
UNAME := $(shell uname -s)
# The version, as the program shows it: v<number in Cargo.toml>-<commit>, with "-dirty" when what
# goes into the program has uncommitted changes (the same paths as INPUTS in build.rs). Outside git
# it is just v<number>. ANYBOT_GIT_COMMIT is handed to cargo, so the program built here and the
# dist file names say the same thing (build.rs takes it instead of asking git again).
VERSION_NUMBER := $(shell sed -n 's/^version *= *"\(.*\)"/\1/p' Cargo.toml | head -n 1)
GIT_COMMIT := $(shell git rev-parse --short=7 HEAD 2>/dev/null)$(shell git status --porcelain --untracked-files=no -- src box fonts icons mobile Cargo.toml Cargo.lock build.rs 2>/dev/null | grep -q . && echo -dirty)
export ANYBOT_GIT_COMMIT ?= $(GIT_COMMIT)
VERSION ?= v$(VERSION_NUMBER)$(if $(ANYBOT_GIT_COMMIT),-$(ANYBOT_GIT_COMMIT))
TARGET := $(or $(CARGO_TARGET_DIR),target)
APPDIR ?= $(HOME)/Applications
BIN := $(TARGET)/release/anybot
RELAY := $(if $(CARGO_TARGET_DIR),$(CARGO_TARGET_DIR),relay/target)/release/anybot-relay
# make update PULL=0: install this checkout as it is, without pulling the newest first.
PULL ?= 1
.PHONY: all build app relay run serve setup relay-run test deps doctor install uninstall update update-app update-hands update-server update-box server-logs hands server dist clean help check-rust lint-sh sign-setup
all: build
help:
@echo "AnyBot $(VERSION)"
@echo ""
@echo " make 建置 AnyBot 和帳號伺服器(正式版)"
@echo " make run 直接打開 AnyBot(不安裝)"
@echo " make install 安裝:Mac → $(APPDIR)/AnyBot.app;Linux → ~/.local/bin/anybot 和選單"
@echo " make uninstall 移除安裝的程式(你的 Bot 和聊天不會刪)"
@echo ""
@echo " make update 更新這台裝的 AnyBot(伺服器、App、手腳,自己認)"
@echo " make update-app 只更新桌面 App(有背景服務也一起重新啟動)"
@echo " make update-hands 只更新 Linux 手腳(換新程式、重新啟動背景服務)"
@echo " make update-server 只更新伺服器(帳號伺服器 + 大腦,Docker)"
@echo " 加 PULL=0:不拉新版,裝現在這份原始碼"
@echo " make update-box 重建「Bot 的電腦」映像並換上新電腦(檔案和登入保留)"
@echo " 有視窗的大腦請優先用設定 › 電腦 › 更新 Bot 的電腦(會先存快照)"
@echo ""
@echo " make deps 裝好建置需要的東西:系統套件和 Rust(Linux 會問 sudo 密碼)"
@echo " make doctor 檢查這台缺什麼"
@echo " make test 跑測試"
@echo " make lint-sh 檢查 shell 腳本(語法、中文標點黏變數)"
@echo ""
@echo " make serve 不開視窗執行(伺服器、一直開著的電腦)"
@echo " make setup 把這台接到你的 AnyBot(終端機裡的引導畫面)"
@echo " make hands Linux:裝好虛擬螢幕等,再接成大腦的手腳"
@echo " make relay-run 在這台跑帳號伺服器(埠 8080,資料在 ./relay-data)"
@echo " make server 在伺服器上架好帳號伺服器 + 大腦 + HTTPS(Docker);再跑一次可以改設定"
@echo " make server-logs 看伺服器上大腦的紀錄"
@echo " 伺服器的設定只在 deploy/server/.env;改了之後 make update-server"
@echo ""
@echo " make dist 打包發行檔到 dist/(Mac:.app 的 zip;Linux:三個 tar.gz)"
@echo " make clean 清掉建置結果"
build: app relay
check-rust:
@command -v $(CARGO) >/dev/null 2>&1 || { echo; echo " ✗ 找不到 Rust(cargo)。先執行:make deps"; echo; exit 1; }
@set -- $$(rustc --version | sed -n 's/^rustc \([0-9][0-9]*\)\.\([0-9][0-9]*\).*/\1 \2/p'); { [ "$${1:-0}" -gt 1 ] || { [ "$${1:-0}" -eq 1 ] && [ "$${2:-0}" -ge 95 ]; }; } || { echo; echo " ✗ Rust 太舊($$(rustc --version)),要 1.95 以上。執行:rustup update stable(或 make deps)"; echo; exit 1; }
app: check-rust
$(CARGO) build --release --bin anybot
relay: check-rust
cd relay && $(CARGO) build --release
run: app
$(BIN)
serve: app
$(BIN) --serve
setup: app
$(BIN) setup
relay-run: relay
$(RELAY) --port 8080 --data relay-data
test: check-rust
$(CARGO) test --bin anybot
cd relay && $(CARGO) test
lint-sh:
@sh deploy/ci/lint-sh.sh
deps:
sh ./install.sh deps
doctor:
@echo "AnyBot $(VERSION) · $(UNAME) $$(uname -m)"
@command -v git >/dev/null && echo " ✓ git" || echo " ✗ git:make deps"
@command -v cc >/dev/null && echo " ✓ C 編譯器" || echo " ✗ C 編譯器:make deps"
@command -v $(CARGO) >/dev/null && echo " ✓ $$(rustc --version)" || echo " ✗ Rust:make deps"
ifeq ($(UNAME),Linux)
@pkg-config --exists alsa 2>/dev/null && echo " ✓ ALSA(libasound2-dev)" || echo " ✗ ALSA 開發套件:make deps"
@command -v docker >/dev/null && echo " ✓ $$(docker --version)" || echo " · 沒有 Docker(只有伺服器和 Bot 自己的電腦需要;make server 會自動裝)"
endif
ifeq ($(UNAME),Darwin)
@xcode-select -p >/dev/null 2>&1 && echo " ✓ Xcode Command Line Tools" || echo " ✗ Xcode Command Line Tools:make deps"
endif
@test -x $(BIN) && echo " ✓ 已建置 $(BIN)" || echo " · 還沒建置:make"
@command -v docker >/dev/null 2>&1 && { docker volume inspect anybot-box-home >/dev/null 2>&1 && echo " ✓ Bot 的電腦的資料碟 anybot-box-home(檔案和登入都在裡面)" || echo " · 沒有 anybot-box-home 資料碟(Bot 的電腦還沒建立過,或被清除過——有快照會在下次建立時自動還原)"; } || true
@d="$${HOME}/Library/Application Support/AnyBot/box/snapshots"; [ -d "$$d" ] || d="$${HOME}/.local/share/AnyBot/box/snapshots"; [ -d "$$d" ] && echo " ✓ 快照 $$(ls "$$d" 2>/dev/null | grep -c '^snapshot-') 份(留最近 7 天+之前每週一份;重設前也會先存)" || echo " · 還沒有快照(Bot 的電腦每天自動存,重設前也會先存)"
install: app
ifeq ($(UNAME),Darwin)
KEEP_APP=1 sh deploy/mac/bundle.sh $(VERSION)
mkdir -p "$(APPDIR)"
rm -rf "$(APPDIR)/AnyBot.app"
mv dist/AnyBot.app "$(APPDIR)/AnyBot.app"
mkdir -p "$(HOME)/.local/bin"
ln -sfn "$(APPDIR)/AnyBot.app/Contents/MacOS/AnyBot" "$(HOME)/.local/bin/anybot"
@echo "已安裝 $(APPDIR)/AnyBot.app(第一次打開:按右鍵 › 打開)"
else ifneq ($(findstring MINGW,$(UNAME)),)
powershell.exe -NoProfile -ExecutionPolicy Bypass -File install.ps1 app
else ifeq ($(OS),Windows_NT)
powershell.exe -NoProfile -ExecutionPolicy Bypass -File install.ps1 app
else
sh deploy/linux/install.sh
endif
uninstall:
ifeq ($(UNAME),Darwin)
rm -rf "$(APPDIR)/AnyBot.app"
else
rm -f "$(HOME)/.local/bin/anybot" "$(HOME)/.local/share/applications/anybot.desktop"
rm -f "$(HOME)/.local/share/icons/hicolor/scalable/apps/anybot.svg" "$(HOME)/.local/share/icons/hicolor/256x256/apps/anybot.png" "$(HOME)/.local/share/icons/hicolor/128x128/apps/anybot.png"
endif
@echo "已移除程式。你的 Bot、聊天和設定還在(要全部刪掉請看 docs/install.md)。"
hands: app
sh deploy/linux/hands.sh --deps
# Pull, rebuild, swap the installed program, restart what runs in the background (deploy/update.sh).
update:
@PULL=$(PULL) MAKE="$(MAKE)" sh deploy/update.sh auto
update-app:
@PULL=$(PULL) MAKE="$(MAKE)" sh deploy/update.sh app
update-hands:
@PULL=$(PULL) MAKE="$(MAKE)" sh deploy/update.sh hands
update-server:
@PULL=$(PULL) MAKE="$(MAKE)" sh deploy/update.sh server
# The box (Bot 的電腦): rebuild the image from box/ and take the container down, so the running
# brain opens a new computer from the new image the next time a bot uses one (create_container
# on demand). The home volume (anybot-box-home) is untouched: files and browser logins stay.
# Same effect as 設定 › 電腦 › 更新 Bot 的電腦, minus its snapshot and progress display — on a
# brain with a window, prefer the settings page. A box renamed per brain (ANYBOT_BOX_NAME) and a
# box image of its own (ANYBOT_BOX_IMAGE) are respected.
update-box:
@if [ "$(PULL)" = "1" ]; then git pull --ff-only || { echo " ✗ git pull 沒成功(有未送出的修改?),先處理好再更新"; exit 1; }; fi
@command -v docker >/dev/null 2>&1 || { echo " ✗ 這台沒有 docker,建不了 Bot 的電腦"; exit 1; }
@V=$$(sed -n 's/^LABEL anybot.box.version="\([0-9]*\)"/\1/p' box/Dockerfile); \
echo "建立 Bot 的電腦映像(box 第 $$V 版,第一次要幾分鐘)…"; \
docker build -q -t $${ANYBOT_BOX_IMAGE:-anybot-box:1} box/ >/dev/null || { echo " ✗ 建立映像檔失敗"; exit 1; }; \
echo "映像檔完成。"
@docker rm -f $${ANYBOT_BOX_NAME:-anybot-box} >/dev/null 2>&1 && echo "舊電腦已停。" || true
@echo "Bot 下次用電腦時會用新映像開機(檔案和登入保留)。"
# The author, once: sign every commit so installs can check where an update came from.
sign-setup:
@sh deploy/sign-setup.sh
server-logs:
@bash deploy/server/setup.sh --logs
server:
sh ./install.sh server
dist: build
ifeq ($(UNAME),Darwin)
sh deploy/mac/bundle.sh $(VERSION)
else
sh deploy/ci/package-linux.sh $(VERSION)
endif
clean:
$(CARGO) clean
cd relay && $(CARGO) clean
rm -rf dist