#
# makedefs
# Copyright (C) ARM Limited 1998-2000. All rights reserved.
#
# Generic Makefile
#
# Provides default rules for project building and automatic dependancy
# generation. Designed for solaris make but should work with gnumake.
#
# $Id: makedefs,v 1.14 2000/07/28 15:39:28 tdouglas Exp $
#
# INTERFACE
# =========
#
# The following variables may be set before including this file.
# All variable are OPTIONAL and set to default values if you do not set them.
#
# BINARIES      = A list of target binaries/libraries to be built on a make.
# INCLUDES      = A comma separated list of include directories.
# SUBPROJECTS   = A list of directories of subprojects that must be built
#                 before this project.
# WHICHVARIANTS = A list of project variants to build on a "make all"
# SRCDIR        = Directory to find source files - this defaults to "src"
# SUBDIRS       = A list of subdirectories of SRCDIR that contain source files
# VARIANTS_DIR  = Path to put variant builds - defaults to "variants/"
#
# XCFLAGS       = Extra C flags (to apply to all variants after standard C flags)
# ADSXCFLAGS    = Extra C flags (to be applied for ADS builds only)
# SDTXCFLAGS    = Extra C flags (to be applied for non ADS builds only)
# XASFLAGS      = Extra Assembler flags (to apply to all variants after standard flags)
# ADSXASFLAGS   = Extra AS flags (ADS builds only)
# SDTXASFLAGS   = EXtra AS flags (non ADS builds only)
# XLDFLAGS      = Extra Linker flags (before standard flags)
# XLPFLAGS      = Extra Partial link flags (before standard flags)
# XARFLAGS      = Extra Archiver flags (before standard flags)
# XCLIB         = Additional C libraries
#
# Note: these are extra flags only.  They cannot be used to override
#       the flag settings used for each variant
#
#
# OUTPUTS
# =======
#
# The following variables are set up by this file, depending on the target type
#
# CC      = C compiler to use
# AS      = Assembler to use
# LD      = Linker to use
# LP      = Partial linker to use
# AR      = Library arciver to use
# CLIB    = Default C library to use
# VARIANT = The name of the variant being built
#
# OBJDIR  = Path (includes /) for output objects
# SRCDIR  = Directory assumed for source files
# VARIANTS_DIR = Directory assumed for variant builds
#
# CFLAGS  = C flags (including XCFLAGS)
# ASFLAGS = Assembler flags (including XASFLAGS)
# LDFLAGS = Linker flags (including XLDFLAGS) and -o ending
# LPFLAGS = Partial linker flags (including XLPFLAGS) and -o ending
# ARFLAGS = Archiver flags (including XARFLAGS) and -o ending
#
# BITS    = 16 or 32 for Thumb or ARM code
# BYTESEX = l or b for little or big endian memory
#
# CBUILD  = Default command for building C files with dependancy generate
# ASBUILD = Default command for building Assembler files with
#           dependancy generate
#
# The following defult rules are defined. They can be overwritten or
# extra ones added.
#
# $(OBJDIR)%.o:: $(SRCDIR)/%.c
#	$(CBUILD)
#	
# $(OBJDIR)%.o:: $(SRCDIR)/%.s
#	$(ASBUILD)
#
# It is up to you to generate the link/archive lines but they should
# be of the form:
#
# $(TARGET): $(TARGET_OBJECTS)
#	$(LD)  $(LDFLAGS) $(TARGET) $(TARGET_OBJECTS) $(CLIB)
# or    $(LP)  $(LPFLAGS) $(TARGET) $(TARGET_OBJECTS)
# or    $(AR)  $(ARFLAGS) $(TARGET) $(TARGET_OBJECTS)
#
# for each target in the $(BINARIES) list.
#
#
# TARGETS
# =======
#
# The following build variants are provided by default
#
# ArmLittleRelease    - Build project for ARM in little endian release mode
# ArmLittleDebug      - Build project for ARM in little endian debug mode
# ArmBigRelease       - Build project for ARM in big endian release mode
# ArmBigDebug         - Build project for ARM in big endian debug mode
# ThumbLittleRelease  - Build project for Thumb in little endian release mode
# ThumbLittleDebug    - Build project for Thumb in little endian debug mode
# ThumbBigRelease     - Build project for Thumb in big endian release mode
# ThumbBigDebug       - Build project for Thumb in big endian debug mode
# Riscos              - Build project for RISC OS (an unsupported option)
#                       (the environment variable RISCOSTOOLS must be set up)
#
# You may add your own variant builds by adding rules after this file is
# included. The rule should use $(MAKEVARIANT). See the implementations below
# for the default variants above. Or the rule can call one of the
# default variants.
# For example:
#  $(MAKE) $(MFLAGS) VARIANT="Arm3Rel" ArmLittleRelease
# Note: If X*FLAGS are used with flag options that would be overridden by the
#       default variant, a new rule must be defined instead of defining a
#       rule based on an existing variant

# TOOLS

SHELL = /bin/sh
ACC   = armcc
TCC   = tcc
AAS   = armasm
TAS   = \$$\$$tasm
LP    = $(LD)

# Compiler flags
#
# -f  = turn on extra features (all off by default)
#   a = check for data flow anomalies e.g. variable used before being set
#   h = checks external objects are declared before use
# -W+ = enable warnings, as below
#   a = use of = in a condition context (on by default)
#   b = use of non int type for bitfields (on by default)
#   d = declaration without argument types (on by default)
#   f = inventing extern int foo() (on by default)
#   g = header file not guarded (off by default)
#   l = lower precision in wider context (off by default)
#   n = implicit narrowing cast (off by default)
#   u = future compatibility issues (e.g. C++) (off by default)
#   v = implicit return in non-void context (on by default)
#   x = function declared but not used (on by default)

##SDT compiler defaulted to ARM6.
##For Arm Developer Suite the C compiler and Assembler default to -cpu ARM7TDMI, or
##whatever the restricted toolkit constraints file specifies, for example 
## ARM922T for Altera.
##Remove -cpu arm7tm, and let the toolkit provide the default.
##D.Earlam 9.Nov.2000 

CFLAGS_DEF     = -fah -W+glnu $$nofpswst
CFLAGS_ARCH3   = -fah -W+glnu -cpu arm7\$$\$$nofpswst
CFLAGS_DEFAULT = -fah -W+glnu \$$\$$nofpswst
CFLAGS_THUMB   = -fah -W+glnu\$$\$$nofpswst
CFLAGS_DEBUG   = -g
CFLAGS_RISCOS  = -apcs 3/26bit/hardfp/fp/swst -J$(RISCOSTOOLS)/include -D__riscos -arch 3

# Assembler flags
#
# -cr = checks LDM and STM register lists for increasing register number

ASFLAGS_DEF     = -cr $$noswst
ASFLAGS_ARCH3   = -cr -cpu arm7\$$\$$noswst
ASFLAGS_DEFAULT = -cr \$$\$$noswst
ASFLAGS_THUMB   = -cr\$$\$$inter
ASFLAGS_DEBUG   = -g
ASFLAGS_RISCOS  = -apcs 3/26bit/swst/fp

# Linker/Loader flags
#
# totals = total code and data sizes in the image, separate for object and library
# interwork = lists all calls for which ARM/Thumb interworking veneer was necessary
# unused = lists all unused AREAs, when used with remove area

LDFLAGS_DEFAULT = -o
LDFLAGS_RISCOS  = -noscanlib -aif -o

# C library
#
CLIB_DEFAULT = $(XCLIB)
CLIB_RISCOS  = $(RISCOSTOOLS)/lib/stubsa.o

#####################################
# Help variants and status variants #
#####################################

help::
	@echo "The syntax for 'make' is:" ; \
	echo "" ; \
	echo "    make [variant]" ; \
	echo "" ; \
	echo "where the following variants are supported:" ; \
	echo "" ; \
	echo "a) default variants..." ; \
	echo "    all     - build all project specific variants" ; \
	echo "    clean   - remove all object (.o) and dependency (.d) files" ; \
	echo "    clobber - remove all existence of a build" ; \
	echo "    help    - print this help" ; \
	echo "" ; \
	echo "b) project specific variants..." ; \
	for var in $(WHICHVARIANTS) .dummy ; do \
	  [ "$$var" != ".dummy" ] && echo "    $$var" ; \
	done; \
	echo "" ; \
	if [ -z "$(SUBPROJECTS)" ] ; then \
	  echo "There are no subprojects at this level." ; \
	else \
	  echo "Making at this level will also 'make' the following subprojects:" ;\
	  echo "" ;\
	  for sub in $(SUBPROJECTS) .dummy ; do \
	    [ -f $$sub/makefile ] && echo "    $$sub" ;\
	  done ;\
	fi; \
	echo ""

############################
# Variant Top Level Builds #
############################

all:: $(WHICHVARIANTS)

MAKEVARIANT = $(MAKE) $(MFLAGS) variant vVARIANT="$@"

# ARM250 toolkit standard variants

Release::; @$(MAKEVARIANT)

Debug::; @$(MAKEVARIANT) \
	vCFLAGS="$(CFLAGS_DEFAULT) $(CFLAGS_DEBUG)" \
	vASFLAGS="$(ASFLAGS_DEFAULT) $(ASFLAGS_DEBUG)"

DebugRel::; @$(MAKEVARIANT) \
	vCFLAGS="$(CFLAGS_DEFAULT) $(CFLAGS_DEBUG) -O2" \
	vASFLAGS="$(ASFLAGS_DEFAULT) $(ASFLAGS_DEBUG)"

# Full named variants

Arch3LittleRelease::; @$(MAKEVARIANT) \
	vCFLAGS="$(CFLAGS_ARCH3)" \
	vASFLAGS="$(ASFLAGS_ARCH3)"

Arch3LittleDebug::; @$(MAKEVARIANT) \
	vCFLAGS="$(CFLAGS_ARCH3) $(CFLAGS_DEBUG)" \
	vASFLAGS="$(ASFLAGS_ARCH3) $(ASFLAGS_DEBUG)"

ArmLittleRelease::; @$(MAKEVARIANT)

ArmLittleDebug::; @$(MAKEVARIANT) \
	vCFLAGS="$(CFLAGS_DEFAULT) $(CFLAGS_DEBUG)" \
	vASFLAGS="$(ASFLAGS_DEFAULT) $(ASFLAGS_DEBUG)"

ArmBigRelease::; @$(MAKEVARIANT) \
	vCFLAGS="$(CFLAGS_DEFAULT) -bi" \
	vASFLAGS="$(ASFLAGS_DEFAULT) -bi" BYTESEX="b"

ArmBigDebug::; @$(MAKEVARIANT) \
	vCFLAGS="$(CFLAGS_DEFAULT) -bi $(CFLAGS_DEBUG)" \
	vASFLAGS="$(ASFLAGS_DEFAULT) -bi $(ASFLAGS_DEBUG)" BYTESEX="b"

ThumbLittleRelease::; @$(MAKEVARIANT) \
	vCFLAGS="$(CFLAGS_THUMB)" \
	vASFLAGS="$(ASFLAGS_THUMB)" \
	vCC="$(TCC)" vAS="$(TAS)" BITS="16"

ThumbLittleDebug::; @$(MAKEVARIANT) \
	vCFLAGS="$(CFLAGS_THUMB) $(CFLAGS_DEBUG)" \
	vASFLAGS="$(ASFLAGS_THUMB) $(ASFLAGS_DEBUG)" \
	vCC="$(TCC)" vAS="$(TAS)" BITS="16"

ThumbBigRelease::; @$(MAKEVARIANT) \
	vCFLAGS="$(CFLAGS_THUMB) -bi" \
	vASFLAGS="$(ASFLAGS_THUMB) -bi" \
	vCC="$(TCC)" vAS="$(TAS)" BITS="16" BYTESEX="b"

ThumbBigDebug::; @$(MAKEVARIANT) \
	vCFLAGS="$(CFLAGS_THUMB) -bi $(CFLAGS_DEBUG)" \
	vASFLAGS="$(ASFLAGS_THUMB) -bi $(ASFLAGS_DEBUG)" \
	vCC="$(TCC)" vAS="$(TAS)" BITS="16" BYTESEX="b"

Riscos::; @$(MAKEVARIANT) \
	vCFLAGS="$(CFLAGS_DEFAULT) $(CFLAGS_RISCOS)" \
	vASFLAGS="$(ASFLAGS_DEFAULT) $(ASFLAGS_RISCOS)" \
	vLDFLAGS="$(LDFLAGS_RISCOS)" \
	vCLIB="$(CLIB_DEFAULT) $(CLIB_RISCOS)" BITS="26"
	
#######################
# GCC build rules     #
#######################

GCCRUN = export DEPENDENCIES_OUTPUT; gcc
	
GccRelease::; @$(MAKEVARIANT) \
	vCFLAGS="-c -pedantic -Wall" vLDFLAGS="-o" CDEP="-o _@ -c _<" \
	vCC="DEPENDENCIES_OUTPUT='\$$$$(@:%.o=%.d) \$$$$(@)'; $(GCCRUN)" \
	vAS="gcc" vLD="gcc" vAR="ar" vARFLAGS="-c -r" VSN="gcc"

GccDebug::; @$(MAKEVARIANT) \
	vCFLAGS="-c -pedantic -Wall -g -DDEBUG" vLDFLAGS="-o" CDEP="-o _@ -c _<" \
	vCC="DEPENDENCIES_OUTPUT='\$$$$(@:%.o=%.d) \$$$$(@)'; $(GCCRUN)" \
	vAS="gcc" vLD="gcc" vAR="ar" vARFLAGS="-c -r" VSN="gcc"


#######################
# Default Rules       #
#######################

CBUILD  = $(CC) $(CFLAGS) $(CDEP)
ASBUILD = $(AS) $(ASFLAGS) -depend $(@:%.o=%.d) -o $@ $<

# Default rule for C files

$(OBJDIR)%.o:: $(SRCDIR)/%.c
	$(CBUILD)
	
# Default rule for Assembler files

$(OBJDIR)%.o:: $(SRCDIR)/%.s
	$(ASBUILD)


#########
# Build #
#########

# Actually perform the build for a variant:
#
# Target "variant" depends on "makevariant" so variant can be redefined
# First build subprojects in this variant (if there are any)
# Then if $(BINARIES) is non empty:
#  Set up the variables for the given variant (& defaults)
#  Create the variants directory and .depends (or gnumake fails)
#  Build all our binaries with these variables (using 'make build')
#  Update all dependencies
# Remove top level (dummy) .depends to clean up

variant::
	@variant="$(VARIANT)"; [ -z "$$variant" ] && variant="$(vVARIANT)"; \
	for subproj in $(SUBPROJECTS) .dummy; do \
	  if [ -f $$subproj/makefile ]; then \
	    ( cd $$subproj; touch .depends; $(MAKE) $(MFLAGS) $$variant; ) \
	  fi; \
	done; \
	if [ -n "$(BINARIES)" ]; then \
	  cc="$(vCC)";           [ -z "$$cc" ]      && cc="$(ACC)" ; \
	  if [ -z "$(VSN)" ] && $$cc 2>&1 | grep ADS > /dev/null; then \
	    nofpswst=""; noswst=""; armar="armar"; arcr="-create"; lpart="-partial"; \
	    inter=" -apcs /interwork -PD \"INTER SETA 2\""; \
	    cdep="-depend \$$(@:%.o=%.d) -o \$$@ -c \$$<"; tasm="armasm -16"; \
	    xcflags="$(ADSXCFLAGS)"; xasflags="$(ADSXASFLAGS)"; \
	  else \
	    nofpswst=" -apcs /nofp/noswst"; noswst=" -apcs /noswst"; \
	    inter=" -PD \"INTER SETA 1\""; lpart="-aof"; tasm="tasm"; \
	    cdep="-MD- -o \$$@ -c \$$< > \$$(@:%.o=%.d)"; armar="armlib"; arcr="-c -o"; \
	    xcflags="$(SDTXCFLAGS)"; xasflags="$(SDTXASFLAGS)"; \
	  fi; \
	  vardir="$(VARIANTS_DIR)";  [ -z "$$vardir" ] && vardir="variants/"; \
	  includes=" -I$(INCLUDES)"; [ -z "$(INCLUDES)" ] && includes=""; \
	  objdir="$(vOBJDIR)";   [ -z "$$objdir" ]  && objdir="$${vardir}$$variant/"; \
	  cflags="$(vCFLAGS)";   [ -z "$$cflags" ]  && cflags="$(CFLAGS_DEF)"; \
	  asflags="$(vASFLAGS)"; [ -z "$$asflags" ] && asflags="$(ASFLAGS_DEF)"; \
	  ldflags="$(vLDFLAGS)"; [ -z "$$ldflags" ] && ldflags="$(LDFLAGS_DEFAULT)"; \
	  lpflags="$(vLPFLAGS)"; [ -z "$$lpflags" ] && lpflags="$$lpart -o"; \
	  arflags="$(vARFLAGS)"; [ -z "$$arflags" ] && arflags="$$arcr"; \
	  clib="$(vCLIB)";       [ -z "$$clib" ]    && clib="$(CLIB_DEFAULT)" ; \
	  as="$(vAS)";           [ -z "$$as" ]      && as="$(AAS)" ; \
	  ld="$(vLD)";           [ -z "$$ld" ]      && ld="armlink"; \
	  ar="$(vAR)";           [ -z "$$ar" ]      && ar="$$armar"; \
	  srcdir="$(SRCDIR)";    [ -z "$$srcdir" ]  && srcdir="src" ; \
	  bits="$(BITS)";        [ -z "$$bits" ]    && bits="32" ; \
	  bytesex="$(BYTESEX)";  [ -z "$$bytesex" ] && bytesex="l" ; \
	  [ -n "$(CDEP)" ] && cdep="$(CDEP:_%=\$$%)"; \
	  echo "    Building '$$variant' at '`pwd`'"; \
	  [ -d "$$objdir" ]   || mkdir -p $$objdir; touch $$objdir.depends; \
	  [ -z "$(SUBDIRS)" ] || ( cd $$objdir; mkdir -p $(SUBDIRS) ); \
	  $(MAKE) $(MFLAGS) build VARIANT="$$variant" OBJDIR="$$objdir" \
	    CC="$$cc" AS="$$as" LD="$$ld" AR="$$ar" \
	    CFLAGS="$$cflags$$includes $(XCFLAGS) $$xcflags" \
	    ASFLAGS="$$asflags$$includes $(XASFLAGS) $$xasflags" \
	    LDFLAGS="$(XLDFLAGS) $$ldflags" \
	    LPFLAGS="$(XLPFLAGS) $$lpflags" \
	    ARFLAGS="$(XARFLAGS) $$arflags" \
	    CDEP="$$cdep" CLIB="$$clib" \
	    SRCDIR="$$srcdir" VARIANTS_DIR="$$vardir" \
	    BITS="$$bits" BYTESEX="$$bytesex"; \
	  echo "    Finished '$$variant' build"; \
	  ( cd $$objdir; \
	    rm -f .depends; \
	    for file in *.d $(SUBDIRS:%=%/*.d); do \
	      if [ -f $$file ]; then cat $$file >> .depends; fi; \
	    done; \
	  ) \
	fi; \
	rm -f .depends
	
build:: $(BINARIES)

# if the build failed then we must still pull together the dependency files

.FAILED:
	@variant="$(VARIANT)"; [ -z "$$variant" ] && variant="$(vVARIANT)"; \
	vardir="$(VARIANTS_DIR)"; [ -z "$$vardir" ] && vardir="variants/"; \
	objdir="$(vOBJDIR)"; [ -z "$$objdir" ]  && objdir="$${vardir}$$variant/"; \
	if [ -f $$objdir/.depends ]; then  ( \
	  cd $$objdir; \
	  rm -f .depends; \
	  for file in *.d $(SUBDIRS:%=%/*.d); do \
	    if [ -f $$file ]; then cat $$file >> .depends; fi; \
	  done; \
	) fi; \
	rm -f .depends
	
####################
# Clean up targets #
####################

#
# Remove object files and dependencies including all subprojects
#
clean::
	@for subproj in $(SUBPROJECTS) .dummy; do \
	  if [ -f $$subproj/makefile ]; then \
	    ( cd $$subproj; $(MAKE) $(MFLAGS) clean; ) \
	  fi; \
	done; \
	vardir=$(VARIANTS_DIR); [ -z "$$vardir" ] && vardir="variants/"; \
	for variant in $$vardir*; do \
	  if [ -f $$variant/.depends ]; then ( \
	    cd $$variant; \
	    rm -f *.o $(SUBDIRS:%=%/*.o) *.d $(SUBDIRS:%=%/*.d) .depends; \
	    touch .depends; \
	  ) fi; \
	done; \
	rm -f .depends

#
# Remove all trace of a make including all subprojects
#
clobber::
	@for subproj in $(SUBPROJECTS) .dummy; do \
	  if [ -f $$subproj/makefile ]; then \
	    ( cd $$subproj; $(MAKE) $(MFLAGS) clobber; ) \
	  fi; \
	done; \
	vardir=$(VARIANTS_DIR); [ -z "$$vardir" ] && vardir="variants/"; \
	if [ "$$vardir" = "./" ]; then \
	  for variant in $$vardir*; do \
	    if [ -f $$variant/.depends ]; then \
	      rm -r -f $$variant; \
	    fi; \
	  done; \
	else \
	  rm -r -f $$vardir; \
	fi; \
	rm -f .depends
	
##############################
# Create the dependency file #
##############################
	
# Create the initial (empty) depency file if one doesn't exist
# This also ensures that the target directory $(OBJDIR) exists

$(OBJDIR).depends::
	@[ -z "$(OBJDIR)" -o -d "$(OBJDIR)" ] || mkdir -p $(OBJDIR); \
	touch $(OBJDIR).depends

# Include the dependencies (or empty file generated above)

include $(OBJDIR).depends
