##########################################
# These are for gcc 2.95.2 in the sky cross env #
# See
# o ee-gcc295 binaries
#   http://www.snsys.com/files/psx2/gcc-2.95.2-ee-1.1-bin.zip
# o GCC homepage
#   http://www.gnu.org/software/gcc
# o GCC compilations flags
#   http://www.gnu.org/software/gcc/onlinedocs/gcc_2.html
##########################################

ifeq ($(SMALLCODE), 1)
C_OPT = -Os
else
C_OPT = -O3
endif

C_OPT += \
  -fcaller-saves \
  -fcse-follow-jumps \
  -fcse-skip-blocks \
  -fdelayed-branch \
  -fexpensive-optimizations \
  -ffast-math \
  -fgcse \
  -fmove-all-movables \
  -freduce-all-givs \
  -fregmove \
  -frerun-cse-after-loop \
  -frerun-loop-opt \
  -fschedule-insns2 \
  -fstrength-reduce \
  -fstrict-aliasing \
  -fthread-jumps \
  -funroll-loops 

#  -fdata-sections \
#  -ffunction-sections \

#  -fdelete-null-pointer-checks \
#  -foptimize-register-moves \
#  -fsingle-precision-constant \
#  -falign-functions=n \
#  -falign-jumps=n \
#  -falign-labels=n \
#  -falign-loops=n \
#  -fbranch-probabilities \
#  -fdce \
#  -ffloat-store \
#  -fforce-addr \
#  -fforce-mem \
#  -finline-functions \
#  -finline-limit=n \
#  -fkeep-inline-functions \
#  -fno-default-inline \
#  -fno-defer-pop \
#  -fno-function-cse \
#  -fno-inline \
#  -fno-math-errno \
#  -fno-peephole \
#  -fomit-frame-pointer \
#  -foptimize-sibling-calls \
#  -fschedule-insns \
#  -fssa \
#  -funroll-all-loops \

ifeq ($(COPTIMIZE), 1)
CFLAGS += $(C_OPT)
else
CFLAGS += -O0
#------------
# # gcc requires the generation of a dependency tree, and hence at least -O1,
# # to detect any use of uninitialized variables
# so build optimised then. Code generation options take priority
#------------
endif

ifeq ($(CDEBUG), 1)
CFLAGS += -g
endif

ifeq ($(PG), 1)
CFLAGS += -pg -DPG
ifeq ($(PGA), 1)
CFLAGS += -DPGA
endif
endif

ifeq ($(SCE_11), 1)
CFLAGS += -DSCE_11
endif

# These seem to be platform specific, not compiler specific

ifeq ($(NOASM), 1)
CFLAGS += -DNOASM 
endif

CFLAGS += -DRW_USE_SPF -D__FAST_MATH__

# flag that we're building a PS2 build 
CFLAGS += -DSKY
 
ifeq ("$(COMPILER)", "")
COMPILER=cc
endif

ifeq ("$(LINKER)", "")
# It should be this, but we have a rather strange idea of LINKER
LINKER=link.exe
endif

CWARNINGS =					\
  -W						\
  -Wall						\
  -Wcast-qual					\
  -Wchar-subscripts				\
  -Wcomment					\
  -Werror-implicit-function-declaration		\
  -Wformat					\
  -Wid-clash-31					\
  -Wimplicit					\
  -Wimplicit-function-declaration		\
  -Wimplicit-int				\
  -Wimport					\
  -Winline					\
  -Wlong-long					\
  -Wmain					\
  -Wmissing-noreturn				\
  -Wmultichar					\
  -Wnested-externs				\
  -Wno-import					\
  -Wparentheses					\
  -Wpointer-arith				\
  -Wreturn-type					\
  -Wsign-compare				\
  -Wswitch					\
  -Wtrigraphs					\
  -Wunknown-pragmas				\
  -Wunused					\
  -Wwrite-strings

#  -Wbad-function-cast
#  -Wpadded
#  -Wcast-align
#  -Wredundant-decls
#  -Wshadow
#  -Wtraditional
#  -Wundef
#  -pedantic
#  -Wmissing-declarations /* sce include files fire these warnings*/
#  -Wmissing-prototypes   /* sce include files fire these warnings*/
#  -Wstrict-prototypes    /* sce include files fire these warnings */
#  -w
#  -Wconversion
#  -Wfloat-equal
#  -Wpacked
#  -Wunreachable-code
#  -Wunused-function
#  -Wunused-label
#  -Wunused-parameter
#  -Wunused-value
#  -Wunused-variable
#  -Waggregate-return
#  -Werror
#  -Wlarger-than-len
#  -fsyntax-only

ifeq ($(COPTIMIZE), 1)
CWARNINGS += -Wuninitialized
endif

CC        = ee-gcc295 
CPP       = $(CC)
COUT      = -o
LOUT      = -e
SOUT      = -S -o $(@F)
CPPOPT    = -E -P
CFLAGS   += $(CWARNINGS) -fno-common
CFLAGS   += -Wa,-I$(DEVDIR)/$(RWTARGET)
CFLAGS   += -Wa,-al=$(OBJDIR)/$(basename $(notdir $<)).lsc
CFLAGS   += -c

AS        = ee-gcc295 -Q # -Q seems to avoid temporary file race conditions
AOUT      = -o
ASFLAGS   = -c -xassembler-with-cpp
ASFLAGS  += -Wa,-al=$(OBJDIR)/$(basename $(notdir $<)).lsa

DVPASM        = ee-dvp-as
DVPAOUT       = -o

NM        = ee-nm

ifeq ($(CDEBUG), 1)
ASFLAGS     += --gstabs
#DVPASMFLAGS += --gstabs
endif

# Librarian options (for append, we include the source lib again)
AR		= ee-ar -r
ARCREATEFLAGS   = $@ $^
ARAPPENDFLAGS	= -out:$@ $@

# How to run programs (no need for shell/simulator)
RUNIT		=

# We do want assembler

CFLAGS += -I/usr/local/sce/ee/include -I/usr/local/sce/gsb/includ
CFLAGS += -fno-rtti -fno-exceptions


# Dependency checking using GCC -M


# http://intratech.csl.com/gnu/gcc_2.html#SEC11
# Options Controlling the Preprocessor
# 
# -M   Tell the preprocessor to output a rule suitable for make describing
#      the dependencies of each object file. For each source file, the
#      preprocessor outputs one make-rule whose target is the object file
#      name for that source file and whose dependencies are all the #include
#      header files it uses. This rule may be a single line or may be
#      continued with `\'-newline if it is long. The list of rules is printed
#      on standard output instead of the preprocessed C program. `-M' implies
#      `-E'. Another way to specify output of a make rule is by setting the
#      environment variable DEPENDENCIES_OUTPUT (see section Environment
#      Variables Affecting GCC).
# -MM  Like `-M' but the output mentions only the user header files included
#      with `#include "file"'. System header files included with `#include
#      <file>' are omitted.
# -MD  Like `-M' but the dependency information is written to a file made by
#      replacing ".c" with ".d" at the end of the input file names. This is
#      in addition to compiling the file as specified---`-MD' does not
#      inhibit ordinary compilation the way `-M' does. In Mach, you can use
#      the utility md to merge multiple dependency files into a single
#      dependency file suitable for using with the `make' command.
# -MMD
#      Like `-MD' except mention only user header files, not system header
#      files.
# -MG  Treat missing header files as generated files and assume they live in
#      the same directory as the source file. If you specify `-MG', you must
#      also specify either `-M' or `-MM'. `-MG' is not supported with `-MD'
#      or `-MMD'.


ifneq ($(findstring clean,$(MAKECMDGOALS)),clean)
WITH_DEPEND = 1
endif

$(OBJDIR)/%.d : %.c
	$(MD) $(OBJDIR)
	$(SHELL) -ec '$(CC) -MM $(CFLAGS) $< \
		| sed "s/.*\.o[ :]*/ $(subst /,\/,$(OBJDIR)/)$*.$(O) $(subst /,\/,$@) : /g" \
		> $@; \
		[ -s $@ ] || rm -f $@'

$(OBJDIR)/%.hd : %.h
	$(MD) $(OBJDIR)
	$(SHELL) -ec '$(CC) -MM $(CFLAGS) -x c $<			\
		| sed "s/.*\.o: /$(subst /,\/,$@) /g"	\
		| sed "s/$(notdir $<) /$(notdir $<) : "/		\
		| sed "s/$(notdir $<)$$/$(notdir $<) :"/		\
		> $@;							\
		[ -s $@ ] || rm -f $@'
