#
#  PROJECT
#      PlanetLab
#
#  DESCRIPTION
#      The Makefile for the Example sensor server
#
#  DEVELOPER
#      Scott C. Karlin   (scott@cs.princeton.edu)
#
#  HISTORY
#      04 Apr 2003  sck  Initial Version
#

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

##############################################################################
#
#  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  =
LIBS += ${LIBSROOT}/Libssrv/lib/i386/libssrv.a
LIBS += ${LIBSROOT}/Libhttpe/lib/i386/libhttpe.a

##############################################################################
#
#  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  = exampleSS.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  = exampleSS


.PHONY : all install clean veryclean distclean


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


install:    all
ifneq ($(strip $(INSTALL_DIR)),.)
	cp --update $(TARGETS) $(INSTALL_DIR)
	@echo $(TARGETS) > $(INSTALL_DIR)/.cvsignore
endif


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


veryclean:  clean
	rm -f $(TARGETS)


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



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

exampleSS:  exampleSS.o
	$(link)

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