xref: /freebsd/contrib/libpcap/mkdep (revision 79b015331632b33f22bc8d8fef136c2672f2241f)
1#!/bin/sh -
2#
3# Copyright (c) 1994, 1996
4#	The Regents of the University of California.  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) 5/5/88
14#
15
16PATH=/bin:/usr/bin:/usr/ucb:/usr/local:/usr/local/bin:/usr/sfw/bin
17export PATH
18
19MAKE=Makefile			# default makefile name is "Makefile"
20CC=cc				# default C compiler is "cc"
21DEPENDENCY_CFLAG=-M		# default dependency-generation flag is -M
22
23while :
24	do case "$1" in
25		# -c allows you to specify the C compiler
26		-c)
27			CC=$2
28			shift; shift ;;
29
30		# -f allows you to select a makefile name
31		-f)
32			MAKE=$2
33			shift; shift ;;
34
35		# -m allows you to specify the dependency-generation flag
36		-m)
37			DEPENDENCY_CFLAG=$2
38			shift; shift ;;
39
40		# the -p flag produces "program: program.c" style dependencies
41		# so .o's don't get produced
42		-p)
43			SED='s;\.o;;'
44			shift ;;
45		*)
46			break ;;
47	esac
48done
49
50if [ $# = 0 ] ; then
51	echo 'usage: mkdep [-p] [-c cc] [-f makefile] [-m dependency-cflag] [flags] file ...'
52	exit 1
53fi
54
55if [ ! -w $MAKE ]; then
56	echo "mkdep: no writeable file \"$MAKE\""
57	exit 1
58fi
59
60TMP=/tmp/mkdep$$
61
62trap 'rm -f $TMP ; exit 1' 1 2 3 13 15
63
64cp $MAKE ${MAKE}.bak
65
66sed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP
67
68cat << _EOF_ >> $TMP
69# DO NOT DELETE THIS LINE -- mkdep uses it.
70# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
71
72_EOF_
73
74# If your compiler doesn't have -M, add it.  If you can't, the next two
75# lines will try and replace the "cc -M".  The real problem is that this
76# hack can't deal with anything that requires a search path, and doesn't
77# even try for anything using bracket (<>) syntax.
78#
79# egrep '^#include[ 	]*".*"' /dev/null $* |
80# sed -e 's/:[^"]*"\([^"]*\)".*/: \1/' -e 's/\.c/.o/' |
81
82# XXX this doesn't work with things like "-DDECLWAITSTATUS=union\ wait"
83$CC $DEPENDENCY_CFLAG $* |
84sed "
85	s; \./; ;g
86	$SED" |
87awk '{
88	if ($1 != prev) {
89		if (rec != "")
90			print rec;
91		rec = $0;
92		prev = $1;
93	}
94	else {
95		if (length(rec $2) > 78) {
96			print rec;
97			rec = $0;
98		}
99		else
100			rec = rec " " $2
101	}
102}
103END {
104	print rec
105}' >> $TMP
106
107cat << _EOF_ >> $TMP
108
109# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
110_EOF_
111
112# copy to preserve permissions
113cp $TMP $MAKE
114rm -f ${MAKE}.bak $TMP
115exit 0
116