]> git.sur5r.net Git - openldap/blob - build/mkdep
53ef2a049f850cbf07d498a01d00657382c1cf05
[openldap] / build / mkdep
1 #! /bin/sh -
2 # Copyright 1998,1999 The OpenLDAP Foundation
3 # COPYING RESTRICTIONS APPLY.  See COPYRIGHT File in top level directory
4 # of this package for details.
5 #
6 # Portions
7 # Copyright (c) 1987 Regents of the University of California.
8 # All rights reserved.
9 #
10 # Redistribution and use in source and binary forms are permitted
11 # provided that the above copyright notice and this paragraph are
12 # duplicated in all such forms and that any documentation,
13 # advertising materials, and other materials related to such
14 # distribution and use acknowledge that the software was developed
15 # by the University of California, Berkeley.  The name of the
16 # University may not be used to endorse or promote products derived
17 # from this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 #
22 #       @(#)mkdep.sh    5.12 (Berkeley) 6/30/88
23 #
24 # We now use whatever path is already set by the invoker
25 #PATH=/bin:/usr/bin:/usr/ucb
26 #export PATH
27
28 set -e                          # exit immediately if any errors occur
29
30 MAKE=Makefile                   # default makefile name is "Makefile"
31 NOSLASH="no"                    # by default, / dependencies are included
32 SRCDIR=""
33 SED=cat
34
35 : ${CC=cc}                                      # use cc by default
36 : ${CC_MKDEP=$CC}                       # select default compiler to generate dependencies
37 : ${CC_MKDEP_FLAGS="-M"}        # cc -M usually produces dependencies
38
39 while :
40         do case "$1" in
41                 # the -s flag removes dependencies to files that begin with /
42                 -s)
43                         NOSLASH=yes;
44                         shift ;;
45
46                 # -f allows you to select a makefile name
47                 -f)
48                         MAKE=$2
49                         shift; shift ;;
50
51                 # -d allows you to select a VPATH directory
52                 -d)
53                         SRCDIR=$2
54                         shift; shift ;;
55
56                 # -c allows you to select a compiler to use to generate
57                 # dependencies.  Leaves $CC alone.
58                 -c)
59                         CC_MKDEP=$2
60                         shift; shift ;;
61
62                 # the -p flag produces "program: program.c" style dependencies
63                 # so .o's don't get produced
64                 -p)
65                         SED='sed -e s;\.o;;'
66                         shift ;;
67
68
69                 # the -l flag produces libtool compatible dependencies
70                 -l)
71                         SED='sed -e s;\.o:;.lo:;'
72                         shift ;;
73
74 #               -*)     shift ;;
75
76                 *)
77                         break ;;
78         esac
79 done
80
81 if test $# = 0 ; then
82         echo 'usage: mkdep [-p] [-s] [-c cc] [-f makefile] [-d srcdir] [flags] file ...'
83         exit 1
84 fi
85
86 if test ! -w $MAKE ; then
87         echo "mkdep: no writeable file \"$MAKE\""
88         exit 1
89 fi
90
91 TMP=/tmp/mkdep$$
92
93 trap 'rm -f $TMP.sed $TMP ; exit 1' 1 2 3 13 15
94
95 cp $MAKE ${MAKE}.bak
96
97 sed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP
98
99 cat << _EOF_ >> $TMP
100 # DO NOT DELETE THIS LINE -- mkdep uses it.
101 # DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.
102
103 _EOF_
104
105 # If your compiler doesn't have -M, add it.  If you can't, the next two
106 # lines will try and replace the "cc -M".  The real problem is that this
107 # hack can't deal with anything that requires a search path, and doesn't
108 # even try for anything using bracket (<>) syntax.
109 #
110 # egrep '^#include[     ]*".*"' /dev/null $* |
111 # sed -e 's/:[^"]*"\([^"]*\)".*/: \1/' -e 's/\.c/.o/' |
112
113 if test "x$SRCDIR" = "x" ; then
114         files=$*
115 else
116         files=
117         for i in $* ; do
118                 if test -f $i ; then
119                         files="$files $i"
120                 elif test -f $SRCDIR/$i ; then
121                         files="$files $SRCDIR/$i"
122                 else
123                         files="$files $i"
124                 fi
125         done
126         CC="$CC -I$SRCDIR"
127 fi
128
129 cat << _EOF_ >> $TMP
130
131 #
132 # files: $*
133 # command: $CC_MKDEP $CC_MKDEP_FLAGS $files
134 #
135
136 _EOF_
137
138 $CC_MKDEP $CC_MKDEP_FLAGS $files | \
139         sed -e 's; \./; ;g' | \
140         $SED > $TMP.sed
141 # dont pipe to awk.  SGI awk wants a filename as argument.
142 # (or '-', but I do not know if all other awks support that.)
143 awk '
144 $1 ~ /:/ {
145         filenm=$1;
146         dep=substr($0, length(filenm)+1);
147 }
148 $1 !~ /:/ {
149         dep=$0;
150 }
151 /.*/ {
152         split(dep, depends, " ");
153         for(d in depends) {
154                 dfile = depends[d];
155                 if (( noslash == "yes") && (dfile ~ /^\// )) next
156                 if ( length(dfile) < 2 ) continue
157                 rec = filenm " " dfile;
158                 print rec;
159         }
160     }
161 ' noslash="$NOSLASH" $TMP.sed >> $TMP
162
163
164 cat << _EOF_ >> $TMP
165
166 # IF YOU PUT ANYTHING HERE IT WILL GO AWAY
167 _EOF_
168
169 # copy to preserve permissions
170 cp $TMP $MAKE
171 rm -f ${MAKE}.bak $TMP.sed $TMP
172 exit 0