############### FRAMEWORK MAKEFILE ################
############# $Name: tekkotsu-2_4 $ ###############
############### $Revision: 1.33 $ #################
########## $Date: 2005/08/07 04:11:02 $ ###########

# Make sure the default target is 'all' by listing it first
all:


###################################################
##             ENVIRONMENT SETUP                 ##
###################################################
# Use the default project Environment.conf if none has been
# specified.  If a project initiated make, it should have
# defined this variable for us to use its configuration.
TEKKOTSU_ROOT:=$(shell pwd | sed 's/ /\\ /g')
TEKKOTSU_ENVIRONMENT_CONFIGURATION?=$(TEKKOTSU_ROOT)/project/Environment.conf
include $(TEKKOTSU_ENVIRONMENT_CONFIGURATION)


#############  MAKEFILE VARIABLES  ################

# Would you like some more compiler flags?  We like lots of warnings.
# There are some files with exceptions to these flags - MMCombo*.cc
# need to have optimizations turned off, and several TinyFTPD sources
# have -Weffc++ and -DOPENR_DEBUG turned off.  If you want to modify
# these exceptions, look in the middle of the 'Makefile Machinery'
# section. (grep/search for the file name)

ifeq ($(TEKKOTSU_TARGET_PLATFORM),PLATFORM_APERIOS)
  PLATFORM_FLAGS= \
	  -isystem $(OPENRSDK_ROOT)/OPEN_R/include/MCOOP \
	  -isystem $(OPENRSDK_ROOT)/OPEN_R/include/R4000 \
	  -isystem $(OPENRSDK_ROOT)/OPEN_R/include \
	  -isystem aperios/include \
	  $(if $(TEKKOTSU_DEBUG),-DOPENR_DEBUG,) \
	  `aperios/bin/xml2-config --cflags`
else
  PLATFORM_FLAGS=`xml2-config --cflags`
endif

CXXFLAGS= \
	$(if $(TEKKOTSU_DEBUG),-g -fno-inline -DDEBUG,) \
	$(if $(TEKKOTSU_DEBUG),,-O2 -frename-registers -fomit-frame-pointer) \
	-pipe -ffast-math -fno-common \
	-Wall -W -Wshadow -Wlarger-than-8192 -Wpointer-arith -Wcast-qual \
	-Woverloaded-virtual -Weffc++ -Wdeprecated -Wnon-virtual-dtor \
	-I$(TEKKOTSU_ROOT) -I$(TEKKOTSU_ROOT)/Motion/roboop \
	-I$(TEKKOTSU_ROOT)/Shared/newmat \
	-D$(TEKKOTSU_TARGET_PLATFORM)  -D$(TEKKOTSU_TARGET_MODEL) \
	$(PLATFORM_FLAGS)

INCLUDE_PCH=$(if $(TEKKOTSU_PCH),-include $(TK_BD)/$(TEKKOTSU_PCH))


###################################################
##              SOURCE CODE LIST                 ##
###################################################

# Find all of the source files: (except temp files in build directory)
# You shouldn't need to change anything here unless you want to add
# external libraries or new directories for the search
SRCSUFFIX:=.cc
SRC_DIRS:=Behaviors Events IPC Motion Shared Sound Vision Wireless
SRCS:=$(shell find $(SRC_DIRS) -name "*$(SRCSUFFIX)")

# We should also make sure these libraries are ready to go
# Note we've been lucky that these libraries happen to use a different
# source suffix, so we don't have to filter them out of SRCS
USERLIBS:= Motion/roboop \
           Shared/newmat \


###################################################
##             MAKEFILE MACHINERY                ##
###################################################
# Hopefully, you shouldn't have to change anything down here...

.PHONY: all compile clean cleanDeps reportTarget docs dox doc alldocs cleanDoc updateLibs $(USERLIBS) tools platformBuild update install

all:
	@echo "Running $(MAKE) from the root directory will first build the"
	@echo "Tekkotsu library for Aperios (AIBO), and then for the local"
	@echo "platform.  The Environment.conf from the template project"
	@echo "directory will be used, which currently contains:"
	@echo "";
	@echo "  Target model: $(TEKKOTSU_TARGET_MODEL)"
	@echo "  Build directory: $(TEKKOTSU_BUILDDIR)"
	@echo "";
	@echo "You will want to run make from your project directory in order"
	@echo "to produce executables..."
	@echo ""
	export TEKKOTSU_TARGET_PLATFORM=PLATFORM_APERIOS; \
	$(MAKE) compile
	export TEKKOTSU_TARGET_PLATFORM=PLATFORM_LOCAL; \
	$(MAKE) compile
	@echo "Build successful."

update install:
	@echo "You probably want to be running make from within your project's directory"
	@echo "You can run $(MAKE) from within the root Tekkotsu directory to build"
	@echo "libtekkotsu.a for both Aperios and the host platform, which will then"
	@echo "be linked against by the projects and tools."
	@echo "However, only a project can install or update to memory stick."
	@echo "You can use the template project directory if you want to build a stick"
	@echo "with the standard demo behaviors."

# Don't want to try to remake this - give an error if not found
$(TEKKOTSU_ROOT)/project/Environment.conf:
	@echo "Could not find Environment file - check the default project directory still exists"
	@exit 1

# The object file for each of the source files
OBJS:=$(addprefix $(TK_BD)/,$(SRCS:$(SRCSUFFIX)=.o))

# list of all source files of all components, sorted to remove
# duplicates.  This gives us all the source files which we care about,
# all in one place.
DEPENDS:=$(addprefix $(TK_BD)/,$(SRCS:$(SRCSUFFIX)=.d) $(addsuffix .d,$(TEKKOTSU_PCH)))


ifeq ($(TEKKOTSU_TARGET_PLATFORM),PLATFORM_APERIOS)
include aperios/Makefile.aperios
else
include local/Makefile.local
endif

%.gch:
	@mkdir -p $(dir $@)
	@src=$(patsubst $(TK_BD)/%,%,$*); \
	echo "Pre-compiling $$src..."; \
	$(CXX) $(CXXFLAGS) -x c++-header -c $$src -o $@ > $*.log 2>&1; \
        retval=$$?; \
        cat $*.log | $(FILTERSYSWARN) | $(COLORFILT) | $(TEKKOTSU_LOGVIEW); \
        test $$retval -eq 0;

%.d:
	@mkdir -p $(dir $@)
	@src=$(patsubst %.d,%.cc,$(patsubst $(TK_BD)%,$(TEKKOTSU_ROOT)%,$@)); \
	if [ ! -f "$$src" ] ; then \
		echo "Generating source file '$$src'"; \
		$(MAKE) "$$src" ; \
	fi; \
	echo "$@..." | sed 's@.*$(TK_BD)/@Generating @'; \
	$(CXX) $(CXXFLAGS) -MP -MG -MT "$@" -MT "$(@:.d=.o)" -MM "$$src" > $@

$(TK_BD)/$(TEKKOTSU_PCH).d:
	@mkdir -p $(dir $@)
	@src=$(TEKKOTSU_PCH); \
	echo "$@..." | sed 's@.*$(TK_BD)/@Generating @'; \
	$(CXX) $(CXXFLAGS) -MP -MG -MT "$@" -MT "$(@:.d=.gch)" -MM "$$src" > $@

ifneq ($(MAKECMDGOALS),)
ifeq ($(filter clean% docs dox doc alldocs,$(MAKECMDGOALS)),)
-include $(DEPENDS)
ifeq ($(TEKKOTSU_TARGET_PLATFORM),PLATFORM_APERIOS)
-include $(TK_BD)/aperios/aperios.d
endif
endif
endif

compile: tools reportTarget updateLibs platformBuild

tools:
	@$(MAKE) -C tools

dox docs doc:
	docs/builddocs --update --tree --search --detailed

alldocs:
	docs/builddocs --update --all --tree --search --detailed

reportTarget:
	@echo " ** Targeting $(TEKKOTSU_TARGET_MODEL) for build on $(TEKKOTSU_TARGET_PLATFORM) ** ";
	@echo " ** TEKKOTSU_DEBUG is $(if $(TEKKOTSU_DEBUG),ON ,OFF) ** ";

updateLibs: $(USERLIBS)

$(USERLIBS):
	@echo "$@:"; \
	export TEKKOTSU_ENVIRONMENT_CONFIGURATION="$(TEKKOTSU_ENVIRONMENT_CONFIGURATION)"; \
	$(MAKE) -C "$@"

$(TK_BD)/libtekkotsu.a: $(OBJS)
	@echo "Linking object files..."
	@printf "$@ <- "; echo "[...]" | sed 's@$(TK_BD)/@@g';
	@rm -f $@;
	@$(AR) $@ $(OBJS);
	@$(AR2) $@

%.h :
	@if [ "$(notdir $@)" = "def.h" -o "$(notdir $@)" = "entry.h" ] ; then \
		echo "WARNING: You shouldn't be seeing this message.  Report that you did." ; \
		echo "         Try a clean recompile." ; \
	fi;
	@echo "ERROR: Seems to be a missing header file '$@'...";
	@echo "       Someone probably forgot to check a file into CVS.";
	@echo "       I'll try to find where it's being included from:";
	@find . -name "*.h" -exec grep -H "$(notdir $@)" \{\} \; ;
	@find . -name "*.cc" -exec grep -H "$(notdir $@)" \{\} \; ;
	@echo "";
	@echo "You might need to rebuild the dependancy files ('make cleanDeps') to get rid of this error.";
	@exit 1

#don't try to make random files via this implicit chain
%:: %.o ;

%.o: $(if $(TEKKOTSU_PCH),$(TK_BD)/$(TEKKOTSU_PCH).gch)
	@mkdir -p $(dir $@)
	@src=$(patsubst %.o,%$(SRCSUFFIX),$(patsubst $(TK_BD)/%,%,$@)); \
	echo "Compiling $$src..."; \
	$(CXX) $(CXXFLAGS) $(INCLUDE_PCH) -o $@ -c $$src > $*.log 2>&1; \
	retval=$$?; \
	cat $*.log | $(FILTERSYSWARN) | $(COLORFILT) | $(TEKKOTSU_LOGVIEW); \
	test $$retval -eq 0;

clean:
	@printf "Cleaning all ~ files corresponding to .cc files..."
	@rm -f $(addsuffix ~,$(SRCS)) $(SRCS:$(SRCSUFFIX)=.h~)
	@printf "done.\n"
	rm -rf $(TEKKOTSU_BUILDDIR)
	(cd $(TEKKOTSU_ROOT)/tools && $(MAKE) clean);
	for dir in `ls aperios` ; do \
		if [ "$$dir" = "CVS" ] ; then continue; fi; \
		if [ -f "aperios/$$dir" ] ; then continue; fi; \
		rm -f "aperios/$$dir/$${dir}Stub."{h,cc} "aperios/$$dir/def.h" "aperios/$$dir/entry.h" ; \
	done

cleanDeps:
	@printf "Cleaning all .d files from build directory..."
	@find "$(TK_BD)/../.." -name "*.d" -exec rm \{\} \;
	@printf "done.\n"

cleanDoc:
	docs/builddocs --clean



