]> git.ipfire.org Git - thirdparty/cups.git/blob - config-scripts/cups-compiler.m4
Prevent underflow (unlikely, but possible)
[thirdparty/cups.git] / config-scripts / cups-compiler.m4
1 dnl
2 dnl Compiler stuff for CUPS.
3 dnl
4 dnl Copyright 2007-2017 by Apple Inc.
5 dnl Copyright 1997-2007 by Easy Software Products, all rights reserved.
6 dnl
7 dnl These coded instructions, statements, and computer programs are the
8 dnl property of Apple Inc. and are protected by Federal copyright
9 dnl law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 dnl which should have been included with this file. If this file is
11 dnl missing or damaged, see the license at "http://www.cups.org/".
12 dnl
13
14 dnl Clear the debugging and non-shared library options unless the user asks
15 dnl for them...
16 INSTALL_STRIP=""
17 OPTIM=""
18 AC_SUBST(INSTALL_STRIP)
19 AC_SUBST(OPTIM)
20
21 AC_ARG_WITH(optim, [ --with-optim set optimization flags ])
22 AC_ARG_ENABLE(debug, [ --enable-debug build with debugging symbols])
23 AC_ARG_ENABLE(debug_guards, [ --enable-debug-guards build with memory allocation guards])
24 AC_ARG_ENABLE(debug_printfs, [ --enable-debug-printfs build with CUPS_DEBUG_LOG support])
25 AC_ARG_ENABLE(unit_tests, [ --enable-unit-tests build and run unit tests])
26
27 dnl For debugging, keep symbols, otherwise strip them...
28 if test x$enable_debug = xyes; then
29 OPTIM="-g"
30 else
31 INSTALL_STRIP="-s"
32 fi
33
34 dnl Debug printfs can slow things down, so provide a separate option for that
35 if test x$enable_debug_printfs = xyes; then
36 CFLAGS="$CFLAGS -DDEBUG"
37 CXXFLAGS="$CXXFLAGS -DDEBUG"
38 fi
39
40 dnl Debug guards use an extra 4 bytes for some structures like strings in the
41 dnl string pool, so provide a separate option for that
42 if test x$enable_debug_guards = xyes; then
43 CFLAGS="$CFLAGS -DDEBUG_GUARDS"
44 CXXFLAGS="$CXXFLAGS -DDEBUG_GUARDS"
45 fi
46
47 dnl Unit tests take up time during a compile...
48 if test x$enable_unit_tests = xyes; then
49 if test "$build" != "$host"; then
50 AC_MSG_ERROR([Sorry, cannot build unit tests when cross-compiling.])
51 fi
52
53 UNITTESTS="unittests"
54 else
55 UNITTESTS=""
56 fi
57 AC_SUBST(UNITTESTS)
58
59 dnl Setup general architecture flags...
60 AC_ARG_WITH(archflags, [ --with-archflags set default architecture flags ])
61 AC_ARG_WITH(ldarchflags, [ --with-ldarchflags set program architecture flags ])
62
63 if test -z "$with_archflags"; then
64 ARCHFLAGS=""
65 else
66 ARCHFLAGS="$with_archflags"
67 fi
68
69 if test -z "$with_ldarchflags"; then
70 if test "$host_os_name" = darwin; then
71 # Only create Intel programs by default
72 LDARCHFLAGS="`echo $ARCHFLAGS | sed -e '1,$s/-arch ppc64//'`"
73 else
74 LDARCHFLAGS="$ARCHFLAGS"
75 fi
76 else
77 LDARCHFLAGS="$with_ldarchflags"
78 fi
79
80 AC_SUBST(ARCHFLAGS)
81 AC_SUBST(LDARCHFLAGS)
82
83 dnl Read-only data/program support on Linux...
84 AC_ARG_ENABLE(relro, [ --enable-relro build with the GCC relro option])
85
86 dnl Update compiler options...
87 CXXLIBS="${CXXLIBS:=}"
88 AC_SUBST(CXXLIBS)
89
90 PIEFLAGS=""
91 AC_SUBST(PIEFLAGS)
92
93 RELROFLAGS=""
94 AC_SUBST(RELROFLAGS)
95
96 if test -n "$GCC"; then
97 # Add GCC-specific compiler options...
98 if test -z "$OPTIM"; then
99 if test "x$with_optim" = x; then
100 # Default to optimize-for-size and debug
101 OPTIM="-Os -g"
102 else
103 OPTIM="$with_optim $OPTIM"
104 fi
105 fi
106
107 # Generate position-independent code as needed...
108 if test $PICFLAG = 1; then
109 OPTIM="-fPIC $OPTIM"
110 fi
111
112 # The -fstack-protector option is available with some versions of
113 # GCC and adds "stack canaries" which detect when the return address
114 # has been overwritten, preventing many types of exploit attacks.
115 AC_MSG_CHECKING(whether compiler supports -fstack-protector)
116 OLDCFLAGS="$CFLAGS"
117 CFLAGS="$CFLAGS -fstack-protector"
118 AC_TRY_LINK(,,
119 if test "x$LSB_BUILD" = xy; then
120 # Can't use stack-protector with LSB binaries...
121 OPTIM="$OPTIM -fno-stack-protector"
122 else
123 OPTIM="$OPTIM -fstack-protector"
124 fi
125 AC_MSG_RESULT(yes),
126 AC_MSG_RESULT(no))
127 CFLAGS="$OLDCFLAGS"
128
129 if test "x$LSB_BUILD" != xy; then
130 # The -fPIE option is available with some versions of GCC and
131 # adds randomization of addresses, which avoids another class of
132 # exploits that depend on a fixed address for common functions.
133 #
134 # Not available to LSB binaries...
135 AC_MSG_CHECKING(whether compiler supports -fPIE)
136 OLDCFLAGS="$CFLAGS"
137 case "$host_os_name" in
138 darwin*)
139 CFLAGS="$CFLAGS -fPIE -Wl,-pie"
140 AC_TRY_COMPILE(,,[
141 PIEFLAGS="-fPIE -Wl,-pie"
142 AC_MSG_RESULT(yes)],
143 AC_MSG_RESULT(no))
144 ;;
145
146 *)
147 CFLAGS="$CFLAGS -fPIE -pie"
148 AC_TRY_COMPILE(,,[
149 PIEFLAGS="-fPIE -pie"
150 AC_MSG_RESULT(yes)],
151 AC_MSG_RESULT(no))
152 ;;
153 esac
154 CFLAGS="$OLDCFLAGS"
155 fi
156
157 if test "x$with_optim" = x; then
158 # Add useful warning options for tracking down problems...
159 OPTIM="-Wall -Wno-format-y2k -Wunused $OPTIM"
160
161 AC_MSG_CHECKING(whether compiler supports -Wno-unused-result)
162 OLDCFLAGS="$CFLAGS"
163 CFLAGS="$CFLAGS -Werror -Wno-unused-result"
164 AC_TRY_COMPILE(,,
165 [OPTIM="$OPTIM -Wno-unused-result"
166 AC_MSG_RESULT(yes)],
167 AC_MSG_RESULT(no))
168 CFLAGS="$OLDCFLAGS"
169
170 AC_MSG_CHECKING(whether compiler supports -Wsign-conversion)
171 OLDCFLAGS="$CFLAGS"
172 CFLAGS="$CFLAGS -Werror -Wsign-conversion"
173 AC_TRY_COMPILE(,,
174 [OPTIM="$OPTIM -Wsign-conversion"
175 AC_MSG_RESULT(yes)],
176 AC_MSG_RESULT(no))
177 CFLAGS="$OLDCFLAGS"
178
179 AC_MSG_CHECKING(whether compiler supports -Wno-tautological-compare)
180 OLDCFLAGS="$CFLAGS"
181 CFLAGS="$CFLAGS -Werror -Wno-tautological-compare"
182 AC_TRY_COMPILE(,,
183 [OPTIM="$OPTIM -Wno-tautological-compare"
184 AC_MSG_RESULT(yes)],
185 AC_MSG_RESULT(no))
186 CFLAGS="$OLDCFLAGS"
187
188 # Additional warning options for development testing...
189 if test -d .svn; then
190 OPTIM="-Werror $OPTIM"
191 fi
192 fi
193
194 case "$host_os_name" in
195 darwin*)
196 # -D_FORTIFY_SOURCE=2 adds additional object size
197 # checking, basically wrapping all string functions
198 # with buffer-limited ones. Not strictly needed for
199 # CUPS since we already use buffer-limited calls, but
200 # this will catch any additions that are broken.
201 CFLAGS="$CFLAGS -D_FORTIFY_SOURCE=2"
202 ;;
203
204 linux*)
205 # The -z relro option is provided by the Linux linker command to
206 # make relocatable data read-only.
207 if test x$enable_relro = xyes; then
208 RELROFLAGS="-Wl,-z,relro,-z,now"
209 fi
210 ;;
211 esac
212 else
213 # Add vendor-specific compiler options...
214 case $host_os_name in
215 sunos*)
216 # Solaris
217 if test -z "$OPTIM"; then
218 if test "x$with_optim" = x; then
219 OPTIM="-xO2"
220 else
221 OPTIM="$with_optim $OPTIM"
222 fi
223 fi
224
225 if test $PICFLAG = 1; then
226 OPTIM="-KPIC $OPTIM"
227 fi
228 ;;
229 *)
230 # Running some other operating system; inform the user they
231 # should contribute the necessary options to
232 # cups-support@cups.org...
233 echo "Building CUPS with default compiler optimizations; contact"
234 echo "cups-devel@cups.org with uname and compiler options needed"
235 echo "for your platform, or set the CFLAGS and LDFLAGS environment"
236 echo "variables before running configure."
237 ;;
238 esac
239 fi
240
241 # Add general compiler options per platform...
242 case $host_os_name in
243 linux*)
244 # glibc 2.8 and higher breaks peer credentials unless you
245 # define _GNU_SOURCE...
246 OPTIM="$OPTIM -D_GNU_SOURCE"
247 ;;
248 esac