#
# Makefile
#
# x-kernel v3.2
#
# Copyright (c) 1991  Arizona Board of Regents
#
#
#   Configuration options: 
#
#	XPLATFORM	supporting platform (e.g., "sunos")
#	MACHINE		binary type (e.g., "sparc")
#	HOWTOCOMPILE	either DEBUG or OPTIMIZE

XPLATFORM = sunos
HOWTOCOMPILE = DEBUG
MACHINE = sparc

sunos_LIBS = -llwp

OPTIMIZECFLAGS = -O
OPTIMIZECPPFLAGS = -DNDEBUG -DMSGOPT

DEBUGCFLAGS = -g
DEBUGCPPFLAGS = 

HOW = $(HOWTOCOMPILE)$(XPLATFORM)

XRT = ../..

INCLUDES = -I. -I$(XRT)/include -I$(XRT)/include/prot -I$(XRT)/etc \
	   -I$(XRT)/$(XPLATFORM)/include
CPPFLAGS = -DMSGOPT $(INCLUDES) $($(HOWTOCOMPILE)CPPFLAGS)
CFLAGS =  $($(HOWTOCOMPILE)CFLAGS) $(CPPFLAGS)

PROTDIR = $(XRT)/protocols
PLATDIR = $(XRT)/$(XPLATFORM)
LIBPROT = $(PROTDIR)/libProt_$(HOW) 
LIBPXK =  $(PLATDIR)/libPxk_$(HOW)
XLIBS = $(LIBPROT) $(LIBPXK)
LIBCFLAGS = -DMSGOPT $($(HOWTOCOMPILE)CFLAGS)
MAKEFILE=Makefile

CC = gcc
SED = sed
MAKE = make
REALCC = /bin/cc


# PCDEFINITIONS BEGIN HERE
# Don't edit these lines, compose uses them
PCDOTOS = ./${HOW}/udptest.o \

PCDOTCS = ./udptest.c \

PCDOTSS = 
# PCDEFINITIONS END HERE


PRIVOBJ = \
	${HOW}/localinit.o \
	${HOW}/protocols.o

OBJ =	\
	${PRIVOBJ} \
	${PCDOTOS}

PRIVSRC = \
	localinit.c \
	protocols.c

SRC = 	\
	${PRIVSRC} \
	$(PCDOTCS)

INC = \
	${PCDOTHS} 

all:	xkernel

xall:	libProt libPxk xkernel

obj:
	@echo $(OBJ)

% :: RCS/%,v
DEFAULTRULE = $(CC) -c $(CFLAGS) -o $@ $<


# IMPLICIT RULES BEGIN HERE
# Don't edit these lines, compose uses them

./$(HOW)/%.o : ./%.c
	$(DEFAULTRULE)

# IMPLICIT RULES END HERE

	
xkernel:	$(OBJ) $(XLIBS)
	@echo linking xkernel ...
	@$(CC) -n -v -o xkernel $(OBJ) $(XLIBS) $($(XPLATFORM)_LIBS) 

compose: graph.comp
	$(XRT)/bin/compose_$(XPLATFORM) $(XLIBS) <graph.comp
	cp -p Makefile Makefile.bak
	$(SED) -f protocols.sed <Makefile | $(SED) -e '/^@/s/^/	/' >Maketemp
	cp Maketemp Makefile
	rm Maketemp protocols.sed

#
# xkernel libraries
#
#
# forcing rules
#
.PHONY: libs libProt libPxk
libs: libProt libPxk

libProt:
	cd $(PROTDIR); $(MAKE) CFLAGS="$(LIBCFLAGS)" HOW:=$(HOW)
libPxk:
	cd $(PLATDIR); $(MAKE) CFLAGS="$(LIBCFLAGS)" 
#
# non-forcing rules
#
$(LIBPROT):
	make libProt
$(LIBPXK):
	make libPxk


libdotodirs:
	cd $(PROTDIR); $(MAKE) dotodirs
	cd $(PLATDIR); $(MAKE) dotodirs

dotodirs:  libdotodirs
	-if [ ! -d DEBUG$(XPLATFORM) ] ; then umask 2; mkdir DEBUG$(XPLATFORM) ; fi
	-if [ ! -d OPTIMIZE$(XPLATFORM) ] ; then umask 2; mkdir OPTIMIZE$(XPLATFORM) ; fi


setup:	dotodirs
	cp ../Template/localinit.c . ; chmod +w localinit.c
	cp ../Template/graph.comp . ; chmod +w graph.comp


#
# srcList and hdrList:  lists of all source and header files in both 
# the local directory and the libraries
#
# non-forcing rules

#hdrList: $(XLIBS)
hdrList: 
	@echo rebuilding header list
	@make hdrlist
#srcList: $(XLIBS)
srcList: 
	@echo rebuilding source list
	@make srclist
#
# forcing rules
.PHONY: srclist hdrlist
srclist:
	@make SRCLIST > srcList

hdrlist:
	@make HDRLIST > hdrList


SRCLIST HDRLIST:
	@make local$@ > .files$$$$ ; \
	(cd ../../sunos; \
		$(MAKE) $@ | fixRelPaths sunos | fixRelPathsDown 2) \
	  	>> .files$$$$; \
	(cd ../../protocols; \
		$(MAKE) $@ | fixRelPaths protocols | fixRelPathsDown 2) \
	  	>> .files$$$$; \
	cat .files$$$$ | sort | uniq ; \
	rm .files$$$$

#
# RCS related targets
#
ci:	srcList hdrList
	@for file in `make ciFilter` ; do \
 	    rcsdiff $$file > /dev/null 2>&1 || { echo $$file ; ci -q -u $$file ; } ; \
	done

ci2:	srcList hdrList
	@for file in `make ciFilter` ; do \
 	    rcsdiff $$file > /dev/null 2>&1 || { rcsdiff $$file ; echo $$file ; ci -q -u $$file ; } ; \
	done
	
ciFilter:	
	@for file in `cat srcList hdrList | egrep -v \
		'^/|localinit.c|protocols.c|protocols.h'` ; do \
	    XX=`echo $$XX $$file` ; \
	done ; \
	echo $$XX 

#
# RCSNAME should be specified on command line
#
rcssave: srcList hdrList
	@for file in `make ciFilter` ; do \
 	    echo $$file ; \
	    REV=`rlog -h $$file | fgrep 'head:' | awk ' {print $$2 } ' `; \
	    rcs -N$(RCSNAME):$$REV $$file ; \
	done
	




lint:
	@echo "Running lint"
	@lint -hbxuzn ${CPPFLAGS} ${SRC} | \
		egrep -v 'possible pointer alignment problem|questionable conversion of function pointer'

#
# tags: creates a tag file for all xkernel source and header files
# (including libraries)
#
.PHONY: tags
tags:	srcList hdrList
	make TAGS ALLSRC="`cat srcList hdrList | tr '\012' ' '`"

TAGS:	$(ALLSRC)
	@etags -tw -f TAGS $(ALLSRC)


allclean: clean
	cd $(XRT)/protocols; $(MAKE) clean
	cd $(XRT)/$(XPLATFORM); $(MAKE) clean

alldepend: depend
	cd $(XRT)/protocols; $(MAKE) depend
	cd $(XRT)/$(XPLATFORM); $(MAKE) depend

cleanjunk:
	@echo Removing dotss ...
	@rm -f  *.c2 *.ccom *.cpp *.bak *.ckp *.ss all.* cscope.dep \
	    cscope.out cscope.tsort routines.sizes routines.list \
	    allnames.dep ${ASM}

clean:	cleanjunk
	@echo Removing dotos ...
	@echo $(OBJ)
	@rm -f $(OBJ)

#
# grep:  greps for the target G in all xkernel source and header files
# example usage:  make grep G="x_create"
#
G = xkernel
GREP = egrep
GFLAGS=

grep:	srcList hdrList
	-@$(GREP) -n $(GFLAGS) -e "$G" `cat srcList` `cat hdrList`


depend: localdepend
include $(XRT)/bin/Makefile.local


# DO NOT DELETE THIS LINE -- make depend uses it
./${HOW}/localinit.o : localinit.c ../../include/debug.h protocols.h 
./${HOW}/protocols.o : protocols.c ../../include/upi.h ../../include/idmap.h \
  ../../include/msg.h protocols.h 
./${HOW}/udptest.o : ./udptest.c /usr/include/stdio.h ../../include/xkernel.h \
  ../../include/upi.h ../../include/idmap.h ../../include/msg.h \
  ../../include/event.h ../../include/debug.h ../../include/assert.h \
  ../../etc/site.h ../../sunos/include/platform.h /usr/include/sys/types.h \
  /usr/include/sys/stdtypes.h /usr/include/sys/sysmacros.h \
  /usr/include/netinet/in.h ../../sunos/include/process.h \
  /usr/include/lwp/lwp.h /usr/include/sys/time.h /usr/include/time.h \
  ../../include/prot/ip.h ../../include/prot/udp.h 
