This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Module::CoreList for 5.31.7
[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 # (especially about things like long long, which are not in C89) will still be
150 # valid if we now add flags like -std=c89.
151
152 pedantic=''
153 case "$gccansipedantic" in
154 define) pedantic='-pedantic' ;;
155 esac
156
157 case "$gccversion" in
158 '') ;;
159 [12].*) ;; # gcc versions 1 (gasp!) and 2 are not good for this.
160 Intel*) ;; # # Is that you, Intel C++?
161 #
162 # NOTE 1: the -std=c89 without -pedantic is a bit pointless.
163 # Just -std=c89 means "if there is room for interpretation,
164 # interpret the C89 way."  It does NOT mean "strict C89" on its own.
165 # You need to add the -pedantic for that.  To do this with Configure,
166 # do -Dgccansipedantic (note that the -ansi is included in any case,
167 # the option is a bit oddly named, for historical reasons.)
168 #
169 # NOTE 2: -pedantic necessitates adding a couple of flags:
170 # * -PERL_GCC_PEDANTIC so that the perl code can adapt: there's nothing
171 #   added by gcc itself to indicate pedanticness.
172 # * -Wno-overlength-strings under -DDEBUGGING because quite many of
173 #   the LEAVE_with_name() and assert() calls generate string literals
174 #   longer then the ANSI minimum of 509 bytes.
175 #
176 # NOTE 3: the relative order of these options matters:
177 # -Wextra before -W
178 # -std=c89 before -ansi
179 # -pedantic* before -Werror=d-a-s
180 #
181 *) warns=-std=c89 -ansi $pedantic \
182         -Werror=pointer-arith \
183         -Wextra -W \
184         -Wc++-compat -Wwrite-strings
185    # declaration after statement is normal in C++ rather than an
186    # extension and compilers complain if we try to warn about it
187    case "$d_cplusplus" in
188    define) ;;
189    *) warns="$warns -Werror=declaration-after-statement" ;;
190    esac
191    for opt in $warns
192     do
193        case " $ccflags " in
194        *" $opt "*) ;; # Skip if already there.
195        *) rm -f _cflags$_exe
196           flags="-DPERL_NO_INLINE_FUNCTIONS $ccflags $warn $stdflags $opt"
197           case "$opt" in
198           *-pedantic*) flags="$flags -DPERL_GCC_PEDANTIC" ;;
199           esac
200           # echo "opt = $opt, flags = $flags"
201           cmd="$cc $flags _cflags.c -o _cflags$_exe"
202           out="`$cmd 2>&1`"
203           # echo "$cmd --> $out"
204           case "$out" in
205           *"unrecognized"*) ;;
206           *"unknown"*) ;;
207           *"implicit declaration"*) ;; # Was something useful hidden?
208           *"Invalid"*) ;;
209           *"is valid for C"*) ;;
210           *) if test -x _cflags$_exe
211              then
212                case "$opt" in
213                -std*)
214                  echo "cflags.SH: Adding $opt."
215                  stdflags="$stdflags $opt"
216                  ;;
217                -ansi)
218                  # -std=c89 is the modern form of -ansi, so add
219                  # -ansi only if -std=c89 is not there already.
220                  case " $stdflags " in
221                  *-std=c89*) ;;
222                  *)
223                    echo "cflags.SH: Adding $opt."
224                    stdflags="$stdflags $opt"
225                    ;;
226                  esac
227                  ;;
228                -W)
229                  # -Wextra is the modern form of -W, so add
230                  # -W only if -Wextra is not there already.
231                  case " $warn " in
232                  *-Wextra*) ;;
233                  *)
234                    echo "cflags.SH: Adding $opt."
235                    warn="$warn $opt"
236                    ;;
237                  esac
238                  ;;
239                -Werror=declaration-after-statement)
240                   # -pedantic* (with -std=c89) covers -Werror=d-a-s.
241                   case "$stdflags$warn" in
242                   *-std=c89*-pedantic*|*-pedantic*-std=c89*) ;;
243                   *)
244                      echo "cflags.SH: Adding $opt."
245                      warn="$warn $opt"
246                      ;;
247                   esac
248                   ;;
249                -Werror=pointer-arith)
250                   # -pedantic* covers -Werror=p-a
251                   case "$warn" in
252                   *-pedantic*) ;;
253                   *)
254                      echo "cflags.SH: Adding $opt."
255                      warn="$warn $opt"
256                      ;;
257                   esac
258                   ;;
259                *)
260                   echo "cflags.SH: Adding $opt."
261                   warn="$warn $opt"
262                   ;;
263                esac
264              fi
265              ;;
266           esac
267           ;;
268        esac
269        case "$ccflags$warn" in
270        *-pedantic*)
271          overlength=''
272          case "$ccflags$optimize" in
273          *-DDEBUGGING*) overlength='-Wno-overlength-strings' ;;
274          esac
275          for opt2 in -DPERL_GCC_PEDANTIC $overlength
276          do
277            case "$ccflags$warn" in
278            *"$opt2"*) ;;
279            *) echo "cflags.SH: Adding $opt2 because of -pedantic."
280               warn="$warn $opt2" ;;
281            esac
282          done
283          ;;
284        esac
285     done
286     ;;
287 esac
288 rm -f _cflags.c _cflags$_exe
289
290 case "$gccversion" in
291 '') ;;
292 *)
293   case "$warn$ccflags" in
294   *-pedantic*)
295     # If we have -Duse64bitint (or equivalent) in effect and the quadtype
296     # has become 'long long', gcc -pedantic* becomes unbearable
297     # (moreso when combined with -Wall) because long long and LL and %lld|%Ld
298     # become warn-worthy.  So let's drop the -pedantic in that case.
299     #
300     # Similarly, since 'long long' isn't part of C89, FreeBSD 6.2 headers
301     # don't declare atoll() under -std=c89, but we need it.  In general,
302     # insisting on -std=c89 is inconsistent with insisting on using
303     # 'long long'. So drop -std=c89 and -ansi as well if we're using
304     # 'long long' as our main integral type.
305     #
306     # usedtrace (DTrace) uses unportable features (dollars in identifiers,
307     # and gcc statement expressions), it is just easier to turn off pedantic.
308     remove=''
309     case "$quadtype:$ivtype:$sPRId64:$usedtrace" in
310     *"long long"*|*lld*|*Ld*) remove='long long' ;;
311     *) case "$usedtrace" in
312        define) remove='usedtrace' ;;
313        esac
314        ;;
315     esac
316     case "$remove" in
317     '') ;;
318     *) echo "cflags.SH: Removing -pedantic*, -std=c89, and -ansi because of $remove."
319       ccflags=`echo $ccflags|sed -e 's/-pedantic-errors/ /' -e 's/-pedantic/ /' -e 's/-std=c89/ /' -e 's/-ansi/ /' -e 's/-DPERL_GCC_PEDANTIC/ /'`
320       warn=`echo $warn|sed -e 's/-pedantic-errors/ /' -e 's/-pedantic/ /' -e 's/-ansi/ /' -e 's/-DPERL_GCC_PEDANTIC/ /'`
321       stdflags=`echo $stdflags|sed -e 's/-std=c89/ /'`
322       ;;
323     esac
324     ;;
325   esac
326   ;;
327 esac
328
329 # Older clang releases are not wise enough for -Wunused-value.
330 case "$gccversion" in
331 *"Apple LLVM "[34]*|*"Apple LLVM version "[34]*)
332   for f in -Wno-unused-value
333   do
334     echo "cflags.SH: Adding $f because clang version '$gccversion'" 
335     warn="$warn $f"
336   done
337   ;;
338 esac
339
340 # The quadmath Q format specifier will cause -Wformat to whine.
341 case "$gccversion" in
342 '') ;;
343 *) case "$usequadmath" in
344    define)
345      for f in -Wno-format
346      do
347        echo "cflags.SH: Adding $f because of usequadmath."
348        warn="$warn $f"
349      done
350     ;;
351   esac
352   ;;
353 esac
354
355 case "$cc" in
356 *g++*)
357   # Extra paranoia in case people have bad canned ccflags:
358   # bad in the sense that the flags are accepted by g++,
359   # but then whined about.
360   #
361   # -Werror=d-a-s option is valid for g++, by definition,
362   # but we remove it just for cleanliness and shorter command lines.
363   for f in -Wdeclaration-after-statement \
364                 -Werror=declaration-after-statement \
365                 -Wc++-compat \
366                 -std=c89
367   do
368     case "$ccflags$warn" in
369     *"$f"*)
370       echo "cflags.SH: Removing $f because of g++."
371       ccflags=`echo $ccflags|sed 's/$f/ /'`
372       warn=`echo $warn|sed 's/$f/ /'`
373       ;;
374     esac
375   done
376   ;;
377 esac
378
379 for f in -Wdeclaration-after-statement -Werror=declaration-after-statement \
380          -Wpointer-arith -Werror=pointer-arith
381 do
382   case "$cppflags" in
383   *"$f"*)
384     echo "cflags.SH: Removing $f from cppflags."
385     cppflags=`echo $cppflags|sed 's/$f/ /'` ;;
386   esac
387 done
388
389 # If usethreads and clang, add -Wthread-safety for clang 3.6 or later.
390 # gccversion is defined also for clang, because compat, use that for matching.
391 # Apple overwrites clang version with XCode version, see hints/darwin.sh
392 # for the gory details.  Aggressively forward-proofing.
393 case "$usethreads" in
394 define)
395 case "$gccversion" in
396 *" Clang 3."[56789]*|*" Clang "[456]*|*"Apple LLVM 6.1"*|*"Apple LLVM "[789]*)
397   for f in -Wthread-safety
398   do
399     case " $warn " in
400     *" $f "*) ;; # Skip if already there.
401     *)
402       echo "cflags.SH: Adding $f because usethreads and clang and gccversion '$gccversion'"
403       warn="$warn $f"
404       ;;
405     esac
406   done
407 ;;
408 esac
409 ;;
410 esac
411
412 echo "cflags.SH: cc       = $cc"
413 echo "cflags.SH: ccflags  = $ccflags"
414 echo "cflags.SH: stdflags = $stdflags"
415 echo "cflags.SH: optimize = $optimize"
416 echo "cflags.SH: warn     = $warn"
417
418 # Code to set any extra flags here.
419 extra=''
420
421 # Protect double or single quotes for better restoring of ccflags.
422 myccflags=`echo $ccflags | sed -e 's/"/\\\"/g' -e "s/'/\\\'/g"`
423
424 echo "Extracting cflags (with variable substitutions)"
425 # This section of the file will have variable substitutions done on it.
426 # Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
427 # Protect any dollar signs and backticks that you do not want interpreted
428 # by putting a backslash in front.  You may delete these comments.
429 rm -f cflags
430 $spitshell >cflags <<!GROK!THIS!
431 $startsh
432
433 # !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
434
435 # This file is generated by cflags.SH
436
437 # Used to restore possible edits by cflags.SH.
438 myccflags="$myccflags"
439
440 # Extra warnings, used e.g. for gcc.
441 warn="$warn"
442 # Extra standardness.
443 stdflags="$stdflags"
444 # Extra extra.
445 extra="$extra"
446 # what do executables look like?
447 _exe="$_exe"
448
449 !GROK!THIS!
450
451 # In the following dollars and backticks do not need the extra backslash.
452 $spitshell >>cflags <<'!NO!SUBS!'
453 case $PERL_CONFIG_SH in
454 '')
455         if test -f config.sh; then TOP=.;
456         elif test -f ../config.sh; then TOP=..;
457         elif test -f ../../config.sh; then TOP=../..;
458         elif test -f ../../../config.sh; then TOP=../../..;
459         elif test -f ../../../../config.sh; then TOP=../../../..;
460         else
461                 echo "Can't find config.sh."; exit 1
462         fi
463         . $TOP/config.sh
464         ccflags="$myccflags"  # Restore possible edits by cflags.SH.
465         ;;
466 esac
467
468 # syntax: cflags [optimize=XXX] [file[.suffix]] ...
469 #   displays the proposed compiler command line for each 'file'
470 #
471 #   with no file, dispalys it for all *.c files.
472 #   The optimise=XXX arg (if present) is evalled, setting the default
473 #   value of the $optimise variable, which is output on the command line
474 #   (but which may be overridden for specific files below)
475
476 case "X$1" in
477 Xoptimize=*|X"optimize=*")
478         eval "$1"
479         shift
480         ;;
481 esac
482
483 case $# in
484 0) set *.c; echo "The current C flags are:" ;;
485 esac
486
487 set `echo "$* " | sed -e 's/\.[oc] / /g' -e 's/\.obj / /g' -e "s/\\$obj_ext / /g"`
488
489 for file do
490
491     case "$#" in
492     1) ;;
493     *) echo $n "    $file.c     $c" ;;
494     esac
495
496     # allow variables like toke_cflags to be evaluated
497
498     case "$file" in
499     */*) ;;
500     *) eval 'eval ${'"${file}_cflags"'-""}' ;;
501     esac
502
503     # or customize here
504
505     case "$file" in
506     regcomp) : work around http://bugs.debian.org/754054
507         case $archname in
508         mips-*|mipsel-*)
509             optimize="$optimize -fno-tree-vrp";;
510         esac;;
511     *) ;;
512
513     # Customization examples follow.
514     #
515     # The examples are intentionally unreachable as the '*)' case above always
516     # matches. To use them, move before the '*)' and edit as appropriate.
517     # It is not a good idea to set ccflags to an absolute value here, as it
518     # often contains general -D defines which are needed for correct
519     # compilation. It is better to edit ccflags as shown, using interpolation
520     # to add flags, or sed to remove flags.
521
522     av) ccflags=`echo $ccflags | sed -e s/-pipe//` ;;
523     deb) ccflags="$ccflags -fno-jump-tables" ;;
524     hv) warn=`echo $warn | sed -e s/-Wextra//` ;;
525     toke) optimize=-O0 ;;
526     esac
527
528     echo "$cc -c -DPERL_CORE $ccflags $stdflags $optimize $warn $extra"
529
530     . $TOP/config.sh
531
532     # end per file behaviour
533 done
534 !NO!SUBS!
535 chmod 755 cflags
536 $eunicefix cflags