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. */
95 #include <sys/types.h>
98 #include <sys/param.h>
100 #ifdef I_SYS_RESOURCE
101 #include <sys/resource.h>
104 #include <sys/select.h>
106 #if defined(HAS_SOCKET) && !defined(VMS) && !defined(WIN32) /* See perl.h. */
107 #include <sys/socket.h>
110 #include <sys/stat.h>
113 #include <sys/time.h>
116 #include <sys/times.h>
119 #include <sys/wait.h>
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
124 int main(int argc, char *argv[]) {
126 /* Add here test code found to be problematic in some gcc platform. */
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. */
135 int t0c = (STRLEN)t0a == t0b;
137 printf("%s: %d\n", argv[0], argc);
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.
145 return (!t0c && (iv == 42)) ? 0 : -1; /* Try to avoid 'unused' warnings. */
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
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.
161 case "$gccansipedantic" in
162 define) pedantic='-pedantic' ;;
165 case "$gccversion" in
167 [12]*) ;; # gcc versions 1 (gasp!) and 2 are not good for this.
168 Intel*) ;; # # Is that you, Intel C++?
170 # NOTE 1: the -std=c89 without -pedantic is a bit pointless,
171 # so we will not add it here. You will have to use e.g.
172 # Configure -Accflags=-std=c89
174 # Just -std=c89 means "if there is room for interpretation,
175 # interpret the C89 way." It does NOT mean "strict C89" on its own.
176 # You need to add the -pedantic for that. To do this with Configure,
177 # do -Dgccansipedantic (note that the -ansi is included in any case,
178 # the option is a bit oddly named, for historical reasons.)
180 # Furthermore, -std=c89 disables/hides/makes harder to use certain
181 # non-C89 features like long long.
183 # NOTE 2: -pedantic necessitates adding a couple of flags:
184 # * -PERL_GCC_PEDANTIC so that the perl code can adapt: there's nothing
185 # added by gcc itself to indicate pedanticness.
186 # * -Wno-overlength-strings under -DDEBUGGING because quite many of
187 # the LEAVE_with_name() and assert() calls generate string literals
188 # longer then the ANSI minimum of 509 bytes.
190 # NOTE 3: the relative order of these options matters:
191 # -Wextra before -W, and -pedantic* before -Werror=d-a-s.
193 *) for opt in -ansi $pedantic \
194 -Werror=declaration-after-statement \
196 -Wc++-compat -Wwrite-strings
199 *" $opt "*) ;; # Skip if already there.
200 *) rm -f _cflags$_exe
201 flags="-DPERL_NO_INLINE_FUNCTIONS $ccflags $warn $stdflags $opt"
203 *-pedantic*) flags="$flags -DPERL_GCC_PEDANTIC" ;;
205 # echo "opt = $opt, flags = $flags"
206 cmd="$cc $flags _cflags.c -o _cflags$_exe"
208 # echo "$cmd --> $out"
212 *"implicit declaration"*) ;; # Was something useful hidden?
214 *"is valid for C"*) ;;
215 *) if test -x _cflags$_exe
219 echo "cflags.SH: Adding $opt."
220 stdflags="$stdflags $opt"
224 # -Wextra is the modern form of -W, so add
225 # -W only if -Wextra is not there already.
229 echo "cflags.SH: Adding $opt."
234 -Werror=declaration-after-statement)
235 # -pedantic* (with -std=c89) covers -Werror=d-a-s.
236 case "$stdflags$warn" in
237 *-std=c89*-pedantic*|*-pedantic*-std=c89*) ;;
239 echo "cflags.SH: Adding $opt."
245 echo "cflags.SH: Adding $opt."
255 case "$ccflags$warn" in
258 case "$ccflags$optimize" in
259 *-DDEBUGGING*) overlength='-Wno-overlength-strings' ;;
261 for opt2 in -DPERL_GCC_PEDANTIC $overlength
263 case "$ccflags$warn" in
265 *) echo "cflags.SH: Adding $opt2 because of -pedantic."
266 warn="$warn $opt2" ;;
274 rm -f _cflags.c _cflags$_exe
276 case "$gccversion" in
279 case "$warn$ccflags" in
281 # If we have -Duse64bitint (or equivalent) in effect and the quadtype
282 # has become 'long long', gcc -pedantic* becomes unbearable
283 # (moreso when combined with -Wall) because long long and LL and %lld|%Ld
284 # become warn-worthy. So let's drop the -pedantic in that case.
286 # Similarly, since 'long long' isn't part of C89, FreeBSD 6.2 headers
287 # don't declare atoll() under -std=c89, but we need it. In general,
288 # insisting on -std=c89 is inconsistent with insisting on using
289 # 'long long'. So drop -std=c89 and -ansi as well if we're using
290 # 'long long' as our main integral type.
292 # usedtrace (DTrace) uses unportable features (dollars in identifiers,
293 # and gcc statement expressions), it is just easier to turn off pedantic.
295 case "$quadtype:$ivtype:$sPRId64:$usedtrace" in
296 *"long long"*|*lld*|*Ld*) remove='long long' ;;
297 *) case "$usedtrace" in
298 define) remove='usedtrace' ;;
304 *) echo "cflags.SH: Removing -pedantic*, -std=c89, and -ansi because of $remove."
305 ccflags=`echo $ccflags|sed -e 's/-pedantic-errors/ /' -e 's/-pedantic/ /' -e 's/-std=c89/ /' -e 's/-ansi/ /' -e 's/-DPERL_GCC_PEDANTIC/ /'`
306 warn=`echo $warn|sed -e 's/-pedantic-errors/ /' -e 's/-pedantic/ /' -e 's/-ansi/ /' -e 's/-DPERL_GCC_PEDANTIC/ /'`
307 stdflags=`echo $stdflags|sed -e 's/-std=c89/ /'`
315 # Older clang releases are not wise enough for -Wunused-value.
316 case "$gccversion" in
317 *"Apple LLVM "[34]*|*"Apple LLVM version "[34]*)
318 for f in -Wno-unused-value
320 echo "cflags.SH: Adding $f because clang version '$gccversion'"
326 # The quadmath Q format specifier will cause -Wformat to whine.
327 case "$gccversion" in
329 *) case "$usequadmath" in
333 echo "cflags.SH: Adding $f because of usequadmath."
343 # Extra paranoia in case people have bad canned ccflags:
344 # bad in the sense that the flags are accepted by g++,
345 # but then whined about.
347 # -Werror=d-a-s option is valid for g++, by definition,
348 # but we remove it just for cleanliness and shorter command lines.
349 for f in -Wdeclaration-after-statement \
350 -Werror=declaration-after-statement \
354 case "$ccflags$warn" in
356 echo "cflags.SH: Removing $f because of g++."
357 ccflags=`echo $ccflags|sed 's/$f/ /'`
358 warn=`echo $warn|sed 's/$f/ /'`
365 for f in -Wdeclaration-after-statement -Werror=declaration-after-statement
369 echo "cflags.SH: Removing $f from cppflags."
370 cppflags=`echo $cppflags|sed 's/$f/ /'` ;;
374 echo "cflags.SH: cc = $cc"
375 echo "cflags.SH: ccflags = $ccflags"
376 echo "cflags.SH: stdflags = $stdflags"
377 echo "cflags.SH: optimize = $optimize"
378 echo "cflags.SH: warn = $warn"
380 # Code to set any extra flags here.
383 echo "Extracting cflags (with variable substitutions)"
384 # This section of the file will have variable substitutions done on it.
385 # Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
386 # Protect any dollar signs and backticks that you do not want interpreted
387 # by putting a backslash in front. You may delete these comments.
389 $spitshell >cflags <<!GROK!THIS!
392 # !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
394 # This file is generated by cflags.SH
396 # Used to restore possible edits by cflags.SH.
398 # Extra warnings, used e.g. for gcc.
400 # Extra standardness.
404 # what do executables look like?
409 # In the following dollars and backticks do not need the extra backslash.
410 $spitshell >>cflags <<'!NO!SUBS!'
411 case $PERL_CONFIG_SH in
413 if test -f config.sh; then TOP=.;
414 elif test -f ../config.sh; then TOP=..;
415 elif test -f ../../config.sh; then TOP=../..;
416 elif test -f ../../../config.sh; then TOP=../../..;
417 elif test -f ../../../../config.sh; then TOP=../../../..;
419 echo "Can't find config.sh."; exit 1
422 ccflags="$myccflags" # Restore possible edits by cflags.SH.
426 # syntax: cflags [optimize=XXX] [file[.suffix]] ...
427 # displays the proposed compiler command line for each 'file'
429 # with no file, dispalys it for all *.c files.
430 # The optimise=XXX arg (if present) is evalled, setting the default
431 # value of the $optimise variable, which is output on the command line
432 # (but which may be overridden for specific files below)
435 Xoptimize=*|X"optimize=*")
442 0) set *.c; echo "The current C flags are:" ;;
445 set `echo "$* " | sed -e 's/\.[oc] / /g' -e 's/\.obj / /g' -e "s/\\$obj_ext / /g"`
451 *) echo $n " $file.c $c" ;;
454 # allow variables like toke_cflags to be evaluated
456 if echo $file | grep -v / >/dev/null
458 eval 'eval ${'"${file}_cflags"'-""}'
464 regcomp) : work around http://bugs.debian.org/754054
467 optimize="$optimize -fno-tree-vrp";;
471 # Customization examples follow.
473 # The examples are intentionally unreachable as the '*)' case above always
474 # matches. To use them, move before the '*)' and edit as appropriate.
475 # It is not a good idea to set ccflags to an absolute value here, as it
476 # often contains general -D defines which are needed for correct
477 # compilation. It is better to edit ccflags as shown, using interpolation
478 # to add flags, or sed to remove flags.
480 av) ccflags=`echo $ccflags | sed -e s/-pipe//` ;;
481 deb) ccflags="$ccflags -fno-jump-tables" ;;
482 hv) warn=`echo $warn | sed -e s/-Wextra//` ;;
483 toke) optimize=-O0 ;;
486 # Can we perhaps use $ansi2knr here
487 echo "$cc -c -DPERL_CORE $ccflags $stdflags $optimize $warn $extra"
491 # end per file behaviour