# plugin_nvtop - SCORPIOX BOT open-source local GPU telemetry plugin
#
# Targets:
#   make            native build  -> ./plugin_nvtop
#   make musl       static musl x86_64 -> ./plugin_nvtop (fully static)
#   make arm64      static musl aarch64 -> ./plugin_nvtop.arm64
#   make win64      MinGW-w64 static -> ./plugin_nvtop.exe
#   make macos      Apple clang -> ./plugin_nvtop (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 -Wno-overlength-strings
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 -Wno-overlength-strings
WIN_CC        ?= x86_64-w64-mingw32-gcc
WIN_CFLAGS    := -O2 -std=c99 -Wall -Wextra -static -Wno-overlength-strings
SRC           := src/plugin_nvtop.c
BIN           := plugin_nvtop
EXE           := plugin_nvtop.exe

.PHONY: all native musl arm64 win64 macos test clean 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
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).musl $(BIN).arm64 \
	      $(BIN).macos-arm64 $(BIN).macos-x86_64
	rm -rf bin

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