# ⛧ Hades Gate — Makefile
#
# Targets:
#   all        — Build library and examples
#   lib        — Build hades_gate.o only
#   test       — Build test executable
#   clean      — Remove build artifacts
#   msvc       — Build with MSVC (Windows)
#   mingw      — Build with MinGW (Cross-platform)
#
# Usage:
#   Windows (MSVC):    nmake /f Makefile msvc
#   Windows (MinGW):   mingw32-make
#   Linux (Cross):     make CC=x86_64-w64-mingw32-gcc

# Compiler detection and configuration
ifeq ($(OS),Windows_NT)
    # Windows settings
    RM = del /Q
    MKDIR = mkdir
    RMDIR = rmdir /S /Q
    EXE_EXT = .exe
    OBJ_EXT = .obj
    LIB_EXT = .lib
    
    # Default to MSVC on Windows if not specified
    ifeq ($(CC),)
        CC = cl
        CFLAGS = /O2 /GS- /nologo
        LDFLAGS = /link /NOLOGO
        AR = lib
        ARFLAGS = /NOLOGO
    else
        # MinGW on Windows
        CFLAGS ?= -Os -masm=intel -fno-asynchronous-unwind-tables -Wall
        LDFLAGS ?= -lkernel32 -lntdll -s
        OBJ_EXT = .o
        LIB_EXT = .a
    endif
else
    # Linux/Unix cross-compile settings
    CC ?= x86_64-w64-mingw32-gcc
    CFLAGS ?= -Os -masm=intel -fno-asynchronous-unwind-tables -Wall
    LDFLAGS ?= -lkernel32 -lntdll -s
    RM = rm -f
    RMDIR = rm -rf
    MKDIR = mkdir -p
    EXE_EXT = .exe
    OBJ_EXT = .o
    LIB_EXT = .a
endif

# Directories
SRC_DIR = src
INC_DIR = include
EXA_DIR = examples
BLD_DIR = build

# Files
SRC = $(SRC_DIR)/hades_gate.c
OBJ = $(BLD_DIR)/hades_gate$(OBJ_EXT)
LIB = $(BLD_DIR)/hades_gate$(LIB_EXT)

# Test files
TEST_SRC = $(EXA_DIR)/test.c
TEST_EXE = $(BLD_DIR)/hades_test$(EXE_EXT)

.PHONY: all lib test clean dirs msvc mingw help

# Default target
all: dirs lib test

# Create build directory
dirs:
ifeq ($(OS),Windows_NT)
	@if not exist $(BLD_DIR) $(MKDIR) $(BLD_DIR)
else
	@$(MKDIR) $(BLD_DIR) 2>/dev/null || true
endif

# Build library object
lib: dirs
ifeq ($(findstring cl,$(CC)),cl)
	# MSVC build
	$(CC) /c $(CFLAGS) $(SRC) /Fo$(OBJ)
else
	# GCC/MinGW build
	$(CC) $(CFLAGS) -c $(SRC) -o $(OBJ)
endif
	@echo [*] Built Hades Gate library

# Build static library
$(LIB): lib
ifeq ($(findstring cl,$(CC)),cl)
	$(AR) $(ARFLAGS) /OUT:$(LIB) $(OBJ)
else
	$(AR) rcs $(LIB) $(OBJ)
endif

# Build test executable
test: dirs lib
ifeq ($(findstring cl,$(CC)),cl)
	# MSVC build with explicit ntdll linking
	$(CC) $(CFLAGS) $(SRC) $(TEST_SRC) /Fe$(TEST_EXE) /link /NOLOGO ntdll.lib
else
	# GCC/MinGW build
	$(CC) $(CFLAGS) $(SRC) $(TEST_SRC) -o $(TEST_EXE) $(LDFLAGS)
endif
	@echo [*] Built test executable: $(TEST_EXE)

# Build with MSVC explicitly
msvc:
	$(MAKE) CC=cl CFLAGS="/O2 /GS- /nologo" all

# Build with MinGW explicitly
mingw:
ifeq ($(OS),Windows_NT)
	$(MAKE) CC=gcc CFLAGS="-Os -masm=intel -fno-asynchronous-unwind-tables -Wall" all
else
	$(MAKE) CC=x86_64-w64-mingw32-gcc all
endif

# Run test
run: test
	@echo.
	@echo [*] Running Hades Gate test...
	@echo ================================
	$(TEST_EXE)

# Clean build artifacts
clean:
ifeq ($(OS),Windows_NT)
	@if exist $(BLD_DIR) $(RMDIR) $(BLD_DIR)
	@del *.exe *.obj *.o *.a *.lib 2>nul || true
else
	$(RM) -r $(BLD_DIR)
	$(RM) *.exe *.o
endif
	@echo [*] Clean complete

# Help
help:
	@echo "╛ Hades Gate Makefile"
	@echo "====================="
	@echo ""
	@echo "Targets:"
	@echo "  all      - Build library and test executable"
	@echo "  lib      - Build only the Hades Gate library"
	@echo "  test     - Build the test executable"
	@echo "  run      - Build and run the test"
	@echo "  clean    - Remove all build artifacts"
	@echo "  msvc     - Force MSVC build (Windows)"
	@echo "  mingw    - Force MinGW build"
	@echo "  help     - Show this help"
	@echo ""
	@echo "Examples:"
	@echo "  make                      # Build with default compiler"
	@echo "  make CC=gcc              # Use MinGW on Windows"
	@echo "  make clean && make all   # Full rebuild"
	@echo "  nmake /f Makefile msvc   # MSVC on Windows"
	@echo ""
	@echo "Cross-compile from Linux:"
	@echo "  sudo apt install mingw-w64"
	@echo "  make CC=x86_64-w64-mingw32-gcc"
