include ../../../Makeincl

TARGETS = soccer03.hex
# automatically generate test*.hex targets from test*.cc sources
TARGETS += $(patsubst %.cc,%.hex,$(wildcard test*.cc))
TARGET_OBJECTS = $(TARGETS:%.hex=%.o)
TARGET_SOURCES = $(TARGETS:%.hex=%.cc)
SOURCES = $(wildcard classes*/*/*.cc)
HEADERS = $(wildcard classes*/*/*.hh)
OBJECTS = $(SOURCES:%.cc=%.o)
OBJECTS_BASE = $(notdir $(OBJECTS))
OBJDIR = obj

vpath %.hh $(wildcard classes*/*)
vpath %.cc $(wildcard classes*/*)
vpath %.o $(OBJDIR)

CC = gcc68
CXX = g++68
HXX = h++68 # the "header compiler"; just for debugging
AR = m68k-eyebot-elf-ar

CPPFLAGS =  -DEYEBOT -DREACT -Iclasses -Iclasses_s03 -Ihdt -DDEBUG
#CPPFLAGS = -DEYEBOT -Iclasses -Iclasses_s03
#CPPFLAGS = -Iclasses
LDLIBS = -lmind -lm

CFLAGS = -W -Wall -Wno-unused -Wunreachable-code #-Werror
# optimizing CFLAGS:
#CFLAGS+= -Os -finline-functions -finline-limit=80

CXXFLAGS:= $(CFLAGS) -fno-implicit-templates
#optimizing CXXFLAGS:
#CXXFLAGS+= -fno-exceptions -fno-enforce-eh-specs # do not need exceptions
#CXXFLAGS+= -fvtable-gc -ffunction-sections # garbage collecting in vtables

#optimizing LDFLAGS:
#LDFLAGS+= -Wl,--gc-sections # garbage collecting in vtables
#LDFLAGS += -Wl,-verbose


#for gcc 2.95 support, deprecated
#CPPFLAGS += -DNO_VDTOR
ifneq ($(findstring NO_VDTOR,$(CPPFLAGS)),)
  CXXFLAGS += -Winline -Wno-non-virtual-dtor
endif

# .PHONY: all
all: $(TARGETS) -lmind

# .PHONY: headers
headers: # "compiles" the header files; for debugging
	for h in $(HEADERS); do $(HXX) $$h $(CPPFLAGS) $(CXXFLAGS); done

-lmind: $(OBJECT_BASE)
	$(AR) rcs libmind.a $(addprefix $(OBJDIR)/,$(OBJECTS_BASE))


%.o: %.cc $(OBJDIR)
	$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $(OBJDIR)/$@
%.hex:
	$(CXX) $(CXXFLAGS) $(LDFLAGS) $(OBJDIR)/$(@:%.hex=%.o) -o $@ $(LDLIBS)

$(TARGETS): $(OBJECTS_BASE) $(TARGET_OBJECTS)

Dependencies: $(TARGET_SOURCES) $(TARGET_HEADERS)
	-$(RM) Dependencies
	$(CPP) -MM -MF Dependencies $(CPPFLAGS) $(SOURCES) $(TARGET_SOURCES)

$(OBJDIR):
	mkdir $(OBJDIR)

# .PHONY: clean
clean:
	-$(RM) $(TARGETS) $(TARGET_OBJECTS) *.o *.hex Dependencies
	cd $(OBJDIR) && $(RM) *.o
#	rmdir $(OBJDIR)
#	rm -rf doc/


ifneq ($(MAKECMDGOALS),clean)
-include Dependencies
endif
