This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make also cflags.SH clean with g++ -Wunused-*
[perl5.git] / cflags.SH
CommitLineData
1f00b0d6
S
1#!/bin/sh
2
cb9238a7
DM
3# Generate the cflags script, which is used to determine what cflags
4# to pass to the compiler.
5# We create a temporary test c program and repeatedly compile it with
6# various candidate flags, and from the compiler output, determine what
7# flags are supported.
8# From this we initialise the following variables in the cflags script:
9#
10# $warn
11# $stdflags
12# $extra
13# $_exe
14
a02608de 15case $PERL_CONFIG_SH in
1c3d792e 16'')
a0d0e21e
LW
17 if test -f config.sh; then TOP=.;
18 elif test -f ../config.sh; then TOP=..;
19 elif test -f ../../config.sh; then TOP=../..;
20 elif test -f ../../../config.sh; then TOP=../../..;
21 elif test -f ../../../../config.sh; then TOP=../../../..;
22 else
23 echo "Can't find config.sh."; exit 1
24 fi
25 . $TOP/config.sh
26 ;;
1c3d792e 27esac
44213caa
DM
28# This forces SH files to create target in same directory as SH file.
29# This is so that make depend always knows where to find SH derivatives.
1c3d792e
LW
30case "$0" in
31*/*) cd `expr X$0 : 'X\(.*\)/'` ;;
32esac
bc730b18 33
37723ebb 34if test -f config_h.SH -a ! -f config.h; then
6ef8aa7c 35 . ./config_h.SH
11e2f395 36 CONFIG_H=already-done
6ef8aa7c
RGS
37fi
38
bc730b18
JH
39warn=''
40
41# Add -Wall for the core modules iff gcc and not already -Wall
42case "$gccversion" in
43'') ;;
44Intel*) ;; # The Intel C++ plays gcc on TV but is not really it.
45*) case "$ccflags" in
46 *-Wall*) ;;
47 *) warn="$warn -Wall" ;;
48 esac
49 ;;
50esac
51
a07cd53d
JH
52# Create a test source file for testing what options can be fed to
53# gcc in this system; include a selection of most common and commonly
54# hairy include files.
55
56cat >_cflags.c <<__EOT__
57#include "EXTERN.h"
58#include "perl.h"
59/* The stdio.h, errno.h, and setjmp.h should be there in any ANSI C89. */
60#include <stdio.h>
61#include <errno.h>
62#include <setjmp.h>
63/* Just in case the inclusion of perl.h did not
64 * pull in enough system headers, let's try again. */
65#ifdef I_STDLIB
66#include <stdlib.h>
67#endif
68#ifdef I_STDDEF
69#include <stddef.h>
70#endif
71#ifdef I_STDARG
72#include <stdarg.h>
73#endif
74#ifdef I_LIMITS
75#include <limits.h>
76#endif
77#ifdef I_DIRENT
78#include <dirent.h>
79#endif
80#ifdef I_UNISTD
81#include <unistd.h>
82#endif
463bdbaf 83#ifdef I_SYS_TYPES
a07cd53d
JH
84#include <sys/types.h>
85#endif
463bdbaf 86#ifdef I_SYS_PARAM
a07cd53d
JH
87#include <sys/param.h>
88#endif
463bdbaf 89#ifdef I_SYS_RESOURCE
a07cd53d
JH
90#include <sys/resource.h>
91#endif
463bdbaf 92#ifdef I_SYS_SELECT
a07cd53d
JH
93#include <sys/select.h>
94#endif
95#if defined(HAS_SOCKET) && !defined(VMS) && !defined(WIN32) /* See perl.h. */
96#include <sys/socket.h>
97#endif
463bdbaf 98#ifdef I_SYS_STAT
a07cd53d
JH
99#include <sys/stat.h>
100#endif
463bdbaf 101#ifdef I_SYS_TIME
a07cd53d
JH
102#include <sys/time.h>
103#endif
463bdbaf 104#ifdef I_SYS_TIMES
a07cd53d
JH
105#include <sys/times.h>
106#endif
463bdbaf 107#ifdef I_SYS_WAIT
a07cd53d
JH
108#include <sys/wait.h>
109#endif
110/* The gcc -ansi can cause a lot of noise in Solaris because of:
111 /usr/include/sys/resource.h:148: warning: 'struct rlimit64' declared inside parameter list
112 */
113int main(int argc, char *argv[]) {
114
115/* Add here test code found to be problematic in some gcc platform. */
116
117/* Off_t/off_t is a struct in Solaris with largefiles, and with gcc -ansi
118 * that struct cannot be compared in some gcc releases with a flat
119 * integer, such as a STRLEN. */
120
215ed6ce 121 IV iv;
a07cd53d
JH
122 Off_t t0a = 2;
123 STRLEN t0b = 3;
124 int t0c = t0a == t0b;
125
215ed6ce
AD
126/* In FreeBSD 6.2 (and probably other releases too), with -Duse64bitint,
127 perl will use atoll(3). However, that declaration is hidden in <stdlib.h>
128 if we force the compiler to use -std=c89 mode.
129*/
130 iv = Atol("42");
131
132 return (!t0c && (iv == 42)) ? 0 : -1; /* Try to avoid 'unused' warnings. */
a07cd53d
JH
133}
134__EOT__
135
136stdflags=''
bc730b18 137
215ed6ce
AD
138# Further gcc warning options. Build up a list of options that work.
139# Note that some problems may only show up with combinations of options,
140# e.g. a warning might show up only with -Wall -ansi, not with either
141# one individually.
813665af 142# TODO: Ponder whether to migrate this back to Configure so hints files can
215ed6ce 143# tweak it. Also, be paranoid about whether results we've deduced in Configure
813665af 144# (especially about things like long long, which are not in C89) will still be
215ed6ce
AD
145# valid if we now add flags like -std=c89.
146
bc730b18
JH
147case "$gccversion" in
148'') ;;
a07cd53d
JH
149[12]*) ;; # gcc versions 1 (gasp!) and 2 are not good for this.
150Intel*) ;; # # Is that you, Intel C++?
db7198b5
RB
151*) for opt in -ansi -std=c89 -W -Wextra -Wdeclaration-after-statement \
152 -Wendif-labels -Wc++-compat -Wwrite-strings
bc730b18
JH
153 do
154 case " $ccflags " in
a07cd53d
JH
155 *" $opt "*) ;; # Skip if already there.
156 *) rm -f _cflags$_exe
4b76cb9d 157 case "`$cc -DPERL_NO_INLINE_FUNCTIONS $ccflags $warn $stdflags $opt _cflags.c -o _cflags$_exe 2>&1`" in
bc730b18 158 *"unrecognized"*) ;;
d39195d4 159 *"unknown"*) ;;
215ed6ce 160 *"implicit declaration"*) ;; # Was something useful hidden?
bc730b18 161 *"Invalid"*) ;;
9ac4c107 162 *"is valid for C"*) ;;
a07cd53d
JH
163 *) if test -x _cflags$_exe
164 then
165 case "$opt" in
166 -std*) stdflags="$stdflags $opt" ;;
167 *) warn="$warn $opt" ;;
168 esac
169 fi
170 ;;
bc730b18
JH
171 esac
172 ;;
173 esac
bc730b18
JH
174 done
175 ;;
176esac
a07cd53d 177rm -f _cflags.c _cflags$_exe
bc730b18 178
9ac4c107
JH
179case "$gccversion" in
180'') ;;
181*)
3e8416a3
RGS
182 if [ "$gccansipedantic" = "" ]; then
183 # If we have -Duse64bitint (or equivalent) in effect and the quadtype
184 # has become 'long long', gcc -pedantic becomes unbearable (moreso
185 # when combined with -Wall) because long long and LL and %lld|%Ld
186 # become warn-worthy. So let's drop the -pedantic in that case.
187 case "$quadtype:$sPRId64" in
188 "long long"*|*lld*|*Ld*)
5bd6d383 189 echo "cflags.SH: Removing -pedantic and warn because of quadtype='long long'."
3e8416a3
RGS
190 ccflags="`echo $ccflags|sed 's/-pedantic/ /'`"
191 warn="`echo $warn|sed 's/-pedantic/ /'`"
192 ;;
193 esac
215ed6ce
AD
194 # Similarly, since 'long long' isn't part of C89, FreeBSD 6.2 headers
195 # don't declare atoll() under -std=c89, but we need it. In general,
196 # insisting on -std=c89 is inconsistent with insisting on using
813665af 197 # 'long long'. So drop -std=c89 and -ansi as well if we're using
215ed6ce
AD
198 # 'long long' as our main integral type.
199 case "$ivtype" in
200 "long long")
5bd6d383 201 echo "cflags.SH: Removing -pedantic, -std=c89, and -ansi because of ivtype='long long'."
215ed6ce
AD
202 ccflags=`echo $ccflags|sed -e 's/-pedantic/ /' -e 's/-std=c89/ /' -e 's/-ansi/ /'`
203 warn=`echo $warn|sed -e 's/-pedantic/ /' -e 's/-ansi/ /'`
a5105380 204 stdflags=`echo $stdflags|sed -e 's/-std=c89/ /'`
215ed6ce
AD
205 ;;
206 esac
3e8416a3 207 fi
5cf489d6
RGS
208 # Using certain features (like the gcc statement expressions)
209 # requires knowing whether -pedantic has been specified.
9ac4c107 210 case "$warn$ccflags" in
5bd6d383
JH
211 *-pedantic*)
212 echo "cflags.SH: Adding -DPERL_PEDANTIC because of -pedantic."
213 warn="$warn -DPERL_GCC_PEDANTIC"
214 ;;
215 esac
216 ;;
217esac
218
219case "$cc" in
220*g++*)
221 # Extra paranoia in case people have bad canned ccflags:
222 # bad in the sense that the flags are accepted by g++,
223 # but then whined about.
224 for f in -Wdeclaration-after-statement -std=c89
225 do
226 case "$ccflags" in
227 *"$f"*)
228 echo "cflags.SH: Removing $ccflags because of g++."
229 ccflags=`echo $ccflags|sed 's/$f/ /'` ;;
230 esac
231 done
232 ;;
233esac
234
235for f in -Wdeclaration-after-statement
236do
237 case "$cppflags" in
238 *"$f"*)
239 echo "cflags.SH: Removing $f from cppflags."
240 cppflags=`echo $cppflags|sed 's/$f/ /'` ;;
241 esac
242done
243
10edeb5d 244# Code to set any extra flags here.
a07cd53d 245extra=''
11fcce85 246
2b317908 247echo "Extracting cflags (with variable substitutions)"
44213caa
DM
248# This section of the file will have variable substitutions done on it.
249# Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
250# Protect any dollar signs and backticks that you do not want interpreted
251# by putting a backslash in front. You may delete these comments.
165b7424 252rm -f cflags
2b317908 253$spitshell >cflags <<!GROK!THIS!
ecfc5424 254$startsh
bc730b18 255
cb9238a7
DM
256# !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
257
258# This file is generated by cflags.SH
259
5bd6d383 260# Used to restore possible edits by cflags.SH.
57d2761b 261myccflags="$ccflags"
11fcce85 262# Extra warnings, used e.g. for gcc.
bc730b18 263warn="$warn"
11fcce85
JH
264# Extra standardness.
265stdflags="$stdflags"
b1e55cab
JH
266# Extra extra.
267extra="$extra"
dcff826f
YO
268# what do executables look like?
269_exe="$_exe"
bc730b18 270
2b317908
LW
271!GROK!THIS!
272
44213caa 273# In the following dollars and backticks do not need the extra backslash.
2b317908 274$spitshell >>cflags <<'!NO!SUBS!'
a02608de 275case $PERL_CONFIG_SH in
2b317908 276'')
a0d0e21e
LW
277 if test -f config.sh; then TOP=.;
278 elif test -f ../config.sh; then TOP=..;
279 elif test -f ../../config.sh; then TOP=../..;
280 elif test -f ../../../config.sh; then TOP=../../..;
281 elif test -f ../../../../config.sh; then TOP=../../../..;
282 else
283 echo "Can't find config.sh."; exit 1
284 fi
285 . $TOP/config.sh
57d2761b 286 ccflags="$myccflags" # Restore possible edits by cflags.SH.
a0d0e21e
LW
287 ;;
288esac
289
ba0f5503
DM
290# syntax: cflags [optimize=XXX] [file[.suffix]] ...
291# displays the proposed compiler command line for each 'file'
292#
293# with no file, dispalys it for all *.c files.
294# The optimise=XXX arg (if present) is evalled, setting the default
295# value of the $optimise variable, which is output on the command line
296# (but which may be overridden for specific files below)
75550b4e 297
1167a30e 298case "X$1" in
26102cc7 299Xoptimize=*|X"optimize=*")
1167a30e
IZ
300 eval "$1"
301 shift
302 ;;
303esac
304
1c3d792e
LW
305case $# in
3060) set *.c; echo "The current C flags are:" ;;
1c3d792e 307esac
2b317908 308
578789a7 309set `echo "$* " | sed -e 's/\.[oc] / /g' -e 's/\.obj / /g' -e "s/\\$obj_ext / /g"`
2b317908 310
1c3d792e
LW
311for file do
312
313 case "$#" in
314 1) ;;
2b317908 315 *) echo $n " $file.c $c" ;;
1c3d792e
LW
316 esac
317
44213caa 318 # allow variables like toke_cflags to be evaluated
2b317908 319
8736538c
AS
320 if echo $file | grep -v / >/dev/null
321 then
322 eval 'eval ${'"${file}_cflags"'-""}'
323 fi
2b317908 324
44213caa 325 # or customize here
2b317908 326
1c3d792e 327 case "$file" in
1c3d792e 328 *) ;;
5729ffdd 329
5bd6d383
JH
330 # Customization examples follow.
331 #
332 # The examples are intentionally unreachable as the '*)' case above always
44213caa
DM
333 # matches. To use them, move before the '*)' and edit as appropriate.
334 # It is not a good idea to set ccflags to an absolute value here, as it
335 # often contains general -D defines which are needed for correct
336 # compilation. It is better to edit ccflags as shown, using interpolation
337 # to add flags, or sed to remove flags.
5729ffdd 338
5bd6d383
JH
339 av) ccflags=`echo $ccflags | sed -e s/-pipe//` ;;
340 deb) ccflags="$ccflags -fno-jump-tables" ;;
341 hv) warn=`echo $warn | sed -e s/-Wextra//` ;;
342 toke) optimize=-O0 ;;
7e827271 343 esac
666ea192 344
44213caa 345 # Can we perhaps use $ansi2knr here
b1e55cab 346 echo "$cc -c -DPERL_CORE $ccflags $stdflags $optimize $warn $extra"
2b317908 347
a0d0e21e 348 . $TOP/config.sh
2b317908 349
a7d59441 350 # end per file behaviour
1c3d792e 351done
2b317908 352!NO!SUBS!
a0d0e21e 353chmod 755 cflags
2b317908 354$eunicefix cflags