This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[UTIL] Perl system does not default to $_
[metaconfig.git] / dist / U / mkdep.U
1 ?RCS: $Id: mkdep.U 1 2006-08-24 12:32:52Z rmanfredi $
2 ?RCS:
3 ?RCS: Copyright (c) 1991-1997, 2004-2006, Raphael Manfredi
4 ?RCS: 
5 ?RCS: You may redistribute only under the terms of the Artistic Licence,
6 ?RCS: as specified in the README file that comes with the distribution.
7 ?RCS: You may reuse parts of this distribution only within the terms of
8 ?RCS: that same Artistic Licence; a copy of which may be found at the root
9 ?RCS: of the source tree for dist 4.0.
10 ?RCS:
11 ?RCS: $Log: mkdep.U,v $
12 ?RCS: Revision 3.0.1.4  1997/02/28  16:13:07  ram
13 ?RCS: patch61: replaced .o with $_o all over the place
14 ?RCS:
15 ?RCS: Revision 3.0.1.3  1994/10/29  16:25:02  ram
16 ?RCS: patch36: added ?F: line for metalint file checking
17 ?RCS:
18 ?RCS: Revision 3.0.1.2  1994/01/24  14:15:12  ram
19 ?RCS: patch16: cppflags is now an optional dependency
20 ?RCS:
21 ?RCS: Revision 3.0.1.1  1993/12/15  08:22:39  ram
22 ?RCS: patch15: force zero exit status in all mkdep scripts
23 ?RCS:
24 ?RCS: Revision 3.0  1993/08/18  12:09:16  ram
25 ?RCS: Baseline for dist 3.0 netwide release.
26 ?RCS:
27 ?X:
28 ?X: This unit generates the script 'mkdep' in the top-level directory.
29 ?X:     It is up to the makefile to use it for automatic dependencies
30 ?X: generation (usage: 'mkdep [cppflags] -- *.c').
31 ?X:
32 ?MAKE:mkdep: spitshell startsh cpp +cppflags grep contains rm sed sort uniq \
33         Getfile Oldconfig eunicefix cat +cc cppminus test pkgsrc _o
34 ?MAKE:  -pick add $@ %<
35 ?S:mkdep:
36 ?S:     This variable holds the name of a command to generate makefile
37 ?S:     dependencies on the standard output. It is the pathname of the
38 ?S:     generated mkdep script.
39 ?S:.
40 ?F:mkdep
41 ?T:IFS arg flags takeflags srcfile toplev filebase inc dir files file dep c
42 : find out how to generate dependencies
43 echo " "
44 echo "Checking how to generate makefile dependencies on your machine..." >&4
45 ?X: We are in the UU directory
46 toplev=`cd ..;pwd`
47 $cat >dep.c <<'EOCP'
48 #include "dep.h"
49 EOCP
50 ?X: Empty dep.h causes RIOS to barf
51 $cat >dep.h <<'EOCP'
52
53 EOCP
54 takeflags='flags=""
55 case "$@" in
56 *--*)
57         for arg
58         do
59                 shift
60                 case "$arg" in
61                 --) break;;
62                 *) flags="$flags $arg";;
63                 esac
64         done;;
65 esac'
66 case "$mkdep" in
67 '')
68         ;;
69 *)
70         if test -f "$mkdep" &&
71                 $mkdep dep.c >dep.out 2>/dev/null &&
72                 $contains "dep$_o:.*dep\.h" dep.out >/dev/null 2>&1
73         then
74                 echo "$mkdep works."
75 ?X: If they copied a config.sh from some other place, they'll have a working
76 ?X: mkdep script probably, but not located at the top of the sources.  And
77 ?X: the Makefiles generated by jmake expect an mkdep script in the top dir.
78                 case "$mkdep" in
79                 "$pkgsrc/mkdep");;
80                 *)      echo "(copying it to $pkgsrc)"
81                         cp $mkdep $pkgsrc/mkdep 2>/dev/null
82                         if $pkgsrc/mkdep dep.c >dep.out 2>/dev/null &&
83                                 $contains "dep$_o:.*dep\.h" dep.out >/dev/null 2>&1; then
84                                 mkdep=$pkgsrc/mkdep
85                         else
86                                 echo "Hmm... The copy failed or something... Guessing again..."
87                                 mkdep=
88                         fi
89                         ;;
90                 esac
91         else
92                 mkdep=
93         fi
94 esac
95
96 case "$mkdep" in
97 '')
98 ?X: have to figure something out: Try cpp
99 ?X: have to pass source names one by one to cpp...
100         $spitshell > ../mkdep <<EOM
101 $startsh
102 $takeflags
103 for srcfile
104 do
105         $cpp -M $cppflags \$flags \$srcfile 2>/dev/null
106 done
107 exit 0
108 EOM
109         mkdep=$toplev/mkdep
110         chmod +x $mkdep
111         $eunicefix $mkdep
112         if $mkdep dep.c >dep.out 2>/dev/null &&
113                 $contains "dep$_o:.*dep\.h" dep.out >/dev/null 2>&1
114         then
115                 echo "Looks like we can use $cpp -M."
116         else
117                 mkdep=
118         fi
119         ;;
120 esac
121
122 case "$mkdep" in
123 '')
124 ?X: have to figure something out: on NeXT, use cc -MM
125 ?X: have to pass source names one by one to cc...
126         $spitshell > ../mkdep <<EOM
127 $startsh
128 $takeflags
129 for srcfile
130 do
131         $cc -MM $cppflags \$flags \$srcfile 2>/dev/null
132 done
133 exit 0
134 EOM
135         mkdep=$toplev/mkdep
136         chmod +x $mkdep
137         $eunicefix $mkdep
138         if $mkdep dep.c >dep.out 2>/dev/null &&
139                 $contains "dep$_o: dep.h" dep.out >/dev/null 2>&1
140         then
141                 echo "Looks like we can use $cc -MM."
142         else
143                 mkdep=
144         fi
145         ;;
146 esac
147
148 case "$mkdep" in
149 '')
150 ?X: still no luck -- try something fancier.
151         $spitshell >../mkdep <<EOS
152 $startsh
153 $takeflags
154 for srcfile
155 do
156         case "\$srcfile" in
157         *.c) c='.c';;
158         *.y) c='.y';;
159         *.l) c='.l';;
160         esac
161         filebase=\`basename \$srcfile \$c\`
162         <\$srcfile $cpp $cppminus $cppflags \$flags 2>/dev/null | \\
163         $sed -e '/^# *[0-9]/!d' \\
164                 -e 's/^.*"\(.*\)".*\$/'\$filebase'$_o: \1/' \\
165                 -e 's|: \./|: |' \\
166                 -e 's|: *$|: '\$srcfile'|' | \\
167         $grep -v '^#' | $sort | $uniq
168 done
169 exit 0
170 EOS
171         mkdep=$toplev/mkdep
172         chmod +x $mkdep
173         $eunicefix $mkdep
174         if $mkdep dep.c >dep.out 2>/dev/null &&
175                 $contains "dep$_o:.*dep\.h" dep.out >/dev/null 2>&1
176         then
177                 echo "A shell script using $cpp does the trick."
178         else
179                 echo "$cpp doesn't seem to be any use at all."
180                 $spitshell >../mkdep <<EOS
181 $startsh
182 $takeflags
183 files="\$@"
184 set X \$flags
185 shift
186 inc='.'
187 while test \$# -gt 0
188 do
189         case "\$1" in
190         -I) 
191                 shift
192                 inc="\$inc:\$1"
193                 ;;
194         -I*)
195                 dir=\`echo \$1 | sed -e 's/^-I//'\`
196                 inc="\$inc:\$dir"
197                 ;;
198         esac
199         shift
200 done
201 set X \$files
202 shift
203 trap "$rm -f /tmp/mkdep\$\$; exit 1" 1 2 3 15
204 for srcfile
205 do
206         case "\$srcfile" in
207         *.c) c='.c';;
208         *.y) c='.y';;
209         *.l) c='.l';;
210         esac
211         filebase=\`basename \$srcfile \$c\`
212         echo \$filebase$_o: \$srcfile
213         $grep '^#[  ]*include' \$srcfile /dev/null | \
214         $sed -n -e 's/#[   ]*include[  ]*//' \\
215                 -e '/<\(.*\)>/ d' \\
216                 -e 's/:[^"]*"\([^"]*\)".*/: \1/' \\
217                 -e "s/\\.c:/$_o:/p" > /tmp/mkdep\$\$
218 ?X: Deal with directories specified via -I requests to locate files
219         IFS=': '
220         while read file dep; do
221                 for dir in \$inc; do
222                         if $test -f "\$dir/\$dep"; then
223                                 dep="\$dir/\$dep"
224                                 break
225                         fi
226                 done
227                 echo "\$file: \$dep" | $sed -e 's,: \./,: ,'
228         done </tmp/mkdep\$\$
229         IFS=' '
230         $rm -f /tmp/mkdep\$\$
231 done
232 exit 0
233 EOS
234                 mkdep=$toplev/mkdep
235                 chmod +x $mkdep
236                 $eunicefix $mkdep
237                 if $mkdep dep.c >dep.out 2>/dev/null &&
238                         $contains "dep$_o:.*dep\.h" dep.out >/dev/null 2>&1
239                 then
240                         cat << EOM
241
242 I can use a script with grep instead, but it will make some incorrect
243 dependencies, since it doesn't understand about conditional compilation.
244 Moreover, some dependencies may be missing, because scanning won't be
245 a recursive process.
246 If you have a program which generates makefile dependencies, you may want
247 to use it.  If not, you can use the script and edit the Makefile by hand
248 if you need to.
249 EOM
250                 else
251                         mkdep=
252                         cat << EOM
253
254 I can't seem to generate makefile dependencies at all!  Perhaps you have a
255 program that does?  If you don't, you might look at the mkdep script to
256 see if you can create one which works.
257 EOM
258                 fi
259         fi
260 esac
261 echo " "
262 dflt="$mkdep"
263 fn=f~/
264 rp="Name of program to make makefile dependencies?"
265 . ./getfile
266 mkdep="$ans"
267 $rm -f dep.c dep.h dep$_o dep.out
268