[comp.sys.encore] mkdep

rickert@CS.NIU.EDU (Neil Rickert) (06/09/90)

 Its really great to have an 'mkdep' with Umax 4.3

 To test it, I 'cd'ed to a source directory and entered 'make depend'

 Sure enough, it compiled all of my source code, and removed all of the
dependencies from Makefile.

  We really shouldn't need to do it this way, but here's a 'mkdep'
that seems to work:
  (I used to use this with Umax 4.2).

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  mkdep
# Wrapped by rickert@mp.cs.niu.edu on Fri Jun  8 16:30:18 1990
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'mkdep' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'mkdep'\"
else
echo shar: Extracting \"'mkdep'\" \(2902 characters\)
sed "s/^X//" >'mkdep' <<'END_OF_FILE'
X#!/bin/sh -
X#
X# Copyright (c) 1988 The Regents of the University of California.
X# All rights reserved.
X#
X# Redistribution and use in source and binary forms are permitted
X# provided that the above copyright notice and this paragraph are
X# duplicated in all such forms and that any documentation,
X# advertising materials, and other materials related to such
X# distribution and use acknowledge that the software was developed
X# by the University of California, Berkeley.  The name of the
X# University may not be used to endorse or promote products derived
X# from this software without specific prior written permission.
X# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
X# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
X# WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
X#
X#	@(#)mkdep.sh.old.compiler	5.2 (Berkeley) 10/24/88
X#
X
X# This is a version of mkdep that works pretty well
X# with compilers that don't have -M.
X
X# Modified by Neil Rickert, <rickert@cs.niu.edu> Feb 1990, so
X# that it seems to work on Umax 4.2, and supports the 
X#     make [-f Makefile.name] depend
X# format in recent Makefiles.
X#
X
PATH=/bin:/usr/bin:/usr/ucb:/lib:/usr/lib
X
MAKE=Makefile			# default makefile name is "Makefile"
X
INCL=
X
while :
X	do case "$1" in
X		# -f allows you to select a makefile name
X		-f)
X			MAKE=$2
X			shift; shift ;;
X
X		# the -p flag produces "program: program.c" style dependencies
X		# so .o's don't get produced
X		-p)
X			SED='s;\.o;;'
X			shift ;;
X		*)
X			break ;;
X	esac
done
X
if [ $# = 0 ] ; then
X	echo 'usage: mkdep [-f depend_file] [cc_flags] file ...'
X	exit 1
fi
X
if [ ! -w $MAKE ]; then
X	echo "mkdep: no writeable file \"$MAKE\""
X	exit 1
fi
X
TMP=/tmp/mkdep$$
trap 'rm -f $TMP ; exit 1' 1 2 3 13 15
X
cp $MAKE ${MAKE}.bak
X
sed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP
X
cat << _EOF_ >> $TMP
X# DO NOT DELETE THIS LINE -- mkdep uses it.
X# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
X
X_EOF_
X
for i do
X	case "$i" in
X	-c|-M)
X		;;
X	-I*)
X		INCL="$INCL $i";;
X	-D*|-O|-U*)
X		FLAGS="$FLAGS $i";;
X	*)
X		# assume source file
X		# put '$dep' in front of dependencies
X		dep=`echo "$i" | sed -e 's,/,\\\\/,g' -e 's/\.c$/.o/'`
X
X		# Find includes, remove leading numerics, remove ./,
X		# remove double quotes, and remove trailing numerics.
X		# Sort that, discarding duplicates, and add '$dep'.
X		cc -E $INCL $FLAGS "$i" | sed -e '
X			/^#/!d
X			s/# [0-9]* //
X			s,"./,",
X			s/"\(.*\)"/\1/
X			s/ [ 0-9]*$//' |
X		sort -u | sed -e "s/^/$dep: /";;
X	esac
done |
sed "
X	s; \./; ;g
X	/\.c:$/d
X	$SED" |
awk '{
X	if ($1 != prev) {
X		if (rec != "")
X			print rec;
X		rec = $0;
X		prev = $1;
X	}
X	else {
X		if (length(rec $2) > 78) {
X			print rec;
X			rec = $0;
X		}
X		else
X			rec = rec " " $2
X	}
X}
XEND {
X	print rec
X}' >> $TMP
X
cat << _EOF_ >> $TMP
X
X# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
X_EOF_
X
X# copy to preserve permissions
cp $TMP $MAKE
rm -f ${MAKE}.bak $TMP
exit 0
END_OF_FILE
if test 2902 -ne `wc -c <'mkdep'`; then
    echo shar: \"'mkdep'\" unpacked with wrong size!
fi
chmod +x 'mkdep'
# end of 'mkdep'
fi
echo shar: End of shell archive.
exit 0

loverso@Xylogics.COM (John Robert LoVerso) (06/12/90)

Here's a "mkdep" that uses "apply" to run the dependencies in parallel.
Under UMAX4.2, you must have a copy of the 4.3BSD "cpp" around to get
"cpp -M"; under UMAX4.3 you can just use "cc -M".

Actually, under UMAX4.3, you should *really* not "make depend" at all,
but "cc -MD".  This is a clever hack which compiles the file *and*
builds dependencies (into .d files).  You then add the following line
to the end of the link-command:
	cat *.d > .depends
Unfortuneately, UMAX4.3 "make" has not picked up the post-tahoe feature
of automatically reading a ".depends" file for dependencies.  You have to
explicitly use:
	make -f Makefile -f .depends
The advantage to "-MD" is that you don't have to do two passes, and
on a huge source tree (like for the Annex) this is a big win.

John
--

#!/bin/sh -
#
# Mods by John Robert LoVerso:
# Added "-s" to suppress system (/usr/include) dependencies.
# Modified 8/88 to work in parallel via apply.
# Modified 6/90 to only output one dependency tag.
# $Id: mkdep,v 2.3 90/06/11 19:01:35 loverso Rel $
#
# Copyright (c) 1987 Regents of the University of California.
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that the above copyright notice and this paragraph are
# duplicated in all such forms and that any documentation,
# advertising materials, and other materials related to such
# distribution and use acknowledge that the software was developed
# by the University of California, Berkeley.  The name of the
# University may not be used to endorse or promote products derived
# from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
#	@(#)mkdep.sh	5.12 (Berkeley) 6/30/88
#

#PATH=/bin:/usr/bin:/usr/ucb
#export PATH

MAKE=Makefile			# default makefile name is "Makefile"

MAKEDEP='for i do $cpp -M $i; done'

cpp=cpp

# for apply
SHELL=/bin/sh; export SHELL

# use `cc -M' or `cpp -M'
#MAKEDEP='cc -M $OTHERARGS $*'
MAKEDEP='for i do $cpp -M $OTHERARGS $i; done'
OTHERARGS=

while :
	do case "$1" in
		# -f allows you to select a makefile name
		-f)
			MAKE=$2
			shift; shift ;;
		# the -p flag produces "program: program.c" style dependencies
		# so .o's don't get produced
		-p)
			SED='s;\.o;;'
			shift ;;
		# -s disregards system dependencies (i.e., /usr/include)
		-s)
			SYSSED='/\/usr\/include/d'
			shift ;;
		# set PARALLEL
		-#*)
			PARALLEL=`echo $1 | sed -e 's/-#//'`
			export PARALLEL
			shift ;;

		-*)
			OTHERARGS="$OTHERARGS $1"
			shift ;;

		*)
			break ;;
	esac
done

if [ $# = 0 ] ; then
	echo 'usage: mkdep [-sp] [-#n] [-f makefile] [flags] file ...'
	exit 1
fi

if [ ! -w $MAKE ]; then
	echo "mkdep: no writeable file \"$MAKE\""
	exit 1
fi

# Use apply for parallelism
if [ ${PARALLEL-1} -gt 1 ]; then
	MAKEDEP="apply '$cpp -M $OTHERARGS' \$*"
	# this should take less overhead, but might finish asymetrically
	#MAKEDEP="apply -h 'cc -M $OTHERARGS' \$*"
fi

TMP=/tmp/mkdep$$
trap 'rm -f $TMP ; exit 1' 1 2 3 13 15

cp $MAKE ${MAKE}.bak

sed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP

cat << _EOF_ >> $TMP
# DO NOT DELETE THIS LINE -- mkdep uses it.
# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.

_EOF_

# If your compiler doesn't have -M, add it.
# If it doesn't, try the 4.3BSD or GNU cpp (with -M).
# If you can't do that, the next two
# lines will try and replace the "cc -M".  The real problem is that this
# hack can't deal with anything that requires a search path, and doesn't
# even try for anything using bracket (<>) syntax.
#
# egrep '^#include[ 	]*".*"' /dev/null $* |
# sed -e 's/:[^"]*"\([^"]*\)".*/: \1/' -e 's/\.c/.o/' |

eval $MAKEDEP |
sed "
	s; \./; ;g
	:loop
	s;\.\./[^ /]*/\.\.;..;g
	t loop
	$SED
	$SYSSED" |
sort -u |
awk '{
	if ($1 != prev) {
		if (rec != "")
			print rec;
		rec = $0;
		prev = $1;
	}
	else {
# use `> 78', `print rec;' and `rec = $0;' for old-style
		if (length(rec $2) > 76) {
			print rec " \\";
			rec = "  " $2;
		}
		else
			rec = rec " " $2
	}
}
END {
	print rec
}' >> $TMP

cat << _EOF_ >> $TMP

# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
_EOF_

# copy to preserve permissions
cp $TMP $MAKE
rm -f ${MAKE}.bak $TMP
exit 0