1dnl
2dnl Compiler stuff for CUPS.
3dnl
4dnl Copyright 2007-2018 by Apple Inc.
5dnl Copyright 1997-2007 by Easy Software Products, all rights reserved.
6dnl
7dnl Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
8dnl
9
10dnl Clear the debugging and non-shared library options unless the user asks
11dnl for them...
12INSTALL_STRIP=""
13AC_SUBST(INSTALL_STRIP)
14
15AC_ARG_WITH(optim, [  --with-optim            set optimization flags ],
16	OPTIM="$withval",
17	OPTIM="")
18AC_SUBST(OPTIM)
19
20AC_ARG_ENABLE(debug, [  --enable-debug          build with debugging symbols])
21AC_ARG_ENABLE(debug_guards, [  --enable-debug-guards   build with memory allocation guards])
22AC_ARG_ENABLE(debug_printfs, [  --enable-debug-printfs  build with CUPS_DEBUG_LOG support])
23AC_ARG_ENABLE(unit_tests, [  --enable-unit-tests     build and run unit tests])
24
25dnl For debugging, keep symbols, otherwise strip them...
26if test x$enable_debug = xyes -a "x$OPTIM" = x; then
27	OPTIM="-g"
28else
29	INSTALL_STRIP="-s"
30fi
31
32dnl Debug printfs can slow things down, so provide a separate option for that
33if test x$enable_debug_printfs = xyes; then
34	CFLAGS="$CFLAGS -DDEBUG"
35	CXXFLAGS="$CXXFLAGS -DDEBUG"
36fi
37
38dnl Debug guards use an extra 4 bytes for some structures like strings in the
39dnl string pool, so provide a separate option for that
40if test x$enable_debug_guards = xyes; then
41	CFLAGS="$CFLAGS -DDEBUG_GUARDS"
42	CXXFLAGS="$CXXFLAGS -DDEBUG_GUARDS"
43fi
44
45dnl Unit tests take up time during a compile...
46if test x$enable_unit_tests = xyes; then
47        if test "$build" != "$host"; then
48                AC_MSG_ERROR([Sorry, cannot build unit tests when cross-compiling.])
49        fi
50
51	UNITTESTS="unittests"
52else
53	UNITTESTS=""
54fi
55AC_SUBST(UNITTESTS)
56
57dnl Setup general architecture flags...
58AC_ARG_WITH(archflags, [  --with-archflags        set default architecture flags ])
59AC_ARG_WITH(ldarchflags, [  --with-ldarchflags      set program architecture flags ])
60
61if test -z "$with_archflags"; then
62	ARCHFLAGS=""
63else
64	ARCHFLAGS="$with_archflags"
65fi
66
67if test -z "$with_ldarchflags"; then
68	if test "$host_os_name" = darwin; then
69		# Only create Intel programs by default
70		LDARCHFLAGS="`echo $ARCHFLAGS | sed -e '1,$s/-arch ppc64//'`"
71	else
72		LDARCHFLAGS="$ARCHFLAGS"
73	fi
74else
75	LDARCHFLAGS="$with_ldarchflags"
76fi
77
78AC_SUBST(ARCHFLAGS)
79AC_SUBST(LDARCHFLAGS)
80
81dnl Read-only data/program support on Linux...
82AC_ARG_ENABLE(relro, [  --enable-relro          build with the GCC relro option])
83
84dnl Clang/GCC address sanitizer...
85AC_ARG_ENABLE(sanitizer, [  --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
100if test -n "$GCC"; then
101	# Add GCC-specific compiler options...
102
103        # Address sanitizer is a useful tool to use when developing/debugging
104        # code but adds about 2x overhead...
105	if test x$enable_sanitizer = xyes; then
106		# Use -fsanitize=address with debugging...
107		OPTIM="$OPTIM -g -fsanitize=address"
108	else
109		# Otherwise use the Fortify enhancements to catch any unbounded
110		# string operations...
111		CFLAGS="$CFLAGS -D_FORTIFY_SOURCE=2"
112		CXXFLAGS="$CXXFLAGS -D_FORTIFY_SOURCE=2"
113	fi
114
115	# Default optimization options...
116	if test -z "$OPTIM"; then
117		# Default to optimize-for-size and debug
118		OPTIM="-Os -g"
119	fi
120
121	# Generate position-independent code as needed...
122	if test $PICFLAG = 1; then
123    		OPTIM="-fPIC $OPTIM"
124	fi
125
126	# The -fstack-protector option is available with some versions of
127	# GCC and adds "stack canaries" which detect when the return address
128	# has been overwritten, preventing many types of exploit attacks.
129	AC_MSG_CHECKING(whether compiler supports -fstack-protector)
130	OLDCFLAGS="$CFLAGS"
131	CFLAGS="$CFLAGS -fstack-protector"
132	AC_TRY_LINK(,,
133		if test "x$LSB_BUILD" = xy; then
134			# Can't use stack-protector with LSB binaries...
135			OPTIM="$OPTIM -fno-stack-protector"
136		else
137			OPTIM="$OPTIM -fstack-protector"
138		fi
139		AC_MSG_RESULT(yes),
140		AC_MSG_RESULT(no))
141	CFLAGS="$OLDCFLAGS"
142
143	if test "x$LSB_BUILD" != xy; then
144		# The -fPIE option is available with some versions of GCC and
145		# adds randomization of addresses, which avoids another class of
146		# exploits that depend on a fixed address for common functions.
147		#
148		# Not available to LSB binaries...
149		AC_MSG_CHECKING(whether compiler supports -fPIE)
150		OLDCFLAGS="$CFLAGS"
151		case "$host_os_name" in
152			darwin*)
153				CFLAGS="$CFLAGS -fPIE -Wl,-pie"
154				AC_TRY_COMPILE(,,[
155					PIEFLAGS="-fPIE -Wl,-pie"
156					AC_MSG_RESULT(yes)],
157					AC_MSG_RESULT(no))
158				;;
159
160			*)
161				CFLAGS="$CFLAGS -fPIE -pie"
162				AC_TRY_COMPILE(,,[
163					PIEFLAGS="-fPIE -pie"
164					AC_MSG_RESULT(yes)],
165					AC_MSG_RESULT(no))
166				;;
167		esac
168		CFLAGS="$OLDCFLAGS"
169	fi
170
171	# Add useful warning options for tracking down problems...
172	WARNING_OPTIONS="-Wall -Wno-format-y2k -Wunused -Wno-unused-result -Wsign-conversion"
173
174	# Test GCC version for certain warning flags since -Werror
175	# doesn't trigger...
176	gccversion=`$CC --version | head -1 | awk '{print $NF}'`
177	case "$gccversion" in
178		7.* | 8.*)
179			WARNING_OPTIONS="$WARNING_OPTIONS -Wno-format-truncation -Wno-tautological-compare"
180			;;
181	esac
182
183	# Additional warning options for development testing...
184	if test -d .git; then
185		WARNING_OPTIONS="-Werror -Wno-error=deprecated-declarations $WARNING_OPTIONS"
186	fi
187else
188	# Add vendor-specific compiler options...
189	case $host_os_name in
190		sunos*)
191			# Solaris
192			if test -z "$OPTIM"; then
193				OPTIM="-xO2"
194			fi
195
196			if test $PICFLAG = 1; then
197				OPTIM="-KPIC $OPTIM"
198			fi
199			;;
200		*)
201			# Running some other operating system; inform the user
202			# they should contribute the necessary options via
203			# Github...
204			echo "Building CUPS with default compiler optimizations; contact the CUPS developers on Github"
205			echo "(https://github.com/apple/cups/issues) with the uname and compiler options needed for"
206			echo "your platform, or set the CFLAGS and LDFLAGS environment variables before running"
207			echo "configure."
208			;;
209	esac
210fi
211
212# Add general compiler options per platform...
213case $host_os_name in
214	linux*)
215		# glibc 2.8 and higher breaks peer credentials unless you
216		# define _GNU_SOURCE...
217		OPTIM="$OPTIM -D_GNU_SOURCE"
218
219		# The -z relro option is provided by the Linux linker command to
220		# make relocatable data read-only.
221		if test x$enable_relro = xyes; then
222			RELROFLAGS="-Wl,-z,relro,-z,now"
223		fi
224		;;
225esac
226