xref: /original-bsd/usr.bin/mkdep/mkdep.sh (revision ec35a16d)
1#!/bin/sh -
2#
3# Copyright (c) 1991, 1993
4#	The Regents of the University of California.  All rights reserved.
5#
6# %sccs.include.redist.sh%
7#
8#	@(#)mkdep.sh	8.1 (Berkeley) 06/06/93
9#
10
11PATH=/bin:/usr/bin:/usr/ucb:/usr/old/bin
12export PATH
13
14D=.depend			# default dependency file is .depend
15append=0
16
17while :
18	do case "$1" in
19		# -a appends to the depend file
20		-a)
21			append=1
22			shift ;;
23
24		# -f allows you to select a makefile name
25		-f)
26			D=$2
27			shift; shift ;;
28
29		# the -p flag produces "program: program.c" style dependencies
30		# so .o's don't get produced
31		-p)
32			SED='s;\.o ; ;'
33			shift ;;
34		*)
35			break ;;
36	esac
37done
38
39if [ $# = 0 ] ; then
40	echo 'usage: mkdep [-p] [-f depend_file] [cc_flags] file ...'
41	exit 1
42fi
43
44TMP=/tmp/mkdep$$
45
46trap 'rm -f $TMP ; exit 1' 1 2 3 13 15
47
48cc -M $* |
49sed "
50	s; \./; ;g
51	/\.c:$/d
52	$SED" |
53awk '{
54	if ($1 != prev) {
55		if (rec != "")
56			print rec;
57		rec = $0;
58		prev = $1;
59	}
60	else {
61		if (length(rec $2) > 78) {
62			print rec;
63			rec = $0;
64		}
65		else
66			rec = rec " " $2
67	}
68}
69END {
70	print rec
71}' > $TMP
72
73if [ $? != 0 ]; then
74	echo 'mkdep: compile failed.'
75	rm -f $TMP
76	exit 1
77fi
78
79if [ $append = 1 ]; then
80	cat $TMP >> $D
81	rm -f $TMP
82else
83	mv $TMP $D
84fi
85exit 0
86