Home | History | Annotate | Line # | Download | only in dist
mkdep revision 1.1.1.5
      1  1.1.1.5  christos #!/bin/sh -e
      2      1.1  christos #
      3      1.1  christos # Copyright (c) 1994, 1996
      4      1.1  christos #	The Regents of the University of California.  All rights reserved.
      5      1.1  christos #
      6      1.1  christos # Redistribution and use in source and binary forms are permitted
      7      1.1  christos # provided that this notice is preserved and that due credit is given
      8      1.1  christos # to the University of California at Berkeley. The name of the University
      9      1.1  christos # may not be used to endorse or promote products derived from this
     10      1.1  christos # software without specific prior written permission. This software
     11      1.1  christos # is provided ``as is'' without express or implied warranty.
     12      1.1  christos #
     13      1.1  christos #	@(#)mkdep.sh	5.11 (Berkeley) 5/5/88
     14      1.1  christos #
     15      1.1  christos 
     16      1.1  christos MAKE=Makefile			# default makefile name is "Makefile"
     17      1.1  christos CC=cc				# default C compiler is "cc"
     18  1.1.1.2  christos DEPENDENCY_CFLAG=-M		# default dependency-generation flag is -M
     19  1.1.1.4  christos SOURCE_DIRECTORY=.		# default source directory is the current directory
     20      1.1  christos 
     21  1.1.1.4  christos # No command-line flags seen yet.
     22  1.1.1.4  christos flags=""
     23      1.1  christos while :
     24      1.1  christos 	do case "$1" in
     25      1.1  christos 		# -c allows you to specify the C compiler
     26      1.1  christos 		-c)
     27      1.1  christos 			CC=$2
     28      1.1  christos 			shift; shift ;;
     29      1.1  christos 
     30      1.1  christos 		# -f allows you to select a makefile name
     31      1.1  christos 		-f)
     32      1.1  christos 			MAKE=$2
     33      1.1  christos 			shift; shift ;;
     34      1.1  christos 
     35  1.1.1.2  christos 		# -m allows you to specify the dependency-generation flag
     36  1.1.1.2  christos 		-m)
     37  1.1.1.2  christos 			DEPENDENCY_CFLAG=$2
     38  1.1.1.2  christos 			shift; shift ;;
     39  1.1.1.2  christos 
     40      1.1  christos 		# the -p flag produces "program: program.c" style dependencies
     41      1.1  christos 		# so .o's don't get produced
     42      1.1  christos 		-p)
     43      1.1  christos 			SED='s;\.o;;'
     44      1.1  christos 			shift ;;
     45  1.1.1.4  christos 
     46  1.1.1.4  christos 		# -s allows you to specify the source directory
     47  1.1.1.4  christos 		-s)
     48  1.1.1.4  christos 			SOURCE_DIRECTORY=$2
     49  1.1.1.4  christos 			shift; shift ;;
     50  1.1.1.4  christos 
     51  1.1.1.4  christos 		# other command-line flag
     52  1.1.1.4  christos 		-*)
     53  1.1.1.4  christos 			flags="$flags $1"
     54  1.1.1.4  christos 			shift ;;
     55  1.1.1.4  christos 
     56      1.1  christos 		*)
     57      1.1  christos 			break ;;
     58      1.1  christos 	esac
     59      1.1  christos done
     60      1.1  christos 
     61      1.1  christos if [ $# = 0 ] ; then
     62  1.1.1.4  christos 	echo 'usage: mkdep [-p] [-c cc] [-f makefile] [-m dependency-cflag] [-s source-directory] [flags] file ...'
     63      1.1  christos 	exit 1
     64      1.1  christos fi
     65      1.1  christos 
     66  1.1.1.5  christos if [ ! -w "$MAKE" ]; then
     67      1.1  christos 	echo "mkdep: no writeable file \"$MAKE\""
     68      1.1  christos 	exit 1
     69      1.1  christos fi
     70      1.1  christos 
     71  1.1.1.5  christos TMP=${TMPDIR:-/tmp}/mkdep$$
     72      1.1  christos 
     73  1.1.1.5  christos trap 'rm -f "$TMP" ; exit 1' HUP INT QUIT PIPE TERM
     74      1.1  christos 
     75  1.1.1.5  christos cp "$MAKE" "${MAKE}.bak"
     76      1.1  christos 
     77  1.1.1.5  christos sed -e '/DO NOT DELETE THIS LINE/,$d' < "$MAKE" > "$TMP"
     78      1.1  christos 
     79  1.1.1.5  christos cat << _EOF_ >> "$TMP"
     80      1.1  christos # DO NOT DELETE THIS LINE -- mkdep uses it.
     81      1.1  christos # DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
     82      1.1  christos 
     83      1.1  christos _EOF_
     84      1.1  christos 
     85      1.1  christos # If your compiler doesn't have -M, add it.  If you can't, the next two
     86      1.1  christos # lines will try and replace the "cc -M".  The real problem is that this
     87      1.1  christos # hack can't deal with anything that requires a search path, and doesn't
     88      1.1  christos # even try for anything using bracket (<>) syntax.
     89      1.1  christos #
     90  1.1.1.4  christos # grep -E '^#include[[:blank:]]*".*"' /dev/null $* |
     91      1.1  christos # sed -e 's/:[^"]*"\([^"]*\)".*/: \1/' -e 's/\.c/.o/' |
     92      1.1  christos 
     93  1.1.1.4  christos #
     94  1.1.1.4  christos # Construct a list of source files with paths relative to the source directory.
     95  1.1.1.4  christos #
     96  1.1.1.4  christos sources=""
     97  1.1.1.5  christos for srcfile in "$@"
     98  1.1.1.4  christos do
     99  1.1.1.4  christos 	sources="$sources $SOURCE_DIRECTORY/$srcfile"
    100  1.1.1.4  christos done
    101  1.1.1.4  christos 
    102      1.1  christos # XXX this doesn't work with things like "-DDECLWAITSTATUS=union\ wait"
    103  1.1.1.5  christos # $flags and $sources are meant to expand
    104  1.1.1.5  christos # shellcheck disable=SC2086
    105  1.1.1.5  christos "$CC" "$DEPENDENCY_CFLAG" $flags $sources |
    106      1.1  christos sed "
    107      1.1  christos 	s; \./; ;g
    108  1.1.1.5  christos 	$SED" >> "$TMP"
    109      1.1  christos 
    110  1.1.1.5  christos cat << _EOF_ >> "$TMP"
    111      1.1  christos 
    112      1.1  christos # IF YOU PUT ANYTHING HERE IT WILL GO AWAY
    113      1.1  christos _EOF_
    114      1.1  christos 
    115      1.1  christos # copy to preserve permissions
    116  1.1.1.5  christos cp "$TMP" "$MAKE"
    117  1.1.1.5  christos rm -f "${MAKE}.bak" "$TMP"
    118      1.1  christos exit 0
    119