# The Matrix connector of the Messages app.
#
# Two Olm backends: the pure-Go one (build tag goolm; the default, a
# static binary, no C tool chain) and libolm (cgo; libolm is built
# from source here, statically). Run inside the distrobox:
#
#   lovely-wm/scripts/box.sh make -C core-apps/messages/connectors/matrix <target>
#
#   make test            tests, pure-Go backend
#   make test-libolm     tests, libolm backend (builds libolm for the host first)
#   make selftest        the self-test against the built-in mock homeserver
#   make cross           lovely-connector-matrix for NetBSD/arm64, pure Go  (out/)
#   make cross-libolm    the same with libolm linked statically            (out/)
#   make build           a Linux binary of the host, pure Go                (out/)
#   make olm-host olm-netbsd   libolm $(OLM_VERSION) built from source     (.build/)

TOP        := $(abspath ../../../..)
OUT        := out
BUILD      := .build
OLM_VERSION := 3.2.16
OLM_SRC    := $(BUILD)/olm-$(OLM_VERSION)
OLM_URL    := https://gitlab.matrix.org/matrix-org/olm/-/archive/$(OLM_VERSION)/olm-$(OLM_VERSION).tar.gz
OLM_HOST   := $(abspath $(BUILD)/host/install)
OLM_NETBSD := $(abspath $(BUILD)/netbsd-arm64/install)
TOOLS      := $(TOP)/build/tools/bin
SYSROOT    := $(TOP)/build/destdir
GOFLAGS_GOOLM := -tags goolm
LDFLAGS    := -s -w
PKG        := ./cmd/lovely-connector-matrix

.PHONY: all build cross cross-libolm test test-libolm selftest selftest-spawn olm-fetch olm-host olm-netbsd vet clean

all: cross

build: | $(OUT)
	CGO_ENABLED=0 go build $(GOFLAGS_GOOLM) -ldflags "$(LDFLAGS)" -o $(OUT)/lovely-connector-matrix $(PKG)

cross: | $(OUT)
	CGO_ENABLED=0 GOOS=netbsd GOARCH=arm64 go build $(GOFLAGS_GOOLM) -ldflags "$(LDFLAGS)" -o $(OUT)/lovely-connector-matrix.netbsd-arm64 $(PKG)

# libolm linked statically; libstdc++ and libc from the sysroot.
cross-libolm: olm-netbsd | $(OUT)
	CGO_ENABLED=1 GOOS=netbsd GOARCH=arm64 \
	CC=$(TOOLS)/aarch64--netbsd-gcc CXX=$(TOOLS)/aarch64--netbsd-g++ \
	CGO_CFLAGS="--sysroot=$(SYSROOT) -I$(OLM_NETBSD)/include" \
	CGO_LDFLAGS="--sysroot=$(SYSROOT) -L$(OLM_NETBSD)/lib" \
	go build -ldflags "$(LDFLAGS) -linkmode external -extldflags -static" -o $(OUT)/lovely-connector-matrix.netbsd-arm64-libolm $(PKG)

test:
	CGO_ENABLED=0 go test $(GOFLAGS_GOOLM) -count=1 ./...

test-libolm: olm-host
	CGO_ENABLED=1 CGO_CFLAGS="-I$(OLM_HOST)/include" CGO_LDFLAGS="-L$(OLM_HOST)/lib" go test -count=1 ./...

vet:
	CGO_ENABLED=0 go vet $(GOFLAGS_GOOLM) ./...

selftest: build
	$(OUT)/lovely-connector-matrix -selftest

# The connector as a child process on descriptor 3, as the daemon
# starts it: the memory reported is the connector's alone.
selftest-spawn: build
	LOVELY_MATRIX_SELFTEST_SPAWN=$(abspath $(OUT)/lovely-connector-matrix) $(OUT)/lovely-connector-matrix -selftest

olm-fetch: $(OLM_SRC)/CMakeLists.txt

$(OLM_SRC)/CMakeLists.txt:
	mkdir -p $(BUILD)
	curl -fsSL -o $(BUILD)/olm.tar.gz $(OLM_URL)
	tar -C $(BUILD) -xzf $(BUILD)/olm.tar.gz
	rm $(BUILD)/olm.tar.gz

olm-host: $(OLM_HOST)/lib/libolm.a

$(OLM_HOST)/lib/libolm.a: $(OLM_SRC)/CMakeLists.txt
	cmake -S $(OLM_SRC) -B $(BUILD)/host -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DOLM_TESTS=OFF -DCMAKE_INSTALL_PREFIX=$(OLM_HOST) >/dev/null
	cmake --build $(BUILD)/host -j8 >/dev/null
	cmake --install $(BUILD)/host >/dev/null

olm-netbsd: $(OLM_NETBSD)/lib/libolm.a

$(OLM_NETBSD)/lib/libolm.a: $(OLM_SRC)/CMakeLists.txt
	cmake -S $(OLM_SRC) -B $(BUILD)/netbsd-arm64 -DCMAKE_TOOLCHAIN_FILE=$(abspath olm-toolchain.cmake) -DTOP=$(TOP) \
	    -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DOLM_TESTS=OFF -DCMAKE_INSTALL_PREFIX=$(OLM_NETBSD) >/dev/null
	cmake --build $(BUILD)/netbsd-arm64 -j8 >/dev/null
	cmake --install $(BUILD)/netbsd-arm64 >/dev/null

$(OUT):
	mkdir -p $(OUT)

clean:
	rm -rf $(OUT) $(BUILD)/host $(BUILD)/netbsd-arm64
