############### FRAMEWORK MAKEFILE ################
############# $Name: tekkotsu-3_0 $ ###############
############### $Revision: 1.50 $ #################
########## $Date: 2006/09/26 21:53:57 $ ###########

# 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 $(shell echo "$(TEKKOTSU_ENVIRONMENT_CONFIGURATION)" | sed 's/ /\\ /g')


#############  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` -isystem /usr/include/libpng12
endif

CXXFLAGS:= \
	$(if $(TEKKOTSU_DEBUG),$(TEKKOTSU_DEBUG),$(TEKKOTSU_OPTIMIZE)) \
	-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) $(CXXFLAGS)

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 DualCoding Events IPC Motion Shared Sound Vision Wireless
SRCS:=$(shell find $(SRC_DIRS) -name "*$(SRCSUFFIX)" | xargs ls -t)

# 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...

#delete automatic suffix list
.SUFFIXES:

.PHONY: all compile clean cleanDeps reportTarget testEnv docs dox doc alldocs cleanDoc updateTools updateLibs $(USERLIBS) 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 ""
	$(MAKE) TEKKOTSU_TARGET_PLATFORM=PLATFORM_APERIOS compile
	$(MAKE) TEKKOTSU_TARGET_PLATFORM=PLATFORM_LOCAL compile
	@echo "Build successful."

update install sim:
	@echo ""
	@echo "You probably want to be running make from within your project's directory"
	@echo ""
	@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 ""
	@echo "However, you can only install or update to memory stick from within a project."
	@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)))


TOOLS_BUILT_FLAG:=$(TEKKOTSU_BUILDDIR)/.toolsBuilt

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;

$(TK_BD)/%.d: %.cc
	@mkdir -p $(dir $@)
	@src=$(patsubst %.d,%.cc,$(patsubst $(TK_BD)%,$(TEKKOTSU_ROOT)%,$@)); \
	if [ ! -f "$$src" ] ; then \
		echo "ERROR: Missing source file '$$src'... you shouldn't be seeing this"; \
		exit 1; \
	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" > $@

EMPTYDEPS:=$(shell find $(TK_BD) -type f -name "*\.d" -size 0 -print -exec rm \{\} \;)
ifneq ($(EMPTYDEPS),)
EMPTYDEPS:=$(shell echo "Empty dependency files detected: $(EMPTYDEPS)" > /dev/tty)
endif

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

compile: reportTarget updateTools updateLibs platformBuild

$(TOOLS_BUILT_FLAG):
	@$(MAKE) TOOLS_BUILT_FLAG="$(TOOLS_BUILT_FLAG)" -C tools

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

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

ifeq ($(MAKELEVEL),0)
reportTarget: testEnv
	@echo " ** Targeting $(TEKKOTSU_TARGET_MODEL) for build on $(TEKKOTSU_TARGET_PLATFORM) ** ";
	@echo " ** TEKKOTSU_DEBUG is $(if $(TEKKOTSU_DEBUG),ON: $(TEKKOTSU_DEBUG),OFF) ** ";
	@echo " ** TEKKOTSU_OPTIMIZE is $(if $(TEKKOTSU_DEBUG),DISABLED BY DEBUG,$(if $(TEKKOTSU_OPTIMIZE),ON: $(TEKKOTSU_OPTIMIZE),OFF)) ** ";
else
reportTarget: ;
endif

testEnv:
	@if [ ! -d "$(OPENRSDK_ROOT)" ] ; then \
		echo "ERROR: OPEN-R SDK not found at '$(OPENRSDK_ROOT)', check installation." ; \
		exit 1; \
	fi;
	@if $(CXX) -v > /dev/null 2>&1 ; then true; else \
			echo "ERROR: c++ compiler not found at '$(CXX)', check installation." ; \
			exit 1; \
	fi;
	@if [ ! -d "$(OPENRSDK_ROOT)/OPEN_R" ] ; then \
		echo "ERROR: OPEN-R SDK header files missing, check installation." ; \
		exit 1; \
	fi;

updateTools: | $(TOOLS_BUILT_FLAG) 
	$(MAKE) -C tools

updateLibs: $(USERLIBS)

$(USERLIBS): | $(TOOLS_BUILT_FLAG)
	@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 :
	@echo "ERROR: Seems to be a missing header file '$@'...";
	@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." ; \
		exit 1; \
	fi;
	@echo "       Someone probably forgot to check a file into CVS.";
	@echo "       I'll try to find where it's being included from:";
	@echo "       if this was a file you recently deleted, just make again after this completes. (will update dependency files)";
	@find . -name "*.h" -exec grep -H "$(notdir $@)" \{\} \; ;
	@find . -name "*.cc" -exec grep -H "$(notdir $@)" \{\} \; ;
	@find $(TK_BD) -name "*.d" -exec grep -qH "$(notdir $@)" \{\} \; -exec rm \{\} \; ;
	@exit 1

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

%.o: $(if $(TEKKOTSU_PCH),$(TK_BD)/$(TEKKOTSU_PCH).gch) | $(TOOLS_BUILT_FLAG)
	@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)
	$(MAKE) TOOLS_BUILT_FLAG="$(TOOLS_BUILT_FLAG)" -C tools 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 "$(TEKKOTSU_BUILDDIR)" -name "*.d" -exec rm \{\} \;
	@printf "done.\n"

cleanDoc:
	docs/builddocs --clean



