######################################################################
#
# Test makefile for gnu make 3.80 "bug"
#
# Targets:
#
#		make clean:		Clean out all OBJ files
#		make deps:		Create updated dependency list
#   	make [all]:		Compile and link as necessary for building test program
#
#		To manifest the bug, invoke: make -B
#
######################################################################

TARGETNAME:=test

LIBSOURCES:= 	\
	test1.c		\
	test2.c		\
	test3.c		\
	test4.c

######################################################################
#
# Makefile.rules - Common makefile rules for all app subcomponents
#
# Prerequisities:
#		LIBSOURCES = Source files (for library)
#		BINSOURCES = Source files (for binary)
#		TARGETNAME = Target project name
#		LIBNAME = Library filename, or derived from TARGETNAME, as lib$(TARGETNAME).a
#		BINNAME = Binary filename, or same as TARGETNAME
#		ADDL_CFLAGS = Additional compiler flags
#		ADDL_DEFINES = Definitions for the compiler (separated with spaces)
#		ADDL_INCLUDE_PATH = Additional include file path (separated with colon)
#		ADDL_TARGETS = Additional targets (for make clean)
#		ADDL_LIBS = Additional libraries for building binary file
#       ADDL_LIB_PATH = Additional path to libraries for building binary file
#		ADDL_LFLAGS = Additional linker flags
#		VERSION = Version number for the target
#
# Targets:
#
#		make clean:		Clean out all generated files
#		make deps:		Create updated dependency list
#   	make [all]:	Compile and link as necessary for building LOM library
#
#
######################################################################
armdir=..
armincdir=$(armdir)/inc
appdir=$(armdir)/app
appincdir=$(appdir)/inc
libdir=$(appdir)/lib
bindir=$(appdir)/bin

# We need this for later macro expansion
empty:=
space:=$(empty) $(empty)


######################################################################
#
# Just name the 'phony targets'
#
.PHONY: all bin clean deps lib dist-clean


# Derive INCLUDES from the paths we defined above
INCPATH=.:$(top)/../inc:$(armincdir):$(appincdir):$(ADDL_INCLUDE_PATH)
INCLUDES=-I $(subst :,$(space) -I $(space),$(INCPATH))

# Dependency files
DEPPATH=$(PWD)

# Tell make where to look for .h and .d files
vpath %.h $(INCPATH)
vpath %.a $(libdir)
vpath %.dep $(DEPPATH)


DEFINES = -D _SVID_SOURCE
ifdef ADDL_DEFINES
DEFINES += -D $(subst $(space),$(space) -D $(space),$(ADDL_DEFINES))
endif

CROSSFLAGS = -march=armv4 -mtune=arm9tdmi -g -mapcs-32 -fno-builtin
CFLAGS += -Wall -O2 $(INCLUDES) $(CROSSFLAGS) $(ADDL_CFLAGS) $(DEFINES)

AR = arm-linux-ar
CC = arm-linux-gcc

LDFLAGS=-static -W1 -static-libgcc

LDADD=-lgcc
ifdef ADDL_LIBS
LDADD+= -l$(subst $(space),$(space) -l,$(ADDL_LIBS)) 
endif

LIBPATH=-L /opt/mtwk/tools/arm-linux/gcc-3.2.3-glibc-2.2.5/arm-linux/lib -L $(nanoxlibdir)
ifdef ADDL_LIB_PATH
LIBPATH+= -L $(subst $(space),$(space) -L $(space),$(ADDL_LIB_PATH))
endif

ifdef BINSOURCES
BINOBJECTS:= $(BINSOURCES:.c=.o)
ifndef BINNAME
BINNAME:=$(TARGETNAME)
endif

else
BINOBJECTS:=
BINNAME:=
endif

ifdef LIBSOURCES
LIBOBJECTS:= $(LIBSOURCES:.c=.o)
ifndef LIBNAME
LIBNAME:=lib$(TARGETNAME).a
endif

else
LIBOBJECTS:=
LIBNAME:=
endif

SOURCES = $(LIBSOURCES) $(BINSOURCES)

# The list of .d files...
DEPENDS:= $(SOURCES:.c=.dep)

DEPENDENCIES:=$(foreach depfile,$(DEPENDS),$(DEPPATH)/$(depfile))

# **********************************************************************]
# The default target is 'all'
#
all: $(BINNAME) $(LIBNAME)

######################################################################
#
# This will make the various .dep files
#
$(DEPENDS): %.dep: %.c
	set -e; $(CC) -MM $(CFLAGS) $< \
		| sed 's/\($*\)\.o[ :]*/\1.o $@ :/g' > $(DEPPATH)/$@;	
		[ -s $(DEPPATH)/$@ ] || rm -f $(DEPPATH)/$@


# **********************************************************************
# Only if a binary is to be produced
#
ifdef BINSOURCES
bin: $(BINNAME)

# It appears that the nano-X library must be the last one specified...
$(BINNAME): $(BINOBJECTS)
	$(CC) $(LIBPATH) $(LDFLAGS) $(ADDL_LFLAGS) $(BINOBJECTS) -o $(BINNAME) $(LDADD) -l$(nanoxclientlib)

endif


# **********************************************************************
# Only if a library is to be produced
#
ifdef LIBSOURCES
lib: $(LIBNAME)

$(LIBNAME): $(LIBOBJECTS)
	$(AR) rv $(LIBNAME) $(LIBOBJECTS)

endif

######################################################################
# Only if we are to install (copy libs) to production lib area
#
install: $(LIBNAME) $(BINNAME)
ifdef LIBSOURCES
	cp $(LIBNAME) $(libdir)
endif
ifdef BINSOURCES
	cp $(BINNAME) $(bindir)
endif

######################################################################
#
# Include the rules for the .o and .dep files
# We really only want to do this if not 'make deps' or make for DEPENDS
#
ifneq ($(MAKECMDGOALS),deps)
ifneq ($(MAKECMDGOALS),$(DEPENDS))
include $(DEPENDS)
endif
endif


# **********************************************************************]
# clean up the bones
#
clean:
	rm -f $(BINOBJECTS) $(BINNAME) $(LIBNAME) $(LIBOBJECTS) $(ADDL_TARGETS) $(DEPENDS)


# **********************************************************************]
# clean up the distributables
#
dist-clean:
ifdef LIBSOURCES
	rm $(libdir)/$(LIBNAME)
endif
ifdef BINSOURCES
	rm $(bindir)/$(BINNAME)
endif


# **********************************************************************
# deps is contingent on the .dep files and Makefile
#
# ### NOTE: The bug is manifested whether or not Makefile is a dependency here ###
#
deps:	$(DEPENDS) # Makefile




