• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1dnl
2dnl Compiler tests for CUPS.
3dnl
4dnl Copyright © 2020-2024 by OpenPrinting.
5dnl Copyright © 2007-2018 by Apple Inc.
6dnl Copyright © 1997-2007 by Easy Software Products, all rights reserved.
7dnl
8dnl Licensed under Apache License v2.0.  See the file "LICENSE" for more
9dnl information.
10dnl
11
12dnl Clear the debugging and non-shared library options unless the user asks
13dnl for them...
14INSTALL_STRIP=""
15AC_SUBST(INSTALL_STRIP)
16
17AC_ARG_WITH([optim], AS_HELP_STRING([--with-optim], [set optimization flags]), [
18    OPTIM="$withval"
19], [
20    OPTIM=""
21])
22AC_SUBST([OPTIM])
23
24AC_ARG_ENABLE([debug], AS_HELP_STRING([--enable-debug], [build with debugging symbols]))
25AC_ARG_ENABLE([debug_guards], AS_HELP_STRING([--enable-debug-guards], [build with memory allocation guards]))
26AC_ARG_ENABLE([debug_printfs], AS_HELP_STRING([--enable-debug-printfs], [build with CUPS_DEBUG_LOG support]))
27AC_ARG_ENABLE([maintainer], AS_HELP_STRING([--enable-maintainer], [turn on maintainer mode (warnings as errors)]))
28AC_ARG_ENABLE([unit_tests], AS_HELP_STRING([--enable-unit-tests], [build and run unit tests]))
29
30dnl For debugging, keep symbols, otherwise strip them...
31AS_IF([test x$enable_debug = xyes -a "x$OPTIM" = x], [
32    OPTIM="-g"
33], [
34    INSTALL_STRIP="-s"
35])
36
37dnl Debug printfs can slow things down, so provide a separate option for that
38AS_IF([test x$enable_debug_printfs = xyes], [
39    CFLAGS="$CFLAGS -DDEBUG"
40    CXXFLAGS="$CXXFLAGS -DDEBUG"
41])
42
43dnl Debug guards use an extra 4 bytes for some structures like strings in the
44dnl string pool, so provide a separate option for that
45AS_IF([test x$enable_debug_guards = xyes], [
46    CFLAGS="$CFLAGS -DDEBUG_GUARDS"
47    CXXFLAGS="$CXXFLAGS -DDEBUG_GUARDS"
48])
49
50dnl Unit tests take up time during a compile...
51AS_IF([test x$enable_unit_tests = xyes], [
52    AS_IF([test "$build" != "$host"], [
53	AC_MSG_ERROR([Sorry, cannot build unit tests when cross-compiling.])
54    ])
55
56    UNITTESTS="unittests"
57], [
58    UNITTESTS=""
59])
60AC_SUBST([UNITTESTS])
61
62dnl Setup general architecture flags...
63AC_ARG_WITH([archflags], AS_HELP_STRING([--with-archflags], [set default architecture flags]))
64AC_ARG_WITH([ldarchflags], AS_HELP_STRING([--with-ldarchflags], [set program architecture flags]))
65
66AS_IF([test -z "$with_archflags"], [
67    ARCHFLAGS=""
68], [
69    ARCHFLAGS="$with_archflags"
70])
71
72AS_IF([test -z "$with_ldarchflags"], [
73    LDARCHFLAGS="$ARCHFLAGS"
74], [
75    LDARCHFLAGS="$with_ldarchflags"
76])
77
78AC_SUBST([ARCHFLAGS])
79AC_SUBST([LDARCHFLAGS])
80
81dnl Read-only data/program support on Linux...
82AC_ARG_ENABLE([relro], AS_HELP_STRING([--enable-relro], [build with the relro option]))
83
84dnl Clang/GCC address sanitizer...
85AC_ARG_ENABLE([sanitizer], AS_HELP_STRING([--enable-sanitizer], [build with AddressSanitizer]))
86
87dnl Update compiler options...
88CXXLIBS="${CXXLIBS:=}"
89AC_SUBST([CXXLIBS])
90
91PIEFLAGS=""
92AC_SUBST([PIEFLAGS])
93
94RELROFLAGS=""
95AC_SUBST([RELROFLAGS])
96
97WARNING_OPTIONS=""
98AC_SUBST([WARNING_OPTIONS])
99
100AS_IF([test -n "$GCC"], [
101    # Add GCC/Clang compiler options...
102
103    # Address sanitizer is a useful tool to use when developing/debugging
104    # code but adds about 2x overhead...
105    AS_IF([test x$enable_sanitizer = xyes], [
106	# Use -fsanitize=address with debugging...
107	OPTIM="$OPTIM -g -fsanitize=address"
108    ], [echo "$CXXFLAGS $CFLAGS" | grep -q _FORTIFY_SOURCE], [
109        # Don't add _FORTIFY_SOURCE if it is already there
110    ], [
111	# Otherwise use the Fortify enhancements to catch any unbounded
112	# string operations...
113	CFLAGS="$CFLAGS -D_FORTIFY_SOURCE=3"
114	CXXFLAGS="$CXXFLAGS -D_FORTIFY_SOURCE=3"
115    ])
116
117    # Default optimization options...
118    AS_IF([test -z "$OPTIM"], [
119	# Default to optimize-for-size and debug
120	OPTIM="-Os -g"
121    ])
122
123    # Generate position-independent code as needed...
124    AS_IF([test $PICFLAG = 1], [
125	OPTIM="-fPIC $OPTIM"
126    ])
127
128    # The -fstack-protector-strong and -fstack-protector options are available
129    # with some versions of GCC and adds "stack canaries" which detect
130    # when the return address has been overwritten, preventing many types of exploit attacks.
131    # First check for -fstack-protector-strong, then for -fstack-protector...
132    AC_MSG_CHECKING([whether compiler supports -fstack-protector-strong])
133    OLDCFLAGS="$CFLAGS"
134    CFLAGS="$CFLAGS -fstack-protector-strong"
135    AC_LINK_IFELSE([AC_LANG_PROGRAM()], [
136	OPTIM="$OPTIM -fstack-protector-strong"
137	AC_MSG_RESULT([yes])
138    ], [
139	AC_MSG_RESULT([no])
140	AC_MSG_CHECKING([whether compiler supports -fstack-protector])
141	CFLAGS="$OLDCFLAGS -fstack-protector"
142	AC_LINK_IFELSE([AC_LANG_PROGRAM()], [
143	    OPTIM="$OPTIM -fstack-protector"
144	    AC_MSG_RESULT([yes])
145	], [
146	    AC_MSG_RESULT([no])
147	])
148    ])
149    CFLAGS="$OLDCFLAGS"
150
151    AS_IF([test "x$LSB_BUILD" != xy], [
152	# The -fPIE option is available with some versions of GCC and
153	# adds randomization of addresses, which avoids another class of
154	# exploits that depend on a fixed address for common functions.
155	#
156	# Not available to LSB binaries...
157	AC_MSG_CHECKING([whether compiler supports -fPIE])
158	OLDCFLAGS="$CFLAGS"
159	AS_CASE(["$host_os_name"], [darwin*], [
160	    CFLAGS="$CFLAGS -fPIE -Wl,-pie"
161	    AC_LINK_IFELSE([AC_LANG_PROGRAM()], [
162		PIEFLAGS="-fPIE -Wl,-pie"
163		AC_MSG_RESULT([yes])
164	    ], [
165		AC_MSG_RESULT([no])
166	    ])
167	], [*], [
168	    CFLAGS="$CFLAGS -fPIE -pie"
169	    AC_COMPILE_IFELSE([AC_LANG_PROGRAM()], [
170		PIEFLAGS="-fPIE -pie"
171		AC_MSG_RESULT([yes])
172	    ], [
173		AC_MSG_RESULT([no])
174	    ])
175	])
176	CFLAGS="$OLDCFLAGS"
177    ])
178
179    dnl Show all standard warnings + unused variables when compiling...
180    WARNING_OPTIONS="-Wall -Wunused"
181
182    dnl Drop some not-useful/unreliable warnings...
183    for warning in char-subscripts deprecated-declarations format-truncation format-y2k switch unused-result; do
184	AC_MSG_CHECKING([whether compiler supports -Wno-$warning])
185
186	OLDCFLAGS="$CFLAGS"
187	CFLAGS="$CFLAGS -Wno-$warning -Werror"
188
189	AC_COMPILE_IFELSE([AC_LANG_PROGRAM()], [
190	    AC_MSG_RESULT(yes)
191	    WARNING_OPTIONS="$WARNING_OPTIONS -Wno-$warning"
192        ], [
193	    AC_MSG_RESULT(no)
194	])
195
196	CFLAGS="$OLDCFLAGS"
197    done
198
199    dnl Maintainer mode enables -Werror...
200    AS_IF([test x$enable_maintainer = xyes], [
201	WARNING_OPTIONS="$WARNING_OPTIONS -Werror"
202    ])
203], [
204    # Add vendor-specific compiler options...
205    AS_CASE([$host_os_name], [sunos* | solaris*], [
206	# Solaris
207	AS_IF([test -z "$OPTIM"], [
208	    OPTIM="-xO2"
209	])
210
211	AS_IF([test $PICFLAG = 1], [
212	    OPTIM="-KPIC $OPTIM"
213	])
214    ], [*], [
215	# Running some other operating system; inform the user
216	# they should contribute the necessary options via
217	# GitHub...
218	echo "Building CUPS with default compiler optimizations."
219	echo "Contact the OpenPrinting CUPS developers on GitHub with the uname and compiler"
220	echo "options needed for your platform, or set the CFLAGS and LDFLAGS environment"
221	echo "variables before running configure."
222	echo ""
223	echo "https://github.com/openprinting/cups"
224    ])
225])
226
227# Add general compiler options per platform...
228AS_CASE([$host_os_name], [linux*], [
229    # glibc 2.8 and higher breaks peer credentials unless you
230    # define _GNU_SOURCE...
231    OPTIM="$OPTIM -D_GNU_SOURCE"
232
233    # The -z relro option is provided by the Linux linker command to
234    # make relocatable data read-only.
235    AS_IF([test x$enable_relro = xyes], [
236	RELROFLAGS="-Wl,-z,relro,-z,now"
237    ])
238])
239