#
# Common Make options used in the construction of the RenderWare SDK and
# plugin libraries
#

# Include user custom options file. These options define the behaviour of the
# current build. Note: the options.mak file is taken from the directory that
# make is called from - not the directory containing this file.

ifndef RWOPTIONS
RWOPTIONS = $(CURDIR)/options.mak
endif
-include $(RWOPTIONS)

# Check that the required build options (RWTARGET, RWCOMPILER, RWOS) have been
# defined.

checkopt:
ifeq ("$(RWTARGET)", "")
	@echo ERROR: target undefined
	@echo
	@error
endif
ifeq ("$(RWCOMPILER)", "")
	@echo ERROR: compiler undefined
	@echo
	@error
endif
ifeq ("$(RWOS)", "")
	@echo
	@echo ERROR: os undefined
	@error
endif

#
# If user has not specified buld options via options.mak then use these
# default values

# by default, automatic dependency generation is off
ifndef RWDEPEND
RWDEPEND = 0
endif

# by default, C profiling is off
ifndef CPROFILE
CPROFILE = 0
endif

# by default, MicroSoft Working Set Tuner is off
ifndef MSWST
MSWST = 0
endif

# by default, Pipeline 2 X86 Assembler off
ifndef P2X86ASM
P2X86ASM=0
endif

# by default, C optimisation is off when a C debug build is used.
ifndef COPTIMIZE
ifeq ($(CDEBUG), 0)
ifeq ($(CPROFILE), 0)
ifeq ($(MSWST), 0)
COPTIMIZE = 1
else
COPTIMIZE = 0
endif
else
COPTIMIZE = 0
endif
else
COPTIMIZE = 0
endif
endif

# by default, parameter validation is off for a non RWDEBUG build
ifndef RWVALIDATEPARAM
ifeq ($(RWDEBUG), 1)
RWVALIDATEPARAM = 1
else
RWVALIDATEPARAM = 0
endif
endif

# by default, metrics are disabled
ifndef RWMETRICS
RWMETRICS=0
endif

# by default, single precsion floating point maths is off
ifndef RW_USE_SPF
RW_USE_SPF = 0
endif

# by default, inline suppression is off
ifndef RWSUPPRESSINLINE
RWSUPPRESSINLINE = 0
endif

# by default, inline optimization pragma suppression is off
ifndef RWSUPPRESSOPTIMIZATIONPRAGMAS
RWSUPPRESSOPTIMIZATIONPRAGMAS = 0
endif

# by default, MSDEV workaround is off
ifndef RWEMULATEINTELSIMD
RWEMULATEINTELSIMD = 0
endif

# by default, SIMD operations are off
ifndef RWSIMD
RWSIMD = 0
endif

# by default, RW4 is off
ifndef RW4
RW4 = 0
endif

# by default, _IBM_CHAR is off
ifndef _IBM_CHAR
_IBM_CHAR = 0
endif

#
# Define the common directories

# all directories are defined relative to BASEDIR
ifndef BASEDIR
# BASEDIR = .
BASEDIR = $(CURDIR)
# ------------------------------------------
# setting BASDEIR=$(CURDIR) enables absolute path names
# which makes a better job of debug symbols for
# profiling. debugging etc.
# see also
# http://www.fsf.org/software/make/make.html
# ------------------------------------------
endif

BUILDTOOLDIR = $(BASEDIR)/buildtools

# binaries used in the build process
ifndef BINDIR
BINDIR     = $(BASEDIR)/bin
endif

# define the directory where object files and libraries will be located.
# Note: each target creates a separate set of object, library and header files

ifeq ($(RWFULLPLATFORM), 1)
RWPLATFORM    = $(RWOS)/$(RWCOMPILER)/$(RWTARGET)
else
RWPLATFORM    = $(RWTARGET)
endif  # ifeq ($(RWFULLPLATFORM), 1)

ifeq ($(RWMETRICS), 1)
ifeq ($(RWOUTPUT), "")
SDKLIBDIR    = $(BASEDIR)/lib/$(RWPLATFORM)/metrics
OBJDIR       = obj/$(RWPLATFORM)m
DEPDIR       = dep/$(RWPLATFORM)m
else
SDKLIBDIR    = $(BASEDIR)/lib/$(RWPLATFORM)/$(RWOUTPUT)/metrics
OBJDIR       = obj/$(RWPLATFORM)m/$(RWOUTPUT)
DEPDIR       = dep/$(RWPLATFORM)m/$(RWOUTPUT)
endif
else
ifeq ($(RWDEBUG), 1)
ifeq ("$(RWOUTPUT)", "")
SDKLIBDIR    = $(BASEDIR)/lib/$(RWPLATFORM)/debugKodiak
OBJDIR       = obj/$(RWPLATFORM)dKodiak
DEPDIR       = dep/$(RWPLATFORM)dKodiak
else
SDKLIBDIR    = $(BASEDIR)/lib/$(RWPLATFORM)/$(RWOUTPUT)/debugKodiak
OBJDIR       = obj/$(RWPLATFORM)dKodiak/$(RWOUTPUT)
DEPDIR       = dep/$(RWPLATFORM)dKodiak/$(RWOUTPUT)
endif
else
ifeq ($(CPROFILE), 1)
ifeq ("$(RWOUTPUT)", "")
SDKLIBDIR    = $(BASEDIR)/lib/$(RWPLATFORM)/profile
OBJDIR       = obj/$(RWPLATFORM)p
DEPDIR       = dep/$(RWPLATFORM)p
else
SDKLIBDIR    = $(BASEDIR)/lib/$(RWPLATFORM)/$(RWOUTPUT)/profile
OBJDIR       = obj/$(RWPLATFORM)p/$(RWOUTPUT)
DEPDIR       = dep/$(RWPLATFORM)p/$(RWOUTPUT)
endif
else
ifeq ($(MSWST), 1)
ifeq ("$(RWOUTPUT)", "")
SDKLIBDIR    = $(BASEDIR)/lib/$(RWPLATFORM)/mswst
OBJDIR       = obj/$(RWPLATFORM)wst
DEPDIR       = dep/$(RWPLATFORM)wst
else
SDKLIBDIR    = $(BASEDIR)/lib/$(RWPLATFORM)/$(RWOUTPUT)/mswst
OBJDIR       = obj/$(RWPLATFORM)wst/$(RWOUTPUT)
DEPDIR       = dep/$(RWPLATFORM)wst/$(RWOUTPUT)
endif
else
ifeq ("$(RWOUTPUT)", "")
SDKLIBDIR    = $(BASEDIR)/lib/$(RWPLATFORM)
OBJDIR       = obj/$(RWPLATFORM)
DEPDIR       = dep/$(RWPLATFORM)
else
SDKLIBDIR    = $(BASEDIR)/lib/$(RWPLATFORM)/$(RWOUTPUT)
OBJDIR       = obj/$(RWPLATFORM)/$(RWOUTPUT)
DEPDIR       = dep/$(RWPLATFORM)/$(RWOUTPUT)
endif
endif
endif
endif
endif

SDKINCDIR    = $(BASEDIR)/include/$(RWPLATFORM)

SDKAUTODOCDIR= $(BASEDIR)/autodocs
SDKDOCDIR    = $(SDKAUTODOCDIR)/rtf
SDKHLPDIR    = $(SDKAUTODOCDIR)/hlp
SDKHTMDIR    = $(SDKAUTODOCDIR)/htm

# Plugins need to be able to find the C files contained here
VPATH = $(SDKINCDIR)

# Library prefix and extension - optional
ifndef LIBPREFIX
LIBPREFIX =
endif

ifndef LIBEXT
LIBEXT = lib
endif

# Object extension
ifndef O
O = obj
endif

# ASM extension
ifndef S
S = s
endif

#
# Basic compilation flags
#
CFLAGS = -I. -I$(SDKINCDIR) $(C_INC)
CODEGENCFLAGS =

#
# Turn on RenderWare debug
#
ifeq ($(RWDEBUG), 1)
CFLAGS += -DRWDEBUG
endif

#
# Turn on parameter validation
#
ifeq ($(RWVALIDATEPARAM), 1)
CFLAGS += -DRWVALIDATEPARAM
endif

#
# Turn on metrics
#
ifeq ($(RWMETRICS), 1)
CFLAGS += -DRWMETRICS
endif

#
# Turn on RenderWare memory debug -- for internal/CSL use.
#
ifeq ($(RWMEMDEBUG), 1)
CFLAGS += -DRWMEMDEBUG
endif

#
# Turn on verbose RenderWare debug -- for internal/CSL use.
#
ifeq ($(RWVERBOSE), 1)
CFLAGS += -DRWVERBOSE
endif

#
# Turn on tracing
#
ifeq ($(RWTRACE), 1)
CFLAGS += -DRWTRACE
endif

#
# Turn on single precision floating point arithmetic
#
ifeq ($(RW_USE_SPF), 1)
CFLAGS += -DRW_USE_SPF
endif

#
# Turn on inline suppression
#
ifeq ($(RWSUPPRESSINLINE), 1)
CFLAGS += -DRWSUPPRESSINLINE
endif

#
# Turn on optimization pragma suppression
#
ifeq ($(RWSUPPRESSOPTIMIZATIONPRAGMAS), 1)
CFLAGS += -DRWSUPPRESSOPTIMIZATIONPRAGMAS
endif

#
# Turn on MSDEV workaround
#
ifeq ($(RWEMULATEINTELSIMD), 1)
CFLAGS += -DRWEMULATEINTELSIMD
endif

#
# Turn on SIMD operations
#
ifeq ($(RWSIMD), 1)
CFLAGS += -DRWSIMD
endif

#
# Turn on RW4
#
ifeq ($(RW4), 1)
CFLAGS += -DRW4
endif

#
# Turn on _IBM_CHAR
#
ifeq ($(_IBM_CHAR), 1)
CFLAGS += -D_IBM_CHAR
endif

#
# Turn on P2X86ASM
#
ifeq ($(P2X86ASM), 1)
CFLAGS += -DP2X86ASM
endif

#
# Turn on PS2MANAGER
#
ifeq ($(PS2MANAGER), 1)
CFLAGS += -DPS2MANAGER
endif

#
# Include Compiler specific options
#
-include $(BASEDIR)/makecom/$(RWOS)/$(RWCOMPILER)

#
# Include OS specific options
#
-include $(BASEDIR)/makeos/$(RWOS)

#
# Standard phony targets
#
.PHONY : all install distclean clean checkopt

#
# Default Build Rules
#

.SUFFIXES:
.SUFFIXES: .h .ipp .c .cpp .$(O) .rpe

$(OBJDIR)/%.$(O) : %.c
	$(CC) $(CFLAGS) $(CODEGENCFLAGS) $(COUT)$@ $(CURDIR)/$<

$(OBJDIR)/%.$(O) : %.cpp
	$(CC) $(CFLAGS) $(CODEGENCFLAGS) $(COUT)$@ $(CURDIR)/$<

%.ipp : %.c
	$(CC) $(CPPOPT) $(CFLAGS) $(CODEGENCFLAGS) $(CURDIR)/$< > $@

%.ipp : %.cpp
	$(CC) $(CPPOPT) $(CFLAGS) $(CODEGENCFLAGS) $(CURDIR)/$< > $@

# Rule to generate the .rpe error files used by the plugins
# Note the cheesy hack using $(@F) since PC's are incapable of
# redirecting to a filename containing forward slash characters
#
%.rpe : rperror.h
	$(CPP) $(CPPOPT) $(CFLAGS) $< > $(@F)
	$(CP)  $(@F) $@
	$(RM)  $(@F)

# Some compilers require all modules used to have unique names. Since
# the plugins all use rpdbgerr.c, this rule allows the generation of a
# unique filename for each one from a common source file

$(OBJDIR)/u%.$(O) : rpdbgerr.c
	$(CC) $(CFLAGS) $(CODEGENCFLAGS) $(COUT)$@ $(CURDIR)/$<

$(OBJDIR)/%.$(O) : %.$(ASM)
	$(AS) $(ASFLAGS) $(AOUT)$@ $<

# Rule to find labels in dsm files, and reverse-alphabetically
# sort these to bring any duplications together.
# The ee-gcc linker appears to have a bug whereby it can become
# confused by duplicate labels

dsmlabel :
	$(BINDIR)/cygfind . -name "*.dsm" \
	-exec $(BINDIR)/egrep -n -H "\:" {} \; | \
	$(BINDIR)/sed "s/;.*$$//" | \
	$(BINDIR)/egrep ":.*:.*:" | \
	$(BINDIR)/rev | \
	$(BINDIR)/cygsort | \
	$(BINDIR)/rev

# Rule to find questionable global exports for linkage
# from RW library files
# All global exports should have "R" or "_r" prefixes
# See 18.2  Naming Conventions
# http://intratech.csl.com/Coding_Standards.html#tth_sEc18.2

ifeq ("$(NM)", "")
NM        = $(BINDIR)/nm --demangle=gnu
endif

rwquestionable :
	-$(BINDIR)/cygfind $(BASEDIR)/lib/$(RWPLATFORM) -type f	\
	-exec $(NM)						\
		--defined-only					\
		--extern-only					\
		--format=posix					\
		--line-numbers					\
		--print-file-name {} \;  |			\
	$(BINDIR)/egrep -v ' (R|_r)'
