#
#  PROJECT
#      Comma Separated Value Library (Libcsv)
#
#  DESCRIPTION
#      Linux (i386) Makefile for testing Libcsv
#
#  DEVELOPER
#      Scott C. Karlin   (scott@cs.princeton.edu)
#
#  HISTORY
#      21 May 2003  sck  Initial Version
#
#  CVS ID
#      $Id: Makefile,v 1.1 2003/05/23 17:05:53 scott Exp $
#

##############################################################################
#
#  Libraries
#     The LIBS variable contains a space separated list of library
#     archives of the form "DIRECTORY/lib/libNAME.a" or the form
#     "DIRECTORY/lib/ARCH/libNAME.a".  This makefile will automatically
#     generate -I compiler directives of the form "-IDIRECTORY/include"
#     for each library.  Note that the order is important and that
#     libraries may be listed multiple times, if needed.
#
LIBS  = $(LIBSROOT)/Libcsv/lib/i386/libcsv.a

##############################################################################
#
#  Search path
#
vpath %.h ..
vpath %.c ..
vpath %.S ..

##############################################################################
#
#  Header Directories
#     List any additional header directories (beyond the current directory
#     and those implied by LIBS) here.
#
HDR_DIRS  =

##############################################################################
#
#  Generate library flags from LIBS
#
LIB_DIR_FLAGS  = $(patsubst %/,-L%,$(sort $(dir $(LIBS))))
LIB_NAME_FLAGS = $(patsubst lib%.a,-l%,$(notdir $(LIBS)))

##############################################################################
#
#  Generate the names of the include directories from LIBS
#     This code assumes that ARCH can never be "lib".
#     Note that the order of include directories is not same as LIBS.
#
LIB_DIRS         = $(dir $(LIBS))
LIB_ARCH_DIRS    = $(filter-out %/lib/,$(LIB_DIRS))
LIB_NOARCH_DIRS  = $(filter     %/lib/,$(LIB_DIRS))

#  LIB_ARCH_DIRS   contains directories of the form "DIRECTORY/lib/ARCH/"
#  LIB_NOARCH_DIRS contains directories of the form "DIRECTORY/lib/"

LIB_NOARCH_DIRS += $(dir $(patsubst %/,%,$(LIB_ARCH_DIRS)))

INC_DIRS = $(sort $(patsubst %/lib/,%/include,$(LIB_NOARCH_DIRS)))

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

##############################################################################
#
#  Definitions for Compiler, Preprocessor, Linker, etc.
#
CC       = gcc
CPPFLAGS = $(INC_FLAGS)
CFLAGS   = -Wall -Wstrict-prototypes -O2
#CFLAGS  += -g -rdynamic

LDFLAGS  = $(LIB_DIR_FLAGS)
LDFLAGS += $(LIB_NAME_FLAGS)
#LDFLAGS += -g -rdynamic -pg

##############################################################################
#
#  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)

##############################################################################
#
#  Source Files
#     This is the list of all source files (.c and .S) needed to build
#     any of the TARGETS.
#
C_SRCS = main.c
S_SRCS =

##############################################################################
#
#  Dependencies and Objects
#
DEPS   = $(strip $(C_SRCS:.c=.d)  $(S_SRCS:.S=.d))
OBJS   = $(strip $(C_SRCS:.c=.o)  $(S_SRCS:.S=.o))

##############################################################################
#
#  Targets
#
TARGETS = main

.PHONY : all install clean veryclean distclean

all:        $(TARGETS)


install:    all
	@echo '*.d as_map*' $(TARGETS) > .cvsignore


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


veryclean:  clean
	rm -f $(TARGETS)


distclean:  veryclean


##############################################################################
#
#  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 $(DEPS)),)
-include $(DEPS)
endif
endif

##############################################################################
#
#  Target Dependencies
#

define link
$(CC) -o $@ $^ $(LDFLAGS)
endef

main:  main.o
	$(link)

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