#
#  PROJECT
#      SortDNS
#
#  DESCRIPTION
#      The Makefile
#
#  DEVELOPER
#      Scott C. Karlin   <scott@cs.princeton.edu>
#
#  HISTORY
#      22 Sep 2004  sck  Initial Version
#
#  CVS ID
#      $Id: Makefile,v 1.1.1.1 2004/09/22 21:13:12 sck-SortDNS Exp $
#

##############################################################################
#
#  Environment Assumptions
#
#  RPMBUILD is the directory where RPMs are built
#

##############################################################################
#
#  Identification
#
#  Any changes here must be sync'ed with changes made to sortDNS.spec
#
NAME    = sortDNS
VERSION = 0.01
RELEASE = 1

##############################################################################
#
#  Install Directory
#     Set to "." (no quotes) if there is no install directory
#
INSTALL_DIR = .

##############################################################################
#
#  Shorthand macros
#
RPM_NVR   = $(NAME)-$(VERSION)-$(RELEASE)
TAR_NV    = $(NAME)-$(VERSION)
RPM_FILES = $(RPM_NVR).src.rpm $(RPM_NVR).i386.rpm

##############################################################################
#
#  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.  Libraries may be listed multiple times as needed.
#
LIBS  =

##############################################################################
#
#  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.
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 -g

LDFLAGS  = $(LIB_DIR_FLAGS)
LDFLAGS += -g
LDFLAGS += $(LIB_NAME_FLAGS)

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

##############################################################################
#
#  Pattern Matching Rules
#
%.o : %.c
	$(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  = sortDNS.c

S_SRCS  =

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

##############################################################################
#
#  Targets
#
TARGETS  = sortDNS


.PHONY : all install clean veryclean distclean


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


install:    all
ifneq ($(strip $(INSTALL_DIR)),.)
	cp --update $(TARGETS) $(INSTALL_DIR)
endif


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


veryclean:  clean
	rm -f $(TARGETS)
	rm -f $(TAR_NV).tgz


distclean:  veryclean
ifneq ($(strip $(INSTALL_DIR)),.)
	cd $(INSTALL_DIR) ; rm -f $(TARGETS)
endif
	rm -f $(RPM_FILES)


$(TAR_NV).tgz:     Makefile sortDNS.c
	tar zcvf $@ $^


# FIXME: check that $(RPMBUILD) is a directory before copying
$(RPM_FILES): $(TAR_NV).tgz $(NAME).spec
	cp $(TAR_NV).tgz $(RPMBUILD)/SOURCES
	cp $(NAME).spec $(RPMBUILD)/SPECS
	rpmbuild -ba $(RPMBUILD)/SPECS/$(NAME).spec
	cp $(RPMBUILD)/SRPMS/$(RPM_NVR).src.rpm .
	cp $(RPMBUILD)/RPMS/i386/$(RPM_NVR).i386.rpm .

rpm:	$(RPM_FILES)

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

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

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

sortDNS:  sortDNS.o
	$(link)

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