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