compiler_options.m4 5.55 KB
Newer Older
1
AC_DEFUN([_NM_COMPILER_FLAG], [
2
	CFLAGS_SAVED="$CFLAGS"
3
	CFLAGS="$CFLAGS $GLIB_CFLAGS -Werror $1"
4
	AC_MSG_CHECKING([whether $1 works as expected])
5 6 7 8

	AC_COMPILE_IFELSE([AC_LANG_SOURCE([[]])], [
		AC_COMPILE_IFELSE([AC_LANG_SOURCE([[$2]])], [
			AC_MSG_RESULT(yes)
9 10
			CFLAGS="$CFLAGS_SAVED"
			$3
11 12
		],[
			AC_MSG_RESULT(no)
13 14
			CFLAGS="$CFLAGS_SAVED"
			$4
15 16 17 18 19 20 21
		])
	],[
		AC_MSG_RESULT(not supported)
		CFLAGS="$CFLAGS_SAVED"
	])
])

22
dnl Check whether a particular compiler flag is supported,
23 24
dnl append it to the specified variable if the check succeeds.
dnl NM_COMPILER_FLAG([ENV-VAR], [FLAG], [ACTION-IF-SUPPORTED], [ACTION-IF-NOT-SUPPORTED])
25
AC_DEFUN([NM_COMPILER_FLAG], [
26 27 28 29
        _NM_COMPILER_FLAG([$2], [], [
		eval "AS_TR_SH([$1])='$$1 $2'"
		$3
	], [$4])
30 31 32
])

dnl Check whether a particular warning is not emitted with code provided,
33
dnl append an option to disable the warning to a specified variable if the check fails.
34
dnl NM_COMPILER_WARNING([ENV-VAR], [WARNING], [C-SNIPPET])
35
AC_DEFUN([NM_COMPILER_WARNING], [
36
        _NM_COMPILER_FLAG([-W$2], [$3], [eval "AS_TR_SH([$1])='$$1 -W$2'"], [eval "AS_TR_SH([$1])='$$1 -Wno-$2'"])
37 38
])

39
dnl NM_COMPILER_WARNINGS([ENV-VAR], [MORE-WARNINGS])
40 41
AC_DEFUN([NM_COMPILER_WARNINGS],
[AC_ARG_ENABLE(more-warnings,
42
	AS_HELP_STRING([--enable-more-warnings], [Possible values: no/yes/error]),
43
	set_more_warnings="$enableval",set_more_warnings=$2)
44
AC_MSG_CHECKING(for more warnings)
45 46
if test "$GCC" = "yes" -a "$set_more_warnings" != "no"; then
	AC_MSG_RESULT(yes)
47 48 49

	dnl This is enabled in clang by default, makes little sense,
	dnl and causes the build to abort with -Werror.
50 51 52 53
	CFLAGS_SAVED="$CFLAGS"
	CFLAGS="$CFLAGS -Qunused-arguments"
	AC_COMPILE_IFELSE([AC_LANG_SOURCE([])], eval "AS_TR_SH([$1])='$$1 -Qunused-arguments'", [])
	CFLAGS="$CFLAGS_SAVED"
54

55 56 57 58
	dnl clang only warns about unknown warnings, unless
	dnl called with "-Werror=unknown-warning-option"
	dnl Test if the compiler supports that, and if it does
	dnl attach it to the CFLAGS.
59
	NM_COMPILER_WARNING([$1], [unknown-warning-option], [])
60

61
	CFLAGS_MORE_WARNINGS="-Wall"
62 63 64

	if test "x$set_more_warnings" = xerror; then
		CFLAGS_MORE_WARNINGS="$CFLAGS_MORE_WARNINGS -Werror"
65 66
	fi

67
	for option in \
68
		      -Wextra \
69 70
		      -Wdeclaration-after-statement \
		      -Wfloat-equal \
71 72 73
		      -Wformat-nonliteral \
		      -Wformat-security \
		      -Wimplicit-fallthrough \
74 75
		      -Wimplicit-function-declaration \
		      -Winit-self \
76
		      -Wlogical-op \
77
		      -Wmissing-declarations \
78
		      -Wmissing-include-dirs \
79 80 81
		      -Wmissing-prototypes \
		      -Wpointer-arith \
		      -Wshadow \
82
		      -Wshift-negative-value \
83 84
		      -Wstrict-prototypes \
		      -Wundef \
85
		      -Wvla \
86
		      -Wno-duplicate-decl-specifier \
87
		      -Wno-format-truncation \
88 89 90 91 92
		      -Wno-format-y2k \
		      -Wno-missing-field-initializers \
		      -Wno-pragmas \
		      -Wno-sign-compare \
		      -Wno-unused-parameter \
93
		      ; do
94
		dnl GCC 4.4 does not warn when checking for -Wno-* flags (https://gcc.gnu.org/wiki/FAQ#wnowarning)
95
		_NM_COMPILER_FLAG([-Wall $(printf '%s' "$option" | sed 's/^-Wno-/-W/')], [],
96
		                  [CFLAGS_MORE_WARNINGS="$CFLAGS_MORE_WARNINGS $option"], [])
97 98
	done
	unset option
99 100 101 102

	dnl Disable warnings triggered by known compiler problems

	dnl https://bugzilla.gnome.org/show_bug.cgi?id=745821
103
	NM_COMPILER_WARNING([$1], [unknown-attributes], [#include <glib.h>])
104

105
	dnl https://bugzilla.gnome.org/show_bug.cgi?id=744473
106
	NM_COMPILER_WARNING([$1], [typedef-redefinition], [#include <gio/gio.h>])
107

108
	dnl https://llvm.org/bugs/show_bug.cgi?id=21614
109
	NM_COMPILER_WARNING([$1], [array-bounds],
110 111 112 113 114
		[#include <string.h>]
		[void f () { strcmp ("something", "0"); }]
	)

	dnl https://llvm.org/bugs/show_bug.cgi?id=22949
115
	NM_COMPILER_WARNING([$1], [parentheses-equality],
116 117 118 119 120 121
		[#include <sys/wait.h>]
		[void f () { if (WIFCONTINUED(0)) return; }]
	)

	dnl systemd-dhcp's log_internal macro and our handle_warn are sometimes
	dnl used in void context,u sometimes in int. Makes clang unhappy.
122
	NM_COMPILER_WARNING([$1], [unused-value],
123 124
		[#define yolo ({ (666 + 666); })]
		[int f () { int i = yolo; yolo; return i; }]
125 126
	)

127
	dnl a new warning in gcc 8, glib 2.55 doesn't play nice yet
128
	dnl https://bugzilla.gnome.org/show_bug.cgi?id=793272
129
	NM_COMPILER_WARNING([$1], [cast-function-type],
130 131 132 133 134 135 136 137
		[#include <glib-object.h>]
		[typedef struct { GObject parent; } NMObject;]
		[typedef struct { GObjectClass parent; } NMObjectClass;]
		[static void nm_object_init (NMObject *object) { } ]
		[static void nm_object_class_init (NMObjectClass *object) { }]
		[G_DEFINE_TYPE (NMObject, nm_object, G_TYPE_OBJECT)]
	)

138
	eval "AS_TR_SH([$1])='$CFLAGS_MORE_WARNINGS $$1'"
139 140 141 142
else
	AC_MSG_RESULT(no)
fi
])
143 144 145 146

AC_DEFUN([NM_LTO],
[AC_ARG_ENABLE(lto, AS_HELP_STRING([--enable-lto], [Enable Link Time Optimization for smaller size (default: no)]))
if (test "${enable_lto}" = "yes"); then
147
	CC_CHECK_FLAG_APPEND([lto_flags], [CFLAGS], [-flto -flto-partition=none])
148
	if (test -n "${lto_flags}"); then
149
		CFLAGS="-flto -flto-partition=none $CFLAGS"
150 151 152
	else
		AC_MSG_ERROR([Link Time Optimization -flto is not supported.])
	fi
153
else
154
	enable_lto='no'
155 156 157 158 159 160 161 162 163
fi
])

AC_DEFUN([NM_LD_GC],
[AC_ARG_ENABLE(ld-gc, AS_HELP_STRING([--enable-ld-gc], [Enable garbage collection of unused symbols on linking (default: auto)]))
if (test "${enable_ld_gc}" != "no"); then
	CC_CHECK_FLAG_APPEND([ld_gc_flags], [CFLAGS], [-fdata-sections -ffunction-sections -Wl,--gc-sections])
	if (test -n "${ld_gc_flags}"); then
		enable_ld_gc="yes"
164
		CFLAGS="$ld_gc_flags $CFLAGS"
165 166 167 168 169 170 171 172 173 174
	else
		if (test "${enable_ld_gc}" = "yes"); then
			AC_MSG_ERROR([Unused symbol eviction requested but not supported.])
		else
			enable_ld_gc="no"
		fi
	fi
fi
])