blob: 6e8dbd6ffa56838ce18871f442544e5cca11952d [file] [log] [blame]
The Android Open Source Project478ab6c2009-03-03 19:30:05 -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 Project478ab6c2009-03-03 19:30:05 -080016MAKE=Makefile # default makefile name is "Makefile"
17CC=cc # default C compiler is "cc"
JP Abgrall511eca32014-02-12 13:46:45 -080018DEPENDENCY_CFLAG=-M # default dependency-generation flag is -M
Haibo Huangee759ce2021-01-05 21:34:29 -080019SOURCE_DIRECTORY=. # default source directory is the current directory
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080020
Haibo Huangee759ce2021-01-05 21:34:29 -080021# No command-line flags seen yet.
22flags=""
The Android Open Source Project478ab6c2009-03-03 19:30:05 -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 Abgrall511eca32014-02-12 13:46:45 -080035 # -m allows you to specify the dependency-generation flag
36 -m)
37 DEPENDENCY_CFLAG=$2
38 shift; shift ;;
39
The Android Open Source Project478ab6c2009-03-03 19:30:05 -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 ;;
Haibo Huangee759ce2021-01-05 21:34:29 -080045
46 # -s allows you to specify the source directory
47 -s)
48 SOURCE_DIRECTORY=$2
49 shift; shift ;;
50
51 # -include takes an argument
52 -include)
53 flags="$flags $1 $2"
54 shift; shift ;;
55
56 # other command-line flag
57 -*)
58 flags="$flags $1"
59 shift ;;
60
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080061 *)
62 break ;;
63 esac
64done
65
66if [ $# = 0 ] ; then
Haibo Huangee759ce2021-01-05 21:34:29 -080067 echo 'usage: mkdep [-p] [-c cc] [-f makefile] [-m dependency-cflag] [-s source-directory] [flags] file ...'
The Android Open Source Project478ab6c2009-03-03 19:30:05 -080068 exit 1
69fi
70
71if [ ! -w $MAKE ]; then
72 echo "mkdep: no writeable file \"$MAKE\""
73 exit 1
74fi
75
76TMP=/tmp/mkdep$$
77
78trap 'rm -f $TMP ; exit 1' 1 2 3 13 15
79
80cp $MAKE ${MAKE}.bak
81
82sed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP
83
84cat << _EOF_ >> $TMP
85# DO NOT DELETE THIS LINE -- mkdep uses it.
86# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
87
88_EOF_
89
90# If your compiler doesn't have -M, add it. If you can't, the next two
91# lines will try and replace the "cc -M". The real problem is that this
92# hack can't deal with anything that requires a search path, and doesn't
93# even try for anything using bracket (<>) syntax.
94#
95# egrep '^#include[ ]*".*"' /dev/null $* |
96# sed -e 's/:[^"]*"\([^"]*\)".*/: \1/' -e 's/\.c/.o/' |
97
Haibo Huangee759ce2021-01-05 21:34:29 -080098#
99# Construct a list of source files with paths relative to the source directory.
100#
101sources=""
102for srcfile in $*
103do
104 sources="$sources $SOURCE_DIRECTORY/$srcfile"
105done
106
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800107# XXX this doesn't work with things like "-DDECLWAITSTATUS=union\ wait"
Haibo Huangee759ce2021-01-05 21:34:29 -0800108$CC $DEPENDENCY_CFLAG $flags $sources |
The Android Open Source Project478ab6c2009-03-03 19:30:05 -0800109sed "
110 s; \./; ;g
111 $SED" |
112awk '{
113 if ($1 != prev) {
114 if (rec != "")
115 print rec;
116 rec = $0;
117 prev = $1;
118 }
119 else {
120 if (length(rec $2) > 78) {
121 print rec;
122 rec = $0;
123 }
124 else
125 rec = rec " " $2
126 }
127}
128END {
129 print rec
130}' >> $TMP
131
132cat << _EOF_ >> $TMP
133
134# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
135_EOF_
136
137# copy to preserve permissions
138cp $TMP $MAKE
139rm -f ${MAKE}.bak $TMP
140exit 0