xref: /openbsd-src/gnu/usr.bin/perl/cflags.SH (revision e068048151d29f2562a32185e21a8ba885482260)
1#!/bin/sh
2
3# Generate the cflags script, which is used to determine what cflags
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
15# various candidate flags, and from the compiler output, determine what
16# flags are supported.
17#
18# From this we initialise the following variables in the cflags script:
19#
20#   $myccflags (possibly edited version of $Config{ccflags})
21#   $warn
22#   $stdflags
23#   $extra
24#   $_exe
25
26case $PERL_CONFIG_SH in
27'')
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	;;
38esac
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.
41case "$0" in
42*/*) cd `expr X$0 : 'X\(.*\)/'` ;;
43esac
44
45if test -f config_h.SH -a ! -f config.h; then
46    . ./config_h.SH
47    CONFIG_H=already-done
48fi
49
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
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#include <stdlib.h>
77#include <stddef.h>
78#include <stdarg.h>
79#include <limits.h>
80#ifdef I_DIRENT
81#include <dirent.h>
82#endif
83#ifdef I_UNISTD
84#include <unistd.h>
85#endif
86#ifdef I_SYS_TYPES
87#include <sys/types.h>
88#endif
89#ifdef I_SYS_PARAM
90#include <sys/param.h>
91#endif
92#ifdef I_SYS_RESOURCE
93#include <sys/resource.h>
94#endif
95#ifdef I_SYS_SELECT
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
101#ifdef I_SYS_STAT
102#include <sys/stat.h>
103#endif
104#ifdef I_SYS_TIME
105#include <sys/time.h>
106#endif
107#ifdef I_SYS_TIMES
108#include <sys/times.h>
109#endif
110#ifdef I_SYS_WAIT
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
124  IV iv;
125  Off_t t0a = 2;
126  STRLEN t0b = 3;
127  int t0c = (STRLEN)t0a == t0b;
128
129  printf("%s: %d\n", argv[0], argc);
130
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. */
138}
139__EOT__
140
141stdflags=''
142
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.
147# TODO:  Ponder whether to migrate this back to Configure so hints files can
148# tweak it.  Also, be paranoid about whether results we've deduced in Configure
149# will still be valid if we now add flags like -std=c99.
150
151pedantic=''
152case "$gccansipedantic" in
153define) pedantic='-pedantic' ;;
154esac
155
156case "$gccversion" in
157'') ;;
158[12].*) ;; # gcc versions 1 (gasp!) and 2 are not good for this.
159Intel*) ;; # # Is that you, Intel C++?
160#
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.
168# You need to add the -pedantic for that.  To do this with Configure,
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.
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
182#   longer then the ANSI C99 minimum of 4095 bytes.
183#
184# NOTE 3: the relative order of these options matters:
185# -Wextra before -W
186# -W before -Wno-long-long -Wno-declaration-after-statement
187#
188*) warns="$pedantic \
189	-Werror=pointer-arith \
190	-Werror=vla \
191	-Wextra -W \
192	-Wno-long-long -Wno-declaration-after-statement \
193	-Wc++-compat -Wwrite-strings"
194   case " $ccflags " in
195   *" -std="*) ;; # Already have -std=...
196   *) warns="-std=c99 $warns" ;;
197   esac
198   for opt in $warns
199    do
200       case " $ccflags " in
201       *" $opt "*) ;; # Skip if already there.
202       *) rm -f _cflags$_exe
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
212          *"unrecognized"*) ;;
213          *"unknown"*) ;;
214          *"implicit declaration"*) ;; # Was something useful hidden?
215          *"Invalid"*) ;;
216          *"is valid for C"*) ;;
217          *) if test -x _cflags$_exe
218             then
219               case "$opt" in
220               -std*)
221                 echo "cflags.SH: Adding $opt."
222                 stdflags="$stdflags $opt"
223                 ;;
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                 ;;
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                  ;;
245               *)
246                  echo "cflags.SH: Adding $opt."
247                  warn="$warn $opt"
248                  ;;
249               esac
250             fi
251             ;;
252          esac
253          ;;
254       esac
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
271    done
272    ;;
273esac
274rm -f _cflags.c _cflags$_exe
275
276case "$gccversion" in
277'') ;;
278*)
279  case "$warn$ccflags" in
280  *-pedantic*)
281    # For -std=c99 -pedantic, only the %Ld format seems to be warn-worthy.
282    # 'long long' and '%lld' are now kosher.
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
288    **Ld*) remove='Ld' ;;
289    *) case "$usedtrace" in
290       define) remove='usedtrace' ;;
291       esac
292       ;;
293    esac
294    case "$remove" in
295    '') ;;
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/ /'`
299      ;;
300    esac
301    ;;
302  esac
303  ;;
304esac
305
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
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
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.
337  for f in -Wc++-compat -std=c99
338  do
339    case "$ccflags$warn" in
340    *"$f"*)
341      echo "cflags.SH: Removing $f because of g++."
342      ccflags=`echo $ccflags|sed 's/$f/ /'`
343      warn=`echo $warn|sed 's/$f/ /'`
344      ;;
345    esac
346  done
347  ;;
348esac
349
350for f in -Wpointer-arith -Werror=pointer-arith
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
359# If usethreads and clang, add -Wthread-safety for clang 3.6 or later.
360# gccversion is defined also for clang, because compat, use that for matching.
361# Apple overwrites clang version with XCode version, see hints/darwin.sh
362# for the gory details.  Aggressively forward-proofing.
363case "$usethreads" in
364define)
365case "$gccversion" in
366*" Clang 3."[56789]*|*" Clang "[456]*|*"Apple LLVM 6.1"*|*"Apple LLVM "[789]*)
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
382# gcc version 12 and 13 are overly aggressive with use-after-free warnings
383# and have false positives on code that shouldn't warn, and they haven't
384# sorted it out yet. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108115
385case "$gccversion" in
386"1"[23]*)
387    for f in -Wno-use-after-free
388    do
389        echo "cflags.SH: Adding $f because of false positives in gccversion '$gccversion'"
390        warn="$warn $f"
391    done
392;;
393esac
394
395echo "cflags.SH: cc       = $cc"
396echo "cflags.SH: ccflags  = $ccflags"
397echo "cflags.SH: stdflags = $stdflags"
398echo "cflags.SH: optimize = $optimize"
399echo "cflags.SH: warn     = $warn"
400
401# Code to set any extra flags here.
402extra=''
403
404# Protect double or single quotes for better restoring of ccflags.
405myccflags=`echo $ccflags | sed -e 's/"/\\\"/g' -e "s/'/\\\'/g"`
406
407echo "Extracting cflags (with variable substitutions)"
408# This section of the file will have variable substitutions done on it.
409# Move anything that needs config subs from !NO!SUBS! section to !GROK!THIS!.
410# Protect any dollar signs and backticks that you do not want interpreted
411# by putting a backslash in front.  You may delete these comments.
412rm -f cflags
413$spitshell >cflags <<!GROK!THIS!
414$startsh
415
416# !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
417
418# This file is generated by cflags.SH
419
420# Used to restore possible edits by cflags.SH.
421myccflags="$myccflags"
422
423# Extra warnings, used e.g. for gcc.
424warn="$warn"
425# Extra standardness.
426stdflags="$stdflags"
427# Extra extra.
428extra="$extra"
429# what do executables look like?
430_exe="$_exe"
431
432!GROK!THIS!
433
434# In the following dollars and backticks do not need the extra backslash.
435$spitshell >>cflags <<'!NO!SUBS!'
436case $PERL_CONFIG_SH in
437'')
438	if test -f config.sh; then TOP=.;
439	elif test -f ../config.sh; then TOP=..;
440	elif test -f ../../config.sh; then TOP=../..;
441	elif test -f ../../../config.sh; then TOP=../../..;
442	elif test -f ../../../../config.sh; then TOP=../../../..;
443	else
444		echo "Can't find config.sh."; exit 1
445	fi
446	. $TOP/config.sh
447        ccflags="$myccflags"  # Restore possible edits by cflags.SH.
448	;;
449esac
450
451# syntax: cflags [optimize=XXX] [file[.suffix]] ...
452#   displays the proposed compiler command line for each 'file'
453#
454#   with no file, dispalys it for all *.c files.
455#   The optimise=XXX arg (if present) is evalled, setting the default
456#   value of the $optimise variable, which is output on the command line
457#   (but which may be overridden for specific files below)
458
459case "X$1" in
460Xoptimize=*|X"optimize=*")
461	eval "$1"
462	shift
463	;;
464esac
465
466case $# in
4670) set *.c; echo "The current C flags are:" ;;
468esac
469
470set `echo "$* " | sed -e 's/\.[oc] / /g' -e 's/\.obj / /g' -e "s/\\$obj_ext / /g"`
471
472for file do
473
474    case "$#" in
475    1) ;;
476    *) echo $n "    $file.c	$c" ;;
477    esac
478
479    # allow variables like toke_cflags to be evaluated
480
481    case "$file" in
482    */*) ;;
483    *) eval 'eval ${'"${file}_cflags"'-""}' ;;
484    esac
485
486    # or customize here
487
488    case "$file" in
489    regcomp) : work around http://bugs.debian.org/754054
490        case $archname in
491        mips-*|mipsel-*)
492            optimize="$optimize -fno-tree-vrp";;
493        esac;;
494    *) ;;
495
496    # Customization examples follow.
497    #
498    # The examples are intentionally unreachable as the '*)' case above always
499    # matches. To use them, move before the '*)' and edit as appropriate.
500    # It is not a good idea to set ccflags to an absolute value here, as it
501    # often contains general -D defines which are needed for correct
502    # compilation. It is better to edit ccflags as shown, using interpolation
503    # to add flags, or sed to remove flags.
504
505    av) ccflags=`echo $ccflags | sed -e s/-pipe//` ;;
506    deb) ccflags="$ccflags -fno-jump-tables" ;;
507    hv) warn=`echo $warn | sed -e s/-Wextra//` ;;
508    toke) optimize=-O0 ;;
509    esac
510
511    echo "$cc -c -DPERL_CORE $ccflags $stdflags $optimize $warn $extra"
512
513    . $TOP/config.sh
514
515    # end per file behaviour
516done
517!NO!SUBS!
518chmod 755 cflags
519$eunicefix cflags
520