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