This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcharclass.h: Change to use new inRANGE macro
[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
813665af 149# (especially about things like long long, which are not in C89) will still be
215ed6ce
AD
150# valid if we now add flags like -std=c89.
151
f049db42
JH
152pedantic=''
153case "$gccansipedantic" in
154define) pedantic='-pedantic' ;;
155esac
156
bc730b18
JH
157case "$gccversion" in
158'') ;;
a07cd53d
JH
159[12]*) ;; # gcc versions 1 (gasp!) and 2 are not good for this.
160Intel*) ;; # # Is that you, Intel C++?
f049db42 161#
fd9d4a0b 162# NOTE 1: the -std=c89 without -pedantic is a bit pointless.
abf4b099 163# Just -std=c89 means "if there is room for interpretation,
f049db42
JH
164# interpret the C89 way." It does NOT mean "strict C89" on its own.
165# You need to add the -pedantic for that. To do this with Configure,
166# do -Dgccansipedantic (note that the -ansi is included in any case,
167# the option is a bit oddly named, for historical reasons.)
168#
169# NOTE 2: -pedantic necessitates adding a couple of flags:
170# * -PERL_GCC_PEDANTIC so that the perl code can adapt: there's nothing
171# added by gcc itself to indicate pedanticness.
172# * -Wno-overlength-strings under -DDEBUGGING because quite many of
173# the LEAVE_with_name() and assert() calls generate string literals
174# longer then the ANSI minimum of 509 bytes.
175#
176# NOTE 3: the relative order of these options matters:
bf78513f
JH
177# -Wextra before -W
178# -std=c89 before -ansi
179# -pedantic* before -Werror=d-a-s
abf4b099 180#
bf78513f 181*) for opt in -std=c89 -ansi $pedantic \
d2d49204 182 -Werror=declaration-after-statement \
5997475b 183 -Werror=pointer-arith \
f049db42 184 -Wextra -W \
fae2e960 185 -Wc++-compat -Wwrite-strings
bc730b18
JH
186 do
187 case " $ccflags " in
a07cd53d
JH
188 *" $opt "*) ;; # Skip if already there.
189 *) rm -f _cflags$_exe
f049db42
JH
190 flags="-DPERL_NO_INLINE_FUNCTIONS $ccflags $warn $stdflags $opt"
191 case "$opt" in
192 *-pedantic*) flags="$flags -DPERL_GCC_PEDANTIC" ;;
193 esac
194 # echo "opt = $opt, flags = $flags"
195 cmd="$cc $flags _cflags.c -o _cflags$_exe"
196 out="`$cmd 2>&1`"
197 # echo "$cmd --> $out"
198 case "$out" in
bc730b18 199 *"unrecognized"*) ;;
d39195d4 200 *"unknown"*) ;;
215ed6ce 201 *"implicit declaration"*) ;; # Was something useful hidden?
bc730b18 202 *"Invalid"*) ;;
9ac4c107 203 *"is valid for C"*) ;;
a07cd53d
JH
204 *) if test -x _cflags$_exe
205 then
206 case "$opt" in
be5a9ab8
JH
207 -std*)
208 echo "cflags.SH: Adding $opt."
209 stdflags="$stdflags $opt"
210 ;;
bf78513f
JH
211 -ansi)
212 # -std=c89 is the modern form of -ansi, so add
213 # -ansi only if -std=c89 is not there already.
214 case " $stdflags " in
215 *-std=c89*) ;;
216 *)
217 echo "cflags.SH: Adding $opt."
218 stdflags="$stdflags $opt"
219 ;;
220 esac
221 ;;
863ca36a
JH
222 -W)
223 # -Wextra is the modern form of -W, so add
224 # -W only if -Wextra is not there already.
225 case " $warn " in
226 *-Wextra*) ;;
227 *)
228 echo "cflags.SH: Adding $opt."
229 warn="$warn $opt"
230 ;;
231 esac
232 ;;
233 -Werror=declaration-after-statement)
234 # -pedantic* (with -std=c89) covers -Werror=d-a-s.
235 case "$stdflags$warn" in
236 *-std=c89*-pedantic*|*-pedantic*-std=c89*) ;;
be5a9ab8
JH
237 *)
238 echo "cflags.SH: Adding $opt."
239 warn="$warn $opt"
240 ;;
905ccdc7 241 esac
863ca36a 242 ;;
5997475b
DIM
243 -Werror=pointer-arith)
244 # -pedantic* covers -Werror=p-a
245 case "$warn" in
246 *-pedantic*) ;;
247 *)
248 echo "cflags.SH: Adding $opt."
249 warn="$warn $opt"
250 ;;
251 esac
252 ;;
863ca36a
JH
253 *)
254 echo "cflags.SH: Adding $opt."
255 warn="$warn $opt"
256 ;;
a07cd53d
JH
257 esac
258 fi
259 ;;
bc730b18
JH
260 esac
261 ;;
262 esac
f049db42
JH
263 case "$ccflags$warn" in
264 *-pedantic*)
265 overlength=''
266 case "$ccflags$optimize" in
267 *-DDEBUGGING*) overlength='-Wno-overlength-strings' ;;
268 esac
269 for opt2 in -DPERL_GCC_PEDANTIC $overlength
270 do
271 case "$ccflags$warn" in
272 *"$opt2"*) ;;
273 *) echo "cflags.SH: Adding $opt2 because of -pedantic."
274 warn="$warn $opt2" ;;
275 esac
276 done
277 ;;
278 esac
bc730b18
JH
279 done
280 ;;
281esac
a07cd53d 282rm -f _cflags.c _cflags$_exe
bc730b18 283
9ac4c107
JH
284case "$gccversion" in
285'') ;;
286*)
f049db42
JH
287 case "$warn$ccflags" in
288 *-pedantic*)
3e8416a3 289 # If we have -Duse64bitint (or equivalent) in effect and the quadtype
f049db42
JH
290 # has become 'long long', gcc -pedantic* becomes unbearable
291 # (moreso when combined with -Wall) because long long and LL and %lld|%Ld
3e8416a3 292 # become warn-worthy. So let's drop the -pedantic in that case.
f049db42 293 #
215ed6ce
AD
294 # Similarly, since 'long long' isn't part of C89, FreeBSD 6.2 headers
295 # don't declare atoll() under -std=c89, but we need it. In general,
296 # insisting on -std=c89 is inconsistent with insisting on using
813665af 297 # 'long long'. So drop -std=c89 and -ansi as well if we're using
215ed6ce 298 # 'long long' as our main integral type.
f049db42
JH
299 #
300 # usedtrace (DTrace) uses unportable features (dollars in identifiers,
301 # and gcc statement expressions), it is just easier to turn off pedantic.
302 remove=''
303 case "$quadtype:$ivtype:$sPRId64:$usedtrace" in
304 *"long long"*|*lld*|*Ld*) remove='long long' ;;
305 *) case "$usedtrace" in
306 define) remove='usedtrace' ;;
307 esac
308 ;;
309 esac
310 case "$remove" in
311 '') ;;
312 *) echo "cflags.SH: Removing -pedantic*, -std=c89, and -ansi because of $remove."
313 ccflags=`echo $ccflags|sed -e 's/-pedantic-errors/ /' -e 's/-pedantic/ /' -e 's/-std=c89/ /' -e 's/-ansi/ /' -e 's/-DPERL_GCC_PEDANTIC/ /'`
314 warn=`echo $warn|sed -e 's/-pedantic-errors/ /' -e 's/-pedantic/ /' -e 's/-ansi/ /' -e 's/-DPERL_GCC_PEDANTIC/ /'`
315 stdflags=`echo $stdflags|sed -e 's/-std=c89/ /'`
316 ;;
215ed6ce 317 esac
5bd6d383
JH
318 ;;
319 esac
320 ;;
321esac
322
c57c0df9
JH
323# Older clang releases are not wise enough for -Wunused-value.
324case "$gccversion" in
325*"Apple LLVM "[34]*|*"Apple LLVM version "[34]*)
326 for f in -Wno-unused-value
327 do
328 echo "cflags.SH: Adding $f because clang version '$gccversion'"
329 warn="$warn $f"
330 done
331 ;;
332esac
333
78e91e3f
JH
334# The quadmath Q format specifier will cause -Wformat to whine.
335case "$gccversion" in
336'') ;;
337*) case "$usequadmath" in
338 define)
339 for f in -Wno-format
340 do
341 echo "cflags.SH: Adding $f because of usequadmath."
342 warn="$warn $f"
343 done
344 ;;
345 esac
346 ;;
347esac
348
5bd6d383
JH
349case "$cc" in
350*g++*)
351 # Extra paranoia in case people have bad canned ccflags:
352 # bad in the sense that the flags are accepted by g++,
353 # but then whined about.
d2d49204
JH
354 #
355 # -Werror=d-a-s option is valid for g++, by definition,
356 # but we remove it just for cleanliness and shorter command lines.
357 for f in -Wdeclaration-after-statement \
358 -Werror=declaration-after-statement \
49c60343 359 -Wc++-compat \
d2d49204 360 -std=c89
5bd6d383 361 do
95f0c784 362 case "$ccflags$warn" in
5bd6d383 363 *"$f"*)
95f0c784
JH
364 echo "cflags.SH: Removing $f because of g++."
365 ccflags=`echo $ccflags|sed 's/$f/ /'`
366 warn=`echo $warn|sed 's/$f/ /'`
367 ;;
5bd6d383
JH
368 esac
369 done
370 ;;
371esac
372
5997475b
DIM
373for f in -Wdeclaration-after-statement -Werror=declaration-after-statement \
374 -Wpointer-arith -Werror=pointer-arith
5bd6d383
JH
375do
376 case "$cppflags" in
377 *"$f"*)
378 echo "cflags.SH: Removing $f from cppflags."
379 cppflags=`echo $cppflags|sed 's/$f/ /'` ;;
380 esac
381done
382
2eb17d1d 383# If usethreads and clang, add -Wthread-safety for clang 3.6 or later.
9ce5f19e 384# gccversion is defined also for clang, because compat, use that for matching.
2eb17d1d
JH
385# Apple overwrites clang version with XCode version, see hints/darwin.sh
386# for the gory details. Aggressively forward-proofing.
9ce5f19e
JH
387case "$usethreads" in
388define)
389case "$gccversion" in
2eb17d1d 390*" Clang 3."[56789]*|*" Clang "[456]*|*"Apple LLVM 6.1"*|*"Apple LLVM "[789]*)
9ce5f19e
JH
391 for f in -Wthread-safety
392 do
393 case " $warn " in
394 *" $f "*) ;; # Skip if already there.
395 *)
396 echo "cflags.SH: Adding $f because usethreads and clang and gccversion '$gccversion'"
397 warn="$warn $f"
398 ;;
399 esac
400 done
401;;
402esac
403;;
404esac
405
f049db42
JH
406echo "cflags.SH: cc = $cc"
407echo "cflags.SH: ccflags = $ccflags"
408echo "cflags.SH: stdflags = $stdflags"
409echo "cflags.SH: optimize = $optimize"
410echo "cflags.SH: warn = $warn"
411
10edeb5d 412# Code to set any extra flags here.
a07cd53d 413extra=''
11fcce85 414
7bd81993
JH
415# Protect double or single quotes for better restoring of ccflags.
416myccflags=`echo $ccflags | sed -e 's/"/\\\"/g' -e "s/'/\\\'/g"`
417
2b317908 418echo "Extracting cflags (with variable substitutions)"
44213caa
DM
419# This section of the file will have variable substitutions done on it.
420# Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
421# Protect any dollar signs and backticks that you do not want interpreted
422# by putting a backslash in front. You may delete these comments.
165b7424 423rm -f cflags
2b317908 424$spitshell >cflags <<!GROK!THIS!
ecfc5424 425$startsh
bc730b18 426
cb9238a7
DM
427# !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
428
429# This file is generated by cflags.SH
430
5bd6d383 431# Used to restore possible edits by cflags.SH.
7bd81993
JH
432myccflags="$myccflags"
433
11fcce85 434# Extra warnings, used e.g. for gcc.
bc730b18 435warn="$warn"
11fcce85
JH
436# Extra standardness.
437stdflags="$stdflags"
b1e55cab
JH
438# Extra extra.
439extra="$extra"
dcff826f
YO
440# what do executables look like?
441_exe="$_exe"
bc730b18 442
2b317908
LW
443!GROK!THIS!
444
44213caa 445# In the following dollars and backticks do not need the extra backslash.
2b317908 446$spitshell >>cflags <<'!NO!SUBS!'
a02608de 447case $PERL_CONFIG_SH in
2b317908 448'')
a0d0e21e
LW
449 if test -f config.sh; then TOP=.;
450 elif test -f ../config.sh; then TOP=..;
451 elif test -f ../../config.sh; then TOP=../..;
452 elif test -f ../../../config.sh; then TOP=../../..;
453 elif test -f ../../../../config.sh; then TOP=../../../..;
454 else
455 echo "Can't find config.sh."; exit 1
456 fi
457 . $TOP/config.sh
57d2761b 458 ccflags="$myccflags" # Restore possible edits by cflags.SH.
a0d0e21e
LW
459 ;;
460esac
461
ba0f5503
DM
462# syntax: cflags [optimize=XXX] [file[.suffix]] ...
463# displays the proposed compiler command line for each 'file'
464#
465# with no file, dispalys it for all *.c files.
466# The optimise=XXX arg (if present) is evalled, setting the default
467# value of the $optimise variable, which is output on the command line
468# (but which may be overridden for specific files below)
75550b4e 469
1167a30e 470case "X$1" in
26102cc7 471Xoptimize=*|X"optimize=*")
1167a30e
IZ
472 eval "$1"
473 shift
474 ;;
475esac
476
1c3d792e
LW
477case $# in
4780) set *.c; echo "The current C flags are:" ;;
1c3d792e 479esac
2b317908 480
578789a7 481set `echo "$* " | sed -e 's/\.[oc] / /g' -e 's/\.obj / /g' -e "s/\\$obj_ext / /g"`
2b317908 482
1c3d792e
LW
483for file do
484
485 case "$#" in
486 1) ;;
2b317908 487 *) echo $n " $file.c $c" ;;
1c3d792e
LW
488 esac
489
44213caa 490 # allow variables like toke_cflags to be evaluated
2b317908 491
d4914d8c
AC
492 case "$file" in
493 */*) ;;
494 *) eval 'eval ${'"${file}_cflags"'-""}' ;;
495 esac
2b317908 496
44213caa 497 # or customize here
2b317908 498
1c3d792e 499 case "$file" in
1197364c
NT
500 regcomp) : work around http://bugs.debian.org/754054
501 case $archname in
502 mips-*|mipsel-*)
503 optimize="$optimize -fno-tree-vrp";;
504 esac;;
1c3d792e 505 *) ;;
5729ffdd 506
5bd6d383
JH
507 # Customization examples follow.
508 #
509 # The examples are intentionally unreachable as the '*)' case above always
44213caa
DM
510 # matches. To use them, move before the '*)' and edit as appropriate.
511 # It is not a good idea to set ccflags to an absolute value here, as it
512 # often contains general -D defines which are needed for correct
513 # compilation. It is better to edit ccflags as shown, using interpolation
514 # to add flags, or sed to remove flags.
5729ffdd 515
5bd6d383
JH
516 av) ccflags=`echo $ccflags | sed -e s/-pipe//` ;;
517 deb) ccflags="$ccflags -fno-jump-tables" ;;
518 hv) warn=`echo $warn | sed -e s/-Wextra//` ;;
519 toke) optimize=-O0 ;;
7e827271 520 esac
666ea192 521
b1e55cab 522 echo "$cc -c -DPERL_CORE $ccflags $stdflags $optimize $warn $extra"
2b317908 523
a0d0e21e 524 . $TOP/config.sh
2b317908 525
a7d59441 526 # end per file behaviour
1c3d792e 527done
2b317908 528!NO!SUBS!
a0d0e21e 529chmod 755 cflags
2b317908 530$eunicefix cflags