chiark / gitweb /
finalise 0.4.3
[secnet.git] / ac_prog_cc_no_writeable_strings.m4
1 dnl @synopsis AC_PROG_CC_NO_WRITEABLE_STRINGS(substvar [,hard])
2 dnl
3 dnl Try to find a compiler option that warns when a stringliteral is
4 dnl used in a place that could potentially modify the address. This
5 dnl should warn on giving an stringliteral to a function that asks of
6 dnl a non-const-modified char-pointer.
7 dnl
8 dnl The sanity check is done by looking at string.h which has a set
9 dnl of strcpy definitions that should be defined with const-modifiers
10 dnl to not emit a warning in all so many places.
11 dnl
12 dnl Currently this macro knows about GCC.
13 dnl hopefully will evolve to use:    Solaris C compiler,
14 dnl Digital Unix C compiler, C for AIX Compiler, HP-UX C compiler,
15 dnl and IRIX C compiler.
16 dnl
17 dnl @version $Id: ac_prog_cc_no_writeable_strings.m4,v 1.1 2002/02/20 16:18:18 steve Exp $
18 dnl @author Guido Draheim <guidod@gmx.de>
19
20 dnl [This appears to be a previous version of
21 dnl  ax_cflags_no_writable_strings.m4 which is nowadays to be found in
22 dnl  the Autoconf Archive.  It was imported there on 2007-02-14
23 dnl  in commit 16aee45643e593e2833e4dff19df7b5f14267a79 where the file
24 dnl  has a GPLv2 permission notice.  Therefore I feel justified in
25 dnl  adding the copyright permission notice below: -iwj]
26 dnl
27 dnl  This file is Free Software.  It has been copied into secnet.
28 dnl
29 dnl  Copyright 2002 Guido Draheim
30 dnl
31 dnl  You may redistribute secnet as a whole and/or modify it under the
32 dnl  terms of the GNU General Public License as published by the Free
33 dnl  Software Foundation; either version 3, or (at your option) any
34 dnl  later version.
35 dnl
36 dnl  You may redistribute this file and/or modify it under the terms of
37 dnl  the GNU General Public License as published by the Free Software
38 dnl  Foundation; either version 2, or (at your option) any later
39 dnl  version.
40 dnl
41 dnl  This software is distributed in the hope that it will be useful,
42 dnl  but WITHOUT ANY WARRANTY; without even the implied warranty of
43 dnl  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44 dnl  GNU General Public License for more details.
45 dnl
46 dnl  You should have received a copy of the GNU General Public License
47 dnl  along with this software; if not, see
48 dnl  https://www.gnu.org/licenses/gpl.html.
49
50 AC_DEFUN([AC_PROG_CC_NO_WRITEABLE_STRINGS], [
51   pushdef([CV],ac_cv_prog_cc_no_writeable_strings)dnl
52   hard=$2
53   if test -z "$hard"; then
54     msg="C to warn about writing to stringliterals"
55   else
56     msg="C to prohibit any write to stringliterals"
57   fi
58   AC_CACHE_CHECK($msg, CV, [
59   cat > conftest.c <<EOF
60 #include <string.h>
61 int main (void)
62 {
63    char test[[16]];
64    if (strcpy (test, "test")) return 0;
65    return 1;
66 }
67 EOF
68   dnl GCC
69   if test "$GCC" = "yes"; 
70   then
71         if test -z "$hard"; then
72             CV="-Wwrite-strings"
73         else
74             CV="-fno-writable-strings -Wwrite-strings"
75         fi
76
77         if test -n "`${CC-cc} -c $CV conftest.c 2>&1`" ; then
78             CV="suppressed: string.h"
79         fi
80
81   dnl Solaris C compiler
82   elif  $CC -flags 2>&1 | grep "Xc.*strict ANSI C" > /dev/null 2>&1 &&
83         $CC -c -xstrconst conftest.c > /dev/null 2>&1 &&
84         test -f conftest.o 
85   then
86         # strings go into readonly segment
87         CV="-xstrconst"
88
89         rm conftest.o
90         if test -n "`${CC-cc} -c $CV conftest.c 2>&1`" ; then
91              CV="suppressed: string.h"
92         fi
93   
94   dnl HP-UX C compiler
95   elif  $CC > /dev/null 2>&1 &&
96         $CC -c +ESlit conftest.c > /dev/null 2>&1 &&
97         test -f conftest.o 
98   then
99        # strings go into readonly segment
100         CV="+ESlit"
101         
102         rm conftest.o
103         if test -n "`${CC-cc} -c $CV conftest.c 2>&1`" ; then
104              CV="suppressed: string.h"
105         fi
106
107   dnl Digital Unix C compiler
108   elif ! $CC > /dev/null 2>&1 &&
109         $CC -c -readonly_strings conftest.c > /dev/null 2>&1 &&
110         test -f conftest.o
111   then  
112        # strings go into readonly segment
113         CV="-readonly_strings"
114         
115         rm conftest.o
116         if test -n "`${CC-cc} -c $CV conftest.c 2>&1`" ; then
117              CV="suppressed: string.h"
118         fi
119
120   dnl C for AIX Compiler
121
122   dnl IRIX C compiler
123         # -use_readonly_const is the default for IRIX C, 
124         # puts them into .rodata, but they are copied later.
125         # need to be "-G0 -rdatashared" for strictmode but
126         # I am not sure what effect that has really.
127
128   fi
129   rm -f conftest.*
130   ])
131   if test -z "[$]$1" ; then
132     if test -n "$CV" ; then
133       case "$CV" in
134         suppressed*) $1="" ;; # known but suppressed
135         *)  $1="$CV" ;;
136       esac
137     fi
138   fi
139   AC_SUBST($1)
140   popdef([CV])dnl
141 ])
142
143