xref: /freebsd/contrib/libpcap/mkdep (revision afdbf109c6a661a729938f68211054a0a50d38ac)
1*afdbf109SJoseph Mingrone#!/bin/sh -e
28cf6c252SPaul Traina#
38cf6c252SPaul Traina# Copyright (c) 1994, 1996
48cf6c252SPaul Traina#	The Regents of the University of California.  All rights reserved.
58cf6c252SPaul Traina#
68cf6c252SPaul Traina# Redistribution and use in source and binary forms are permitted
78cf6c252SPaul Traina# provided that this notice is preserved and that due credit is given
88cf6c252SPaul Traina# to the University of California at Berkeley. The name of the University
98cf6c252SPaul Traina# may not be used to endorse or promote products derived from this
108cf6c252SPaul Traina# software without specific prior written permission. This software
118cf6c252SPaul Traina# is provided ``as is'' without express or implied warranty.
128cf6c252SPaul Traina#
138cf6c252SPaul Traina#	@(#)mkdep.sh	5.11 (Berkeley) 5/5/88
148cf6c252SPaul Traina#
158cf6c252SPaul Traina
168cf6c252SPaul TrainaMAKE=Makefile			# default makefile name is "Makefile"
178cf6c252SPaul TrainaCC=cc				# default C compiler is "cc"
18681ed54cSXin LIDEPENDENCY_CFLAG=-M		# default dependency-generation flag is -M
196f9cba8fSJoseph MingroneSOURCE_DIRECTORY=.		# default source directory is the current directory
208cf6c252SPaul Traina
216f9cba8fSJoseph Mingrone# No command-line flags seen yet.
226f9cba8fSJoseph Mingroneflags=""
238cf6c252SPaul Trainawhile :
248cf6c252SPaul Traina	do case "$1" in
258cf6c252SPaul Traina		# -c allows you to specify the C compiler
268cf6c252SPaul Traina		-c)
278cf6c252SPaul Traina			CC=$2
288cf6c252SPaul Traina			shift; shift ;;
298cf6c252SPaul Traina
308cf6c252SPaul Traina		# -f allows you to select a makefile name
318cf6c252SPaul Traina		-f)
328cf6c252SPaul Traina			MAKE=$2
338cf6c252SPaul Traina			shift; shift ;;
348cf6c252SPaul Traina
35681ed54cSXin LI		# -m allows you to specify the dependency-generation flag
36681ed54cSXin LI		-m)
37681ed54cSXin LI			DEPENDENCY_CFLAG=$2
38681ed54cSXin LI			shift; shift ;;
39681ed54cSXin LI
408cf6c252SPaul Traina		# the -p flag produces "program: program.c" style dependencies
418cf6c252SPaul Traina		# so .o's don't get produced
428cf6c252SPaul Traina		-p)
438cf6c252SPaul Traina			SED='s;\.o;;'
448cf6c252SPaul Traina			shift ;;
456f9cba8fSJoseph Mingrone
466f9cba8fSJoseph Mingrone		# -s allows you to specify the source directory
476f9cba8fSJoseph Mingrone		-s)
486f9cba8fSJoseph Mingrone			SOURCE_DIRECTORY=$2
496f9cba8fSJoseph Mingrone			shift; shift ;;
506f9cba8fSJoseph Mingrone
516f9cba8fSJoseph Mingrone		# -include takes an argument
526f9cba8fSJoseph Mingrone		-include)
536f9cba8fSJoseph Mingrone			flags="$flags $1 $2"
546f9cba8fSJoseph Mingrone			shift; shift ;;
556f9cba8fSJoseph Mingrone
566f9cba8fSJoseph Mingrone		# other command-line flag
576f9cba8fSJoseph Mingrone		-*)
586f9cba8fSJoseph Mingrone			flags="$flags $1"
596f9cba8fSJoseph Mingrone			shift ;;
606f9cba8fSJoseph Mingrone
618cf6c252SPaul Traina		*)
628cf6c252SPaul Traina			break ;;
638cf6c252SPaul Traina	esac
648cf6c252SPaul Trainadone
658cf6c252SPaul Traina
668cf6c252SPaul Trainaif [ $# = 0 ] ; then
676f9cba8fSJoseph Mingrone	echo 'usage: mkdep [-p] [-c cc] [-f makefile] [-m dependency-cflag] [-s source-directory] [flags] file ...'
688cf6c252SPaul Traina	exit 1
698cf6c252SPaul Trainafi
708cf6c252SPaul Traina
71*afdbf109SJoseph Mingroneif [ ! -w "$MAKE" ]; then
728cf6c252SPaul Traina	echo "mkdep: no writeable file \"$MAKE\""
738cf6c252SPaul Traina	exit 1
748cf6c252SPaul Trainafi
758cf6c252SPaul Traina
76*afdbf109SJoseph MingroneTMP=${TMPDIR:-/tmp}/mkdep$$
778cf6c252SPaul Traina
78*afdbf109SJoseph Mingronetrap 'rm -f "$TMP" ; exit 1' HUP INT QUIT PIPE TERM
798cf6c252SPaul Traina
80*afdbf109SJoseph Mingronecp "$MAKE" "${MAKE}.bak"
818cf6c252SPaul Traina
82*afdbf109SJoseph Mingronesed -e '/DO NOT DELETE THIS LINE/,$d' < "$MAKE" > "$TMP"
838cf6c252SPaul Traina
84*afdbf109SJoseph Mingronecat << _EOF_ >> "$TMP"
858cf6c252SPaul Traina# DO NOT DELETE THIS LINE -- mkdep uses it.
868cf6c252SPaul Traina# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
878cf6c252SPaul Traina
888cf6c252SPaul Traina_EOF_
898cf6c252SPaul Traina
908cf6c252SPaul Traina# If your compiler doesn't have -M, add it.  If you can't, the next two
918cf6c252SPaul Traina# lines will try and replace the "cc -M".  The real problem is that this
928cf6c252SPaul Traina# hack can't deal with anything that requires a search path, and doesn't
938cf6c252SPaul Traina# even try for anything using bracket (<>) syntax.
948cf6c252SPaul Traina#
95dd744a89SJoseph Mingrone# grep -E '^#include[[:blank:]]*".*"' /dev/null $* |
968cf6c252SPaul Traina# sed -e 's/:[^"]*"\([^"]*\)".*/: \1/' -e 's/\.c/.o/' |
978cf6c252SPaul Traina
986f9cba8fSJoseph Mingrone#
996f9cba8fSJoseph Mingrone# Construct a list of source files with paths relative to the source directory.
1006f9cba8fSJoseph Mingrone#
1016f9cba8fSJoseph Mingronesources=""
102*afdbf109SJoseph Mingronefor srcfile in "$@"
1036f9cba8fSJoseph Mingronedo
1046f9cba8fSJoseph Mingrone	sources="$sources $SOURCE_DIRECTORY/$srcfile"
1056f9cba8fSJoseph Mingronedone
1066f9cba8fSJoseph Mingrone
1078cf6c252SPaul Traina# XXX this doesn't work with things like "-DDECLWAITSTATUS=union\ wait"
108*afdbf109SJoseph Mingrone# $flags and $sources are meant to expand
109*afdbf109SJoseph Mingrone# shellcheck disable=SC2086
110*afdbf109SJoseph Mingrone"$CC" "$DEPENDENCY_CFLAG" $flags $sources |
1118cf6c252SPaul Trainased "
1128cf6c252SPaul Traina	s; \./; ;g
113*afdbf109SJoseph Mingrone	$SED" >> "$TMP"
1148cf6c252SPaul Traina
115*afdbf109SJoseph Mingronecat << _EOF_ >> "$TMP"
1168cf6c252SPaul Traina
1178cf6c252SPaul Traina# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
1188cf6c252SPaul Traina_EOF_
1198cf6c252SPaul Traina
1208cf6c252SPaul Traina# copy to preserve permissions
121*afdbf109SJoseph Mingronecp "$TMP" "$MAKE"
122*afdbf109SJoseph Mingronerm -f "${MAKE}.bak" "$TMP"
1238cf6c252SPaul Trainaexit 0
124