This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldelta: Move MAD notice to "Config" section
[perl5.git] / cflags.SH
1 #!/bin/sh
2
3 # Generate the cflags script, which is used to determine what cflags
4 # to pass to the compiler.
5 # We create a temporary test c program and repeatedly compile it with
6 # various candidate flags, and from the compiler output, determine what
7 # flags are supported.
8 # From this we initialise the following variables in the cflags script:
9 #
10 #   $warn
11 #   $stdflags
12 #   $extra
13 #   $_exe
14
15 case $PERL_CONFIG_SH in
16 '')
17         if test -f config.sh; then TOP=.;
18         elif test -f ../config.sh; then TOP=..;
19         elif test -f ../../config.sh; then TOP=../..;
20         elif test -f ../../../config.sh; then TOP=../../..;
21         elif test -f ../../../../config.sh; then TOP=../../../..;
22         else
23                 echo "Can't find config.sh."; exit 1
24         fi
25         . $TOP/config.sh
26         ;;
27 esac
28 # This forces SH files to create target in same directory as SH file.
29 # This is so that make depend always knows where to find SH derivatives.
30 case "$0" in
31 */*) cd `expr X$0 : 'X\(.*\)/'` ;;
32 esac
33
34 if test -f config_h.SH -a ! -f config.h; then
35     . ./config_h.SH
36     CONFIG_H=already-done
37 fi
38
39 warn=''
40
41 # Add -Wall for the core modules iff gcc and not already -Wall
42 case "$gccversion" in
43 '') ;;
44 Intel*) ;; # The Intel C++ plays gcc on TV but is not really it.
45 *)  case "$ccflags" in
46     *-Wall*) ;;
47     *) warn="$warn -Wall" ;;
48     esac
49     ;;
50 esac
51
52 # Create a test source file for testing what options can be fed to
53 # gcc in this system; include a selection of most common and commonly
54 # hairy include files.
55
56 cat >_cflags.c <<__EOT__
57 #include "EXTERN.h"
58 #include "perl.h"
59 /* The stdio.h, errno.h, and setjmp.h should be there in any ANSI C89. */
60 #include <stdio.h>
61 #include <errno.h>
62 #include <setjmp.h>
63 /* Just in case the inclusion of perl.h did not
64  * pull in enough system headers, let's try again. */
65 #ifdef I_STDLIB
66 #include <stdlib.h>
67 #endif
68 #ifdef I_STDDEF
69 #include <stddef.h>
70 #endif
71 #ifdef I_STDARG
72 #include <stdarg.h>
73 #endif
74 #ifdef I_LIMITS
75 #include <limits.h>
76 #endif
77 #ifdef I_DIRENT
78 #include <dirent.h>
79 #endif
80 #ifdef I_UNISTD
81 #include <unistd.h>
82 #endif
83 #ifdef I_SYS_TYPES
84 #include <sys/types.h>
85 #endif
86 #ifdef I_SYS_PARAM
87 #include <sys/param.h>
88 #endif
89 #ifdef I_SYS_RESOURCE
90 #include <sys/resource.h>
91 #endif
92 #ifdef I_SYS_SELECT
93 #include <sys/select.h>
94 #endif
95 #if defined(HAS_SOCKET) && !defined(VMS) && !defined(WIN32) /* See perl.h. */
96 #include <sys/socket.h>
97 #endif
98 #ifdef I_SYS_STAT
99 #include <sys/stat.h>
100 #endif
101 #ifdef I_SYS_TIME
102 #include <sys/time.h>
103 #endif
104 #ifdef I_SYS_TIMES
105 #include <sys/times.h>
106 #endif
107 #ifdef I_SYS_WAIT
108 #include <sys/wait.h>
109 #endif
110 /* The gcc -ansi can cause a lot of noise in Solaris because of:
111  /usr/include/sys/resource.h:148: warning: 'struct rlimit64' declared inside parameter list
112  */
113 int main(int argc, char *argv[]) {
114
115 /* Add here test code found to be problematic in some gcc platform. */
116
117 /* Off_t/off_t is a struct in Solaris with largefiles, and with gcc -ansi
118  * that struct cannot be compared in some gcc releases with a flat
119  * integer, such as a STRLEN. */
120
121   IV iv;
122   Off_t t0a = 2;
123   STRLEN t0b = 3;
124   int t0c = t0a == t0b;
125
126 /* In FreeBSD 6.2 (and probably other releases too), with -Duse64bitint,
127    perl will use atoll(3).  However, that declaration is hidden in <stdlib.h>
128    if we force the compiler to use -std=c89 mode.
129 */
130   iv = Atol("42");
131
132   return (!t0c && (iv == 42)) ? 0 : -1; /* Try to avoid 'unused' warnings. */
133 }
134 __EOT__
135
136 stdflags=''
137
138 # Further gcc warning options.  Build up a list of options that work.
139 # Note that some problems may only show up with combinations of options,
140 # e.g. a warning might show up only with -Wall -ansi, not with either
141 # one individually.
142 # TODO:  Ponder whether to migrate this back to Configure so hints files can 
143 # tweak it.  Also, be paranoid about whether results we've deduced in Configure
144 # (especially about things like long long, which are not in C89) will still be 
145 # valid if we now add flags like -std=c89.
146
147 case "$gccversion" in
148 '') ;;
149 [12]*) ;; # gcc versions 1 (gasp!) and 2 are not good for this.
150 Intel*) ;; # # Is that you, Intel C++?
151 *)  for opt in -ansi -std=c89 -W -Wextra -Wdeclaration-after-statement \
152                 -Wendif-labels -Wc++-compat -Wwrite-strings
153     do
154        case " $ccflags " in
155        *" $opt "*) ;; # Skip if already there.
156        *) rm -f _cflags$_exe
157           case "`$cc -DPERL_NO_INLINE_FUNCTIONS $ccflags $warn $stdflags $opt _cflags.c -o _cflags$_exe 2>&1`" in
158           *"unrecognized"*) ;;
159           *"unknown"*) ;;
160           *"implicit declaration"*) ;; # Was something useful hidden?
161           *"Invalid"*) ;;
162           *"is valid for C"*) ;;
163           *) if test -x _cflags$_exe
164              then
165                case "$opt" in
166                -std*) stdflags="$stdflags $opt" ;;
167                *) warn="$warn $opt" ;;
168                esac
169              fi
170              ;;
171           esac
172           ;;
173        esac
174     done
175     ;;
176 esac
177 rm -f _cflags.c _cflags$_exe
178
179 # XXX There is something wrong in the below: furious editing on ccflags,
180 # but that ccflags will be overwritten by the source of config.sh in the
181 # extracted cflags.
182
183 case "$gccversion" in
184 '') ;;
185 *)
186   if [ "$gccansipedantic" = "" ]; then
187     # If we have -Duse64bitint (or equivalent) in effect and the quadtype
188     # has become 'long long', gcc -pedantic becomes unbearable (moreso
189     # when combined with -Wall) because long long and LL and %lld|%Ld
190     # become warn-worthy.  So let's drop the -pedantic in that case.
191     case "$quadtype:$sPRId64" in
192     "long long"*|*lld*|*Ld*)
193       ccflags="`echo $ccflags|sed 's/-pedantic/ /'`"
194       warn="`echo $warn|sed 's/-pedantic/ /'`"
195       ;;
196     esac
197     # Similarly, since 'long long' isn't part of C89, FreeBSD 6.2 headers
198     # don't declare atoll() under -std=c89, but we need it.  In general,
199     # insisting on -std=c89 is inconsistent with insisting on using
200     # 'long long'. So drop -std=c89 and -ansi as well if we're using 
201     # 'long long' as our main integral type.
202     case "$ivtype" in
203     "long long")
204         ccflags=`echo $ccflags|sed -e 's/-pedantic/ /' -e 's/-std=c89/ /' -e 's/-ansi/ /'`
205         warn=`echo $warn|sed -e 's/-pedantic/ /' -e 's/-ansi/ /'`
206         stdflags=`echo $stdflags|sed -e 's/-std=c89/ /'`
207         ;;
208     esac
209   fi
210   # Using certain features (like the gcc statement expressions)
211   # requires knowing whether -pedantic has been specified.
212   case "$warn$ccflags" in
213   *-pedantic*) warn="$warn -DPERL_GCC_PEDANTIC" ;;
214   esac
215   ;;
216 esac
217
218 # Code to set any extra flags here.
219 extra=''
220
221 echo "Extracting cflags (with variable substitutions)"
222 # This section of the file will have variable substitutions done on it.
223 # Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
224 # Protect any dollar signs and backticks that you do not want interpreted
225 # by putting a backslash in front.  You may delete these comments.
226 rm -f cflags
227 $spitshell >cflags <<!GROK!THIS!
228 $startsh
229
230 # !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
231
232 # This file is generated by cflags.SH
233
234
235 # Extra warnings, used e.g. for gcc.
236 warn="$warn"
237 # Extra standardness.
238 stdflags="$stdflags"
239 # Extra extra.
240 extra="$extra"
241 # what do executables look like?
242 _exe="$_exe"
243
244 !GROK!THIS!
245
246 # In the following dollars and backticks do not need the extra backslash.
247 $spitshell >>cflags <<'!NO!SUBS!'
248 case $PERL_CONFIG_SH in
249 '')
250         if test -f config.sh; then TOP=.;
251         elif test -f ../config.sh; then TOP=..;
252         elif test -f ../../config.sh; then TOP=../..;
253         elif test -f ../../../config.sh; then TOP=../../..;
254         elif test -f ../../../../config.sh; then TOP=../../../..;
255         else
256                 echo "Can't find config.sh."; exit 1
257         fi
258         . $TOP/config.sh
259         ;;
260 esac
261
262 # syntax: cflags [optimize=XXX] [file[.suffix]] ...
263 #   displays the proposed compiler command line for each 'file'
264 #
265 #   with no file, dispalys it for all *.c files.
266 #   The optimise=XXX arg (if present) is evalled, setting the default
267 #   value of the $optimise variable, which is output on the command line
268 #   (but which may be overridden for specific files below)
269
270 case "X$1" in
271 Xoptimize=*|X"optimize=*")
272         eval "$1"
273         shift
274         ;;
275 esac
276
277 case $# in
278 0) set *.c; echo "The current C flags are:" ;;
279 esac
280
281 set `echo "$* " | sed -e 's/\.[oc] / /g' -e 's/\.obj / /g' -e "s/\\$obj_ext / /g"`
282
283 for file do
284
285     case "$#" in
286     1) ;;
287     *) echo $n "    $file.c     $c" ;;
288     esac
289
290     # allow variables like toke_cflags to be evaluated
291
292     if echo $file | grep -v / >/dev/null
293     then
294       eval 'eval ${'"${file}_cflags"'-""}'
295     fi
296
297     # or customize here
298
299     case "$file" in
300     *) ;;
301
302     # Customization examples follow:
303     av) ccflags=`echo $ccflags | sed -e s/-pipe//` ;;
304     deb) ccflags="$ccflags -fno-jump-tables" ;;
305     hv) warn=`echo $warn | sed -e s/-Wextra//` ;;
306     toke) optimize=-O0 ;;
307     esac
308
309     # The examples are intentionally unreachable as the '*)' case always
310     # matches. To use them, move before the '*)' and edit as appropriate.
311     # It is not a good idea to set ccflags to an absolute value here, as it
312     # often contains general -D defines which are needed for correct
313     # compilation. It is better to edit ccflags as shown, using interpolation
314     # to add flags, or sed to remove flags.
315
316
317     case "$cc" in
318     *g++*)
319       # Extra paranoia in case people have bad canned ccflags:
320       # bad in the sense that the flags are accepted by g++,
321       # but then whined about.
322       for f in -Wdeclaration-after-statement -std=c89
323       do
324         ccflags=`echo $ccflags|sed 's/$f/ /'`
325       done
326       ;;
327     esac
328     cppflags=`echo $cppflags|sed 's/-Wdeclaration-after-statement/ /'`
329
330     case "$cc" in
331     *clang*)
332       # clang complains a lot about -Wunused-value which are not fixable
333       warn="$warn -Wno-unused-value"
334       ;;
335     *g++*)
336       # Without -Wno-unused-variable g++ 4.x compiles are rather unwatchable
337       # because of all the warnings about Perl___notused, and g++ doesn't do
338       # __attribute__((unused)) (and even if at some stage it may, people do
339       # have older gcc installations), and ((void)x) isn't enough to silence
340       # the noises about XS functions not using their cv parameter, so we need
341       # the -Wno-unused-parameter too.
342       # Yes, we lose some valid warnings, but hopefully other compilers
343       # (like gcc) will still pick up those warnings.
344       for o in -Wno-unused-variable -Wno-unused-parameter
345       do
346         case "$warn" in
347         *$o*) ;;
348         *) warn="$warn $o" ;;
349         esac
350       done
351       ;;
352     *)
353       # clang may not be called clang
354       case "`$cc -v 2>&1`" in
355       *clang*)
356         case "$warn" in
357         *-Wno-unused-value) ;;
358         *) warn="$warn -Wno-unused-value"
359         esac
360       esac
361     esac
362
363
364     # Can we perhaps use $ansi2knr here
365     echo "$cc -c -DPERL_CORE $ccflags $stdflags $optimize $warn $extra"
366
367     . $TOP/config.sh
368
369     # end per file behaviour
370 done
371 !NO!SUBS!
372 chmod 755 cflags
373 $eunicefix cflags