# Step 08 -- Full Chain: Complete COFFLoader + Practical BOFs
#
# This Makefile builds the production-ready COFFLoader and all BOFs.
#
# Usage:
#   make              -- build the loader and all BOFs
#   make loader       -- build COFFLoader.exe only
#   make bofs         -- build all BOF object files only
#   make run-hello    -- build and run hello_bof.o
#   make run-whoami   -- build and run whoami_bof.o
#   make msvc         -- build loader with MSVC (cl.exe)
#   make clean        -- remove all build artifacts
#
# Requirements:
#   - MinGW cross-compiler (x86_64-w64-mingw32-gcc) for BOFs
#   - gcc or cl.exe for the loader

# ============================================================
# Toolchain configuration
# ============================================================
CC_HOST    = gcc
CC_MINGW   = x86_64-w64-mingw32-gcc
CFLAGS_BOF = -c
LDFLAGS    = -lkernel32 -luser32 -ladvapi32

# ============================================================
# Targets
# ============================================================
all: loader bofs

# ---- Loader (normal executable, fully linked) ----
loader: COFFLoader.exe

COFFLoader.exe: COFFLoader.c beacon_compatibility.c COFFLoader.h beacon_compatibility.h
	$(CC_HOST) COFFLoader.c beacon_compatibility.c -o COFFLoader.exe $(LDFLAGS)

# ---- BOFs (object files only, NOT linked) ----
bofs: hello_bof.o whoami_bof.o args_bof.o

hello_bof.o: hello_bof.c beacon.h
	$(CC_MINGW) $(CFLAGS_BOF) hello_bof.c -o hello_bof.o

whoami_bof.o: whoami_bof.c beacon.h
	$(CC_MINGW) $(CFLAGS_BOF) whoami_bof.c -o whoami_bof.o

args_bof.o: args_bof.c beacon.h
	$(CC_MINGW) $(CFLAGS_BOF) args_bof.c -o args_bof.o

# ---- Convenience run targets ----
run-hello: COFFLoader.exe hello_bof.o
	./COFFLoader.exe go hello_bof.o

run-whoami: COFFLoader.exe whoami_bof.o
	./COFFLoader.exe go whoami_bof.o

# ---- MSVC build (from Visual Studio Developer Command Prompt) ----
msvc:
	cl.exe COFFLoader.c beacon_compatibility.c /Fe:COFFLoader.exe kernel32.lib user32.lib advapi32.lib

# ---- Cleanup ----
clean:
	rm -f COFFLoader.exe *.o *.obj

.PHONY: all loader bofs run-hello run-whoami msvc clean
