This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Promote v5.36 usage and feature bundles doc
[perl5.git] / makedepend_file.SH
1 #! /bin/sh
2 case $PERL_CONFIG_SH in
3 '')
4         if test -f config.sh; then TOP=.;
5         elif test -f ../config.sh; then TOP=..;
6         elif test -f ../../config.sh; then TOP=../..;
7         elif test -f ../../../config.sh; then TOP=../../..;
8         elif test -f ../../../../config.sh; then TOP=../../../..;
9         else
10                 echo "Can't find config.sh."; exit 1
11         fi
12         . $TOP/config.sh
13         ;;
14 esac
15 : This forces SH files to create target in same directory as SH file.
16 : This is so that make depend always knows where to find SH derivatives.
17 case "$0" in
18 */*) cd `expr X$0 : 'X\(.*\)/'` ;;
19 esac
20
21 echo "Extracting makedepend_file (with variable substitutions)"
22 rm -f makedepend_file
23 $spitshell >makedepend_file <<!GROK!THIS!
24 $startsh
25 # makedepend_file.SH
26 #
27 MAKE=$make
28 trnl='$trnl'
29 !GROK!THIS!
30 $spitshell >>makedepend_file <<'!NO!SUBS!'
31
32 file=$1
33 shift
34 outfile=$1
35 shift
36
37 case $PERL_CONFIG_SH in
38 '')
39         if test -f config.sh; then TOP=.;
40         elif test -f ../config.sh; then TOP=..;
41         elif test -f ../../config.sh; then TOP=../..;
42         elif test -f ../../../config.sh; then TOP=../../..;
43         elif test -f ../../../../config.sh; then TOP=../../../..;
44         else
45                 echo "Can't find config.sh."; exit 1
46         fi
47         . $TOP/config.sh
48         ;;
49 esac
50
51 # Avoid localized gcc messages
52 case "$ccname" in
53     gcc) LC_ALL=C ; export LC_ALL ;;
54 esac
55
56 # We need .. when we are in the x2p directory if we are using the
57 # cppstdin wrapper script.
58 # Put .. and . first so that we pick up the present cppstdin, not
59 # an older one lying about in /usr/local/bin.
60 PATH=".$path_sep..$path_sep$PATH"
61 export PATH
62
63 case "$osname" in
64 amigaos) cat=/bin/cat ;; # must be absolute
65 esac
66
67     case "$osname" in
68     os2)      uwinfix="-e s,\\\\\\\\,/,g" ;;
69     cygwin)   uwinfix="-e s,\\\\\\\\,/,g" ;;
70     posix-bc) uwinfix="-e s/\\*POSIX(\\(.*\\))/\\1/" ;;
71     vos)      uwinfix="-e s/\#/\\\#/" ;;
72     *)        uwinfix="" ;;
73     esac
74     case "$file" in
75     *.c) filebase=`basename $file .c` ;;
76     *.y) filebase=`basename $file .y` ;;
77     esac
78     case "$file" in
79     */*) finc="-I`echo $file | sed 's#/[^/]*$##'`" ;;
80     *)   finc= ;;
81     esac
82     $echo "Finding dependencies for $filebase$_o"
83     # Below, we strip out all but preprocessor directives.
84     # We have to take care of situations like
85     #       #if defined(FOO) BAR   /* comment line 1
86     #                                 more comment lines */
87     # If we just delete text starting from the '/*' to the end of line, we will
88     # screw up cases like
89     #      #if defined(FOO)    /* comment */ \
90     #          && defined(BAR) /* comment */ \
91     #          && defined(BAZ) /* comment */ \
92     #               etc.
93     # Also, in lines like
94     #      #defined FOO(a,b)    a/**/b
95     # the comment may be important and so needs to be retained.
96     # This code processes the single-line comments first; it assumes there is
97     # at most one straightforward comment per continued preprocessor line,
98     # replacing each non-empty comment (and its surrounding white space) by a
99     # single space.  (sed only has a greedy '*' quantifier, so this doesn't
100     # work right if there are multiple comments per line, and strings can look
101     # like comments to it; both are unlikely in a preprocessor statement.) Any
102     # continuation line is joined, and the process repeated on the enlarged
103     # line as long as there are continuations.  At the end, if there are any
104     # comments remaining, they are either completely empty or are like the
105     # first situation.  The latter are just deleted by first deleting to the
106     # end of line (including preceding white space) things that start with '/*'
107     # and the next char isn't a '*'; then things that start with '/**', but the
108     # next char isn't a '/'.  (Subsequent lines of the comment are irrelevant
109     # and get dropped.)  At the end, we unjoin very long lines to avoid
110     # preprocessor limitations
111     ( $echo "#line 2 \"$file\"";                               \
112       $sed -n <$file                                           \
113         -e "/^${filebase}_init(/q"                             \
114         -e ': tstcont'                                         \
115         -e '/^[         ]*#/s|[         ]*/\*..*\*/[    ]*| |' \
116         -e '/\\$/{'                                            \
117             -e 'N'                                             \
118             -e 'b tstcont'                                     \
119         -e '}'                                                 \
120         -e 's/\\\n//g'                                         \
121         -e '/^#line/d'                                         \
122         -e '/^[         ]*#/{'                                 \
123             -e 's|[     ]*/\*[^*].*$||'                        \
124             -e 's|[     ]*/\*\*[^/].*$||'                      \
125             -e 's/.\{255\}/&\\\n/g'                           \
126             -e p                                               \
127         -e '}' | $grep -v ":" ) >UU/$file.c
128
129     # We're not sure why this was there; the #endif is extraneous on modern z/OS
130     #if [ "$osname" = os390 -a "$file" = perly.c ]; then
131     #   $echo '#endif' >>UU/$file.c
132     #fi
133
134     if [ "$osname" = os390 ]; then
135         $cppstdin -DPERL_CORE $finc -I. $cppflags $cppminus <UU/$file.c |
136         $sed \
137             -e '/^#.*<stdin>/d' \
138             -e '/^#.*"-"/d' \
139             -e '/^#.*git_version\.h/d' \
140             -e 's#\.[0-9][0-9]*\.c#'"$file.c#" \
141             -e 's/^[     ]*#[    ]*line/#/' \
142             -e '/^# *[0-9][0-9]* *[".\/]/!d' \
143             -e 's/^.*"\(.*\)".*$/'$filebase'\$(OBJ_EXT): \1/' \
144             -e 's/^# *[0-9][0-9]* \(.*\)$/'$filebase'\$(OBJ_EXT): \1/' \
145             -e 's|: \./|: |' \
146             -e 's|\.c\.c|.c|' $uwinfix | \
147         $uniq | $sort | $uniq >>$outfile
148     else
149         $cppstdin -DPERL_CORE $finc -I. $cppflags $cppminus <UU/$file.c >$$.cout 2>$$.cerr
150         $sed \
151             -e '1d' \
152             -e '/^#.*<stdin>/d' \
153             -e '/^#.*<builtin>/d' \
154             -e '/^#.*<built-in>/d' \
155             -e '/^#.*<command line>/d' \
156             -e '/^#.*<command-line>/d' \
157             -e '/^#.*"-"/d' \
158             -e '/^#.*"\/.*\/"/d' \
159             -e '/: file path prefix .* never used$/d' \
160             -e '/^#.*git_version\.h/d' \
161             -e 's#\.[0-9][0-9]*\.c#'"$file.c#" \
162             -e 's/^[     ]*#[    ]*line/#/' \
163             -e '/^# *[0-9][0-9]* *[".\/]/!d' \
164             -e 's/^.*"\(.*\)".*$/'$filebase'\$(OBJ_EXT): \1/' \
165             -e 's/^# *[0-9][0-9]* \(.*\)$/'$filebase'\$(OBJ_EXT): \1/' \
166             -e 's|: \./|: |' \
167            -e 's|\.c\.c|.c|' $uwinfix $$.cout $$.cerr| \
168         $uniq | $sort | $uniq >>$outfile
169         rm $$.cout
170         rm $$.cerr
171     fi
172     $echo "$filebase\$(OBJ_EXT): $@" >>$outfile
173
174 !NO!SUBS!
175 $eunicefix makedepend_file
176 chmod +x makedepend_file