#Build-Directories, PDIR will be submitted by GT.bash:
#                   PDIR=$(GT_DIR)/Build/GT2005/$(PROCSET)/DebugVersion
#directories
BDIR=$(PDIR)/bin
ODIR=$(PDIR)
PPDIR=$(ODIR)/Processes/$(PROCSET)
SDIR=$(GT_DIR)/Src/Processes/$(PROCSET)
CDIR=$(GT_DIR)/Config/Processes/$(PROCSET)
PREFIX=/usr/local/OPEN_R_SDK

#used programms
CXX=$(PREFIX)/bin/mipsel-linux-g++
AS=$(PREFIX)/bin/mipsel-linux-as
STRIP=$(PREFIX)/bin/mipsel-linux-strip
MKBIN=$(PREFIX)/OPEN_R/bin/mkbin

# don't use parallel make jobs by default
# (try to tune this variable for multiprocessor systems)
#
JOBS="-j1"

# Use distributed compilation if DISTCC_HOSTS is set.
#
#
ifdef DISTCC_HOSTS
  JOBS="-j8"
  CXX := distcc $(CXX)
endif
  
# Use compiler cache if CCACHE_DIR is set.
# (You can even set it to UNC paths like //server/share/cachedir)
#
ifdef CCACHE_DIR
  CXX := ccache $(CXX)
endif


WARNINGS_ON = -Wall -W -Wcast-qual -Wconversion -Wstrict-prototypes -Wmissing-prototypes -pedantic
#produce too many (useless) warnings (or errors): -Wshadow -Wredundant-decls -Weffc++ -Wwrite-strings -Wcast-align
WARNINGS_OFF = -Wno-non-virtual-dtor -Wno-switch -Wno-unused-parameter -Wno-reorder
#add -Wno-unused to avoid unused variable warnings
#add -Wno-uninitialized to avoid uninitialized variable warnings

#optimized: correct architecture, links much faster without -g
#debuggable: needs -g, output of line number only works with standard mips:3000, even -O3 does not disorganisesource code lines too much
OPTIM      = $$(if test "$(OPTIMIZATION)"x = "true"x; then echo -O3 -Wno-deprecated; else echo -g -O3 $(WARNINGS_ON) $(WARNINGS_OFF); fi)
MKBINFLAGS = $$(if test "$(OPTIMIZATION)"x = "true"x; then echo -p $(PREFIX) -A 4300 --no-warn-mismatch; else echo -p $(PREFIX); fi)
ARCH       = $$(if test "$(OPTIMIZATION)"x = "true"x; then echo -march=r4300 -mtune=r4300; fi)
ASFLAGS    = $$(if test "$(OPTIMIZATION)"x = "true"x; then echo -Wa,-W,-march=r4300,-mtune=r4300,-O; else echo -Wa,-W,-O; fi)


CXXFLAGS = $(OPTIM) $(ARCH) -DAPERIOS1_3_2 $(DEBUG_DEFINITIONS) -fmessage-length=0  $(ASFLAGS)


# list of all cpp-files possibly needed for actual ProcessSet for all
# those files directories and odeps have to be built under build/$(PROCSET)
CPPS=find Representations Modules Platform/Aperios1.3.2 Processes/$(PROCSET) Tools -name '*.cpp' | sort -u -f


#some sets of include directories
CPP_INC = $(PREFIX)/OPEN_R/include/R4000 $(PREFIX)/OPEN_R/include $(PREFIX)/OPEN_R/include/stl $(GT_DIR)/Src $(PREFIX)/OPEN_R/include/mcoop
LIB_INC = 


#set of libraries
LIBS = OPENR ObjectComm mcoopstub ERA201D1 antMCOOP Internet


#some files
TMP1 = $(PDIR)/tmpfile1
DEPINC=$(PDIR)/depends.incl


all: ensuredeps objects
	@rm -rf $(SDIR)/GNUmakefile

#build all binaries, useful content will be included in $(DEPINC)
objects:
	@#that's it, we built all binaries!!!

include $(DEPINC)


#create all neccessary directories, fake all odep and bindep and include all these files
ensuredeps: $(GT_DIR)/Make/makefile
	@echo "! `date +%T` ensure existence of all directories"
	@cd $(GT_DIR)/Src && find Representations Modules Platform/Aperios1.3.2 Processes/$(PROCSET) Tools -name '*.cpp' -printf '%h\n' |sort -u -f |gawk 'BEIGN{FS="\n"}{system("mkdir -p $(ODIR)/"$$1)}'
	@echo "! generate all odeps and bindeps in depends.incl"
	@$(GT_DIR)/Bin/Depend.exe $(CXXFLAGS) -D__GNUC__ -I$(GT_DIR)/Src -B$(PDIR) -P$(PROCSET) `cat $(CDIR)/$(PROCSET).ocf |grep "^# objectmapping" |tr -d '\015' |gawk 'BEGIN{FS=" "}{print $$3"="$$4".cpp"}' |tr -s '\012' ' '` \
		|sed "s=v:/=$(REAL_GT_DIR)/="
	@make $(JOBS) -r -f $(GT_DIR)/Make/makefile objects


.SUFFIXES: .bin .o .cpp
        
%.o:
	@echo "! compile `echo $@ |sed 's%$(ODIR)\(.*\).o%$(GT_DIR)/Src\1.cpp%'`"
	@#echo $(CXX) $(CXXFLAGS) $(CPP_INC:%=-I%) -o $@ -c `echo $@ |sed 's%$(ODIR)\(.*\).o%$(GT_DIR)/Src\1.cpp%'`
	@$(CXX) $(CXXFLAGS) $(CPP_INC:%=-I%) -o $@ -c `echo $@ |sed 's%$(ODIR)\(.*\).o%$(GT_DIR)/Src\1.cpp%'` 2>&1 \
	  |sed "s=:\([0-9][0-9]*\):[0-9]*:=(\1) :=" \
	  |sed "s=v:/=$(REAL_GT_DIR)/=" \
	  |sed "s=/usr/local=$$(cygpath -w / |tr '\\' '/')/usr/local=" \
	  |sed "s=\(.*\):\([0-9][0-9]*\): \(.\)=   \1(\2) : error : \3=" \
	  |sed "s=.*from \(.*\):\([0-9][0-9]*\).*=   \1(\2) :=" \
	  |sed "s= error : [eE]rror= error=" \
	  |sed "s= error : [wW]arning= warning=" >&2

##this might be useful for finding strange compile bugs:
#$(CXX) -S $(CXXFLAGS) $(CPP_INC:%=-I%) -o $@.s -c `echo $@ |sed 's%$(ODIR)\(.*\).o%$(GT_DIR)/Src\1.cpp%'` 2>&1 \
#  |sed "s=:\([0-9][0-9]*\):[0-9]*:=(\1) :=" \
#  |sed "s=\(.*\):\([0-9][0-9]*\): \(.\)=   \1(\2) : error : \3=" \
#  |sed "s=.*from \(.*\):\([0-9][0-9]*\).*=   \1(\2) :=" \
#  |sed "s= error : warning= warning=" >&2
#if test -f $@.s; then \
#  $(AS) $(ASFLAGS) -o $@ $@.s 2>&1 |grep -v "Assembler messages" |grep -v "^Error"; \
#  rm -f $@.s; \
#else return 1; fi
##in case of failure simply exit to ensure that we do not try to link
