blob: 656ca099fdbbc91b43385d028aaa288ce44d59bc [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001#!/bin/sh -
2#
3# Copyright (c) 1994, 1996
4# The Regents of the University of California. All rights reserved.
5#
6# Redistribution and use in source and binary forms are permitted
7# provided that this notice is preserved and that due credit is given
8# to the University of California at Berkeley. The name of the University
9# may not be used to endorse or promote products derived from this
10# software without specific prior written permission. This software
11# is provided ``as is'' without express or implied warranty.
12#
13# @(#)mkdep.sh 5.11 (Berkeley) 5/5/88
14#
15
The Android Open Source Project2949f582009-03-03 19:30:46 -080016MAKE=Makefile # default makefile name is "Makefile"
17CC=cc # default C compiler is "cc"
JP Abgrall53f17a92014-02-12 14:02:41 -080018DEPENDENCY_CFLAG=-M # default dependency-generation flag is -M
Elliott Hughes820eced2021-08-20 18:00:50 -070019SOURCE_DIRECTORY=. # default source directory is the current directory
The Android Open Source Project2949f582009-03-03 19:30:46 -080020
Elliott Hughes820eced2021-08-20 18:00:50 -070021# No command-line flags seen yet.
22flags=""
The Android Open Source Project2949f582009-03-03 19:30:46 -080023while :
24 do case "$1" in
25 # -c allows you to specify the C compiler
26 -c)
27 CC=$2
28 shift; shift ;;
29
30 # -f allows you to select a makefile name
31 -f)
32 MAKE=$2
33 shift; shift ;;
34
JP Abgrall53f17a92014-02-12 14:02:41 -080035 # -m allows you to specify the dependency-generation flag
36 -m)
37 DEPENDENCY_CFLAG=$2
38 shift; shift ;;
39
The Android Open Source Project2949f582009-03-03 19:30:46 -080040 # the -p flag produces "program: program.c" style dependencies
41 # so .o's don't get produced
42 -p)
43 SED='s;\.o;;'
44 shift ;;
Elliott Hughes820eced2021-08-20 18:00:50 -070045
46 # -s allows you to specify the source directory
47 -s)
48 SOURCE_DIRECTORY=$2
49 shift; shift ;;
50
51 # other command-line flag
52 -*)
53 flags="$flags $1"
54 shift ;;
55
The Android Open Source Project2949f582009-03-03 19:30:46 -080056 *)
57 break ;;
58 esac
59done
60
61if [ $# = 0 ] ; then
Elliott Hughes820eced2021-08-20 18:00:50 -070062 echo 'usage: mkdep [-p] [-c cc] [-f makefile] [-m dependency-cflag] [-s source-directory] [flags] file ...'
The Android Open Source Project2949f582009-03-03 19:30:46 -080063 exit 1
64fi
65
66if [ ! -w $MAKE ]; then
67 echo "mkdep: no writeable file \"$MAKE\""
68 exit 1
69fi
70
71TMP=/tmp/mkdep$$
72
73trap 'rm -f $TMP ; exit 1' 1 2 3 13 15
74
75cp $MAKE ${MAKE}.bak
76
77sed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP
78
79cat << _EOF_ >> $TMP
80# DO NOT DELETE THIS LINE -- mkdep uses it.
81# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
82
83_EOF_
84
85# If your compiler doesn't have -M, add it. If you can't, the next two
86# lines will try and replace the "cc -M". The real problem is that this
87# hack can't deal with anything that requires a search path, and doesn't
88# even try for anything using bracket (<>) syntax.
89#
90# egrep '^#include[ ]*".*"' /dev/null $* |
91# sed -e 's/:[^"]*"\([^"]*\)".*/: \1/' -e 's/\.c/.o/' |
92
Elliott Hughes820eced2021-08-20 18:00:50 -070093#
94# Construct a list of source files with paths relative to the source directory.
95#
96sources=""
97for srcfile in $*
98do
99 sources="$sources $SOURCE_DIRECTORY/$srcfile"
100done
101
The Android Open Source Project2949f582009-03-03 19:30:46 -0800102# XXX this doesn't work with things like "-DDECLWAITSTATUS=union\ wait"
Elliott Hughes820eced2021-08-20 18:00:50 -0700103$CC $DEPENDENCY_CFLAG $flags $sources |
The Android Open Source Project2949f582009-03-03 19:30:46 -0800104sed "
105 s; \./; ;g
106 $SED" |
107awk '{
108 if ($1 != prev) {
109 if (rec != "")
110 print rec;
111 rec = $0;
112 prev = $1;
113 }
114 else {
115 if (length(rec $2) > 78) {
116 print rec;
117 rec = $0;
118 }
119 else
120 rec = rec " " $2
121 }
122}
123END {
124 print rec
125}' >> $TMP
126
127cat << _EOF_ >> $TMP
128
129# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
130_EOF_
131
132# copy to preserve permissions
133cp $TMP $MAKE
134rm -f ${MAKE}.bak $TMP
135exit 0