xref: /openbsd/usr.bin/mkdep/mkdep.gcc.sh (revision 404b540a)
1#!/bin/sh -
2#
3#	$OpenBSD: mkdep.gcc.sh,v 1.15 2008/08/28 08:39:44 jmc Exp $
4#	$NetBSD: mkdep.gcc.sh,v 1.9 1994/12/23 07:34:59 jtc Exp $
5#
6# Copyright (c) 1991, 1993
7#	The Regents of the University of California.  All rights reserved.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17# 3. Neither the name of the University nor the names of its contributors
18#    may be used to endorse or promote products derived from this software
19#    without specific prior written permission.
20#
21# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31# SUCH DAMAGE.
32#
33#	@(#)mkdep.gcc.sh	8.1 (Berkeley) 6/6/93
34#
35
36#
37# Scan for a -o option in the arguments and record the filename given.
38# This is needed, since "cc -M -o out" writes to the file "out", not to
39# stdout.
40#
41scanfordasho() {
42	while [ $# != 0 ]
43	do case "$1" in
44		-o)
45			file="$2"; shift; shift ;;
46		-o*)
47			file="${1#-o}"; shift ;;
48		*)
49			shift ;;
50		esac
51	done
52}
53
54D=.depend			# default dependency file is .depend
55append=0
56pflag=
57
58while :
59	do case "$1" in
60		# -a appends to the depend file
61		-a)
62			append=1
63			shift ;;
64
65		# -f allows you to select a makefile name
66		-f)
67			D=$2
68			shift; shift ;;
69
70		# the -p flag produces "program: program.c" style dependencies
71		# so .o's don't get produced
72		-p)
73			pflag=p
74			shift ;;
75		*)
76			break ;;
77	esac
78done
79
80if [ $# = 0 ] ; then
81	echo 'usage: mkdep [-ap] [-f file] [flags] file ...'
82	exit 1
83fi
84
85scanfordasho "$@"
86
87TMP=`mktemp /tmp/mkdep.XXXXXXXXXX` || exit 1
88
89trap 'rm -f $TMP ; trap 2 ; kill -2 $$' 1 2 3 13 15
90
91if [ "x$file" = x ]; then
92	${CC:-cc} -M "$@"
93else
94	${CC:-cc} -M "$@" && cat "$file"
95fi |
96if [ x$pflag = x ]; then
97	sed -e 's; \./; ;g' > $TMP
98else
99	sed -e 's;\.o[ ]*:; :;' -e 's; \./; ;g' > $TMP
100fi
101
102if [ $? != 0 ]; then
103	echo 'mkdep: compile failed.'
104	rm -f $TMP
105	exit 1
106fi
107
108if [ $append = 1 ]; then
109	cat $TMP >> $D
110	if [ $? != 0 ]; then
111		echo 'mkdep: append failed.'
112		rm -f $TMP
113		exit 1
114	fi
115else
116	mv -f $TMP $D
117	if [ $? != 0 ]; then
118		echo 'mkdep: rename failed.'
119		rm -f $TMP
120		exit 1
121	fi
122fi
123
124rm -f $TMP
125exit 0
126