This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Porting/Maintainers.pl - NEXT is CUSTOMIZED
[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 #ifdef I_STDLIB
77 #include <stdlib.h>
78 #endif
79 #ifdef I_STDDEF
80 #include <stddef.h>
81 #endif
82 #ifdef I_STDARG
83 #include <stdarg.h>
84 #endif
85 #ifdef I_LIMITS
86 #include <limits.h>
87 #endif
88 #ifdef I_DIRENT
89 #include <dirent.h>
90 #endif
91 #ifdef I_UNISTD
92 #include <unistd.h>
93 #endif
94 #ifdef I_SYS_TYPES
95 #include <sys/types.h>
96 #endif
97 #ifdef I_SYS_PARAM
98 #include <sys/param.h>
99 #endif
100 #ifdef I_SYS_RESOURCE
101 #include <sys/resource.h>
102 #endif
103 #ifdef I_SYS_SELECT
104 #include <sys/select.h>
105 #endif
106 #if defined(HAS_SOCKET) && !defined(VMS) && !defined(WIN32) /* See perl.h. */
107 #include <sys/socket.h>
108 #endif
109 #ifdef I_SYS_STAT
110 #include <sys/stat.h>
111 #endif
112 #ifdef I_SYS_TIME
113 #include <sys/time.h>
114 #endif
115 #ifdef I_SYS_TIMES
116 #include <sys/times.h>
117 #endif
118 #ifdef I_SYS_WAIT
119 #include <sys/wait.h>
120 #endif
121 /* The gcc -ansi can cause a lot of noise in Solaris because of:
122  /usr/include/sys/resource.h:148: warning: 'struct rlimit64' declared inside parameter list
123  */
124 int main(int argc, char *argv[]) {
125
126 /* Add here test code found to be problematic in some gcc platform. */
127
128 /* Off_t/off_t is a struct in Solaris with largefiles, and with gcc -ansi
129  * that struct cannot be compared in some gcc releases with a flat
130  * integer, such as a STRLEN. */
131
132   IV iv;
133   Off_t t0a = 2;
134   STRLEN t0b = 3;
135   int t0c = (STRLEN)t0a == t0b;
136
137   printf("%s: %d\n", argv[0], argc);
138
139 /* In FreeBSD 6.2 (and probably other releases too), with -Duse64bitint,
140    perl will use atoll(3).  However, that declaration is hidden in <stdlib.h>
141    if we force the compiler to use -std=c89 mode.
142 */
143   iv = Atol("42");
144
145   return (!t0c && (iv == 42)) ? 0 : -1; /* Try to avoid 'unused' warnings. */
146 }
147 __EOT__
148
149 stdflags=''
150
151 # Further gcc warning options.  Build up a list of options that work.
152 # Note that some problems may only show up with combinations of options,
153 # e.g. a warning might show up only with -Wall -ansi, not with either
154 # one individually.
155 # TODO:  Ponder whether to migrate this back to Configure so hints files can
156 # tweak it.  Also, be paranoid about whether results we've deduced in Configure
157 # (especially about things like long long, which are not in C89) will still be
158 # valid if we now add flags like -std=c89.
159
160 pedantic=''
161 case "$gccansipedantic" in
162 define) pedantic='-pedantic' ;;
163 esac
164
165 case "$gccversion" in
166 '') ;;
167 [12]*) ;; # gcc versions 1 (gasp!) and 2 are not good for this.
168 Intel*) ;; # # Is that you, Intel C++?
169 #
170 # NOTE 1: the -std=c89 without -pedantic is a bit pointless.
171 # Just -std=c89 means "if there is room for interpretation,
172 # interpret the C89 way."  It does NOT mean "strict C89" on its own.
173 # You need to add the -pedantic for that.  To do this with Configure,
174 # do -Dgccansipedantic (note that the -ansi is included in any case,
175 # the option is a bit oddly named, for historical reasons.)
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 minimum of 509 bytes.
183 #
184 # NOTE 3: the relative order of these options matters:
185 # -Wextra before -W
186 # -std=c89 before -ansi
187 # -pedantic* before -Werror=d-a-s
188 #
189 *)  for opt in -std=c89 -ansi $pedantic \
190                 -Werror=declaration-after-statement \
191                 -Werror=pointer-arith \
192                 -Wextra -W \
193                 -Wc++-compat -Wwrite-strings
194     do
195        case " $ccflags " in
196        *" $opt "*) ;; # Skip if already there.
197        *) rm -f _cflags$_exe
198           flags="-DPERL_NO_INLINE_FUNCTIONS $ccflags $warn $stdflags $opt"
199           case "$opt" in
200           *-pedantic*) flags="$flags -DPERL_GCC_PEDANTIC" ;;
201           esac
202           # echo "opt = $opt, flags = $flags"
203           cmd="$cc $flags _cflags.c -o _cflags$_exe"
204           out="`$cmd 2>&1`"
205           # echo "$cmd --> $out"
206           case "$out" in
207           *"unrecognized"*) ;;
208           *"unknown"*) ;;
209           *"implicit declaration"*) ;; # Was something useful hidden?
210           *"Invalid"*) ;;
211           *"is valid for C"*) ;;
212           *) if test -x _cflags$_exe
213              then
214                case "$opt" in
215                -std*)
216                  echo "cflags.SH: Adding $opt."
217                  stdflags="$stdflags $opt"
218                  ;;
219                -ansi)
220                  # -std=c89 is the modern form of -ansi, so add
221                  # -ansi only if -std=c89 is not there already.
222                  case " $stdflags " in
223                  *-std=c89*) ;;
224                  *)
225                    echo "cflags.SH: Adding $opt."
226                    stdflags="$stdflags $opt"
227                    ;;
228                  esac
229                  ;;
230                -W)
231                  # -Wextra is the modern form of -W, so add
232                  # -W only if -Wextra is not there already.
233                  case " $warn " in
234                  *-Wextra*) ;;
235                  *)
236                    echo "cflags.SH: Adding $opt."
237                    warn="$warn $opt"
238                    ;;
239                  esac
240                  ;;
241                -Werror=declaration-after-statement)
242                   # -pedantic* (with -std=c89) covers -Werror=d-a-s.
243                   case "$stdflags$warn" in
244                   *-std=c89*-pedantic*|*-pedantic*-std=c89*) ;;
245                   *)
246                      echo "cflags.SH: Adding $opt."
247                      warn="$warn $opt"
248                      ;;
249                   esac
250                   ;;
251                -Werror=pointer-arith)
252                   # -pedantic* covers -Werror=p-a
253                   case "$warn" in
254                   *-pedantic*) ;;
255                   *)
256                      echo "cflags.SH: Adding $opt."
257                      warn="$warn $opt"
258                      ;;
259                   esac
260                   ;;
261                *)
262                   echo "cflags.SH: Adding $opt."
263                   warn="$warn $opt"
264                   ;;
265                esac
266              fi
267              ;;
268           esac
269           ;;
270        esac
271        case "$ccflags$warn" in
272        *-pedantic*)
273          overlength=''
274          case "$ccflags$optimize" in
275          *-DDEBUGGING*) overlength='-Wno-overlength-strings' ;;
276          esac
277          for opt2 in -DPERL_GCC_PEDANTIC $overlength
278          do
279            case "$ccflags$warn" in
280            *"$opt2"*) ;;
281            *) echo "cflags.SH: Adding $opt2 because of -pedantic."
282               warn="$warn $opt2" ;;
283            esac
284          done
285          ;;
286        esac
287     done
288     ;;
289 esac
290 rm -f _cflags.c _cflags$_exe
291
292 case "$gccversion" in
293 '') ;;
294 *)
295   case "$warn$ccflags" in
296   *-pedantic*)
297     # If we have -Duse64bitint (or equivalent) in effect and the quadtype
298     # has become 'long long', gcc -pedantic* becomes unbearable
299     # (moreso when combined with -Wall) because long long and LL and %lld|%Ld
300     # become warn-worthy.  So let's drop the -pedantic in that case.
301     #
302     # Similarly, since 'long long' isn't part of C89, FreeBSD 6.2 headers
303     # don't declare atoll() under -std=c89, but we need it.  In general,
304     # insisting on -std=c89 is inconsistent with insisting on using
305     # 'long long'. So drop -std=c89 and -ansi as well if we're using
306     # 'long long' as our main integral type.
307     #
308     # usedtrace (DTrace) uses unportable features (dollars in identifiers,
309     # and gcc statement expressions), it is just easier to turn off pedantic.
310     remove=''
311     case "$quadtype:$ivtype:$sPRId64:$usedtrace" in
312     *"long long"*|*lld*|*Ld*) remove='long long' ;;
313     *) case "$usedtrace" in
314        define) remove='usedtrace' ;;
315        esac
316        ;;
317     esac
318     case "$remove" in
319     '') ;;
320     *) echo "cflags.SH: Removing -pedantic*, -std=c89, and -ansi because of $remove."
321       ccflags=`echo $ccflags|sed -e 's/-pedantic-errors/ /' -e 's/-pedantic/ /' -e 's/-std=c89/ /' -e 's/-ansi/ /' -e 's/-DPERL_GCC_PEDANTIC/ /'`
322       warn=`echo $warn|sed -e 's/-pedantic-errors/ /' -e 's/-pedantic/ /' -e 's/-ansi/ /' -e 's/-DPERL_GCC_PEDANTIC/ /'`
323       stdflags=`echo $stdflags|sed -e 's/-std=c89/ /'`
324       ;;
325     esac
326     ;;
327   esac
328   ;;
329 esac
330
331 # Older clang releases are not wise enough for -Wunused-value.
332 case "$gccversion" in
333 *"Apple LLVM "[34]*|*"Apple LLVM version "[34]*)
334   for f in -Wno-unused-value
335   do
336     echo "cflags.SH: Adding $f because clang version '$gccversion'" 
337     warn="$warn $f"
338   done
339   ;;
340 esac
341
342 # The quadmath Q format specifier will cause -Wformat to whine.
343 case "$gccversion" in
344 '') ;;
345 *) case "$usequadmath" in
346    define)
347      for f in -Wno-format
348      do
349        echo "cflags.SH: Adding $f because of usequadmath."
350        warn="$warn $f"
351      done
352     ;;
353   esac
354   ;;
355 esac
356
357 case "$cc" in
358 *g++*)
359   # Extra paranoia in case people have bad canned ccflags:
360   # bad in the sense that the flags are accepted by g++,
361   # but then whined about.
362   #
363   # -Werror=d-a-s option is valid for g++, by definition,
364   # but we remove it just for cleanliness and shorter command lines.
365   for f in -Wdeclaration-after-statement \
366                 -Werror=declaration-after-statement \
367                 -Wc++-compat \
368                 -std=c89
369   do
370     case "$ccflags$warn" in
371     *"$f"*)
372       echo "cflags.SH: Removing $f because of g++."
373       ccflags=`echo $ccflags|sed 's/$f/ /'`
374       warn=`echo $warn|sed 's/$f/ /'`
375       ;;
376     esac
377   done
378   ;;
379 esac
380
381 for f in -Wdeclaration-after-statement -Werror=declaration-after-statement \
382          -Wpointer-arith -Werror=pointer-arith
383 do
384   case "$cppflags" in
385   *"$f"*)
386     echo "cflags.SH: Removing $f from cppflags."
387     cppflags=`echo $cppflags|sed 's/$f/ /'` ;;
388   esac
389 done
390
391 # If usethreads and clang, add -Wthread-safety for clang 3.6 or later.
392 # gccversion is defined also for clang, because compat, use that for matching.
393 # Apple overwrites clang version with XCode version, see hints/darwin.sh
394 # for the gory details.  Aggressively forward-proofing.
395 case "$usethreads" in
396 define)
397 case "$gccversion" in
398 *" Clang 3."[56789]*|*" Clang "[456]*|*"Apple LLVM 6.1"*|*"Apple LLVM "[789]*)
399   for f in -Wthread-safety
400   do
401     case " $warn " in
402     *" $f "*) ;; # Skip if already there.
403     *)
404       echo "cflags.SH: Adding $f because usethreads and clang and gccversion '$gccversion'"
405       warn="$warn $f"
406       ;;
407     esac
408   done
409 ;;
410 esac
411 ;;
412 esac
413
414 echo "cflags.SH: cc       = $cc"
415 echo "cflags.SH: ccflags  = $ccflags"
416 echo "cflags.SH: stdflags = $stdflags"
417 echo "cflags.SH: optimize = $optimize"
418 echo "cflags.SH: warn     = $warn"
419
420 # Code to set any extra flags here.
421 extra=''
422
423 # Protect double or single quotes for better restoring of ccflags.
424 myccflags=`echo $ccflags | sed -e 's/"/\\\"/g' -e "s/'/\\\'/g"`
425
426 echo "Extracting cflags (with variable substitutions)"
427 # This section of the file will have variable substitutions done on it.
428 # Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
429 # Protect any dollar signs and backticks that you do not want interpreted
430 # by putting a backslash in front.  You may delete these comments.
431 rm -f cflags
432 $spitshell >cflags <<!GROK!THIS!
433 $startsh
434
435 # !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
436
437 # This file is generated by cflags.SH
438
439 # Used to restore possible edits by cflags.SH.
440 myccflags="$myccflags"
441
442 # Extra warnings, used e.g. for gcc.
443 warn="$warn"
444 # Extra standardness.
445 stdflags="$stdflags"
446 # Extra extra.
447 extra="$extra"
448 # what do executables look like?
449 _exe="$_exe"
450
451 !GROK!THIS!
452
453 # In the following dollars and backticks do not need the extra backslash.
454 $spitshell >>cflags <<'!NO!SUBS!'
455 case $PERL_CONFIG_SH in
456 '')
457         if test -f config.sh; then TOP=.;
458         elif test -f ../config.sh; then TOP=..;
459         elif test -f ../../config.sh; then TOP=../..;
460         elif test -f ../../../config.sh; then TOP=../../..;
461         elif test -f ../../../../config.sh; then TOP=../../../..;
462         else
463                 echo "Can't find config.sh."; exit 1
464         fi
465         . $TOP/config.sh
466         ccflags="$myccflags"  # Restore possible edits by cflags.SH.
467         ;;
468 esac
469
470 # syntax: cflags [optimize=XXX] [file[.suffix]] ...
471 #   displays the proposed compiler command line for each 'file'
472 #
473 #   with no file, dispalys it for all *.c files.
474 #   The optimise=XXX arg (if present) is evalled, setting the default
475 #   value of the $optimise variable, which is output on the command line
476 #   (but which may be overridden for specific files below)
477
478 case "X$1" in
479 Xoptimize=*|X"optimize=*")
480         eval "$1"
481         shift
482         ;;
483 esac
484
485 case $# in
486 0) set *.c; echo "The current C flags are:" ;;
487 esac
488
489 set `echo "$* " | sed -e 's/\.[oc] / /g' -e 's/\.obj / /g' -e "s/\\$obj_ext / /g"`
490
491 for file do
492
493     case "$#" in
494     1) ;;
495     *) echo $n "    $file.c     $c" ;;
496     esac
497
498     # allow variables like toke_cflags to be evaluated
499
500     case "$file" in
501     */*) ;;
502     *) eval 'eval ${'"${file}_cflags"'-""}' ;;
503     esac
504
505     # or customize here
506
507     case "$file" in
508     regcomp) : work around http://bugs.debian.org/754054
509         case $archname in
510         mips-*|mipsel-*)
511             optimize="$optimize -fno-tree-vrp";;
512         esac;;
513     *) ;;
514
515     # Customization examples follow.
516     #
517     # The examples are intentionally unreachable as the '*)' case above always
518     # matches. To use them, move before the '*)' and edit as appropriate.
519     # It is not a good idea to set ccflags to an absolute value here, as it
520     # often contains general -D defines which are needed for correct
521     # compilation. It is better to edit ccflags as shown, using interpolation
522     # to add flags, or sed to remove flags.
523
524     av) ccflags=`echo $ccflags | sed -e s/-pipe//` ;;
525     deb) ccflags="$ccflags -fno-jump-tables" ;;
526     hv) warn=`echo $warn | sed -e s/-Wextra//` ;;
527     toke) optimize=-O0 ;;
528     esac
529
530     # Can we perhaps use $ansi2knr here
531     echo "$cc -c -DPERL_CORE $ccflags $stdflags $optimize $warn $extra"
532
533     . $TOP/config.sh
534
535     # end per file behaviour
536 done
537 !NO!SUBS!
538 chmod 755 cflags
539 $eunicefix cflags