AS       := as
LD       := ld
ASFLAGS  := --64

EXAMPLES := exit hello-world fizzbuzz
BINARIES := $(addprefix bin/,$(EXAMPLES))

.PHONY: build help run clean

## Build all binaries (default target)
build: $(BINARIES)

## Display this help screen
help:
	@awk ' \
		/^##/ { doc = substr($$0, 4); next } \
		/^[a-zA-Z0-9_-]+:/ && doc { \
			split($$1, target, ":"); \
			printf "\033[36m%-20s\033[0m %s\n", target[1], doc; \
			doc = "" \
		} \
	' $(MAKEFILE_LIST)

## Build the exit example
bin/exit: exit/exit.s
	$(AS) $(ASFLAGS) -o $@.o $<
	$(LD) -o $@ $@.o
	rm -f $@.o

## Build the hello-world example
bin/hello-world: hello-world/hello.s
	$(AS) $(ASFLAGS) -o $@.o $<
	$(LD) -o $@ $@.o
	rm -f $@.o

## Build the fizzbuzz example
bin/fizzbuzz: fizzbuzz/fizzbuzz.s
	$(AS) $(ASFLAGS) -o $@.o $<
	$(LD) -o $@ $@.o
	rm -f $@.o

## Run the specified CMD example (use CMD="fizzbuzz")
run: clean bin/$(CMD)
	./bin/$(CMD)

## Remove build artifacts
clean:
	rm -f bin/*
