xref: /netbsd-src/tools/host-mkdep/host-mkdep.in (revision 5b84b3983f71fd20a534cfa5d1556623a8aaa717)
1#!@BSHELL@ -
2#
3#	$NetBSD: host-mkdep.in,v 1.16 2005/09/01 21:51:11 jwise 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		-isystem-cxx)
141				CPPFLAGS="$CPPFLAGS -isystem $2"
142				shift 2
143				;;
144
145		-no-cpp-precomp)	# This is a Darwin-specific option.
146				CPPFLAGS="$CPPFLAGS $1"
147				shift
148				;;
149
150		-nostdinc*)	# This is a gcc/g++ ism; ignore if not gcc/g++
151				case "@CFLAGS@" in
152				*-O2*)	# Autoconf puts -O2 when gcc only
153					CPPFLAGS="$CPPFLAGS $1"
154					;;
155				esac
156				shift
157				;;
158
159		-*)
160				echo "$0: Unknown option: $1" 1>&2 # all other -options
161				exit 1
162				;;
163
164		*)
165				SRCS="$SRCS $1"	# source file
166				shift
167				;;
168		esac
169	done
170fi
171
172[ -z "$SRCS" ] && usage
173
174TMP=/tmp/mkdep$$
175rm -f $TMP
176
177trap 'rm -f $TMP; exit 1' 1 2 3 13 15
178
179if $MERGE; then
180	for f in $SRCS; do
181		if [ ! -f "$f" ]; then
182			if ! $QUIET; then echo "$0: Ignoring $f" >&2; fi
183			continue
184		fi
185		while IFS=':'; read target dependants; do
186			IFS=
187			t1="${target#* }"
188			file="${target%.o}"
189			if [ "$t1" = "$target" -a "$file" != "$target" ]; then
190				set_objlist $file
191				target="$objlist"
192			fi
193			echo "$target:$dependants"
194			if "$OPTIONAL"; then
195				echo ".OPTIONAL:$dependants"
196			fi
197		done <$f
198	done >$TMP
199else
200	for f in $SRCS; do
201		file=${f##*/}
202		file=${file%.*}
203		set_objlist $file
204
205		@CPP@ $CPPFLAGS $f | @AWK@ '
206			/^#/ {
207				# Be as tolerant as possible.
208				sub(/^#(line)? [ 0-9]*\"?/, "")
209				sub(/^#(pragma).*/, "")
210				sub(/^<.*/, "")
211				sub(/\".*$/, "")
212				sub(/ [ 0-9]*$/, "")
213
214				if ($0 in seenfiles) next
215				if ($0 ~ /y.tab.c/) next
216
217				seenfiles[$0] = 1
218				print "'"$objlist"'" ": " $0
219				'"$AWK_OPTIONAL"'
220			}
221		' >> $TMP
222	done
223fi
224
225if $APPEND; then
226	cat $TMP >> $OUTFILE
227else
228	cat $TMP > $OUTFILE
229fi
230
231rm -f $TMP
232exit 0
233