host-mkdep.in revision 1.18 1 #!@BSHELL@ -
2 #
3 # $NetBSD: host-mkdep.in,v 1.18 2010/02/20 15:04:03 joerg 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
39 APPEND=false
40 MERGE=false
41 OPTIONAL=false
42 AWK_OPTIONAL=
43 QUIET=false
44 CPPFLAGS=
45 NEWEXT=.o
46 OUTFILE=.depend
47 SRCS=
48
49 usage()
50 {
51 echo "Usage: $0 [-adopq] [-s .suffixes] [-f .depend] -- [flags] file ..." >&2
52 exit 1
53 }
54
55 set_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)
73 while [ $# -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
113 done
114
115 [ $# = 0 ] && usage
116
117 if $MERGE; then
118 SRCS="$*"
119 else
120 #
121 # Process argument list.
122 # This is tricky, because arguments may contain spaces and other
123 # escapes characters. The argument list is used like a tail queue.
124 # $cppargs has one x for each unprocessed argument, so when an
125 # argument is processed, it is shifted and the corresponding number
126 # of x's is removed. The advantage to counting is that suffix removal
127 # works without fork.
128 #
129 cppargs=
130 for arg; do
131 cppargs="x$cppargs"
132 done
133 while [ -n "$cppargs" ]; do
134 case "$1" in
135 -L) # takes an arg, but ignored
136 shift 2
137 cppargs=${cppargs%xx}
138 ;;
139
140 -c|-[lLMOW]*) # takes no extra args
141 shift
142 cppargs=${cppargs%x}
143 ;;
144
145 -[IDU]*)
146 set -- "$@" "$1"
147 shift
148 cppargs=${cppargs%x}
149 ;;
150
151 -[IDU]|-include|-isystem)
152 set -- "$@" "$1" "$2"
153 shift 2
154 cppargs=${cppargs%xx}
155 ;;
156
157 -isystem-cxx|-cxx-isystem)
158 set -- "$@" "-isystem" "$2"
159 shift 2
160 cppargs=${cppargs%xx}
161 ;;
162
163 -no-cpp-precomp) # This is a Darwin-specific option.
164 set -- "$@" "$1"
165 shift
166 cppargs=${cppargs%x}
167 ;;
168
169 -nostdinc*) # This is a gcc/g++ ism; ignore if not gcc/g++
170 case "@CFLAGS@" in
171 *-O2*) # Autoconf puts -O2 when gcc only
172 set -- "$@" "$1"
173 ;;
174 esac
175 shift
176 cppargs=${cppargs%x}
177 ;;
178
179 -*)
180 echo "$0: Unknown option: $1" 1>&2 # all other -options
181 exit 1
182 ;;
183
184 *)
185 SRCS="$SRCS $1" # source file
186 shift
187 cppargs=${cppargs%x}
188 ;;
189 esac
190 done
191 fi
192
193 [ -z "$SRCS" ] && usage
194
195 TMP=/tmp/mkdep$$
196 rm -f $TMP
197
198 trap 'rm -f $TMP; exit 1' 1 2 3 13 15
199
200 if $MERGE; then
201 for f in $SRCS; do
202 if [ ! -f "$f" ]; then
203 if ! $QUIET; then echo "$0: Ignoring $f" >&2; fi
204 continue
205 fi
206 while IFS=':'; read target dependants; do
207 IFS=
208 t1="${target#* }"
209 file="${target%.o}"
210 if [ "$t1" = "$target" -a "$file" != "$target" ]; then
211 set_objlist $file
212 target="$objlist"
213 fi
214 echo "$target:$dependants"
215 if "$OPTIONAL"; then
216 echo ".OPTIONAL:$dependants"
217 fi
218 done <$f
219 done >$TMP
220 else
221 for f in $SRCS; do
222 file=${f##*/}
223 file=${file%.*}
224 set_objlist $file
225
226 @CPP@ "$@" $f | @AWK@ '
227 /^#/ {
228 # Be as tolerant as possible.
229 sub(/^#(line)? [ 0-9]*\"?/, "")
230 sub(/^#(pragma).*/, "")
231 sub(/^<.*/, "")
232 sub(/\".*$/, "")
233 sub(/ [ 0-9]*$/, "")
234
235 if ($0 in seenfiles) next
236 if ($0 ~ /y.tab.c/) next
237
238 seenfiles[$0] = 1
239 print "'"$objlist"'" ": " $0
240 '"$AWK_OPTIONAL"'
241 }
242 ' >> $TMP
243 done
244 fi
245
246 if $APPEND; then
247 cat $TMP >> $OUTFILE
248 else
249 cat $TMP > $OUTFILE
250 fi
251
252 rm -f $TMP
253 exit 0
254