3 # Generate the cflags script, which is used to determine what cflags
4 # to pass to the compiler for compiling the core perl.
6 # This does NOT affect the XS compilation (ext, dist, cpan)
7 # since that uses %Config values directly.
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.
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.
18 # From this we initialise the following variables in the cflags script:
20 # $myccflags (possibly edited version of $Config{ccflags})
26 case $PERL_CONFIG_SH in
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=../../../..;
34 echo "Can't find config.sh."; exit 1
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.
42 */*) cd `expr X$0 : 'X\(.*\)/'` ;;
45 if test -f config_h.SH -a ! -f config.h; then
52 # Add -Wall for the core modules iff gcc and not already -Wall
55 Intel*) ;; # The Intel C++ plays gcc on TV but is not really it.
58 *) warn="$warn -Wall" ;;
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.
67 cat >_cflags.c <<__EOT__
70 /* The stdio.h, errno.h, and setjmp.h should be there in any ANSI C89. */
74 /* Just in case the inclusion of perl.h did not
75 * pull in enough system headers, let's try again. */
87 #include <sys/types.h>
90 #include <sys/param.h>
93 #include <sys/resource.h>
96 #include <sys/select.h>
98 #if defined(HAS_SOCKET) && !defined(VMS) && !defined(WIN32) /* See perl.h. */
99 #include <sys/socket.h>
102 #include <sys/stat.h>
105 #include <sys/time.h>
108 #include <sys/times.h>
111 #include <sys/wait.h>
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
116 int main(int argc, char *argv[]) {
118 /* Add here test code found to be problematic in some gcc platform. */
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. */
127 int t0c = (STRLEN)t0a == t0b;
129 printf("%s: %d\n", argv[0], argc);
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.
137 return (!t0c && (iv == 42)) ? 0 : -1; /* Try to avoid 'unused' warnings. */
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
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.
152 case "$gccansipedantic" in
153 define) pedantic='-pedantic' ;;
156 case "$gccversion" in
158 [12].*) ;; # gcc versions 1 (gasp!) and 2 are not good for this.
159 Intel*) ;; # # Is that you, Intel C++?
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.
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
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.
184 # NOTE 3: the relative order of these options matters:
185 # -Wextra before -Wno-long-long -Wno-declaration-after-statement
187 *) warns="$pedantic \
188 -Werror=pointer-arith \
191 -Wno-long-long -Wno-declaration-after-statement \
192 -Wc++-compat -Wwrite-strings"
194 *" -std="*) ;; # Already have -std=...
195 *) warns="-std=c99 $warns" ;;
200 *" $opt "*) ;; # Skip if already there.
201 *) rm -f _cflags$_exe
202 flags="-DPERL_NO_INLINE_FUNCTIONS $ccflags $warn $stdflags $opt"
204 *-pedantic*) flags="$flags -DPERL_GCC_PEDANTIC" ;;
206 # echo "opt = $opt, flags = $flags"
207 cmd="$cc $flags _cflags.c -o _cflags$_exe"
209 # echo "$cmd --> $out"
213 *"implicit declaration"*) ;; # Was something useful hidden?
215 *"is valid for C"*) ;;
216 *) if test -x _cflags$_exe
220 echo "cflags.SH: Adding $opt."
221 stdflags="$stdflags $opt"
223 -Werror=pointer-arith)
224 # -pedantic* covers -Werror=p-a
228 echo "cflags.SH: Adding $opt."
234 echo "cflags.SH: Adding $opt."
243 case "$ccflags$warn" in
246 case "$ccflags$optimize" in
247 *-DDEBUGGING*) overlength='-Wno-overlength-strings' ;;
249 for opt2 in -DPERL_GCC_PEDANTIC $overlength
251 case "$ccflags$warn" in
253 *) echo "cflags.SH: Adding $opt2 because of -pedantic."
254 warn="$warn $opt2" ;;
262 rm -f _cflags.c _cflags$_exe
264 case "$gccversion" in
267 case "$warn$ccflags" in
269 # For -std=c99 -pedantic, only the %Ld format seems to be warn-worthy.
270 # 'long long' and '%lld' are now kosher.
272 # usedtrace (DTrace) uses unportable features (dollars in identifiers,
273 # and gcc statement expressions), it is just easier to turn off pedantic.
275 case "$quadtype:$ivtype:$sPRId64:$usedtrace" in
276 **Ld*) remove='Ld' ;;
277 *) case "$usedtrace" in
278 define) remove='usedtrace' ;;
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/ /'`
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
299 echo "cflags.SH: Adding $f because clang version '$gccversion'"
305 # The quadmath Q format specifier will cause -Wformat to whine.
306 case "$gccversion" in
308 *) case "$usequadmath" in
312 echo "cflags.SH: Adding $f because of usequadmath."
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
327 case "$ccflags$warn" in
329 echo "cflags.SH: Removing $f because of g++."
330 ccflags=`echo $ccflags|sed 's/$f/ /'`
331 warn=`echo $warn|sed 's/$f/ /'`
338 for f in -Wpointer-arith -Werror=pointer-arith
342 echo "cflags.SH: Removing $f from cppflags."
343 cppflags=`echo $cppflags|sed 's/$f/ /'` ;;
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
353 case "$gccversion" in
354 *" Clang 3."[56789]*|*" Clang "[456]*|*"Apple LLVM 6.1"*|*"Apple LLVM "[789]*)
355 for f in -Wthread-safety
358 *" $f "*) ;; # Skip if already there.
360 echo "cflags.SH: Adding $f because usethreads and clang and gccversion '$gccversion'"
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
375 for f in -Wno-use-after-free
377 echo "cflags.SH: Adding $f because of false positives in gccversion '$gccversion'"
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"
389 # Code to set any extra flags here.
392 # Protect double or single quotes for better restoring of ccflags.
393 myccflags=`echo $ccflags | sed -e 's/"/\\\"/g' -e "s/'/\\\'/g"`
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.
401 $spitshell >cflags <<!GROK!THIS!
404 # !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
406 # This file is generated by cflags.SH
408 # Used to restore possible edits by cflags.SH.
409 myccflags="$myccflags"
411 # Extra warnings, used e.g. for gcc.
413 # Extra standardness.
417 # what do executables look like?
422 # In the following dollars and backticks do not need the extra backslash.
423 $spitshell >>cflags <<'!NO!SUBS!'
424 case $PERL_CONFIG_SH in
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=../../../..;
432 echo "Can't find config.sh."; exit 1
435 ccflags="$myccflags" # Restore possible edits by cflags.SH.
439 # syntax: cflags [optimize=XXX] [file[.suffix]] ...
440 # displays the proposed compiler command line for each 'file'
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)
448 Xoptimize=*|X"optimize=*")
455 0) set *.c; echo "The current C flags are:" ;;
458 set `echo "$* " | sed -e 's/\.[oc] / /g' -e 's/\.obj / /g' -e "s/\\$obj_ext / /g"`
464 *) echo $n " $file.c $c" ;;
467 # allow variables like toke_cflags to be evaluated
471 *) eval 'eval ${'"${file}_cflags"'-""}' ;;
477 regcomp) : work around http://bugs.debian.org/754054
480 optimize="$optimize -fno-tree-vrp";;
484 # Customization examples follow.
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.
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 ;;
499 echo "$cc -c -DPERL_CORE $ccflags $stdflags $optimize $warn $extra"
503 # end per file behaviour