ifneq ($(OS),Windows_NT)
	DEFS = -D_POSIX_C_SOURCE=200112L
endif
CFLAGS  = $(DEFS) -ggdb -std=c11 -Wall -Wextra -pedantic -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition
#CFLAGS  = $(DEFS) -pg -std=c11 -Wall -Wextra -pedantic -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition
#CFLAGS  = $(DEFS) -O3 -std=c11 -Wall -Wextra -pedantic -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition
LDFLAGS = -lm
SOURCE  = main.c util.c tok.c lex.c ir.c parse.c runtime.c vm.c map.c
HEADERS =        util.h tok.h lex.h ir.h parse.h runtime.h vm.h map.h
EXE     = main

OBJ = $(SOURCE:.c=.o)

$(EXE): $(OBJ)
	$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)

%.o: %.c
	$(CC) -c -o $@ $< $(CFLAGS)

deps.mk: $(SOURCE) $(HEADERS)
	@echo "# Automatically generated by $(CC) -MM." > $@
	$(CC) -MM $(SOURCE) >> $@

map_test: map_test.c util.c map.c
	$(CC) -o $@ $< $(CFLAGS) $(LDFLAGS)

run_map_test: map_test
	valgrind ./map_test

pool_test: pool_test.c util.c
	$(CC) -o $@ $< $(CFLAGS) $(LDFLAGS)

run_pool_test: pool_test
	valgrind ./pool_test

.PHONY: clean

clean:
	rm -f $(OBJ) $(EXE) deps.mk gmon.out map_test pool_test

ifneq ($(MAKECMDGOALS),clean)
include deps.mk
endif