################ PROJECT MAKEFILE #################
############# $Name: tekkotsu-2_4_1 $ ###############
############### $Revision: 1.105 $ #################
########## $Date: 2005/07/06 22:53:29 $ ###########
############### $Revision: 1.105 $ #################
########## $Date: 2005/07/06 22:53:29 $ ###########

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

###################################################
##             ENVIRONMENT SETUP                 ##
###################################################
TEKKOTSU_ENVIRONMENT_CONFIGURATION?=$(shell pwd | sed 's/ /\\ /g')/Environment.conf
ifneq ($(filter sim%,$(MAKECMDGOALS)),)
	TEKKOTSU_TARGET_PLATFORM:=PLATFORM_LOCAL
	ifneq ($(filter sim-%,$(MAKECMDGOALS)),)
		TEKKOTSU_TARGET_MODEL:=$(patsubst sim-%,TGT_%,$(filter sim-%,$(MAKECMDGOALS)))
	endif
endif
include $(TEKKOTSU_ENVIRONMENT_CONFIGURATION)
$(shell mkdir -p $(PROJ_BD))


#############  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 $(TEKKOTSU_ROOT)/aperios/include \
	  $(if $(TEKKOTSU_DEBUG),-DOPENR_DEBUG,) \
	  `$(TEKKOTSU_ROOT)/aperios/bin/xml2-config --cflags`
  LDFLAGS=-lObjectComm -lOPENR  -lInternet -lantMCOOP -lERA201D1 \
	  `$(TEKKOTSU_ROOT)/aperios/bin/xml2-config --libs` -ljpeg
else
  PLATFORM_FLAGS=`xml2-config --cflags`
  LDFLAGS=`xml2-config --libs` $(if $(shell locate librt.a),-lrt) -ljpeg
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. -I$(TEKKOTSU_ROOT) -I$(TEKKOTSU_ROOT)/Motion/roboop \
	-I$(TEKKOTSU_ROOT)/Shared/newmat \
	-isystem $(TEKKOTSU_ROOT)/Shared/jpeg-6b \
	-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,
# or platform-specific files in either aperios or sim)
# You shouldn't need to change anything here unless you want to add
# external libraries
SRCSUFFIX=.cc
SRCS:=$(patsubst ./%,%,$(shell find . -name "*$(SRCSUFFIX)" -or -name "$(PROJECT_BUILDDIR)" \! -prune -or -name "templates" \! -prune -or -name "aperios" \! -prune -or -name "local" \! -prune))

# We can also link in third-party libraries
USERLIBS:= $(TK_BD)/libtekkotsu.a \
           $(TK_LIB_BD)/Motion/roboop/libroboop.a \
           $(TK_LIB_BD)/Shared/newmat/libnewmat.a \


###################################################
##             MAKEFILE MACHINERY                ##
###################################################
# Hopefully, you shouldn't have to change anything down here,
# except one or two little things ;)

.PHONY: all compile clean cleanDeps cleanProj reportTarget checkLibs testLibs buildLibs sim

sim all: reportTarget checkLibs compile
ifeq ($(TEKKOTSU_TARGET_PLATFORM),PLATFORM_APERIOS)
	@echo "Type: '$(MAKE) install' to copy all files to the memory stick";
	@echo "  or: '$(MAKE) update' to copy only changed files";
	@echo "  or: '$(TEKKOTSU_ROOT)/tools/{ftpinstall,ftpupdate} <ipaddr> ms' might also be useful";
endif
	@echo "Build successful."

# 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

checkLibs: $(if $(TEKKOTSU_ALWAYS_BUILD),buildLibs,testLibs)

ifeq ($(TEKKOTSU_ALWAYS_BUILD),)
%/libtekkotsu.a:
	echo "It appears that the framework itself has not been compiled."; \
	echo "TEKKOTSU_ROOT = $(TEKKOTSU_ROOT)"; \
	echo "TK_BD = $(TK_BD)"; \
	echo "Press enter to compile it now, ctl-C to cancel."; \
	read; \
	export TEKKOTSU_ENVIRONMENT_CONFIGURATION=$(TEKKOTSU_ENVIRONMENT_CONFIGURATION); \
	$(MAKE) TEKKOTSU_TARGET_MODEL=$(TEKKOTSU_TARGET_MODEL) TEKKOTSU_TARGET_PLATFORM=$(TEKKOTSU_TARGET_PLATFORM) -C "$(TEKKOTSU_ROOT)" compile ;

else
%/libtekkotsu.a:
	@echo "Updating framework build...";
	@export TEKKOTSU_ENVIRONMENT_CONFIGURATION=$(TEKKOTSU_ENVIRONMENT_CONFIGURATION); \
	$(MAKE) TEKKOTSU_TARGET_MODEL=$(TEKKOTSU_TARGET_MODEL) TEKKOTSU_TARGET_PLATFORM=$(TEKKOTSU_TARGET_PLATFORM) -C "$(TEKKOTSU_ROOT)" compile ; 

endif

testLibs:
	@broken=false; \
	for x in $(USERLIBS) ; do \
		if [ \! -r "$$x" ] ; then \
			echo "Could not read framework library '$$x'"; \
			broken=true; \
		fi; \
	done; \
	if $$broken ; then \
		echo "It appears that the framework itself has not been compiled."; \
		echo "TEKKOTSU_ROOT = $(TEKKOTSU_ROOT)"; \
		echo "TK_BD = $(TK_BD)"; \
		echo "Press enter to compile it now, ctl-C to cancel."; \
		read; \
		export TEKKOTSU_ENVIRONMENT_CONFIGURATION=$(TEKKOTSU_ENVIRONMENT_CONFIGURATION); \
		$(MAKE) TEKKOTSU_TARGET_MODEL=$(TEKKOTSU_TARGET_MODEL) TEKKOTSU_TARGET_PLATFORM=$(TEKKOTSU_TARGET_PLATFORM) -C "$(TEKKOTSU_ROOT)" compile ; \
	fi;

buildLibs:
	@echo "Updating framework build...";
	@export TEKKOTSU_ENVIRONMENT_CONFIGURATION=$(TEKKOTSU_ENVIRONMENT_CONFIGURATION); \
	$(MAKE) TEKKOTSU_TARGET_MODEL=$(TEKKOTSU_TARGET_MODEL) TEKKOTSU_TARGET_PLATFORM=$(TEKKOTSU_TARGET_PLATFORM) -C "$(TEKKOTSU_ROOT)" compile ; 

# The object file for each of the source files
OBJS:=$(addprefix $(PROJ_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 $(PROJ_BD)/,$(SRCS:$(SRCSUFFIX)=.d))

ifeq ($(TEKKOTSU_TARGET_PLATFORM),PLATFORM_APERIOS)

include aperios/Makefile.aperios

else

include local/Makefile.local

endif

ifeq ($(filter clean% docs,$(MAKECMDGOALS)),)
-include $(DEPENDS)
ifeq ($(TEKKOTSU_TARGET_PLATFORM),PLATFORM_APERIOS)
-include $(PROJ_BD)/aperios/aperios.d
else
-include $(PROJ_BD)/local/local.d
endif
endif

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

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

%.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 $@)" \{\} \; ;
	@find $(TEKKOTSU_ROOT) -name "*.h" -exec grep -H "$(notdir $@)" \{\} \; ;
	@find $(TEKKOTSU_ROOT) -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
	echo "I don't know anything about `$*'";
	exit 1;

%.o: $(if $(TEKKOTSU_PCH),$(TK_BD)/$(TEKKOTSU_PCH).gch)
	@mkdir -p $(dir $@)
	@src=$(patsubst %.o,%$(SRCSUFFIX),$(if $(findstring $(TK_BD),$@),$(patsubst $(TK_BD)%,$(TEKKOTSU_ROOT)%,$@),$(patsubst $(PROJ_BD)/%,%,$@))); \
	if [ -r dist_hosts.txt ] ; then \
		echo "Adding $@ to job list"; \
		echo "$(CXX) $(CXXFLAGS) -o $@ -c $$src > $*.log 2>&1; retval=\$$?; cat $*.log | $(FILTERSYSWARN) | $(COLORFILT); test \$$retval -eq 0;" >> $(PROJ_BD)/joblist.txt ; \
		touch $@; \
		echo "$@" >> $(PROJ_BD)/touched.txt ; \
	else \
		echo "Compiling $$src..."; \
		$(CXX) $(CXXFLAGS) $(INCLUDE_PCH) -o $@ -c $$src > $*.log 2>&1; \
		retval=$$?; \
		cat $*.log | $(FILTERSYSWARN) | $(COLORFILT) | $(TEKKOTSU_LOGVIEW); \
		test $$retval -eq 0; \
	fi;

$(PROJ_BD)/project.a: $(OBJS)
	@echo "Linking project files..."
	@printf "$@ <- "; echo "$(OBJS)" | sed 's@$(PROJ_BD)/@@g';
	@rm -f $@;
	@$(AR) $@ $(OBJS);
	@$(AR2) $@

clean: cleanProj
	$(if $(TEKKOTSU_ALWAYS_BUILD),$(MAKE) -C $(TEKKOTSU_ROOT) $@)

cleanProj:
	@printf "Cleaning all ~ files corresponding to project .cc files..."
	@rm -f $(addsuffix ~,$(PROJ_SRCS)) $(PROJ_SRCS:$(SRCSUFFIX)=.h~)
	@printf "done.\n"
	rm -rf $(PROJECT_BUILDDIR)
	rm -f $(INSTALL_BINS)
	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:
	@echo "Cleaning all .d files from build directory..."
	find $(PROJ_BD) -name "*.d" -exec rm \{\} \;
	@echo "done."
	$(if $(TEKKOTSU_ALWAYS_BUILD),$(MAKE) -C $(TEKKOTSU_ROOT) $@)
