aboutsummaryrefslogtreecommitdiff
path: root/m4/nx-macros.m4
blob: 816bf40035ed542701114e5ea6b19f732a2d2f27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
dnl nx-macros.m4.  Generated from xorg-macros.m4.in xorgversion.m4 by configure.
dnl
dnl Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
dnl Copyright (c) 2017, Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
dnl
dnl Permission is hereby granted, free of charge, to any person obtaining a
dnl copy of this software and associated documentation files (the "Software"),
dnl to deal in the Software without restriction, including without limitation
dnl the rights to use, copy, modify, merge, publish, distribute, sublicense,
dnl and/or sell copies of the Software, and to permit persons to whom the
dnl Software is furnished to do so, subject to the following conditions:
dnl
dnl The above copyright notice and this permission notice (including the next
dnl paragraph) shall be included in all copies or substantial portions of the
dnl Software.
dnl
dnl THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dnl IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dnl FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
dnl THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dnl LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
dnl FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
dnl DEALINGS IN THE SOFTWARE.

# NX_COMPILER_BRAND
# -------------------
# Checks for various brands of compilers and sets flags as appropriate:
#   GNU gcc - relies on AC_PROG_CC (via AC_PROG_CC_C99) to set GCC to "yes"
#   GNU g++ - relies on AC_PROG_CXX to set GXX to "yes"
#   clang compiler - sets CLANGCC to "yes"
#   Intel compiler - sets INTELCC to "yes"
#   Sun/Oracle Solaris Studio cc - sets SUNCC to "yes"
#
# Derived from https://cgit.freedesktop.org/xorg/util/macros/ and adapted to
# nxcomp{,shad}.

AC_DEFUN([NX_COMPILER_BRAND], [
AC_LANG_CASE(
	[C], [
		AC_REQUIRE([AC_PROG_CC_C99])
	],
	[C++], [
		AC_REQUIRE([AC_PROG_CXX])
	]
)
AC_CHECK_DECL([__clang__], [CLANGCC="yes"], [CLANGCC="no"])
AC_CHECK_DECL([__INTEL_COMPILER], [INTELCC="yes"], [INTELCC="no"])
AC_CHECK_DECL([__SUNPRO_C], [SUNCC="yes"], [SUNCC="no"])
]) # NX_COMPILER_BRAND

# NX_TESTSET_CFLAG(<variable>, <flag>, [<alternative flag>, ...])
# ---------------
# Test if the compiler works when passed the given flag as a command line argument.
# If it succeeds, the flag is appeneded to the given variable.  If not, it tries the
# next flag in the list until there are no more options.
#
# Note that this does not guarantee that the compiler supports the flag as some
# compilers will simply ignore arguments that they do not understand, but we do
# attempt to weed out false positives by using -Werror=unknown-warning-option and
# -Werror=unused-command-line-argument
#
# Derived from https://cgit.freedesktop.org/xorg/util/macros/ and adapted to
# nxcomp{,shad}.

AC_DEFUN([NX_TESTSET_CFLAG], [
m4_if([$#], 0, [m4_fatal([NX_TESTSET_CFLAG was given with an unsupported number of arguments])])
m4_if([$#], 1, [m4_fatal([NX_TESTSET_CFLAG was given with an unsupported number of arguments])])

AC_LANG_COMPILER_REQUIRE

AC_LANG_CASE(
	[C], [
		AC_REQUIRE([AC_PROG_CC_C99])
		define([PREFIX], [C])
		define([CACHE_PREFIX], [cc])
		define([COMPILER], [$CC])
	],
	[C++], [
		define([PREFIX], [CXX])
		define([CACHE_PREFIX], [cxx])
		define([COMPILER], [$CXX])
	]
)

[nx_testset_save_]PREFIX[FLAGS]="$PREFIX[FLAGS]"

if test "x$[nx_testset_]CACHE_PREFIX[_unknown_warning_option]" = "x" ; then
	PREFIX[FLAGS]="$PREFIX[FLAGS] -Werror=unknown-warning-option"
	AC_CACHE_CHECK([if ]COMPILER[ supports -Werror=unknown-warning-option],
			[nx_cv_]CACHE_PREFIX[_flag_unknown_warning_option],
			AC_COMPILE_IFELSE([AC_LANG_SOURCE([int i;])],
					  [nx_cv_]CACHE_PREFIX[_flag_unknown_warning_option=yes],
					  [nx_cv_]CACHE_PREFIX[_flag_unknown_warning_option=no]))
	[nx_testset_]CACHE_PREFIX[_unknown_warning_option]=$[nx_cv_]CACHE_PREFIX[_flag_unknown_warning_option]
	PREFIX[FLAGS]="$[nx_testset_save_]PREFIX[FLAGS]"
fi

if test "x$[nx_testset_]CACHE_PREFIX[_unused_command_line_argument]" = "x" ; then
	if test "x$[nx_testset_]CACHE_PREFIX[_unknown_warning_option]" = "xyes" ; then
		PREFIX[FLAGS]="$PREFIX[FLAGS] -Werror=unknown-warning-option"
	fi
	PREFIX[FLAGS]="$PREFIX[FLAGS] -Werror=unused-command-line-argument"
	AC_CACHE_CHECK([if ]COMPILER[ supports -Werror=unused-command-line-argument],
			[nx_cv_]CACHE_PREFIX[_flag_unused_command_line_argument],
			AC_COMPILE_IFELSE([AC_LANG_SOURCE([int i;])],
					  [nx_cv_]CACHE_PREFIX[_flag_unused_command_line_argument=yes],
					  [nx_cv_]CACHE_PREFIX[_flag_unused_command_line_argument=no]))
	[nx_testset_]CACHE_PREFIX[_unused_command_line_argument]=$[nx_cv_]CACHE_PREFIX[_flag_unused_command_line_argument]
	PREFIX[FLAGS]="$[nx_testset_save_]PREFIX[FLAGS]"
fi

found="no"
m4_foreach([flag], m4_cdr($@), [
	if test $found = "no" ; then
		if test "x$nx_testset_]CACHE_PREFIX[_unknown_warning_option" = "xyes" ; then
			PREFIX[FLAGS]="$PREFIX[FLAGS] -Werror=unknown-warning-option"
		fi

		if test "x$nx_testset_]CACHE_PREFIX[_unused_command_line_argument" = "xyes" ; then
			PREFIX[FLAGS]="$PREFIX[FLAGS] -Werror=unused-command-line-argument"
		fi

		PREFIX[FLAGS]="$PREFIX[FLAGS] ]flag["

dnl Some hackery here since AC_CACHE_VAL can't handle a non-literal varname
		AC_MSG_CHECKING([if ]COMPILER[ supports ]flag[])
		cacheid=AS_TR_SH([nx_cv_]CACHE_PREFIX[_flag_]flag[])
		AC_CACHE_VAL($cacheid,
			     [AC_LINK_IFELSE([AC_LANG_PROGRAM([int i;])],
					     [eval $cacheid=yes],
					     [eval $cacheid=no])])

		PREFIX[FLAGS]="$[nx_testset_save_]PREFIX[FLAGS]"

		eval supported=\$$cacheid
		AC_MSG_RESULT([$supported])
		if test "$supported" = "yes" ; then
			$1="$$1 ]flag["
			found="yes"
		fi
	fi
])
]) # NX_TESTSET_CFLAG

# NX_COMPILER_FLAGS
# ---------------
# Defines BASE_CFLAGS or BASE_CXXFLAGS to contain a set of command line
# arguments supported by the selected compiler which do NOT alter the generated
# code.  These arguments will cause the compiler to print various warnings
# during compilation AND turn a conservative set of warnings into errors.
#
# The set of flags supported by BASE_CFLAGS and BASE_CXXFLAGS will grow in
# future versions of util-macros as options are added to new compilers.
#
# Derived from https://cgit.freedesktop.org/xorg/util/macros/ and adapted to
# nxcomp{,shad}.

AC_DEFUN([NX_COMPILER_FLAGS], [
AC_REQUIRE([NX_COMPILER_BRAND])

AC_ARG_ENABLE(selective-werror,
              AS_HELP_STRING([--disable-selective-werror],
                             [Turn off selective compiler errors. (default: enabled)]),
              [SELECTIVE_WERROR=$enableval],
              [SELECTIVE_WERROR=yes])

AC_LANG_CASE(
        [C], [
                define([PREFIX], [C])
        ],
        [C++], [
                define([PREFIX], [CXX])
        ]
)
# -v is too short to test reliably with NX_TESTSET_CFLAG
if test "x$SUNCC" = "xyes"; then
    [BASE_]PREFIX[FLAGS]="-v"
else
    [BASE_]PREFIX[FLAGS]=""
fi

# This chunk of warnings were those that existed in the legacy CWARNFLAGS
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wall])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wpointer-arith])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wmissing-declarations])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wformat=2], [-Wformat])

AC_LANG_CASE(
	[C], [
		NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wstrict-prototypes])
		NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wmissing-prototypes])
		NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wnested-externs])
		NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wbad-function-cast])
		NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wold-style-definition], [-fd])
		NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wdeclaration-after-statement])
	]
)

# This chunk adds additional warnings that could catch undesired effects.
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wunused])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wuninitialized])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wshadow])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wmissing-noreturn])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wmissing-format-attribute])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wredundant-decls])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wlogical-op])

# These are currently disabled because they are noisy.  They will be enabled
# in the future once the codebase is sufficiently modernized to silence
# them.  For now, I don't want them to drown out the other warnings.
# NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wparentheses])
# NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wcast-align])
# NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wcast-qual])

# Turn some warnings into errors, so we don't accidently get successful builds
# when there are problems that should be fixed.

if test "x$SELECTIVE_WERROR" = "xyes" ; then
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Werror=implicit], [-errwarn=E_NO_EXPLICIT_TYPE_GIVEN -errwarn=E_NO_IMPLICIT_DECL_ALLOWED])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Werror=nonnull])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Werror=init-self])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Werror=main])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Werror=missing-braces])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Werror=sequence-point])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Werror=return-type], [-errwarn=E_FUNC_HAS_NO_RETURN_STMT])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Werror=trigraphs])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Werror=array-bounds])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Werror=write-strings])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Werror=address])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Werror=int-to-pointer-cast], [-errwarn=E_BAD_PTR_INT_COMBINATION])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Werror=pointer-to-int-cast]) # Also -errwarn=E_BAD_PTR_INT_COMBINATION
else
AC_MSG_WARN([You have chosen not to turn some select compiler warnings into errors.  This should not be necessary.  Please report why you needed to do so in a bug report at $PACKAGE_BUGREPORT])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wimplicit])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wnonnull])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Winit-self])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wmain])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wmissing-braces])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wsequence-point])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wreturn-type])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wtrigraphs])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Warray-bounds])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wwrite-strings])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Waddress])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wint-to-pointer-cast])
NX_TESTSET_CFLAG([[BASE_]PREFIX[FLAGS]], [-Wpointer-to-int-cast])
fi

AC_SUBST([BASE_]PREFIX[FLAGS])
]) # NX_COMPILER_FLAGS

dnl Check to see if we're running under Cygwin32.

AC_DEFUN([NX_BUILD_ON_CYGWIN32],
[AC_CACHE_CHECK([for Cygwin32 environment], nxconf_cv_cygwin32,
[AC_TRY_COMPILE(,[return __CYGWIN32__;],
nxconf_cv_cygwin32=yes, nxconf_cv_cygwin32=no)
rm -f conftest*])
CYGWIN32=
test "$nxconf_cv_cygwin32" = yes && CYGWIN32=yes
]) # NX_BUILD_ON_CYGWIN32

dnl Check whether we're building on a AMD64.

AC_DEFUN([NX_BUILD_ON_AMD64],
[AC_CACHE_CHECK([for Amd64 environment], nxconf_cv_amd64,
[AC_TRY_COMPILE(,[return (__amd64__ || __x86_64__);],
nxconf_cv_amd64=yes, nxconf_cv_amd64=no)
rm -f conftest*])
AMD64=
test "$nxconf_cv_amd64" = yes && AMD64=yes
]) # NX_BUILD_ON_AMD64

dnl Check for Darwin environment.

AC_DEFUN([NX_BUILD_ON_DARWIN],
[AC_CACHE_CHECK([for Darwin environment], nxconf_cv_darwin,
[AC_TRY_COMPILE(,[return __APPLE__;],
nxconf_cv_darwin=yes, nxconf_cv_darwin=no)
rm -f conftest*])
DARWIN=
test "$nxconf_cv_darwin" = yes && DARWIN=yes
]) # NX_BUILD_ON_DARWIN

# Check to see if we're running under Solaris.

AC_DEFUN([NX_BUILD_ON_SUN],
[AC_CACHE_CHECK([for Solaris environment], nxconf_cv_sun,
[AC_TRY_COMPILE(,[return __sun;],
nxconf_cv_sun=yes, nxconf_cv_sun=no)
rm -f conftest*])
SUN=
test "$nxconf_cv_sun" = yes && SUN=yes
]) # NX_BUILD_ON_SUN

# Check to see if we're running under FreeBSD.

AC_DEFUN([NX_BUILD_ON_FreeBSD],
[AC_CACHE_CHECK([for FreeBSD environment], nxconf_cv_freebsd,
[AC_TRY_COMPILE(,[return __FreeBSD__;],
nxconf_cv_freebsd=yes, nxconf_cv_freebsd=no)
rm -f conftest*])
FreeBSD=
test "$nxconf_cv_freebsd" = yes && FreeBSD=yes
]) # NX_BUILD_ON_FreeBSD