xref: /freebsd/usr.bin/mkdep/mkdep.sh (revision a35f04fba2ebb8f86d4cbdc710c89a094572b08e)
1#!/bin/sh -
2#
3# Copyright (c) 1991, 1993
4#	The Regents of the University of California.  All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14# 3. Neither the name of the University nor the names of its contributors
15#    may be used to endorse or promote products derived from this software
16#    without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28# SUCH DAMAGE.
29#
30# $FreeBSD$
31#
32#	@(#)mkdep.sh	8.1 (Berkeley) 6/6/93
33#
34
35PATH=/bin:/usr/bin:/usr/ucb:/usr/old/bin
36export PATH
37
38D=.depend			# default dependency file is .depend
39append=0
40
41while :
42	do case "$1" in
43		# -a appends to the depend file
44		-a)
45			append=1
46			shift ;;
47
48		# -f allows you to select a makefile name
49		-f)
50			D=$2
51			shift; shift ;;
52
53		# the -p flag produces "program: program.c" style dependencies
54		# so .o's don't get produced
55		-p)
56			SED='s;\.o ; ;'
57			shift ;;
58		*)
59			break ;;
60	esac
61done
62
63if [ $# = 0 ] ; then
64	echo 'usage: mkdep [-p] [-f depend_file] [cc_flags] file ...'
65	exit 1
66fi
67
68TMP=/tmp/mkdep$$
69
70trap 'rm -f $TMP ; trap 2 ; kill -2 $$' 1 2 3 13 15
71
72cc -M $* |
73sed "
74	s; \./; ;g
75	/\.c:$/d
76	$SED" |
77awk '{
78	if ($1 != prev) {
79		if (rec != "")
80			print rec;
81		rec = $0;
82		prev = $1;
83	}
84	else {
85		if (length(rec $2) > 78) {
86			print rec;
87			rec = $0;
88		}
89		else
90			rec = rec " " $2
91	}
92}
93END {
94	print rec
95}' > $TMP
96
97if [ $? != 0 ]; then
98	echo 'mkdep: compile failed.'
99	rm -f $TMP
100	exit 1
101fi
102
103if [ $append = 1 ]; then
104	cat $TMP >> $D
105	rm -f $TMP
106else
107	mv $TMP $D
108fi
109exit 0
110