xref: /original-bsd/usr.bin/mkdep/mkdep.sh (revision c95cd016)
1#!/bin/sh -
2#
3# Copyright (c) 1987 Regents of the University of California.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms are permitted
7# provided that this notice is preserved and that due credit is given
8# to the University of California at Berkeley. The name of the University
9# may not be used to endorse or promote products derived from this
10# software without specific prior written permission. This software
11# is provided ``as is'' without express or implied warranty.
12#
13#	@(#)mkdep.sh	5.11 (Berkeley) 05/05/88
14#
15
16PATH=/bin:/usr/bin:/usr/ucb
17export PATH
18
19MAKE=Makefile			# default makefile name is "Makefile"
20
21while :
22	do case "$1" in
23		# -f allows you to select a makefile name
24		-f)
25			MAKE=$2
26			shift; shift ;;
27
28		# the -p flag produces "program: program.c" style dependencies
29		# so .o's don't get produced
30		-p)
31			SED='s;\.o;;'
32			shift ;;
33		*)
34			break ;;
35	esac
36done
37
38if [ $# = 0 ] ; then
39	echo 'usage: mkdep [-p] [-f makefile] [flags] file ...'
40	exit 1
41fi
42
43if [ ! -w $MAKE ]; then
44	echo "mkdep: no writeable file \"$MAKE\""
45	exit 1
46fi
47
48TMP=/tmp/mkdep$$
49
50trap 'rm -f $TMP ; exit 1' 1 2 3 13 15
51
52cp $MAKE ${MAKE}.bak
53
54sed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP
55
56cat << _EOF_ >> $TMP
57# DO NOT DELETE THIS LINE -- mkdep uses it.
58# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
59
60_EOF_
61
62# If your compiler doesn't have -M, add it.  If you can't, the next two
63# lines will try and replace the "cc -M".  The real problem is that this
64# hack can't deal with anything that requires a search path, and doesn't
65# even try for anything using bracket (<>) syntax.
66#
67# egrep '^#include[ 	]*".*"' /dev/null $* |
68# sed -e 's/:[^"]*"\([^"]*\)".*/: \1/' -e 's/\.c/.o/' |
69
70cc -M $* |
71sed "
72	s; \./; ;g
73	$SED" |
74awk '{
75	if ($1 != prev) {
76		if (rec != "")
77			print rec;
78		rec = $0;
79		prev = $1;
80	}
81	else {
82		if (length(rec $2) > 78) {
83			print rec;
84			rec = $0;
85		}
86		else
87			rec = rec " " $2
88	}
89}
90END {
91	print rec
92}' >> $TMP
93
94cat << _EOF_ >> $TMP
95
96# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
97_EOF_
98
99# copy to preserve permissions
100cp $TMP $MAKE
101rm -f ${MAKE}.bak $TMP
102exit 0
103