xref: /original-bsd/usr.bin/mkdep/mkdep.sh (revision a76afa45)
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 the above copyright notice and this paragraph are
8# duplicated in all such forms and that any documentation,
9# advertising materials, and other materials related to such
10# distribution and use acknowledge that the software was developed
11# by the University of California, Berkeley.  The name of the
12# University may not be used to endorse or promote products derived
13# from this software without specific prior written permission.
14# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16# WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17#
18#	@(#)mkdep.sh	5.18 (Berkeley) 03/05/89
19#
20PATH=/bin:/usr/bin:/usr/ucb
21export PATH
22
23D=.depend			# default dependency file is .depend
24append=0
25
26while :
27	do case "$1" in
28		# -a appends to the depend file
29		-a)
30			append=1
31			shift ;;
32
33		# -f allows you to select a makefile name
34		-f)
35			D=$2
36			shift; shift ;;
37
38		# the -p flag produces "program: program.c" style dependencies
39		# so .o's don't get produced
40		-p)
41			SED='s;\.o;;'
42			shift ;;
43		*)
44			break ;;
45	esac
46done
47
48if [ $# = 0 ] ; then
49	echo 'usage: mkdep [-p] [-f depend_file] [cc_flags] file ...'
50	exit 1
51fi
52
53TMP=/tmp/mkdep$$
54
55trap 'rm -f $TMP ; exit 1' 1 2 3 13 15
56
57cc -M $* |
58sed "
59	s; \./; ;g
60	/\.c:$/d
61	$SED" |
62awk '{
63	if ($1 != prev) {
64		if (rec != "")
65			print rec;
66		rec = $0;
67		prev = $1;
68	}
69	else {
70		if (length(rec $2) > 78) {
71			print rec;
72			rec = $0;
73		}
74		else
75			rec = rec " " $2
76	}
77}
78END {
79	print rec
80}' > $TMP
81
82if [ $? != 0 ]; then
83	echo 'mkdep: compile failed.'
84	rm -f $TMP
85	exit 1
86fi
87
88if [ $append = 1 ]; then
89	cat $TMP >> $D
90	rm -f $TMP
91else
92	mv $TMP $D
93fi
94exit 0
95