##	Main MPD Makefile.
#
#	To configure, edit "./Configuration".  See documentation.


CC = cc
CFLAGS = -O3 -Wall
MAKE = make
SHELL = /bin/sh

##  MultiMPD architecture, or "uni" for uniprocessor MPD
MULTI = uni


#  recursive make:
RMAKE = $(MAKE) $(MFLAGS) CC="$(CC)" CFLAGS="$(CFLAGS)"



##  Build the entire system.

all:	config
	cd mpd;		$(RMAKE)
	cd mpdl;	$(RMAKE)
	cd csw;		$(RMAKE)
	cd rts;		$(RMAKE)
	cd library;	$(RMAKE)
	cd mpdm;	$(RMAKE)
	cd mpdprof;	$(RMAKE)
	cd mpdv;	$(RMAKE)	
	cd links;	$(RMAKE)



##  Install everything.

install:  paths.sh	; sh install.sh ${DESTDIR}



##  "config" is a target encompassing shared files used in several subdirs

config:	util.o paths.h paths.sh mpdmulti.h mpdmulti.c

util.o:	gen.h util.h paths.h config.h



##  recopy mpdmulti.h and mpdmulti.c when changed or reconfigured

mpdmulti.h: FORCE
	-cmp -s multi/$(MULTI).h mpdmulti.h || cp multi/$(MULTI).h mpdmulti.h

mpdmulti.c: FORCE
	-cmp -s multi/$(MULTI).c mpdmulti.c || cp multi/$(MULTI).c mpdmulti.c

FORCE:
    


##  rebuild paths.h only when the significant variables REALLY change

paths.h: paths.tmp
	-if cmp -s paths.tmp paths.h; then :; else cp paths.tmp paths.h; fi

paths.tmp: paths.sh
	echo  >paths.tmp '/*'
	echo >>paths.tmp ' *	Created mechanically;  DO NOT EDIT THIS FILE.'
	echo >>paths.tmp ' */'
	sed  >>paths.tmp <paths.sh \
	    -n -e 's/"//g' -e 's/.*/#define &"/' -e 's/= */ "/p'

paths.sh:  Configuration Makefile
	echo "#"						   >paths.sh
	echo "#  Created mechanically;  DO NOT EDIT THIS FILE."	  >>paths.sh
	echo "#"						  >>paths.sh
	sed -n >>paths.sh <Configuration  \
	  -e 's/#.*//' -e 's/[ 	]*$$//' -e 's/ *= */=/' \
	  -e 's/="*\(.* [^"]*\)"*$$/="\1"/' -e '/^[A-Z0-9_]* *=/p'
	echo ""							  >>paths.sh



##  cleanup targets follow.  WARNING: these are VERY aggressive
#
#   sclean: clean up source directories, leaving executables
#   gclean: clean up garbage files, but leave executables and intermediate files
#   xclean: remove executables
#   vclean: clean verification suite
#
#   clean:  clean everything

GARBAGE= '(' -type d -name MPDinter -o \
	-type f '('		\
	   -name '*~'		\
	-o -name '*%'		\
	-o -name '.[BC]K.*'	\
	-o -name '.emacs_*'	\
	-o -name '.nfs*'	\
	-o -name '.purify*'	\
	-o -name '*_pure_*.?'	\
	-o -name '_*.[co]'	\
	-o -name '*.orig'	\
	-o -name '*.org'	\
	-o -name '*.old'	\
	-o -name '*.bak'	\
	-o -name '*.new'	\
	-o -name '*.dif'	\
	-o -name '*.out'	\
	-o -name '*.raw'	\
	-o -name '*.rej'	\
	-o -name '*.tmp'	\
	-o -name '*.v[0-9]*'	\
	-o -name core		\
	-o -name 'x?'		\
	')' ')'

clean:	xclean sclean vclean

xclean:	
	cd links;   $(RMAKE) clean
	rm -f mpd/mpd mpdl/mpdl rts/mpdlib.a rts/mpdx mpdm/mpdm \
		mpdprof/mpdprof mpdv/mpdv mpdv/mpdvi mpdv/mpdvsumm \
		library/*.spec library/*.impl library/*.o
gclean:	
	-find `ls -a | egrep -v '^\.*$$|vsuite'` \
	    -depth $(GARBAGE) -print -exec rm -rf '{}' \;

sclean: gclean
	rm -f paths.tmp paths.h paths.sh util.o mpdmulti.c mpdmulti.h
	cd mpd;     $(RMAKE) clean
	cd mpdl;    $(RMAKE) clean
	cd csw;     $(RMAKE) clean
	cd multi;   $(RMAKE) clean
	cd rts;     $(RMAKE) clean
	cd library; $(RMAKE) clean
	cd mpdm;    $(RMAKE) clean
	cd mpdprof; $(RMAKE) clean
	cd mpdv;    $(RMAKE) clean

vclean:
	rm -f vsuite/diag/*/test[0-9]*.mpd
	rm -f vsuite/io/scanf/*.sout 
	rm -f vsuite/parse/long/*/temp.mpd
	rm -f vsuite/quick/easy/Makefile
	-find vsuite -depth '(' $(GARBAGE) -o \
	    -type f '(' \
	       -name '*.o'			\
	    -o -name '[CL]*er.std' -size 0	\
	    -o -perm 777 -o -perm 775		\
	    -o -perm 755 -o -perm 770		\
	    -o -perm 750 -o -perm 700		\
	    ')' ')' -print -exec rm -rf '{}' \;



#  rebuild Makefile dependencies  (works on Solaris)

depend:	paths.h
	cd mpd;	    $(RMAKE) depend
	cd mpdl;    $(RMAKE) depend
	cd csw;	    $(RMAKE) depend
	cd rts;	    $(RMAKE) depend
	cd mpdm;    $(RMAKE) depend
	cd mpdprof; $(RMAKE) depend
	cd mpdv;    $(RMAKE) depend



#  run lint in all subdirectories
#
#  filtering commands are tuned for cc under SunOS 4.x  (not tried recently)

lint:	paths.h
	lint util.c | tee lint.raw | sed -f lint.sed >lint.out
	cd mpd;	    $(RMAKE) lint
	cd mpdl;    $(RMAKE) lint
	cd csw;	    $(RMAKE) lint
	cd rts;	    $(RMAKE) lint
	cd library; $(RMAKE) lint
	cd mpdm;    $(RMAKE) lint
	cd mpdprof; $(RMAKE) lint
	cd mpdv;    $(RMAKE) lint



#  find files with strange protection modes

prot:
	find * '!' -perm 0775 '!' -perm 0664 '!' -type l -exec ls -ld '{}' \;



#  list verification suite  (note: output is in find order, each dir unsorted)

vlist:
	@find vsuite/* -name Script -print | sed -e s.vsuite/.. -e s./Script..



#  collect *.mpd files from verification suite on standard output

EQ = ====================
vsrc:	; @cd vsuite; find * -name '*.mpd' -print | sort \
	  | while read fname; do echo $(EQ) $$fname $(EQ); cat $$fname; done

#  similarly, collect the scripts

EQ = ====================
vscripts:  ; @cd vsuite; find * -name Script -print | sort | sed s./Script.. \
	   | while read dir; do echo $(EQ) $$dir $(EQ); cat $$dir/Script; done



verify:
	mpdv/mpdv

#  run some distributed tests with mpdv

mpdvdist:
	mpdv/mpdv  quick/vm  examples/misc/mbrot  examples/misc/remote  \
	    create/args2  create/chairs  global/3  global/4  global/5  \
	    literal/cap  misc/timeslice2  vm
