# plugin_ports - SCORPIOX BOT open-source local listening socket inspector
#
# Targets:
#   make            native build  -> ./plugin_ports
#   make musl       static musl x86_64 -> ./plugin_ports.musl
#   make arm64      static arm64 musl (aarch64 cross) -> ./plugin_ports.arm64
#   make win64      MinGW-w64 static -> ./plugin_ports.exe
#   make macos      Apple clang -> ./plugin_ports (run this 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
ARM_CC        ?= aarch64-linux-gnu-gcc
ARM_CFLAGS    := -O2 -std=c99 -Wall -Wextra -static
WIN_CC        ?= x86_64-w64-mingw32-gcc
WIN_CFLAGS    := -O2 -std=c99 -Wall -Wextra -static
WIN_LIBS      := -liphlpapi -lws2_32
SRC           := src/plugin_ports.c
BIN           := plugin_ports
EXE           := plugin_ports.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).musl $(SRC)

# Static aarch64 build - Raspberry Pi 5 / ARM64 servers
arm64: $(SRC)
	@echo "==> static arm64 build ($(ARM_CC))"
	$(ARM_CC) $(ARM_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) $(WIN_LIBS)

# 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"
