This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
subroutine redefinitions made with local() shouldn't warn
[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 for compiling the core perl.
5 #
6 # This does NOT affect the XS compilation (ext, dist, cpan)
7 # since that uses %Config values directly.
8 #
9 # For example, since -Wall adds -Wunused-*, a bare -Wall (without
10 # amending that with -Wno-unused-..., or with the PERL_UNUSED_...)
11 # would be too much for XS code because there are too many generated
12 # but often unused things.
13 #
14 # We create a temporary test C program and repeatedly compile it with
15 # various candidate flags, and from the compiler output, determine what
16 # flags are supported.
17 #
18 # From this we initialise the following variables in the cflags script:
19 #
20 #   $myccflags (possibly edited version of $Config{ccflags})
21 #   $warn
22 #   $stdflags
23 #   $extra
24 #   $_exe
25
26 case $PERL_CONFIG_SH in
27 '')
28         if test -f config.sh; then TOP=.;
29         elif test -f ../config.sh; then TOP=..;
30         elif test -f ../../config.sh; then TOP=../..;
31         elif test -f ../../../config.sh; then TOP=../../..;
32         elif test -f ../../../../config.sh; then TOP=../../../..;
33         else
34                 echo "Can't find config.sh."; exit 1
35         fi
36         . $TOP/config.sh
37         ;;
38 esac
39 # This forces SH files to create target in same directory as SH file.
40 # This is so that make depend always knows where to find SH derivatives.
41 case "$0" in
42 */*) cd `expr X$0 : 'X\(.*\)/'` ;;
43 esac
44
45 if test -f config_h.SH -a ! -f config.h; then
46     . ./config_h.SH
47     CONFIG_H=already-done
48 fi
49
50 warn=''
51
52 # Add -Wall for the core modules iff gcc and not already -Wall
53 case "$gccversion" in
54 '') ;;
55 Intel*) ;; # The Intel C++ plays gcc on TV but is not really it.
56 *)  case "$ccflags" in
57     *-Wall*) ;;
58     *) warn="$warn -Wall" ;;
59     esac
60     ;;
61 esac
62
63 # Create a test source file for testing what options can be fed to
64 # gcc in this system; include a selection of most common and commonly
65 # hairy include files.
66
67 cat >_cflags.c <<__EOT__
68 #include "EXTERN.h"
69 #include "perl.h"
70 /* The stdio.h, errno.h, and setjmp.h should be there in any ANSI C89. */
71 #include <stdio.h>
72 #include <errno.h>
73 #include <setjmp.h>
74 /* Just in case the inclusion of perl.h did not
75  * pull in enough system headers, let's try again. */
76 #include <stdlib.h>
77 #include <stddef.h>
78 #include <stdarg.h>
79 #include <limits.h>
80 #ifdef I_DIRENT
81 #include <dirent.h>
82 #endif
83 #ifdef I_UNISTD
84 #include <unistd.h>
85 #endif
86 #ifdef I_SYS_TYPES
87 #include <sys/types.h>
88 #endif
89 #ifdef I_SYS_PARAM
90 #include <sys/param.h>
91 #endif
92 #ifdef I_SYS_RESOURCE
93 #include <sys/resource.h>
94 #endif
95 #ifdef I_SYS_SELECT
96 #include <sys/select.h>
97 #endif
98 #if defined(HAS_SOCKET) && !defined(VMS) && !defined(WIN32) /* See perl.h. */
99 #include <sys/socket.h>
100 #endif
101 #ifdef I_SYS_STAT
102 #include <sys/stat.h>
103 #endif
104 #ifdef I_SYS_TIME
105 #include <sys/time.h>
106 #endif
107 #ifdef I_SYS_TIMES
108 #include <sys/times.h>
109 #endif
110 #ifdef I_SYS_WAIT
111 #include <sys/wait.h>
112 #endif
113 /* The gcc -ansi can cause a lot of noise in Solaris because of:
114  /usr/include/sys/resource.h:148: warning: 'struct rlimit64' declared inside parameter list
115  */
116 int main(int argc, char *argv[]) {
117
118 /* Add here test code found to be problematic in some gcc platform. */
119
120 /* Off_t/off_t is a struct in Solaris with largefiles, and with gcc -ansi
121  * that struct cannot be compared in some gcc releases with a flat
122  * integer, such as a STRLEN. */
123
124   IV iv;
125   Off_t t0a = 2;
126   STRLEN t0b = 3;
127   int t0c = (STRLEN)t0a == t0b;
128
129   printf("%s: %d\n", argv[0], argc);
130
131 /* In FreeBSD 6.2 (and probably other releases too), with -Duse64bitint,
132    perl will use atoll(3).  However, that declaration is hidden in <stdlib.h>
133    if we force the compiler to use -std=c89 mode.
134 */
135   iv = Atol("42");
136
137   return (!t0c && (iv == 42)) ? 0 : -1; /* Try to avoid 'unused' warnings. */
138 }
139 __EOT__
140
141 stdflags=''
142
143 # Further gcc warning options.  Build up a list of options that work.
144 # Note that some problems may only show up with combinations of options,
145 # e.g. a warning might show up only with -Wall -ansi, not with either
146 # one individually.
147 # TODO:  Ponder whether to migrate this back to Configure so hints files can
148 # tweak it.  Also, be paranoid about whether results we've deduced in Configure
149 # will still be valid if we now add flags like -std=c99.
150
151 pedantic=''
152 case "$gccansipedantic" in
153 define) pedantic='-pedantic' ;;
154 esac
155
156 case "$gccversion" in
157 '') ;;
158 [12].*) ;; # gcc versions 1 (gasp!) and 2 are not good for this.
159 Intel*) ;; # # Is that you, Intel C++?
160 #
161 # These comments are adapted from the originals, which were for -std=c89.
162 # I believe that my updates close to correct, and better than throwing the
163 # entire comments away, but please check for discrepencies.
164 #
165 # NOTE 1: the -std=c99 without -pedantic is a bit pointless.
166 # Just -std=c99 means "if there is room for interpretation,
167 # interpret the C99 way."  It does NOT mean "strict C99" on its own.
168 # You need to add the -pedantic for that.  To do this with Configure,
169 # do -Dgccansipedantic (note that this is named from the time when we also
170 # added the -ansi option. That forces -std=c89, so we no longer use it.)
171 # *Because* we aren't adding -std=c99 if we don't have to, but will add -W,
172 # some versions of gcc will accept C99 code but warn about not-C89 features.
173 # (If we added -std=c99 then the warnings enabled by -W would be consistent)
174 # Hence we add -Wno-long-long and -Wno-declaration-after-statement to cover
175 # these cases.
176 #
177 # NOTE 2: -pedantic necessitates adding a couple of flags:
178 # * -PERL_GCC_PEDANTIC so that the perl code can adapt: there's nothing
179 #   added by gcc itself to indicate pedanticness.
180 # * -Wno-overlength-strings under -DDEBUGGING because quite many of
181 #   the LEAVE_with_name() and assert() calls generate string literals
182 #   longer then the ANSI C99 minimum of 4095 bytes.
183 #
184 # NOTE 3: the relative order of these options matters:
185 # -Wextra before -W
186 # -W before -Wno-long-long -Wno-declaration-after-statement
187 #
188 *) warns="$pedantic \
189         -Werror=pointer-arith \
190         -Werror=vla \
191         -Wextra -W \
192         -Wno-long-long -Wno-declaration-after-statement \
193         -Wc++-compat -Wwrite-strings"
194    case " $ccflags " in
195    *" -std="*) ;; # Already have -std=...
196    *) warns="-std=c99 $warns" ;;
197    esac
198    for opt in $warns
199     do
200        case " $ccflags " in
201        *" $opt "*) ;; # Skip if already there.
202        *) rm -f _cflags$_exe
203           flags="-DPERL_NO_INLINE_FUNCTIONS $ccflags $warn $stdflags $opt"
204           case "$opt" in
205           *-pedantic*) flags="$flags -DPERL_GCC_PEDANTIC" ;;
206           esac
207           # echo "opt = $opt, flags = $flags"
208           cmd="$cc $flags _cflags.c -o _cflags$_exe"
209           out="`$cmd 2>&1`"
210           # echo "$cmd --> $out"
211           case "$out" in
212           *"unrecognized"*) ;;
213           *"unknown"*) ;;
214           *"implicit declaration"*) ;; # Was something useful hidden?
215           *"Invalid"*) ;;
216           *"is valid for C"*) ;;
217           *) if test -x _cflags$_exe
218              then
219                case "$opt" in
220                -std*)
221                  echo "cflags.SH: Adding $opt."
222                  stdflags="$stdflags $opt"
223                  ;;
224                -W)
225                  # -Wextra is the modern form of -W, so add
226                  # -W only if -Wextra is not there already.
227                  case " $warn " in
228                  *-Wextra*) ;;
229                  *)
230                    echo "cflags.SH: Adding $opt."
231                    warn="$warn $opt"
232                    ;;
233                  esac
234                  ;;
235                -Werror=pointer-arith)
236                   # -pedantic* covers -Werror=p-a
237                   case "$warn" in
238                   *-pedantic*) ;;
239                   *)
240                      echo "cflags.SH: Adding $opt."
241                      warn="$warn $opt"
242                      ;;
243                   esac
244                   ;;
245                *)
246                   echo "cflags.SH: Adding $opt."
247                   warn="$warn $opt"
248                   ;;
249                esac
250              fi
251              ;;
252           esac
253           ;;
254        esac
255        case "$ccflags$warn" in
256        *-pedantic*)
257          overlength=''
258          case "$ccflags$optimize" in
259          *-DDEBUGGING*) overlength='-Wno-overlength-strings' ;;
260          esac
261          for opt2 in -DPERL_GCC_PEDANTIC $overlength
262          do
263            case "$ccflags$warn" in
264            *"$opt2"*) ;;
265            *) echo "cflags.SH: Adding $opt2 because of -pedantic."
266               warn="$warn $opt2" ;;
267            esac
268          done
269          ;;
270        esac
271     done
272     ;;
273 esac
274 rm -f _cflags.c _cflags$_exe
275
276 case "$gccversion" in
277 '') ;;
278 *)
279   case "$warn$ccflags" in
280   *-pedantic*)
281     # For -std=c99 -pedantic, only the %Ld format seems to be warn-worthy.
282     # 'long long' and '%lld' are now kosher.
283     #
284     # usedtrace (DTrace) uses unportable features (dollars in identifiers,
285     # and gcc statement expressions), it is just easier to turn off pedantic.
286     remove=''
287     case "$quadtype:$ivtype:$sPRId64:$usedtrace" in
288     **Ld*) remove='Ld' ;;
289     *) case "$usedtrace" in
290        define) remove='usedtrace' ;;
291        esac
292        ;;
293     esac
294     case "$remove" in
295     '') ;;
296     *) echo "cflags.SH: Removing -pedantic* -ansi because of $remove."
297       ccflags=`echo $ccflags|sed -e 's/-pedantic-errors/ /' -e 's/-pedantic/ /'`
298       warn=`echo $warn|sed -e 's/-pedantic-errors/ /' -e 's/-pedantic/ /'`
299       ;;
300     esac
301     ;;
302   esac
303   ;;
304 esac
305
306 # Older clang releases are not wise enough for -Wunused-value.
307 case "$gccversion" in
308 *"Apple LLVM "[34]*|*"Apple LLVM version "[34]*)
309   for f in -Wno-unused-value
310   do
311     echo "cflags.SH: Adding $f because clang version '$gccversion'" 
312     warn="$warn $f"
313   done
314   ;;
315 esac
316
317 # The quadmath Q format specifier will cause -Wformat to whine.
318 case "$gccversion" in
319 '') ;;
320 *) case "$usequadmath" in
321    define)
322      for f in -Wno-format
323      do
324        echo "cflags.SH: Adding $f because of usequadmath."
325        warn="$warn $f"
326      done
327     ;;
328   esac
329   ;;
330 esac
331
332 case "$cc" in
333 *g++*)
334   # Extra paranoia in case people have bad canned ccflags:
335   # bad in the sense that the flags are accepted by g++,
336   # but then whined about.
337   for f in -Wc++-compat -std=c99
338   do
339     case "$ccflags$warn" in
340     *"$f"*)
341       echo "cflags.SH: Removing $f because of g++."
342       ccflags=`echo $ccflags|sed 's/$f/ /'`
343       warn=`echo $warn|sed 's/$f/ /'`
344       ;;
345     esac
346   done
347   ;;
348 esac
349
350 for f in -Wpointer-arith -Werror=pointer-arith
351 do
352   case "$cppflags" in
353   *"$f"*)
354     echo "cflags.SH: Removing $f from cppflags."
355     cppflags=`echo $cppflags|sed 's/$f/ /'` ;;
356   esac
357 done
358
359 # If usethreads and clang, add -Wthread-safety for clang 3.6 or later.
360 # gccversion is defined also for clang, because compat, use that for matching.
361 # Apple overwrites clang version with XCode version, see hints/darwin.sh
362 # for the gory details.  Aggressively forward-proofing.
363 case "$usethreads" in
364 define)
365 case "$gccversion" in
366 *" Clang 3."[56789]*|*" Clang "[456]*|*"Apple LLVM 6.1"*|*"Apple LLVM "[789]*)
367   for f in -Wthread-safety
368   do
369     case " $warn " in
370     *" $f "*) ;; # Skip if already there.
371     *)
372       echo "cflags.SH: Adding $f because usethreads and clang and gccversion '$gccversion'"
373       warn="$warn $f"
374       ;;
375     esac
376   done
377 ;;
378 esac
379 ;;
380 esac
381
382 echo "cflags.SH: cc       = $cc"
383 echo "cflags.SH: ccflags  = $ccflags"
384 echo "cflags.SH: stdflags = $stdflags"
385 echo "cflags.SH: optimize = $optimize"
386 echo "cflags.SH: warn     = $warn"
387
388 # Code to set any extra flags here.
389 extra=''
390
391 # Protect double or single quotes for better restoring of ccflags.
392 myccflags=`echo $ccflags | sed -e 's/"/\\\"/g' -e "s/'/\\\'/g"`
393
394 echo "Extracting cflags (with variable substitutions)"
395 # This section of the file will have variable substitutions done on it.
396 # Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
397 # Protect any dollar signs and backticks that you do not want interpreted
398 # by putting a backslash in front.  You may delete these comments.
399 rm -f cflags
400 $spitshell >cflags <<!GROK!THIS!
401 $startsh
402
403 # !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
404
405 # This file is generated by cflags.SH
406
407 # Used to restore possible edits by cflags.SH.
408 myccflags="$myccflags"
409
410 # Extra warnings, used e.g. for gcc.
411 warn="$warn"
412 # Extra standardness.
413 stdflags="$stdflags"
414 # Extra extra.
415 extra="$extra"
416 # what do executables look like?
417 _exe="$_exe"
418
419 !GROK!THIS!
420
421 # In the following dollars and backticks do not need the extra backslash.
422 $spitshell >>cflags <<'!NO!SUBS!'
423 case $PERL_CONFIG_SH in
424 '')
425         if test -f config.sh; then TOP=.;
426         elif test -f ../config.sh; then TOP=..;
427         elif test -f ../../config.sh; then TOP=../..;
428         elif test -f ../../../config.sh; then TOP=../../..;
429         elif test -f ../../../../config.sh; then TOP=../../../..;
430         else
431                 echo "Can't find config.sh."; exit 1
432         fi
433         . $TOP/config.sh
434         ccflags="$myccflags"  # Restore possible edits by cflags.SH.
435         ;;
436 esac
437
438 # syntax: cflags [optimize=XXX] [file[.suffix]] ...
439 #   displays the proposed compiler command line for each 'file'
440 #
441 #   with no file, dispalys it for all *.c files.
442 #   The optimise=XXX arg (if present) is evalled, setting the default
443 #   value of the $optimise variable, which is output on the command line
444 #   (but which may be overridden for specific files below)
445
446 case "X$1" in
447 Xoptimize=*|X"optimize=*")
448         eval "$1"
449         shift
450         ;;
451 esac
452
453 case $# in
454 0) set *.c; echo "The current C flags are:" ;;
455 esac
456
457 set `echo "$* " | sed -e 's/\.[oc] / /g' -e 's/\.obj / /g' -e "s/\\$obj_ext / /g"`
458
459 for file do
460
461     case "$#" in
462     1) ;;
463     *) echo $n "    $file.c     $c" ;;
464     esac
465
466     # allow variables like toke_cflags to be evaluated
467
468     case "$file" in
469     */*) ;;
470     *) eval 'eval ${'"${file}_cflags"'-""}' ;;
471     esac
472
473     # or customize here
474
475     case "$file" in
476     regcomp) : work around http://bugs.debian.org/754054
477         case $archname in
478         mips-*|mipsel-*)
479             optimize="$optimize -fno-tree-vrp";;
480         esac;;
481     *) ;;
482
483     # Customization examples follow.
484     #
485     # The examples are intentionally unreachable as the '*)' case above always
486     # matches. To use them, move before the '*)' and edit as appropriate.
487     # It is not a good idea to set ccflags to an absolute value here, as it
488     # often contains general -D defines which are needed for correct
489     # compilation. It is better to edit ccflags as shown, using interpolation
490     # to add flags, or sed to remove flags.
491
492     av) ccflags=`echo $ccflags | sed -e s/-pipe//` ;;
493     deb) ccflags="$ccflags -fno-jump-tables" ;;
494     hv) warn=`echo $warn | sed -e s/-Wextra//` ;;
495     toke) optimize=-O0 ;;
496     esac
497
498     echo "$cc -c -DPERL_CORE $ccflags $stdflags $optimize $warn $extra"
499
500     . $TOP/config.sh
501
502     # end per file behaviour
503 done
504 !NO!SUBS!
505 chmod 755 cflags
506 $eunicefix cflags