SRC       := src/main.c src/util.c src/detect.c src/render.c
BUILD_DIR := build
BIN_NAME  := pf.exe
BIN       := $(BUILD_DIR)/$(BIN_NAME)

CC := wcl

# wcl glues the output name to -fe=; every other front-end takes "-o name".
# Compilation happens inside $(BUILD_DIR), hence the ../ prefixes.
ifeq ($(CC),wcl)
    # -k4096: the default 2 KB stack overflows. SystemInfo is a ~900-byte
    # local in main(), and load_logo/wrap_info_lines each add a 512-byte
    # buffer beneath it. Small model shares one 64 KB DGROUP between the
    # logo string literals, the stack and the near heap, so this is
    # doubled rather than raised further.
    CFLAGS ?= -q -bt=dos -ms -ox -k4096 -I../include
    OUT    := -fe=$(BIN_NAME)
else ifeq ($(CC),owcc)
    CFLAGS ?= -bcl=dos -ms -O2 -I../include
    OUT    := -o $(BIN_NAME)
else
    CFLAGS ?= -O2 -I../include
    OUT    := -o $(BIN_NAME)
endif

LDFLAGS ?=

.PHONY: all clean djgpp

all: $(BIN)

$(BUILD_DIR):
	@mkdir -p $(BUILD_DIR)

$(BIN): $(SRC) | $(BUILD_DIR)
	@cd $(BUILD_DIR) && $(CC) $(CFLAGS) $(addprefix ../,$(SRC)) $(LDFLAGS) $(OUT)

djgpp:
	# No CFLAGS override: the fallback branch above already supplies
	# -O2 -I../include, and the include path is required because the
	# compile runs inside $(BUILD_DIR).
	$(MAKE) CC=i586-pc-msdosdjgpp-gcc

clean:
	@rm -rf $(BUILD_DIR) src/*.obj *.obj
