host-mkdep.in revision 1.13
1#!@BSHELL@ -
2#
3#	$NetBSD: host-mkdep.in,v 1.13 2003/12/07 20:19:15 dsl Exp $
4#
5# Copyright (c) 1991, 1993
6#	The Regents of the University of California.  All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16# 3. All advertising materials mentioning features or use of this software
17#    must display the following acknowledgement:
18#	This product includes software developed by the University of
19#	California, Berkeley and its contributors.
20# 4. Neither the name of the University nor the names of its contributors
21#    may be used to endorse or promote products derived from this software
22#    without specific prior written permission.
23#
24# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34# SUCH DAMAGE.
35#
36#	@(#)mkdep.old.compiler	8.1 (Berkeley) 6/6/93
37#
38
39APPEND=false
40MERGE=false
41OPTIONAL=false
42AWK_OPTIONAL=
43QUIET=false
44CPPFLAGS=
45NEWEXT=.o
46OUTFILE=.depend
47SRCS=
48
49usage()
50{
51	echo "Usage: $0 [-adopq] [-s .suffixes] [-f .depend] -- [flags] file ..." >&2
52	exit 1
53}
54
55set_objlist()
56{
57	if [ -n "$NEWEXT" ]; then
58		oifs="$IFS"
59		IFS=" ,"
60		set -- $NEWEXT
61		IFS="$oifs"
62		objlist=
63		for suf in $*; do
64			objlist="$objlist${objlist:+ }$file$suf"
65		done
66	else
67		objlist="$file"
68	fi
69}
70
71# A getopt compatible command line parser in shell comands.
72# (don't trust the shell builtin getopts to be in a known state on error)
73while [ $# -gt 0 ]; do
74	option="${1#-}"
75	[ "$option" = "$1" -o -z "$option" ] && break
76	while
77		optarg="${option#?}"
78		option="${option%$optarg}"
79
80		case "-$option" in
81		-a)	APPEND=true;;
82		-d)	MERGE=true;;
83		-o)	OPTIONAL=true; AWK_OPTIONAL='print ".OPTIONAL:" $0';;
84		-p)	NEWEXT=;;
85		-q)	QUIET=true;;
86
87		-[fs])	# Options with arguments
88			[ -z "$optarg" ] && {
89				[ $# = 1 ] && usage
90				shift
91				optarg="$1"
92			}
93			case "-$option" in
94			-f)	OUTFILE="$optarg";;
95			-s)	NEWEXT="$optarg";;
96			esac
97			optarg=
98			;;
99
100		--)	[ -z "$optarg" ] && shift
101			break 2
102			;;
103
104		*)	$MERGE && usage
105			break 2;
106			;;
107		esac
108		[ -n "$optarg" ]
109	do
110		option="$optarg"
111	done
112	shift
113done
114
115[ $# = 0 ] && usage
116
117if $MERGE; then
118	SRCS="$*"
119else
120	while [ $# -gt 0 ]; do
121		case "$1" in
122		-L)			# takes an arg, but ignored
123				shift 2
124				;;
125
126		-c|-[lLMOW]*)		# takes no extra args
127				shift
128				;;
129
130		-[IDU]*)
131				CPPFLAGS="$CPPFLAGS $1"
132				shift
133				;;
134
135		-[IDU]|-include|-isystem)
136				CPPFLAGS="$CPPFLAGS $1 $2"
137				shift 2
138				;;
139
140		-no-cpp-precomp)	# This is a Darwin-specific option.
141				CPPFLAGS="$CPPFLAGS $1"
142				shift
143				;;
144
145		-nostdinc*)	# This is a gcc/g++ ism; ignore if not gcc/g++
146				case "@CFLAGS@" in
147				*-O2*)	# Autoconf puts -O2 when gcc only
148					CPPFLAGS="$CPPFLAGS $1"
149					;;
150				esac	
151				shift
152				;;
153
154		-*)		
155				echo "$0: Unknown option: $1" 1>&2 # all other -options
156				exit 1
157				;;
158
159		*)	
160				SRCS="$SRCS $1"	# source file
161				shift
162				;;
163		esac
164	done
165fi
166
167[ -z "$SRCS" ] && usage
168
169TMP=/tmp/mkdep$$
170rm -f $TMP
171
172trap 'rm -f $TMP; exit 1' 1 2 3 13 15
173
174if $MERGE; then
175	for f in $SRCS; do
176		if [ ! -f "$f" ]; then
177			if ! $QUIET; then echo "$0: Ignoring $f" >&2; fi
178			continue
179		fi
180		while IFS=':'; read target dependants; do
181			IFS=
182			t1="${target#* }"
183			file="${target%.o}"
184			if [ "$t" = "$target" -a "$file" != "$target" ]; then
185				set_objlist $file
186				target="$objlist"
187			fi
188			echo "$target:$dependants"
189			if "$OPTIONAL"; then
190				echo ".OPTIONAL:$dependants"
191			fi
192		done <$f
193	done >$TMP
194else
195	for f in $SRCS; do
196		file=${f##*/}
197		file=${file%.*}
198		set_objlist $file
199
200		@CPP@ $CPPFLAGS $f | @AWK@ '
201			/^#/ {
202				# Be as tolerant as possible.
203				sub(/^#(line)? [ 0-9]*\"?/, "")
204				sub(/^#(pragma).*/, "")
205				sub(/^<.*/, "")
206				sub(/\".*$/, "")
207				sub(/ [ 0-9]*$/, "")
208
209				if ($0 in seenfiles) next
210				if ($0 ~ /y.tab.c/) next
211
212				seenfiles[$0] = 1
213				print "'"$objlist"'" ": " $0
214				'"$AWK_OPTIONAL"'
215			}
216		' >> $TMP
217	done
218fi
219
220if $APPEND; then
221	cat $TMP >> $OUTFILE
222else
223	cat $TMP > $OUTFILE
224fi
225
226rm -f $TMP
227exit 0
228