# Makefile for the Stjupit Dox RoboCup team
# Matus Horvath, horvathm@decef.elf.stuba.sk
#
# 4th december 2002
#
# Coach added by Peter Trebaticky
# 16th december 2002
#
# try: 
#   make LOUD=y
#   make NDEBUG=y
#   make clean
#   make depclean
################################################################################

PLAYER := PlayerExe
GOALIE := GoalieExe
COACH := CoachExe
OUTPUT := Output

COMMON_SRCS := \
	Common/Interact.cpp Common/Main.cpp Common/World/Vector.cpp \
	Common/Support/Debug.cpp Common/Protocol/CommandGen.cpp \
	Common/Protocol/Parser.cpp Common/Protocol/CharString.cpp \
	Common/Protocol/ServerSocket.cpp Common/Protocol/ParseStrings.cpp
COMMON_P_AND_G_SRCS := \
	Player/PlayerKernel.cpp Player/PlayerSkills.cpp \
	Player/World/Ball.cpp Player/World/Movable.cpp \
	Player/World/Myself.cpp Player/World/Params.cpp \
	Player/World/Player.cpp Player/World/State.cpp \
	Player/World/Variables.cpp Player/World/World.cpp \
	Player/Formation.cpp
PLAYER_SRCS := Player/PlayerTactics.cpp 
GOALIE_SRCS := Player/GoalieTactics.cpp
COACH_SRCS := Coach/CoachKernel.cpp Coach/World/CoachMemory.cpp \
	Coach/CoachSynchro.cpp

PLAYER_CFLAGS =
GOALIE_CFLAGS = -DGOALIE
COACH_CFLAGS = -DCOACH

CC          = cc
CXX         = c++
LD          = c++

CPPFLAGS    +=
CFLAGS      += -O2 -I.
CXXFLAGS    +=
LDFLAGS     += -Wall -lm

ifndef NDEBUG

CPPFLAGS    +=
CFLAGS      += -g -DDEBUG
CXXFLAGS    +=
LDFLAGS     +=

else

CPPFLAGS    +=
CFLAGS      +=
CXXFLAGS    +=
LDFLAGS     +=

endif

################################################################################

PLAYER_OBJS := $(sort $(addprefix $(OUTPUT)/$(PLAYER)/, $(addsuffix .o, $(basename $(COMMON_SRCS) $(COMMON_P_AND_G_SRCS) $(PLAYER_SRCS)))))
GOALIE_OBJS := $(sort $(addprefix $(OUTPUT)/$(GOALIE)/, $(addsuffix .o, $(basename $(COMMON_SRCS) $(COMMON_P_AND_G_SRCS) $(GOALIE_SRCS)))))
COACH_OBJS := $(sort $(addprefix $(OUTPUT)/$(COACH)/, $(addsuffix .o, $(basename $(COMMON_SRCS) $(COACH_SRCS)))))

$(OUTPUT)/$(PLAYER)/%.d: %.cpp
	echo -n "$(PLAYER): Finding dependencies for $<... "
	mkdir -p $(dir $@)
	set -e; $(CXX) -MM -MG $(CPPFLAGS) $(CFLAGS) $(CXXFLAGS) $(PLAYER_CFLAGS) $< | sed 's/\($(notdir $*)\)\.o[ :]*/$(addsuffix .o, $(basename $(subst /,\/,$@))) $(subst /,\/,$@) : /g' > $@; [ -s $@ ] || rm -f $@
	echo "done"

$(OUTPUT)/$(GOALIE)/%.d: %.cpp
	echo -n "$(GOALIE): Finding dependencies for $<... "
	mkdir -p $(dir $@)
	set -e; $(CXX) -MM -MG $(CPPFLAGS) $(CFLAGS) $(CXXFLAGS) $(GOALIE_CFLAGS) $< | sed 's/\($(notdir $*)\)\.o[ :]*/$(addsuffix .o, $(basename $(subst /,\/,$@))) $(subst /,\/,$@) : /g' > $@; [ -s $@ ] || rm -f $@
	echo "done"

$(OUTPUT)/$(COACH)/%.d: %.cpp
	echo -n "$(COACH): Finding dependencies for $<... "
	mkdir -p $(dir $@)
	set -e; $(CXX) -MM -MG $(CPPFLAGS) $(CFLAGS) $(CXXFLAGS) $(COACH_CFLAGS) $< | sed 's/\($(notdir $*)\)\.o[ :]*/$(addsuffix .o, $(basename $(subst /,\/,$@))) $(subst /,\/,$@) : /g' > $@; [ -s $@ ] || rm -f $@
	echo "done"

$(OUTPUT)/$(PLAYER)/%.o: %.cpp
	echo -n "$(PLAYER): Compiling $<... "
	mkdir -p $(dir $@)
	$(CXX) -c $(CPPFLAGS) $(CFLAGS) $(CXXFLAGS) $(PLAYER_CFLAGS) -o $@ $<
	echo "done"

$(OUTPUT)/$(GOALIE)/%.o: %.cpp
	echo -n "$(GOALIE): Compiling $<... "
	mkdir -p $(dir $@)
	$(CXX) -c $(CPPFLAGS) $(CFLAGS) $(CXXFLAGS) $(GOALIE_CFLAGS) -o $@ $<
	echo "done"

$(OUTPUT)/$(COACH)/%.o: %.cpp
	echo -n "$(COACH): Compiling $<... "
	mkdir -p $(dir $@)
	$(CXX) -c $(CPPFLAGS) $(CFLAGS) $(CXXFLAGS) $(COACH_CFLAGS) -o $@ $<
	echo "done"

.PHONY: all clean depclean

all: $(OUTPUT)/$(PLAYER)/$(PLAYER) $(OUTPUT)/$(GOALIE)/$(GOALIE) $(OUTPUT)/$(COACH)/$(COACH)

$(OUTPUT)/$(PLAYER)/$(PLAYER): $(PLAYER_OBJS)
	echo -n "$(PLAYER): Linking $@... "
	mkdir -p $(dir $@)
	$(LD) $(CFLAGS) $(CXXFLAGS) $(PLAYER_CFLAGS) -o $@ $^ $(LDFLAGS)
	echo "done"
	
$(OUTPUT)/$(GOALIE)/$(GOALIE): $(GOALIE_OBJS)
	echo -n "$(GOALIE): Linking $@... "
	mkdir -p $(dir $@)
	$(LD) $(CFLAGS) $(CXXFLAGS) $(GOALIE_CFLAGS) -o $@ $^ $(LDFLAGS)
	echo "done"
	
$(OUTPUT)/$(COACH)/$(COACH): $(COACH_OBJS)
	echo -n "$(COACH): Linking $@... "
	mkdir -p $(dir $@)
	$(LD) $(CFLAGS) $(CXXFLAGS) $(COACH_CFLAGS) -o $@ $^ $(LDFLAGS)
	echo "done"
	
clean:
	echo -n "Cleaning up... "
	-rm -f $(OUTPUT)/$(PLAYER)/core* $(OUTPUT)/$(PLAYER)/*.stackdump
	-rm -f $(OUTPUT)/$(PLAYER)/*.o
	-rm -f $(OUTPUT)/$(PLAYER)/$(PLAYER) $(OUTPUT)/$(PLAYER)/$(PLAYER).exe
	-rm -f $(OUTPUT)/$(GOALIE)/core* $(OUTPUT)/$(GOALIE)/*.stackdump
	-rm -f $(OUTPUT)/$(GOALIE)/*.o
	-rm -f $(OUTPUT)/$(GOALIE)/$(GOALIE) $(OUTPUT)/$(GOALIE)/$(GOALIE).exe
	-rm -f $(OUTPUT)/$(COACH)/core* $(OUTPUT)/$(COACH)/*.stackdump
	-rm -f $(OUTPUT)/$(COACH)/*.o
	-rm -f $(OUTPUT)/$(COACH)/$(COACH) $(OUTPUT)/$(COACH)/$(COACH).exe
	echo done

depclean: clean
	echo -n "Cleaning up dependencies... "
	-rm -f $(OUTPUT)/$(PLAYER)/*.d
	-rm -f $(OUTPUT)/$(GOALIE)/*.d
	-rm -f $(OUTPUT)/$(COACH)/*.d
	echo done

ifneq ($(MAKECMDGOALS), clean)
ifneq ($(MAKECMDGOALS), depclean)
-include $(PLAYER_OBJS:.o=.d) $(GOALIE_OBJS:.o=.d) $(COACH_OBJS:.o=.d)
endif
endif

ifndef LOUD
.SILENT:
endif
