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 = t0a == t0b;
137 /* In FreeBSD 6.2 (and probably other releases too), with -Duse64bitint,
138 perl will use atoll(3). However, that declaration is hidden in <stdlib.h>
139 if we force the compiler to use -std=c89 mode.
143 return (!t0c && (iv == 42)) ? 0 : -1; /* Try to avoid 'unused' warnings. */
149 # Further gcc warning options. Build up a list of options that work.
150 # Note that some problems may only show up with combinations of options,
151 # e.g. a warning might show up only with -Wall -ansi, not with either
153 # TODO: Ponder whether to migrate this back to Configure so hints files can
154 # tweak it. Also, be paranoid about whether results we've deduced in Configure
155 # (especially about things like long long, which are not in C89) will still be
156 # valid if we now add flags like -std=c89.
158 case "$gccversion" in
160 [12]*) ;; # gcc versions 1 (gasp!) and 2 are not good for this.
161 Intel*) ;; # # Is that you, Intel C++?
162 # XXX if -pedantic (or -pedantic-errors!) is ever added,
163 # the -Werror=declaration-after-statement can be removed
164 # since -std=c89 -pedantic implies the -Werror=...
165 *) for opt in -ansi -std=c89 -W -Wextra \
166 -Werror=declaration-after-statement \
167 -Wc++-compat -Wwrite-strings
170 *" $opt "*) ;; # Skip if already there.
171 *) rm -f _cflags$_exe
172 case "`$cc -DPERL_NO_INLINE_FUNCTIONS $ccflags $warn $stdflags $opt _cflags.c -o _cflags$_exe 2>&1`" in
175 *"implicit declaration"*) ;; # Was something useful hidden?
177 *"is valid for C"*) ;;
178 *) if test -x _cflags$_exe
181 -std*) stdflags="$stdflags $opt" ;;
182 *) warn="$warn $opt" ;;
192 rm -f _cflags.c _cflags$_exe
194 case "$gccversion" in
197 if [ "$gccansipedantic" = "" ]; then
198 # If we have -Duse64bitint (or equivalent) in effect and the quadtype
199 # has become 'long long', gcc -pedantic becomes unbearable (moreso
200 # when combined with -Wall) because long long and LL and %lld|%Ld
201 # become warn-worthy. So let's drop the -pedantic in that case.
202 case "$quadtype:$sPRId64" in
203 "long long"*|*lld*|*Ld*)
204 echo "cflags.SH: Removing -pedantic and warn because of quadtype='long long'."
205 ccflags="`echo $ccflags|sed 's/-pedantic/ /'`"
206 warn="`echo $warn|sed 's/-pedantic/ /'`"
209 # Similarly, since 'long long' isn't part of C89, FreeBSD 6.2 headers
210 # don't declare atoll() under -std=c89, but we need it. In general,
211 # insisting on -std=c89 is inconsistent with insisting on using
212 # 'long long'. So drop -std=c89 and -ansi as well if we're using
213 # 'long long' as our main integral type.
216 echo "cflags.SH: Removing -pedantic, -std=c89, and -ansi because of ivtype='long long'."
217 ccflags=`echo $ccflags|sed -e 's/-pedantic/ /' -e 's/-std=c89/ /' -e 's/-ansi/ /'`
218 warn=`echo $warn|sed -e 's/-pedantic/ /' -e 's/-ansi/ /'`
219 stdflags=`echo $stdflags|sed -e 's/-std=c89/ /'`
223 # Using certain features (like the gcc statement expressions)
224 # requires knowing whether -pedantic has been specified.
225 case "$warn$ccflags" in
227 echo "cflags.SH: Adding -DPERL_PEDANTIC because of -pedantic."
228 warn="$warn -DPERL_GCC_PEDANTIC"
236 # Extra paranoia in case people have bad canned ccflags:
237 # bad in the sense that the flags are accepted by g++,
238 # but then whined about.
240 # -Werror=d-a-s option is valid for g++, by definition,
241 # but we remove it just for cleanliness and shorter command lines.
242 for f in -Wdeclaration-after-statement \
243 -Werror=declaration-after-statement \
248 echo "cflags.SH: Removing $ccflags because of g++."
249 ccflags=`echo $ccflags|sed 's/$f/ /'` ;;
255 for f in -Wdeclaration-after-statement -Werror=declaration-after-statement
259 echo "cflags.SH: Removing $f from cppflags."
260 cppflags=`echo $cppflags|sed 's/$f/ /'` ;;
264 # Code to set any extra flags here.
267 echo "Extracting cflags (with variable substitutions)"
268 # This section of the file will have variable substitutions done on it.
269 # Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
270 # Protect any dollar signs and backticks that you do not want interpreted
271 # by putting a backslash in front. You may delete these comments.
273 $spitshell >cflags <<!GROK!THIS!
276 # !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
278 # This file is generated by cflags.SH
280 # Used to restore possible edits by cflags.SH.
282 # Extra warnings, used e.g. for gcc.
284 # Extra standardness.
288 # what do executables look like?
293 # In the following dollars and backticks do not need the extra backslash.
294 $spitshell >>cflags <<'!NO!SUBS!'
295 case $PERL_CONFIG_SH in
297 if test -f config.sh; then TOP=.;
298 elif test -f ../config.sh; then TOP=..;
299 elif test -f ../../config.sh; then TOP=../..;
300 elif test -f ../../../config.sh; then TOP=../../..;
301 elif test -f ../../../../config.sh; then TOP=../../../..;
303 echo "Can't find config.sh."; exit 1
306 ccflags="$myccflags" # Restore possible edits by cflags.SH.
310 # syntax: cflags [optimize=XXX] [file[.suffix]] ...
311 # displays the proposed compiler command line for each 'file'
313 # with no file, dispalys it for all *.c files.
314 # The optimise=XXX arg (if present) is evalled, setting the default
315 # value of the $optimise variable, which is output on the command line
316 # (but which may be overridden for specific files below)
319 Xoptimize=*|X"optimize=*")
326 0) set *.c; echo "The current C flags are:" ;;
329 set `echo "$* " | sed -e 's/\.[oc] / /g' -e 's/\.obj / /g' -e "s/\\$obj_ext / /g"`
335 *) echo $n " $file.c $c" ;;
338 # allow variables like toke_cflags to be evaluated
340 if echo $file | grep -v / >/dev/null
342 eval 'eval ${'"${file}_cflags"'-""}'
350 # Customization examples follow.
352 # The examples are intentionally unreachable as the '*)' case above always
353 # matches. To use them, move before the '*)' and edit as appropriate.
354 # It is not a good idea to set ccflags to an absolute value here, as it
355 # often contains general -D defines which are needed for correct
356 # compilation. It is better to edit ccflags as shown, using interpolation
357 # to add flags, or sed to remove flags.
359 av) ccflags=`echo $ccflags | sed -e s/-pipe//` ;;
360 deb) ccflags="$ccflags -fno-jump-tables" ;;
361 hv) warn=`echo $warn | sed -e s/-Wextra//` ;;
362 toke) optimize=-O0 ;;
365 # Can we perhaps use $ansi2knr here
366 echo "$cc -c -DPERL_CORE $ccflags $stdflags $optimize $warn $extra"
370 # end per file behaviour