This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Comment tweaking.
[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. */
76#ifdef I_STDLIB
77#include <stdlib.h>
78#endif
79#ifdef I_STDDEF
80#include <stddef.h>
81#endif
82#ifdef I_STDARG
83#include <stdarg.h>
84#endif
85#ifdef I_LIMITS
86#include <limits.h>
87#endif
88#ifdef I_DIRENT
89#include <dirent.h>
90#endif
91#ifdef I_UNISTD
92#include <unistd.h>
93#endif
463bdbaf 94#ifdef I_SYS_TYPES
a07cd53d
JH
95#include <sys/types.h>
96#endif
463bdbaf 97#ifdef I_SYS_PARAM
a07cd53d
JH
98#include <sys/param.h>
99#endif
463bdbaf 100#ifdef I_SYS_RESOURCE
a07cd53d
JH
101#include <sys/resource.h>
102#endif
463bdbaf 103#ifdef I_SYS_SELECT
a07cd53d
JH
104#include <sys/select.h>
105#endif
106#if defined(HAS_SOCKET) && !defined(VMS) && !defined(WIN32) /* See perl.h. */
107#include <sys/socket.h>
108#endif
463bdbaf 109#ifdef I_SYS_STAT
a07cd53d
JH
110#include <sys/stat.h>
111#endif
463bdbaf 112#ifdef I_SYS_TIME
a07cd53d
JH
113#include <sys/time.h>
114#endif
463bdbaf 115#ifdef I_SYS_TIMES
a07cd53d
JH
116#include <sys/times.h>
117#endif
463bdbaf 118#ifdef I_SYS_WAIT
a07cd53d
JH
119#include <sys/wait.h>
120#endif
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
123 */
124int main(int argc, char *argv[]) {
125
126/* Add here test code found to be problematic in some gcc platform. */
127
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. */
131
215ed6ce 132 IV iv;
a07cd53d
JH
133 Off_t t0a = 2;
134 STRLEN t0b = 3;
135 int t0c = t0a == t0b;
136
215ed6ce
AD
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.
140*/
141 iv = Atol("42");
142
143 return (!t0c && (iv == 42)) ? 0 : -1; /* Try to avoid 'unused' warnings. */
a07cd53d
JH
144}
145__EOT__
146
147stdflags=''
bc730b18 148
215ed6ce
AD
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
152# one individually.
813665af 153# TODO: Ponder whether to migrate this back to Configure so hints files can
215ed6ce 154# tweak it. Also, be paranoid about whether results we've deduced in Configure
813665af 155# (especially about things like long long, which are not in C89) will still be
215ed6ce
AD
156# valid if we now add flags like -std=c89.
157
bc730b18
JH
158case "$gccversion" in
159'') ;;
a07cd53d
JH
160[12]*) ;; # gcc versions 1 (gasp!) and 2 are not good for this.
161Intel*) ;; # # Is that you, Intel C++?
db7198b5
RB
162*) for opt in -ansi -std=c89 -W -Wextra -Wdeclaration-after-statement \
163 -Wendif-labels -Wc++-compat -Wwrite-strings
bc730b18
JH
164 do
165 case " $ccflags " in
a07cd53d
JH
166 *" $opt "*) ;; # Skip if already there.
167 *) rm -f _cflags$_exe
4b76cb9d 168 case "`$cc -DPERL_NO_INLINE_FUNCTIONS $ccflags $warn $stdflags $opt _cflags.c -o _cflags$_exe 2>&1`" in
bc730b18 169 *"unrecognized"*) ;;
d39195d4 170 *"unknown"*) ;;
215ed6ce 171 *"implicit declaration"*) ;; # Was something useful hidden?
bc730b18 172 *"Invalid"*) ;;
9ac4c107 173 *"is valid for C"*) ;;
a07cd53d
JH
174 *) if test -x _cflags$_exe
175 then
176 case "$opt" in
177 -std*) stdflags="$stdflags $opt" ;;
178 *) warn="$warn $opt" ;;
179 esac
180 fi
181 ;;
bc730b18
JH
182 esac
183 ;;
184 esac
bc730b18
JH
185 done
186 ;;
187esac
a07cd53d 188rm -f _cflags.c _cflags$_exe
bc730b18 189
9ac4c107
JH
190case "$gccversion" in
191'') ;;
192*)
3e8416a3
RGS
193 if [ "$gccansipedantic" = "" ]; then
194 # If we have -Duse64bitint (or equivalent) in effect and the quadtype
195 # has become 'long long', gcc -pedantic becomes unbearable (moreso
196 # when combined with -Wall) because long long and LL and %lld|%Ld
197 # become warn-worthy. So let's drop the -pedantic in that case.
198 case "$quadtype:$sPRId64" in
199 "long long"*|*lld*|*Ld*)
5bd6d383 200 echo "cflags.SH: Removing -pedantic and warn because of quadtype='long long'."
3e8416a3
RGS
201 ccflags="`echo $ccflags|sed 's/-pedantic/ /'`"
202 warn="`echo $warn|sed 's/-pedantic/ /'`"
203 ;;
204 esac
215ed6ce
AD
205 # Similarly, since 'long long' isn't part of C89, FreeBSD 6.2 headers
206 # don't declare atoll() under -std=c89, but we need it. In general,
207 # insisting on -std=c89 is inconsistent with insisting on using
813665af 208 # 'long long'. So drop -std=c89 and -ansi as well if we're using
215ed6ce
AD
209 # 'long long' as our main integral type.
210 case "$ivtype" in
211 "long long")
5bd6d383 212 echo "cflags.SH: Removing -pedantic, -std=c89, and -ansi because of ivtype='long long'."
215ed6ce
AD
213 ccflags=`echo $ccflags|sed -e 's/-pedantic/ /' -e 's/-std=c89/ /' -e 's/-ansi/ /'`
214 warn=`echo $warn|sed -e 's/-pedantic/ /' -e 's/-ansi/ /'`
a5105380 215 stdflags=`echo $stdflags|sed -e 's/-std=c89/ /'`
215ed6ce
AD
216 ;;
217 esac
3e8416a3 218 fi
5cf489d6
RGS
219 # Using certain features (like the gcc statement expressions)
220 # requires knowing whether -pedantic has been specified.
9ac4c107 221 case "$warn$ccflags" in
5bd6d383
JH
222 *-pedantic*)
223 echo "cflags.SH: Adding -DPERL_PEDANTIC because of -pedantic."
224 warn="$warn -DPERL_GCC_PEDANTIC"
225 ;;
226 esac
227 ;;
228esac
229
230case "$cc" in
231*g++*)
232 # Extra paranoia in case people have bad canned ccflags:
233 # bad in the sense that the flags are accepted by g++,
234 # but then whined about.
235 for f in -Wdeclaration-after-statement -std=c89
236 do
237 case "$ccflags" in
238 *"$f"*)
239 echo "cflags.SH: Removing $ccflags because of g++."
240 ccflags=`echo $ccflags|sed 's/$f/ /'` ;;
241 esac
242 done
243 ;;
244esac
245
246for f in -Wdeclaration-after-statement
247do
248 case "$cppflags" in
249 *"$f"*)
250 echo "cflags.SH: Removing $f from cppflags."
251 cppflags=`echo $cppflags|sed 's/$f/ /'` ;;
252 esac
253done
254
10edeb5d 255# Code to set any extra flags here.
a07cd53d 256extra=''
11fcce85 257
2b317908 258echo "Extracting cflags (with variable substitutions)"
44213caa
DM
259# This section of the file will have variable substitutions done on it.
260# Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
261# Protect any dollar signs and backticks that you do not want interpreted
262# by putting a backslash in front. You may delete these comments.
165b7424 263rm -f cflags
2b317908 264$spitshell >cflags <<!GROK!THIS!
ecfc5424 265$startsh
bc730b18 266
cb9238a7
DM
267# !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
268
269# This file is generated by cflags.SH
270
5bd6d383 271# Used to restore possible edits by cflags.SH.
57d2761b 272myccflags="$ccflags"
11fcce85 273# Extra warnings, used e.g. for gcc.
bc730b18 274warn="$warn"
11fcce85
JH
275# Extra standardness.
276stdflags="$stdflags"
b1e55cab
JH
277# Extra extra.
278extra="$extra"
dcff826f
YO
279# what do executables look like?
280_exe="$_exe"
bc730b18 281
2b317908
LW
282!GROK!THIS!
283
44213caa 284# In the following dollars and backticks do not need the extra backslash.
2b317908 285$spitshell >>cflags <<'!NO!SUBS!'
a02608de 286case $PERL_CONFIG_SH in
2b317908 287'')
a0d0e21e
LW
288 if test -f config.sh; then TOP=.;
289 elif test -f ../config.sh; then TOP=..;
290 elif test -f ../../config.sh; then TOP=../..;
291 elif test -f ../../../config.sh; then TOP=../../..;
292 elif test -f ../../../../config.sh; then TOP=../../../..;
293 else
294 echo "Can't find config.sh."; exit 1
295 fi
296 . $TOP/config.sh
57d2761b 297 ccflags="$myccflags" # Restore possible edits by cflags.SH.
a0d0e21e
LW
298 ;;
299esac
300
ba0f5503
DM
301# syntax: cflags [optimize=XXX] [file[.suffix]] ...
302# displays the proposed compiler command line for each 'file'
303#
304# with no file, dispalys it for all *.c files.
305# The optimise=XXX arg (if present) is evalled, setting the default
306# value of the $optimise variable, which is output on the command line
307# (but which may be overridden for specific files below)
75550b4e 308
1167a30e 309case "X$1" in
26102cc7 310Xoptimize=*|X"optimize=*")
1167a30e
IZ
311 eval "$1"
312 shift
313 ;;
314esac
315
1c3d792e
LW
316case $# in
3170) set *.c; echo "The current C flags are:" ;;
1c3d792e 318esac
2b317908 319
578789a7 320set `echo "$* " | sed -e 's/\.[oc] / /g' -e 's/\.obj / /g' -e "s/\\$obj_ext / /g"`
2b317908 321
1c3d792e
LW
322for file do
323
324 case "$#" in
325 1) ;;
2b317908 326 *) echo $n " $file.c $c" ;;
1c3d792e
LW
327 esac
328
44213caa 329 # allow variables like toke_cflags to be evaluated
2b317908 330
8736538c
AS
331 if echo $file | grep -v / >/dev/null
332 then
333 eval 'eval ${'"${file}_cflags"'-""}'
334 fi
2b317908 335
44213caa 336 # or customize here
2b317908 337
1c3d792e 338 case "$file" in
1c3d792e 339 *) ;;
5729ffdd 340
5bd6d383
JH
341 # Customization examples follow.
342 #
343 # The examples are intentionally unreachable as the '*)' case above always
44213caa
DM
344 # matches. To use them, move before the '*)' and edit as appropriate.
345 # It is not a good idea to set ccflags to an absolute value here, as it
346 # often contains general -D defines which are needed for correct
347 # compilation. It is better to edit ccflags as shown, using interpolation
348 # to add flags, or sed to remove flags.
5729ffdd 349
5bd6d383
JH
350 av) ccflags=`echo $ccflags | sed -e s/-pipe//` ;;
351 deb) ccflags="$ccflags -fno-jump-tables" ;;
352 hv) warn=`echo $warn | sed -e s/-Wextra//` ;;
353 toke) optimize=-O0 ;;
7e827271 354 esac
666ea192 355
44213caa 356 # Can we perhaps use $ansi2knr here
b1e55cab 357 echo "$cc -c -DPERL_CORE $ccflags $stdflags $optimize $warn $extra"
2b317908 358
a0d0e21e 359 . $TOP/config.sh
2b317908 360
a7d59441 361 # end per file behaviour
1c3d792e 362done
2b317908 363!NO!SUBS!
a0d0e21e 364chmod 755 cflags
2b317908 365$eunicefix cflags