#
#  PROJECT
#      Daemonizer Library (Libdaemon)
#
#  DESCRIPTION
#      The Makefile
#
#  DEVELOPER
#      Scott Karlin      <scott@cs.princeton.edu>
#
#  HISTORY
#      21 Sep 2004  sck  Initial Version
#
#  CVS ID
#      $Id$
#

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

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

##############################################################################
#
#  Name of the library to build
#
LIB     = $(NAME).a

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

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

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

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

##############################################################################
#
#  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.                                # include "self"
INC_FLAGS += $(patsubst %,-I%,$(HDR_DIRS))

##############################################################################
#
#  Compiler Flags
#
CC       = gcc
CPPFLAGS = $(strip ${INC_FLAGS})
CFLAGS   = -Wall -Wstrict-prototypes -O2

AR       = 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 install clean veryclean distclean rpm

all:        $(TARGET)

install:    all
	cp --update --target-directory=$(LIB_DIR)    $(LIB)
	cp --update --target-directory=$(LIB_HDRDIR) $(LIB_HDRS)

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

veryclean:  clean
	rm -f $(TARGET)

distclean:  veryclean

$(TAR_NV).tgz:     Makefile daemon.c daemon.h
	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)
ifneq ($(strip $(LIB_DEPS)),)
-include $(LIB_DEPS)
endif
endif

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

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