#
#  PROJECT
#      CSV Library (Libcsv)
#
#  DESCRIPTION
#      IXP1200 Makefile for Libcsv
#
#  DEVELOPER
#      Scott C. Karlin   (scott@cs.princeton.edu)
#
#  HISTORY
#      24 Apr 2003  sck  Initial Version
#
#  CVS ID
#      $Id: Makefile,v 1.2 2003/05/23 17:05:53 scott Exp $
#

##############################################################################
#
#  Name of the library to build
#
LIBNAME = csv
LIB     = lib$(LIBNAME).a

##############################################################################
#
#  Header file names for users of the library
#     These are the exported headers.
#
LIB_HDRS  = 

##############################################################################
#
#  Sources file names (.c and .S) for the library
#
LIB_C_SRCS  = 
LIB_S_SRCS  =

##############################################################################
#
#  Where to install the library and the headers
#
LIB_DIR     = ../../lib/arm
LIB_HDR_DIR = ../../include

##############################################################################
#
#  Search path includes the parent directory so that files that are common
#  to multiple architectures need not be duplicated.
#
vpath %.h ..
vpath %.c ..
vpath %.S ..

##############################################################################
#
#  Header Directories
#     List any additional header directories needed for the build here.
#
HDR_DIRS  = 

##############################################################################
#
#  Generate header flags from LIBS and HDR_DIRS
#
INC_FLAGS  = -I. -I..                           # include "self"
INC_FLAGS += $(patsubst %,-I%,$(HDR_DIRS))

##############################################################################
#
#  Compiler Flags
#
CC       = arm-unknown-coff-gcc
CPPFLAGS = $(strip ${INC_FLAGS})
CFLAGS   = -nostartfiles -nodefaultlibs -fno-builtin
CFLAGS  += -mcpu=strongarm110 -mapcs-32 -mno-sched-prolog -fvolatile
CFLAGS  += -Wall -Wstrict-prototypes -O2

AR       = arm-unknown-coff-ar
ARFLAGS  = crus

##############################################################################
#
#  The Shell
#
SHELL = /bin/sh

##############################################################################
#
#  Pattern Matching Rules
#
%.o : %.c
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

%.o : %.S
	$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

#
#  These pattern matching rules create dependency files from
#  corresponding source files indicating that the .o and .d files
#  depend on the source (.c or .S) file and all recursively included
#  header (.h) files.
#
define gen-deps
echo Updating dependency makefile $@ from $(notdir $<)
$(SHELL) -ec '$(CC) -MM $(CPPFLAGS) $< \
	| sed '\''s|\($*\)\.o[ :]*|\1.o $@ : |g'\'' > $@'
endef

%.d : %.c
	@$(gen-deps)

%.d : %.S
	@$(gen-deps)

##############################################################################
#
#  Dependencies and Objects
#
LIB_DEPS   = $(strip $(LIB_C_SRCS:.c=.d)  $(LIB_S_SRCS:.S=.d))
LIB_OBJS   = $(strip $(LIB_C_SRCS:.c=.o)  $(LIB_S_SRCS:.S=.o))

##############################################################################
#
#  Targets
#
TARGET = $(LIB)

.PHONY : all checkcc copyhdrs install clean veryclean distclean

all:        checkcc $(TARGET)

checkcc:
	@if [ -z `type -p $(CC)` ]; then \
	   echo "$(CC) Compiler was Not Found in the path."; \
	   exit 1; \
	fi

copyhdrs:      $(LIB_HDRS)
ifneq ($(strip $(LIB_HDRS)),)
	cp --update --target-directory=$(LIB_HDR_DIR) $^
endif

install:    all copyhdrs $(LIB_HDR_DIR)/$(LIBNAME)
	cp --update $(LIB) $(LIB_DIR)

clean:
	@rm -f ,* .,* *~ .emacs_[0-9]* core a.out
	rm -f $(LIB_DEPS) $(LIB_OBJS)

veryclean:  clean
	rm -f $(TARGET)

distclean:  veryclean
	(cd $(LIB_DIR); rm -f $(LIB))
	(cd $(LIB_HDR_DIR); rm -f $(LIB_HDRS) $(LIBNAME))

##############################################################################
#
#  Sanity check that we are not trying to simultaneously build and clean
#
ifeq ($(findstring all,$(MAKECMDGOALS)),all)            #  *all
ifeq ($(findstring clean,$(MAKECMDGOALS)),clean)        #  *clean
$(error Cannot simultaneously build/install and clean)
endif
endif

##############################################################################
#
#  Include Dependency Files
#  (if we are not cleaning)
#
ifneq ($(findstring clean,$(MAKECMDGOALS)),clean)
ifneq ($(strip $(LIB_DEPS)),)
-include $(LIB_DEPS)
endif
endif

##############################################################################
#
#  Target Dependencies
#
$(LIB): $(LIB_OBJS)
	rm -f $@
	$(AR) $(ARFLAGS) $@ $^

$(LIB_HDR_DIR)/$(LIBNAME):
	(cd $(LIB_HDR_DIR); ln -s . $(LIBNAME))

##############################################################################
