Home | History | Annotate | Line # | Download | only in host-mkdep
host-mkdep.in revision 1.11
      1 #!@BSHELL@ -
      2 #
      3 #	$NetBSD: host-mkdep.in,v 1.11 2003/11/10 17:57:43 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 
     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 USAGE="Usage: $0 [-a] [-d] [-o] [-p] [-q] [-s suffix] [-f makefile] [flags] file ..."
     49 
     50 set_objlist()
     51 {
     52 	if [ -n "$NEWEXT" ]; then
     53 		oifs="$IFS"
     54 		IFS=" ,"
     55 		set -- $NEWEXT
     56 		IFS="$oifs"
     57 		objlist=
     58 		for suf in $*; do
     59 			objlist="$objlist${objlist:+ }$file$suf"
     60 		done
     61 	else
     62 		objlist="$file"
     63 	fi
     64 }
     65 
     66 while [ $# -gt 0 ]; do
     67 	case "$1" in
     68 	-a)	APPEND=true
     69 		shift
     70 		;;
     71 
     72 	-d)	MERGE=true
     73 		shift
     74 		;;
     75 
     76 	-f)	OUTFILE=$2
     77 		shift 2
     78 		;;
     79 
     80 	-o)	OPTIONAL=true
     81 		AWK_OPTIONAL='print ".OPTIONAL:" $0'
     82 		shift
     83 		;;
     84 
     85 	-p)	NEWEXT=
     86 		shift
     87 		;;
     88 
     89 	-q)	QUIET=true
     90 		shift
     91 		;;
     92 
     93 	-s)	NEWEXT="$2"
     94 		shift 2
     95 		;;
     96 
     97 	-*)	$MERGE || break;
     98 		echo "$USAGE" >&2
     99 		exit 1
    100 		;;
    101 
    102 	*)	break
    103 		;;
    104 	esac
    105 done
    106 
    107 if [ $# = 0 ]; then
    108 	echo "$USAGE" 1>&2
    109 	exit 1
    110 fi
    111 
    112 if $MERGE; then
    113 	SRCS="$*"
    114 else
    115 	while [ $# -gt 0 ]; do
    116 		case "$1" in
    117 		-L)			# takes an arg, but ignored
    118 				shift 2
    119 				;;
    120 
    121 		-c|-[lLMOW]*)		# takes no extra args
    122 				shift
    123 				;;
    124 
    125 		-[IDU]*)
    126 				CPPFLAGS="$CPPFLAGS $1"
    127 				shift
    128 				;;
    129 
    130 		-[IDU]|-include|-isystem)
    131 				CPPFLAGS="$CPPFLAGS $1 $2"
    132 				shift 2
    133 				;;
    134 
    135 		-no-cpp-precomp)	# This is a Darwin-specific option.
    136 				CPPFLAGS="$CPPFLAGS $1"
    137 				shift
    138 				;;
    139 
    140 		-nostdinc*)	# This is a gcc/g++ ism; ignore if not gcc/g++
    141 				case "@CFLAGS@" in
    142 				*-O2*)	# Autoconf puts -O2 when gcc only
    143 					CPPFLAGS="$CPPFLAGS $1"
    144 					;;
    145 				esac	
    146 				shift
    147 				;;
    148 
    149 		-*)		
    150 				echo "$0: Unknown option: $1" 1>&2 # all other -options
    151 				exit 1
    152 				;;
    153 
    154 		*)	
    155 				SRCS="$SRCS $1"	# source file
    156 				shift
    157 				;;
    158 		esac
    159 	done
    160 fi
    161 
    162 if [ -z "$SRCS" ]; then
    163 	echo "$USAGE" 1>&2
    164 	exit 1
    165 fi
    166 
    167 TMP=/tmp/mkdep$$
    168 rm -f $TMP
    169 
    170 trap 'rm -f $TMP; exit 1' 1 2 3 13 15
    171 
    172 if $MERGE; then
    173 	for f in $SRCS; do
    174 		if [ ! -f "$f" ]; then
    175 			if ! $QUIET; then echo "$0: Ignoring $f" >&2; fi
    176 			continue
    177 		fi
    178 		while IFS=':'; read target dependants; do
    179 			IFS=
    180 			t1="${target#* }"
    181 			file="${target%.o}"
    182 			if [ "$t" = "$target" -a "$file" != "$target" ]; then
    183 				set_objlist $file
    184 				target="$objlist"
    185 			fi
    186 			echo "$target:$dependants"
    187 			if "$OPTIONAL"; then
    188 				echo ".OPTIONAL:$dependants"
    189 			fi
    190 		done <$f
    191 	done >$TMP
    192 else
    193 	for f in $SRCS; do
    194 		file=${f##*/}
    195 		file=${file%.*}
    196 		set_objlist $file
    197 
    198 		@CPP@ $CPPFLAGS $f | @AWK@ '
    199 			/^#/ {
    200 				# Be as tolerant as possible.
    201 				sub(/^#(line)? [ 0-9]*\"?/, "")
    202 				sub(/^#(pragma).*/, "")
    203 				sub(/^<.*/, "")
    204 				sub(/\".*$/, "")
    205 				sub(/ [ 0-9]*$/, "")
    206 
    207 				if ($0 in seenfiles) next
    208 				if ($0 ~ /y.tab.c/) next
    209 
    210 				seenfiles[$0] = 1
    211 				print "'"$objlist"'" ": " $0
    212 				'"$AWK_OPTIONAL"'
    213 			}
    214 		' >> $TMP
    215 	done
    216 fi
    217 
    218 if $APPEND; then
    219 	cat $TMP >> $OUTFILE
    220 else
    221 	cat $TMP > $OUTFILE
    222 fi
    223 
    224 rm -f $TMP
    225 exit 0
    226