]> perl5.git.perl.org Git - perl5.git/blob - cflags.SH This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Deparse: exclude two new test files
[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 -Wno-long-long -Wno-declaration-after-statement
186 #
187 *) warns="$pedantic \
188         -Werror=pointer-arith \
189         -Werror=vla \
190         -Wextra \
191         -Wno-long-long -Wno-declaration-after-statement \
192         -Wc++-compat -Wwrite-strings"
193    case " $ccflags " in
194    *" -std="*) ;; # Already have -std=...
195    *) warns="-std=c99 $warns" ;;
196    esac
197    for opt in $warns
198     do
199        case " $ccflags " in
200        *" $opt "*) ;; # Skip if already there.
201        *) rm -f _cflags$_exe
202           flags="-DPERL_NO_INLINE_FUNCTIONS $ccflags $warn $stdflags $opt"
203           case "$opt" in
204           *-pedantic*) flags="$flags -DPERL_GCC_PEDANTIC" ;;
205           esac
206           # echo "opt = $opt, flags = $flags"
207           cmd="$cc $flags _cflags.c -o _cflags$_exe"
208           out="`$cmd 2>&1`"
209           # echo "$cmd --> $out"
210           case "$out" in
211           *"unrecognized"*) ;;
212           *"unknown"*) ;;
213           *"implicit declaration"*) ;; # Was something useful hidden?
214           *"Invalid"*) ;;
215           *"is valid for C"*) ;;
216           *) if test -x _cflags$_exe
217              then
218                case "$opt" in
219                -std*)
220                  echo "cflags.SH: Adding $opt."
221                  stdflags="$stdflags $opt"
222                  ;;
223                -Werror=pointer-arith)
224                   # -pedantic* covers -Werror=p-a
225                   case "$warn" in
226                   *-pedantic*) ;;
227                   *)
228                      echo "cflags.SH: Adding $opt."
229                      warn="$warn $opt"
230                      ;;
231                   esac
232                   ;;
233                *)
234                   echo "cflags.SH: Adding $opt."
235                   warn="$warn $opt"
236                   ;;
237                esac
238              fi
239              ;;
240           esac
241           ;;
242        esac
243        case "$ccflags$warn" in
244        *-pedantic*)
245          overlength=''
246          case "$ccflags$optimize" in
247          *-DDEBUGGING*) overlength='-Wno-overlength-strings' ;;
248          esac
249          for opt2 in -DPERL_GCC_PEDANTIC $overlength
250          do
251            case "$ccflags$warn" in
252            *"$opt2"*) ;;
253            *) echo "cflags.SH: Adding $opt2 because of -pedantic."
254               warn="$warn $opt2" ;;
255            esac
256          done
257          ;;
258        esac
259     done
260     ;;
261 esac
262 rm -f _cflags.c _cflags$_exe
263
264 case "$gccversion" in
265 '') ;;
266 *)
267   case "$warn$ccflags" in
268   *-pedantic*)
269     # For -std=c99 -pedantic, only the %Ld format seems to be warn-worthy.
270     # 'long long' and '%lld' are now kosher.
271     #
272     # usedtrace (DTrace) uses unportable features (dollars in identifiers,
273     # and gcc statement expressions), it is just easier to turn off pedantic.
274     remove=''
275     case "$quadtype:$ivtype:$sPRId64:$usedtrace" in
276     **Ld*) remove='Ld' ;;
277     *) case "$usedtrace" in
278        define) remove='usedtrace' ;;
279        esac
280        ;;
281     esac
282     case "$remove" in
283     '') ;;
284     *) echo "cflags.SH: Removing -pedantic* -ansi because of $remove."
285       ccflags=`echo $ccflags|sed -e 's/-pedantic-errors/ /' -e 's/-pedantic/ /'`
286       warn=`echo $warn|sed -e 's/-pedantic-errors/ /' -e 's/-pedantic/ /'`
287       ;;
288     esac
289     ;;
290   esac
291   ;;
292 esac
293
294 # Older clang releases are not wise enough for -Wunused-value.
295 case "$gccversion" in
296 *"Apple LLVM "[34]*|*"Apple LLVM version "[34]*)
297   for f in -Wno-unused-value
298   do
299     echo "cflags.SH: Adding $f because clang version '$gccversion'" 
300     warn="$warn $f"
301   done
302   ;;
303 esac
304
305 # The quadmath Q format specifier will cause -Wformat to whine.
306 case "$gccversion" in
307 '') ;;
308 *) case "$usequadmath" in
309    define)
310      for f in -Wno-format
311      do
312        echo "cflags.SH: Adding $f because of usequadmath."
313        warn="$warn $f"
314      done
315     ;;
316   esac
317   ;;
318 esac
319
320 case "$cc" in
321 *g++*)
322   # Extra paranoia in case people have bad canned ccflags:
323   # bad in the sense that the flags are accepted by g++,
324   # but then whined about.
325   for f in -Wc++-compat -std=c99
326   do
327     case "$ccflags$warn" in
328     *"$f"*)
329       echo "cflags.SH: Removing $f because of g++."
330       ccflags=`echo $ccflags|sed 's/$f/ /'`
331       warn=`echo $warn|sed 's/$f/ /'`
332       ;;
333     esac
334   done
335   ;;
336 esac
337
338 for f in -Wpointer-arith -Werror=pointer-arith
339 do
340   case "$cppflags" in
341   *"$f"*)
342     echo "cflags.SH: Removing $f from cppflags."
343     cppflags=`echo $cppflags|sed 's/$f/ /'` ;;
344   esac
345 done
346
347 # If usethreads and clang, add -Wthread-safety for clang 3.6 or later.
348 # gccversion is defined also for clang, because compat, use that for matching.
349 # Apple overwrites clang version with XCode version, see hints/darwin.sh
350 # for the gory details.  Aggressively forward-proofing.
351 case "$usethreads" in
352 define)
353 case "$gccversion" in
354 *" Clang 3."[56789]*|*" Clang "[456]*|*"Apple LLVM 6.1"*|*"Apple LLVM "[789]*)
355   for f in -Wthread-safety
356   do
357     case " $warn " in
358     *" $f "*) ;; # Skip if already there.
359     *)
360       echo "cflags.SH: Adding $f because usethreads and clang and gccversion '$gccversion'"
361       warn="$warn $f"
362       ;;
363     esac
364   done
365 ;;
366 esac
367 ;;
368 esac
369
370 # gcc version 12 and 13 are overly aggressive with use-after-free warnings
371 # and have false positives on code that shouldn't warn, and they haven't
372 # sorted it out yet. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108115
373 case "$gccversion" in
374 "1"[23]*)
375     for f in -Wno-use-after-free
376     do
377         echo "cflags.SH: Adding $f because of false positives in gccversion '$gccversion'"
378         warn="$warn $f"
379     done
380 ;;
381 esac
382
383 echo "cflags.SH: cc       = $cc"
384 echo "cflags.SH: ccflags  = $ccflags"
385 echo "cflags.SH: stdflags = $stdflags"
386 echo "cflags.SH: optimize = $optimize"
387 echo "cflags.SH: warn     = $warn"
388
389 # Code to set any extra flags here.
390 extra=''
391
392 # Protect double or single quotes for better restoring of ccflags.
393 myccflags=`echo $ccflags | sed -e 's/"/\\\"/g' -e "s/'/\\\'/g"`
394
395 echo "Extracting cflags (with variable substitutions)"
396 # This section of the file will have variable substitutions done on it.
397 # Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
398 # Protect any dollar signs and backticks that you do not want interpreted
399 # by putting a backslash in front.  You may delete these comments.
400 rm -f cflags
401 $spitshell >cflags <<!GROK!THIS!
402 $startsh
403
404 # !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
405
406 # This file is generated by cflags.SH
407
408 # Used to restore possible edits by cflags.SH.
409 myccflags="$myccflags"
410
411 # Extra warnings, used e.g. for gcc.
412 warn="$warn"
413 # Extra standardness.
414 stdflags="$stdflags"
415 # Extra extra.
416 extra="$extra"
417 # what do executables look like?
418 _exe="$_exe"
419
420 !GROK!THIS!
421
422 # In the following dollars and backticks do not need the extra backslash.
423 $spitshell >>cflags <<'!NO!SUBS!'
424 case $PERL_CONFIG_SH in
425 '')
426         if 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         elif test -f ../../../../config.sh; then TOP=../../../..;
431         else
432                 echo "Can't find config.sh."; exit 1
433         fi
434         . $TOP/config.sh
435         ccflags="$myccflags"  # Restore possible edits by cflags.SH.
436         ;;
437 esac
438
439 # syntax: cflags [optimize=XXX] [file[.suffix]] ...
440 #   displays the proposed compiler command line for each 'file'
441 #
442 #   with no file, dispalys it for all *.c files.
443 #   The optimise=XXX arg (if present) is evalled, setting the default
444 #   value of the $optimise variable, which is output on the command line
445 #   (but which may be overridden for specific files below)
446
447 case "X$1" in
448 Xoptimize=*|X"optimize=*")
449         eval "$1"
450         shift
451         ;;
452 esac
453
454 case $# in
455 0) set *.c; echo "The current C flags are:" ;;
456 esac
457
458 set `echo "$* " | sed -e 's/\.[oc] / /g' -e 's/\.obj / /g' -e "s/\\$obj_ext / /g"`
459
460 for file do
461
462     case "$#" in
463     1) ;;
464     *) echo $n "    $file.c     $c" ;;
465     esac
466
467     # allow variables like toke_cflags to be evaluated
468
469     case "$file" in
470     */*) ;;
471     *) eval 'eval ${'"${file}_cflags"'-""}' ;;
472     esac
473
474     # or customize here
475
476     case "$file" in
477     regcomp) : work around http://bugs.debian.org/754054
478         case $archname in
479         mips-*|mipsel-*)
480             optimize="$optimize -fno-tree-vrp";;
481         esac;;
482     *) ;;
483
484     # Customization examples follow.
485     #
486     # The examples are intentionally unreachable as the '*)' case above always
487     # matches. To use them, move before the '*)' and edit as appropriate.
488     # It is not a good idea to set ccflags to an absolute value here, as it
489     # often contains general -D defines which are needed for correct
490     # compilation. It is better to edit ccflags as shown, using interpolation
491     # to add flags, or sed to remove flags.
492
493     av) ccflags=`echo $ccflags | sed -e s/-pipe//` ;;
494     deb) ccflags="$ccflags -fno-jump-tables" ;;
495     hv) warn=`echo $warn | sed -e s/-Wextra//` ;;
496     toke) optimize=-O0 ;;
497     esac
498
499     echo "$cc -c -DPERL_CORE $ccflags $stdflags $optimize $warn $extra"
500
501     . $TOP/config.sh
502
503     # end per file behaviour
504 done
505 !NO!SUBS!
506 chmod 755 cflags
507 $eunicefix cflags