]> git.sur5r.net Git - openldap/blob - build/mkdep
a038e067f77014d1c0b58f57bb214597c1f3cf3f
[openldap] / build / mkdep
1 #! /bin/sh -
2 # $OpenLDAP$
3 ## Copyright 1998-2003 The OpenLDAP Foundation.
4 ## All rights reserved.
5 ##
6 ## Redistribution and use in source and binary forms, with or without
7 ## modification, are permitted only as authorized by the OpenLDAP
8 ## Public License.
9 ##
10 ## A copy of this license is available in the file LICENSE in the
11 ## top-level directory of the distribution or, alternatively, at
12 ## <http://www.OpenLDAP.org/license.html>.
13 #
14 ## Portions Copyright (c) 1987 Regents of the University of California.
15 ## All rights reserved.
16 ##
17 ## Redistribution and use in source and binary forms are permitted
18 ## provided that the above copyright notice and this paragraph are
19 ## duplicated in all such forms and that any documentation,
20 ## advertising materials, and other materials related to such
21 ## distribution and use acknowledge that the software was developed
22 ## by the University of California, Berkeley.  The name of the
23 ## University may not be used to endorse or promote products derived
24 ## from this software without specific prior written permission.
25 ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
26 ## IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
27 ## WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 #
29 #       @(#)mkdep.sh    5.12 (Berkeley) 6/30/88
30 #
31 # We now use whatever path is already set by the invoker
32 #PATH=/bin:/usr/bin:/usr/ucb
33 #export PATH
34
35 set -e                          # exit immediately if any errors occur
36
37 MAKE=Makefile                   # default makefile name is "Makefile"
38 NOSLASH="no"                    # by default, / dependencies are included
39 SRCDIR=""
40 SED=cat
41
42 : ${CC=cc}                                      # use cc by default
43
44 # We generally set these via the command line options
45 : ${MKDEP_CC=$CC}                       # select default compiler to generate dependencies
46 : ${MKDEP_CFLAGS="-M"}  # cc -M usually produces dependencies
47
48 while :
49         do case "$1" in
50                 # the -s flag removes dependencies to files that begin with /
51                 -s)
52                         NOSLASH=yes;
53                         shift ;;
54
55                 # -f allows you to select a makefile name
56                 -f)
57                         MAKE=$2
58                         shift; shift ;;
59
60                 # -d allows you to select a VPATH directory
61                 -d)
62                         SRCDIR=$2
63                         shift; shift ;;
64
65                 # -c allows you to override the compiler used to generate dependencies
66                 -c)
67                         MKDEP_CC=$2
68                         shift; shift ;;
69
70                 # -m allows you to override the compiler flags used to generate
71                 # dependencies.
72                 -m)
73                         MKDEP_CFLAGS=$2
74                         shift; shift ;;
75
76                 # the -p flag produces "program: program.c" style dependencies
77                 # so .o's don't get produced
78                 -p)
79                         SED='sed -e s;\.o;;'
80                         shift ;;
81
82                 # the -l flag produces libtool compatible dependencies
83                 -l)
84                         SED='sed -e s;\.o:;.lo:;'
85                         shift ;;
86
87 #               -*)     shift ;;
88
89                 *)
90                         break ;;
91         esac
92 done
93
94 if test $# = 0 ; then
95         echo 'usage: mkdep [-p] [-s] [-c cc] [-m flags] [-f makefile] [-d srcdir] [cppflags] file ...'
96         exit 1
97 fi
98
99 if test ! -w $MAKE ; then
100         echo "mkdep: no writeable file \"$MAKE\""
101         exit 1
102 fi
103
104 TMP=/tmp/mkdep$$
105
106 trap 'rm -f $TMP.sed $TMP ; exit 1' 1 2 3 13 15
107
108 cp $MAKE ${MAKE}.bak
109
110 sed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP
111
112 cat << _EOF_ >> $TMP
113 # DO NOT DELETE THIS LINE -- mkdep uses it.
114 # DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
115
116 _EOF_
117
118 # If your compiler doesn't have -M, you may be able to use -E instead.
119 # The preprocessor must generate lines of the form
120 #   #.* [0-9]* "dependent file" .*
121 # This script will parse out the "dependent file"s to generate the
122 # dependency list.
123
124 if test "x$SRCDIR" = "x" ; then
125         files=$*
126 else
127         files=
128         for i in $* ; do
129                 if test -f $i ; then
130                         files="$files $i"
131                 elif test -f $SRCDIR/$i ; then
132                         files="$files $SRCDIR/$i"
133                 else
134                         files="$files $i"
135                 fi
136         done
137
138         MKDEP_CFLAGS="$MKDEP_CFLAGS -I$SRCDIR"
139 fi
140
141 cat << _EOF_ >> $TMP
142
143 #
144 # files: $*
145 # command: $MKDEP_CC $MKDEP_CFLAGS $files
146 #
147
148 _EOF_
149
150 case $MKDEP_CFLAGS in
151 # Using regular preprocessor output
152         -E*)
153 FLAGS=""
154 FILES=""
155 for i in $files; do
156         case $i in
157         -*)     FLAGS="$FLAGS $i" ;;
158         *)      FILES="$FILES $i" ;;
159         esac
160 done
161 for i in $FILES; do
162         $MKDEP_CC $MKDEP_CFLAGS $FLAGS $i | egrep '^#.*"' > $TMP.sed
163 awk '
164 BEGIN {
165         file = "'$i'"
166         n = split(file, parts, "/")
167         filenm = substr(parts[n], 0, length(parts[n])-1) "o"
168 }
169 {
170         dep = split($3, parts, "\"")
171         dep = parts[2]
172         if (dep ~ "^\./.*") dep = substr(dep, 3, length(dep)-2)
173         if (( noslash == "yes") && (dep ~ /^\// )) continue
174         if (deps[dep] == 0) printf "%s: %s\n", filenm, dep
175         deps[dep] = 1
176 }' noslash="$NOSLASH" $TMP.sed >> $TMP
177 done
178         ;;
179
180         *)
181 # Using -M or some other specific dependency-generating option
182 $MKDEP_CC $MKDEP_CFLAGS $files | \
183         sed -e 's; \./; ;g' -e 's/ :/:/' | \
184         $SED > $TMP.sed
185 # do not pipe to awk.  SGI awk wants a filename as argument.
186 # (or '-', but I do not know if all other awks support that.)
187 awk '
188 $1 ~ /:$/ {
189         filenm=$1
190         dep=substr($0, length(filenm)+1)
191 }
192 $1 !~ /:$/ {
193         dep=$0
194 }
195 /.*/ {
196         if ( length(filenm) < 2 ) next
197         if ( filenm ~ /:.*:$/ ) next
198         split(dep, depends, " ")
199         for(d in depends) {
200                 dfile = depends[d]
201                 if ( length(dfile) < 2 ) continue
202                 if ( dfile ~ /:/ ) continue
203                 if (( noslash == "yes") && (dfile ~ /^\// )) continue
204                 rec = filenm " " dfile
205                 print rec
206         }
207 }
208 ' noslash="$NOSLASH" $TMP.sed >> $TMP
209         ;;
210 esac
211
212
213 cat << _EOF_ >> $TMP
214
215 # IF YOU PUT ANYTHING HERE IT WILL GO AWAY
216 _EOF_
217
218 # copy to preserve permissions
219 cp $TMP $MAKE
220 rm -f ${MAKE}.bak $TMP.sed $TMP
221 exit 0