# plugin_hello — SCORPIOX BOT open-source starter plugin
#
# Targets:
#   make            native build  -> ./plugin_hello
#   make musl       static musl x86_64 -> ./plugin_hello (fully static)
#   make arm64      static aarch64 -> ./plugin_hello.arm64 (Pi 5 / ARM64 servers)
#   make win64      MinGW-w64 static -> ./plugin_hello.exe
#   make macos      Apple clang -> ./plugin_hello (run on macOS)
#   make test       build + run tests/run.sh
#   make clean

CC            ?= cc
CFLAGS        ?= -O2 -std=c99 -Wall -Wextra -pedantic -Wno-overlength-strings
MUSL_CC       ?= musl-gcc
MUSL_CFLAGS   := -O2 -std=c99 -Wall -Wextra -static
ARM64_CC      ?= $(shell command -v aarch64-linux-musl-gcc 2>/dev/null || \
                  command -v aarch64-linux-gnu-gcc 2>/dev/null || \
                  echo aarch64-linux-musl-gcc)
ARM64_CFLAGS  := -O2 -std=c99 -Wall -Wextra -static
WIN_CC        ?= x86_64-w64-mingw32-gcc
WIN_CFLAGS    := -O2 -std=c99 -Wall -Wextra -static
SRC           := src/plugin_hello.c
BIN           := plugin_hello
EXE           := plugin_hello.exe

.PHONY: all native musl arm64 win64 macos test clean distclean help

all: native

native: $(BIN)

$(BIN): $(SRC)
	@echo "==> native build ($(CC))"
	$(CC) $(CFLAGS) -o $(BIN) $(SRC)

# Static musl build — the universal Linux artifact (Alpine, Debian, Arch, Omarchy)
musl: $(SRC)
	@echo "==> static musl build ($(MUSL_CC))"
	$(MUSL_CC) $(MUSL_CFLAGS) -o $(BIN) $(SRC)

# Static aarch64 build — Raspberry Pi 5 / ARM64 servers. Prefers the musl cross
# toolchain; the Debian aarch64-linux-gnu-gcc static link also yields a
# self-contained aarch64 ELF.
arm64: $(SRC)
	@echo "==> static arm64 build ($(ARM64_CC))"
	$(ARM64_CC) $(ARM64_CFLAGS) -o $(BIN).arm64 $(SRC)

# Native Win64 static PE — runs on any Windows 10/11 x64 node
win64: $(SRC)
	@echo "==> MinGW-w64 build ($(WIN_CC))"
	$(WIN_CC) $(WIN_CFLAGS) -o $(EXE) $(SRC)

# Apple clang build (run this on macOS)
macos: $(SRC)
	@echo "==> Apple clang build"
	$(CC) $(CFLAGS) -o $(BIN) $(SRC)

test: native
	@sh tests/run.sh

clean:
	rm -f $(BIN) $(EXE) $(BIN).arm64 $(BIN).musl $(BIN).macos-arm64 $(BIN).macos-x86_64
	rm -rf bin

distclean: clean

help:
	@echo "make | make musl | make arm64 | make win64 | make macos | make test | make clean"
