This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Promote v5.36 usage and feature bundles doc
[perl5.git] / cflags.SH
CommitLineData
1f00b0d6
S
1#!/bin/sh
2
cb9238a7 3# Generate the cflags script, which is used to determine what cflags
ea14a82d
JH
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
cb9238a7
DM
15# various candidate flags, and from the compiler output, determine what
16# flags are supported.
ea14a82d 17#
cb9238a7
DM
18# From this we initialise the following variables in the cflags script:
19#
ea14a82d 20# $myccflags (possibly edited version of $Config{ccflags})
cb9238a7
DM
21# $warn
22# $stdflags
23# $extra
24# $_exe
25
a02608de 26case $PERL_CONFIG_SH in
1c3d792e 27'')
a0d0e21e
LW
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 ;;
1c3d792e 38esac
44213caa
DM
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.
1c3d792e
LW
41case "$0" in
42*/*) cd `expr X$0 : 'X\(.*\)/'` ;;
43esac
bc730b18 44
37723ebb 45if test -f config_h.SH -a ! -f config.h; then
6ef8aa7c 46 . ./config_h.SH
11e2f395 47 CONFIG_H=already-done
6ef8aa7c
RGS
48fi
49
bc730b18
JH
50warn=''
51
52# Add -Wall for the core modules iff gcc and not already -Wall
53case "$gccversion" in
54'') ;;
55Intel*) ;; # 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 ;;
61esac
62
a07cd53d
JH
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
67cat >_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. */
a07cd53d 76#include <stdlib.h>
a07cd53d 77#include <stddef.h>
a07cd53d 78#include <stdarg.h>
a07cd53d 79#include <limits.h>
a07cd53d
JH
80#ifdef I_DIRENT
81#include <dirent.h>
82#endif
83#ifdef I_UNISTD
84#include <unistd.h>
85#endif
463bdbaf 86#ifdef I_SYS_TYPES
a07cd53d
JH
87#include <sys/types.h>
88#endif
463bdbaf 89#ifdef I_SYS_PARAM
a07cd53d
JH
90#include <sys/param.h>
91#endif
463bdbaf 92#ifdef I_SYS_RESOURCE
a07cd53d
JH
93#include <sys/resource.h>
94#endif
463bdbaf 95#ifdef I_SYS_SELECT
a07cd53d
JH
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
463bdbaf 101#ifdef I_SYS_STAT
a07cd53d
JH
102#include <sys/stat.h>
103#endif
463bdbaf 104#ifdef I_SYS_TIME
a07cd53d
JH
105#include <sys/time.h>
106#endif
463bdbaf 107#ifdef I_SYS_TIMES
a07cd53d
JH
108#include <sys/times.h>
109#endif
463bdbaf 110#ifdef I_SYS_WAIT
a07cd53d
JH
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 */
116int 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
215ed6ce 124 IV iv;
a07cd53d
JH
125 Off_t t0a = 2;
126 STRLEN t0b = 3;
f049db42
JH
127 int t0c = (STRLEN)t0a == t0b;
128
129 printf("%s: %d\n", argv[0], argc);
a07cd53d 130
215ed6ce
AD
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. */
a07cd53d
JH
138}
139__EOT__
140
141stdflags=''
bc730b18 142
215ed6ce
AD
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.
813665af 147# TODO: Ponder whether to migrate this back to Configure so hints files can
215ed6ce 148# tweak it. Also, be paranoid about whether results we've deduced in Configure
0c7be120 149# will still be valid if we now add flags like -std=c99.
215ed6ce 150
f049db42
JH
151pedantic=''
152case "$gccansipedantic" in
153define) pedantic='-pedantic' ;;
154esac
155
bc730b18
JH
156case "$gccversion" in
157'') ;;
6bd6308f 158[12].*) ;; # gcc versions 1 (gasp!) and 2 are not good for this.
a07cd53d 159Intel*) ;; # # Is that you, Intel C++?
f049db42 160#
0c7be120
NC
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.
f049db42 168# You need to add the -pedantic for that. To do this with Configure,
0c7be120
NC
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.
f049db42
JH
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
0c7be120 182# longer then the ANSI C99 minimum of 4095 bytes.
f049db42
JH
183#
184# NOTE 3: the relative order of these options matters:
bf78513f 185# -Wextra before -W
0c7be120 186# -W before -Wno-long-long -Wno-declaration-after-statement
abf4b099 187#
694de2a9 188*) warns="$pedantic \
da255c79 189 -Werror=pointer-arith \
7886147f 190 -Werror=vla \
da255c79 191 -Wextra -W \
0c7be120 192 -Wno-long-long -Wno-declaration-after-statement \
dc9f0093 193 -Wc++-compat -Wwrite-strings"
694de2a9
NC
194 case " $ccflags " in
195 *" -std="*) ;; # Already have -std=...
196 *) warns="-std=c99 $warns" ;;
197 esac
da255c79 198 for opt in $warns
bc730b18
JH
199 do
200 case " $ccflags " in
a07cd53d
JH
201 *" $opt "*) ;; # Skip if already there.
202 *) rm -f _cflags$_exe
f049db42
JH
203 flags="-DPERL_NO_INLINE_FUNCTIONS $ccflags $warn $stdflags $opt"
204 case "$opt" in
205 *-pedantic*) flags="$flags -DPERL_GCC_PEDANTIC" ;;
206 esac
207 # echo "opt = $opt, flags = $flags"
208 cmd="$cc $flags _cflags.c -o _cflags$_exe"
209 out="`$cmd 2>&1`"
210 # echo "$cmd --> $out"
211 case "$out" in
bc730b18 212 *"unrecognized"*) ;;
d39195d4 213 *"unknown"*) ;;
215ed6ce 214 *"implicit declaration"*) ;; # Was something useful hidden?
bc730b18 215 *"Invalid"*) ;;
9ac4c107 216 *"is valid for C"*) ;;
a07cd53d
JH
217 *) if test -x _cflags$_exe
218 then
219 case "$opt" in
be5a9ab8
JH
220 -std*)
221 echo "cflags.SH: Adding $opt."
222 stdflags="$stdflags $opt"
223 ;;
863ca36a
JH
224 -W)
225 # -Wextra is the modern form of -W, so add
226 # -W only if -Wextra is not there already.
227 case " $warn " in
228 *-Wextra*) ;;
229 *)
230 echo "cflags.SH: Adding $opt."
231 warn="$warn $opt"
232 ;;
233 esac
234 ;;
5997475b
DIM
235 -Werror=pointer-arith)
236 # -pedantic* covers -Werror=p-a
237 case "$warn" in
238 *-pedantic*) ;;
239 *)
240 echo "cflags.SH: Adding $opt."
241 warn="$warn $opt"
242 ;;
243 esac
244 ;;
863ca36a
JH
245 *)
246 echo "cflags.SH: Adding $opt."
247 warn="$warn $opt"
248 ;;
a07cd53d
JH
249 esac
250 fi
251 ;;
bc730b18
JH
252 esac
253 ;;
254 esac
f049db42
JH
255 case "$ccflags$warn" in
256 *-pedantic*)
257 overlength=''
258 case "$ccflags$optimize" in
259 *-DDEBUGGING*) overlength='-Wno-overlength-strings' ;;
260 esac
261 for opt2 in -DPERL_GCC_PEDANTIC $overlength
262 do
263 case "$ccflags$warn" in
264 *"$opt2"*) ;;
265 *) echo "cflags.SH: Adding $opt2 because of -pedantic."
266 warn="$warn $opt2" ;;
267 esac
268 done
269 ;;
270 esac
bc730b18
JH
271 done
272 ;;
273esac
a07cd53d 274rm -f _cflags.c _cflags$_exe
bc730b18 275
9ac4c107
JH
276case "$gccversion" in
277'') ;;
278*)
f049db42
JH
279 case "$warn$ccflags" in
280 *-pedantic*)
0c7be120
NC
281 # For -std=c99 -pedantic, only the %Ld format seems to be warn-worthy.
282 # 'long long' and '%lld' are now kosher.
f049db42
JH
283 #
284 # usedtrace (DTrace) uses unportable features (dollars in identifiers,
285 # and gcc statement expressions), it is just easier to turn off pedantic.
286 remove=''
287 case "$quadtype:$ivtype:$sPRId64:$usedtrace" in
0c7be120 288 **Ld*) remove='Ld' ;;
f049db42
JH
289 *) case "$usedtrace" in
290 define) remove='usedtrace' ;;
291 esac
292 ;;
293 esac
294 case "$remove" in
295 '') ;;
0c7be120
NC
296 *) echo "cflags.SH: Removing -pedantic* -ansi because of $remove."
297 ccflags=`echo $ccflags|sed -e 's/-pedantic-errors/ /' -e 's/-pedantic/ /'`
298 warn=`echo $warn|sed -e 's/-pedantic-errors/ /' -e 's/-pedantic/ /'`
f049db42 299 ;;
215ed6ce 300 esac
5bd6d383
JH
301 ;;
302 esac
303 ;;
304esac
305
c57c0df9
JH
306# Older clang releases are not wise enough for -Wunused-value.
307case "$gccversion" in
308*"Apple LLVM "[34]*|*"Apple LLVM version "[34]*)
309 for f in -Wno-unused-value
310 do
311 echo "cflags.SH: Adding $f because clang version '$gccversion'"
312 warn="$warn $f"
313 done
314 ;;
315esac
316
78e91e3f
JH
317# The quadmath Q format specifier will cause -Wformat to whine.
318case "$gccversion" in
319'') ;;
320*) case "$usequadmath" in
321 define)
322 for f in -Wno-format
323 do
324 echo "cflags.SH: Adding $f because of usequadmath."
325 warn="$warn $f"
326 done
327 ;;
328 esac
329 ;;
330esac
331
5bd6d383
JH
332case "$cc" in
333*g++*)
334 # Extra paranoia in case people have bad canned ccflags:
335 # bad in the sense that the flags are accepted by g++,
336 # but then whined about.
0c7be120 337 for f in -Wc++-compat -std=c99
5bd6d383 338 do
95f0c784 339 case "$ccflags$warn" in
5bd6d383 340 *"$f"*)
95f0c784
JH
341 echo "cflags.SH: Removing $f because of g++."
342 ccflags=`echo $ccflags|sed 's/$f/ /'`
343 warn=`echo $warn|sed 's/$f/ /'`
344 ;;
5bd6d383
JH
345 esac
346 done
347 ;;
348esac
349
0c7be120 350for f in -Wpointer-arith -Werror=pointer-arith
5bd6d383
JH
351do
352 case "$cppflags" in
353 *"$f"*)
354 echo "cflags.SH: Removing $f from cppflags."
355 cppflags=`echo $cppflags|sed 's/$f/ /'` ;;
356 esac
357done
358
2eb17d1d 359# If usethreads and clang, add -Wthread-safety for clang 3.6 or later.
9ce5f19e 360# gccversion is defined also for clang, because compat, use that for matching.
2eb17d1d
JH
361# Apple overwrites clang version with XCode version, see hints/darwin.sh
362# for the gory details. Aggressively forward-proofing.
9ce5f19e
JH
363case "$usethreads" in
364define)
365case "$gccversion" in
2eb17d1d 366*" Clang 3."[56789]*|*" Clang "[456]*|*"Apple LLVM 6.1"*|*"Apple LLVM "[789]*)
9ce5f19e
JH
367 for f in -Wthread-safety
368 do
369 case " $warn " in
370 *" $f "*) ;; # Skip if already there.
371 *)
372 echo "cflags.SH: Adding $f because usethreads and clang and gccversion '$gccversion'"
373 warn="$warn $f"
374 ;;
375 esac
376 done
377;;
378esac
379;;
380esac
381
f049db42
JH
382echo "cflags.SH: cc = $cc"
383echo "cflags.SH: ccflags = $ccflags"
384echo "cflags.SH: stdflags = $stdflags"
385echo "cflags.SH: optimize = $optimize"
386echo "cflags.SH: warn = $warn"
387
10edeb5d 388# Code to set any extra flags here.
a07cd53d 389extra=''
11fcce85 390
7bd81993
JH
391# Protect double or single quotes for better restoring of ccflags.
392myccflags=`echo $ccflags | sed -e 's/"/\\\"/g' -e "s/'/\\\'/g"`
393
2b317908 394echo "Extracting cflags (with variable substitutions)"
44213caa
DM
395# This section of the file will have variable substitutions done on it.
396# Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
397# Protect any dollar signs and backticks that you do not want interpreted
398# by putting a backslash in front. You may delete these comments.
165b7424 399rm -f cflags
2b317908 400$spitshell >cflags <<!GROK!THIS!
ecfc5424 401$startsh
bc730b18 402
cb9238a7
DM
403# !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
404
405# This file is generated by cflags.SH
406
5bd6d383 407# Used to restore possible edits by cflags.SH.
7bd81993
JH
408myccflags="$myccflags"
409
11fcce85 410# Extra warnings, used e.g. for gcc.
bc730b18 411warn="$warn"
11fcce85
JH
412# Extra standardness.
413stdflags="$stdflags"
b1e55cab
JH
414# Extra extra.
415extra="$extra"
dcff826f
YO
416# what do executables look like?
417_exe="$_exe"
bc730b18 418
2b317908
LW
419!GROK!THIS!
420
44213caa 421# In the following dollars and backticks do not need the extra backslash.
2b317908 422$spitshell >>cflags <<'!NO!SUBS!'
a02608de 423case $PERL_CONFIG_SH in
2b317908 424'')
a0d0e21e
LW
425 if test -f config.sh; then TOP=.;
426 elif 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 else
431 echo "Can't find config.sh."; exit 1
432 fi
433 . $TOP/config.sh
57d2761b 434 ccflags="$myccflags" # Restore possible edits by cflags.SH.
a0d0e21e
LW
435 ;;
436esac
437
ba0f5503
DM
438# syntax: cflags [optimize=XXX] [file[.suffix]] ...
439# displays the proposed compiler command line for each 'file'
440#
441# with no file, dispalys it for all *.c files.
442# The optimise=XXX arg (if present) is evalled, setting the default
443# value of the $optimise variable, which is output on the command line
444# (but which may be overridden for specific files below)
75550b4e 445
1167a30e 446case "X$1" in
26102cc7 447Xoptimize=*|X"optimize=*")
1167a30e
IZ
448 eval "$1"
449 shift
450 ;;
451esac
452
1c3d792e
LW
453case $# in
4540) set *.c; echo "The current C flags are:" ;;
1c3d792e 455esac
2b317908 456
578789a7 457set `echo "$* " | sed -e 's/\.[oc] / /g' -e 's/\.obj / /g' -e "s/\\$obj_ext / /g"`
2b317908 458
1c3d792e
LW
459for file do
460
461 case "$#" in
462 1) ;;
2b317908 463 *) echo $n " $file.c $c" ;;
1c3d792e
LW
464 esac
465
44213caa 466 # allow variables like toke_cflags to be evaluated
2b317908 467
d4914d8c
AC
468 case "$file" in
469 */*) ;;
470 *) eval 'eval ${'"${file}_cflags"'-""}' ;;
471 esac
2b317908 472
44213caa 473 # or customize here
2b317908 474
1c3d792e 475 case "$file" in
1197364c
NT
476 regcomp) : work around http://bugs.debian.org/754054
477 case $archname in
478 mips-*|mipsel-*)
479 optimize="$optimize -fno-tree-vrp";;
480 esac;;
1c3d792e 481 *) ;;
5729ffdd 482
5bd6d383
JH
483 # Customization examples follow.
484 #
485 # The examples are intentionally unreachable as the '*)' case above always
44213caa
DM
486 # matches. To use them, move before the '*)' and edit as appropriate.
487 # It is not a good idea to set ccflags to an absolute value here, as it
488 # often contains general -D defines which are needed for correct
489 # compilation. It is better to edit ccflags as shown, using interpolation
490 # to add flags, or sed to remove flags.
5729ffdd 491
5bd6d383
JH
492 av) ccflags=`echo $ccflags | sed -e s/-pipe//` ;;
493 deb) ccflags="$ccflags -fno-jump-tables" ;;
494 hv) warn=`echo $warn | sed -e s/-Wextra//` ;;
495 toke) optimize=-O0 ;;
7e827271 496 esac
666ea192 497
b1e55cab 498 echo "$cc -c -DPERL_CORE $ccflags $stdflags $optimize $warn $extra"
2b317908 499
a0d0e21e 500 . $TOP/config.sh
2b317908 501
a7d59441 502 # end per file behaviour
1c3d792e 503done
2b317908 504!NO!SUBS!
a0d0e21e 505chmod 755 cflags
2b317908 506$eunicefix cflags