Blame m4/compiler_options.m4

Packit Service d328f3
AC_DEFUN([_NM_COMPILER_FLAG], [
Packit Service d328f3
	CFLAGS_SAVED="$CFLAGS"
Packit Service d328f3
	CFLAGS="$CFLAGS $GLIB_CFLAGS -Werror $1"
Packit Service d328f3
	AC_MSG_CHECKING([whether $1 works as expected])
Packit Service d328f3
Packit Service d328f3
	AC_COMPILE_IFELSE([AC_LANG_SOURCE([[]])], [
Packit Service d328f3
		AC_COMPILE_IFELSE([AC_LANG_SOURCE([[$2]])], [
Packit Service d328f3
			AC_MSG_RESULT(yes)
Packit Service d328f3
			CFLAGS="$CFLAGS_SAVED"
Packit Service d328f3
			$3
Packit Service d328f3
		],[
Packit Service d328f3
			AC_MSG_RESULT(no)
Packit Service d328f3
			CFLAGS="$CFLAGS_SAVED"
Packit Service d328f3
			$4
Packit Service d328f3
		])
Packit Service d328f3
	],[
Packit Service d328f3
		AC_MSG_RESULT(not supported)
Packit Service d328f3
		CFLAGS="$CFLAGS_SAVED"
Packit Service d328f3
	])
Packit Service d328f3
])
Packit Service d328f3
Packit Service d328f3
dnl Check whether a particular compiler flag is supported,
Packit Service d328f3
dnl append it to the specified variable if the check succeeds.
Packit Service d328f3
dnl NM_COMPILER_FLAG([ENV-VAR], [FLAG], [ACTION-IF-SUPPORTED], [ACTION-IF-NOT-SUPPORTED])
Packit Service d328f3
AC_DEFUN([NM_COMPILER_FLAG], [
Packit Service d328f3
        _NM_COMPILER_FLAG([$2], [], [
Packit Service d328f3
		eval "AS_TR_SH([$1])='$$1 $2'"
Packit Service d328f3
		$3
Packit Service d328f3
	], [$4])
Packit Service d328f3
])
Packit Service d328f3
Packit Service d328f3
dnl Check whether a particular warning is not emitted with code provided,
Packit Service d328f3
dnl append an option to disable the warning to a specified variable if the check fails.
Packit Service d328f3
dnl NM_COMPILER_WARNING([ENV-VAR], [C-SNIPPET], [WARNING]])
Packit Service d328f3
AC_DEFUN([NM_COMPILER_WARNING], [
Packit Service d328f3
        _NM_COMPILER_FLAG([-W$2], [$3], [eval "AS_TR_SH([$1])='$$1 -W$2'"], [eval "AS_TR_SH([$1])='$$1 -Wno-$2'"])
Packit Service d328f3
])
Packit Service d328f3
Packit Service d328f3
dnl NM_COMPILER_WARNINGS([ENV-VAR], [MORE-WARNINGS])
Packit Service d328f3
AC_DEFUN([NM_COMPILER_WARNINGS],
Packit Service d328f3
[AC_ARG_ENABLE(more-warnings,
Packit Service d328f3
	AS_HELP_STRING([--enable-more-warnings], [Possible values: no/yes/error]),
Packit Service d328f3
	set_more_warnings="$enableval",set_more_warnings=$2)
Packit Service d328f3
AC_MSG_CHECKING(for more warnings)
Packit Service d328f3
if test "$GCC" = "yes" -a "$set_more_warnings" != "no"; then
Packit Service d328f3
	AC_MSG_RESULT(yes)
Packit Service d328f3
Packit Service d328f3
	dnl This is enabled in clang by default, makes little sense,
Packit Service d328f3
	dnl and causes the build to abort with -Werror.
Packit Service d328f3
	CFLAGS_SAVED="$$1"
Packit Service d328f3
	eval "AS_TR_SH([$1])='$$1 -Qunused-arguments'"
Packit Service d328f3
	AC_COMPILE_IFELSE([AC_LANG_SOURCE([])], [], eval "AS_TR_SH([$1])='$CFLAGS_SAVED'")
Packit Service d328f3
	unset CFLAGS_SAVED
Packit Service d328f3
Packit Service d328f3
	dnl clang only warns about unknown warnings, unless
Packit Service d328f3
	dnl called with "-Werror=unknown-warning-option"
Packit Service d328f3
	dnl Test if the compiler supports that, and if it does
Packit Service d328f3
	dnl attach it to the CFLAGS.
Packit Service d328f3
	NM_COMPILER_WARNING([$1], [unknown-warning-option], [])
Packit Service d328f3
Packit Service d328f3
	CFLAGS_MORE_WARNINGS="-Wall"
Packit Service d328f3
Packit Service d328f3
	if test "x$set_more_warnings" = xerror; then
Packit Service d328f3
		CFLAGS_MORE_WARNINGS="$CFLAGS_MORE_WARNINGS -Werror"
Packit Service d328f3
	fi
Packit Service d328f3
Packit Service d328f3
	for option in \
Packit Service d328f3
		      -Wextra \
Packit Service d328f3
		      -Wdeclaration-after-statement \
Packit Service d328f3
		      -Wfloat-equal \
Packit Service d328f3
		      -Wformat-nonliteral \
Packit Service d328f3
		      -Wformat-security \
Packit Service d328f3
		      -Wimplicit-fallthrough \
Packit Service d328f3
		      -Wimplicit-function-declaration \
Packit Service d328f3
		      -Winit-self \
Packit Service d328f3
		      -Wlogical-op \
Packit Service d328f3
		      -Wmissing-declarations \
Packit Service d328f3
		      -Wmissing-include-dirs \
Packit Service d328f3
		      -Wmissing-prototypes \
Packit Service d328f3
		      -Wpointer-arith \
Packit Service d328f3
		      -Wshadow \
Packit Service d328f3
		      -Wshift-negative-value \
Packit Service d328f3
		      -Wstrict-prototypes \
Packit Service d328f3
		      -Wundef \
Packit Service d328f3
		      -Wvla \
Packit Service d328f3
		      -Wno-duplicate-decl-specifier \
Packit Service d328f3
		      -Wno-format-truncation \
Packit Service d328f3
		      -Wno-format-y2k \
Packit Service d328f3
		      -Wno-missing-field-initializers \
Packit Service d328f3
		      -Wno-pragmas \
Packit Service d328f3
		      -Wno-sign-compare \
Packit Service d328f3
		      -Wno-unused-parameter \
Packit Service d328f3
		      ; do
Packit Service d328f3
		dnl GCC 4.4 does not warn when checking for -Wno-* flags (https://gcc.gnu.org/wiki/FAQ#wnowarning)
Packit Service d328f3
		_NM_COMPILER_FLAG([-Wall $(printf '%s' "$option" | sed 's/^-Wno-/-W/')], [],
Packit Service d328f3
		                  [CFLAGS_MORE_WARNINGS="$CFLAGS_MORE_WARNINGS $option"], [])
Packit Service d328f3
	done
Packit Service d328f3
	unset option
Packit Service d328f3
Packit Service d328f3
	dnl Disable warnings triggered by known compiler problems
Packit Service d328f3
Packit Service d328f3
	dnl https://bugzilla.gnome.org/show_bug.cgi?id=745821
Packit Service d328f3
	NM_COMPILER_WARNING([$1], [unknown-attributes], [#include <glib.h>])
Packit Service d328f3
Packit Service d328f3
	dnl https://bugzilla.gnome.org/show_bug.cgi?id=744473
Packit Service d328f3
	NM_COMPILER_WARNING([$1], [typedef-redefinition], [#include <gio/gio.h>])
Packit Service d328f3
Packit Service d328f3
	dnl https://llvm.org/bugs/show_bug.cgi?id=21614
Packit Service d328f3
	NM_COMPILER_WARNING([$1], [array-bounds],
Packit Service d328f3
		[#include <string.h>]
Packit Service d328f3
		[void f () { strcmp ("something", "0"); }]
Packit Service d328f3
	)
Packit Service d328f3
Packit Service d328f3
	dnl https://llvm.org/bugs/show_bug.cgi?id=22949
Packit Service d328f3
	NM_COMPILER_WARNING([$1], [parentheses-equality],
Packit Service d328f3
		[#include <sys/wait.h>]
Packit Service d328f3
		[void f () { if (WIFCONTINUED(0)) return; }]
Packit Service d328f3
	)
Packit Service d328f3
Packit Service d328f3
	dnl systemd-dhcp's log_internal macro and our handle_warn are sometimes
Packit Service d328f3
	dnl used in void context,u sometimes in int. Makes clang unhappy.
Packit Service d328f3
	NM_COMPILER_WARNING([$1], [unused-value],
Packit Service d328f3
		[#define yolo ({ (666 + 666); })]
Packit Service d328f3
		[int f () { int i = yolo; yolo; return i; }]
Packit Service d328f3
	)
Packit Service d328f3
Packit Service d328f3
	dnl clang 3.9 would like to see "{ { 0 } }" here, but that does not
Packit Service d328f3
	dnl look too wise.
Packit Service d328f3
	NM_COMPILER_WARNING([$1], [missing-braces],
Packit Service d328f3
		[union { int a[1]; int b[2]; } c = { 0 }]
Packit Service d328f3
	)
Packit Service d328f3
Packit Service d328f3
	dnl a new warning in gcc 8, glib 2.55 doesn't play nice yet
Packit Service d328f3
	dnl https://bugzilla.gnome.org/show_bug.cgi?id=793272
Packit Service d328f3
	NM_COMPILER_WARNING([$1], [cast-function-type],
Packit Service d328f3
		[#include <glib-object.h>]
Packit Service d328f3
		[typedef struct { GObject parent; } NMObject;]
Packit Service d328f3
		[typedef struct { GObjectClass parent; } NMObjectClass;]
Packit Service d328f3
		[static void nm_object_init (NMObject *object) { } ]
Packit Service d328f3
		[static void nm_object_class_init (NMObjectClass *object) { }]
Packit Service d328f3
		[G_DEFINE_TYPE (NMObject, nm_object, G_TYPE_OBJECT)]
Packit Service d328f3
	)
Packit Service d328f3
Packit Service d328f3
	eval "AS_TR_SH([$1])='$CFLAGS_MORE_WARNINGS $$1'"
Packit Service d328f3
else
Packit Service d328f3
	AC_MSG_RESULT(no)
Packit Service d328f3
fi
Packit Service d328f3
])
Packit Service d328f3
Packit Service d328f3
AC_DEFUN([NM_LTO],
Packit Service d328f3
[AC_ARG_ENABLE(lto, AS_HELP_STRING([--enable-lto], [Enable Link Time Optimization for smaller size (default: no)]))
Packit Service d328f3
if (test "${enable_lto}" = "yes"); then
Packit Service d328f3
	CC_CHECK_FLAG_APPEND([lto_flags], [CFLAGS], [-flto])
Packit Service d328f3
	if (test -n "${lto_flags}"); then
Packit Service d328f3
		CFLAGS="-flto $CFLAGS"
Packit Service d328f3
	else
Packit Service d328f3
		AC_MSG_ERROR([Link Time Optimization -flto is not supported.])
Packit Service d328f3
	fi
Packit Service d328f3
else
Packit Service d328f3
	enable_lto='no'
Packit Service d328f3
fi
Packit Service d328f3
])
Packit Service d328f3
Packit Service d328f3
AC_DEFUN([NM_LD_GC],
Packit Service d328f3
[AC_ARG_ENABLE(ld-gc, AS_HELP_STRING([--enable-ld-gc], [Enable garbage collection of unused symbols on linking (default: auto)]))
Packit Service d328f3
if (test "${enable_ld_gc}" != "no"); then
Packit Service d328f3
	CC_CHECK_FLAG_APPEND([ld_gc_flags], [CFLAGS], [-fdata-sections -ffunction-sections -Wl,--gc-sections])
Packit Service d328f3
	if (test -n "${ld_gc_flags}"); then
Packit Service d328f3
		enable_ld_gc="yes"
Packit Service d328f3
		CFLAGS="$ld_gc_flags $CFLAGS"
Packit Service d328f3
	else
Packit Service d328f3
		if (test "${enable_ld_gc}" = "yes"); then
Packit Service d328f3
			AC_MSG_ERROR([Unused symbol eviction requested but not supported.])
Packit Service d328f3
		else
Packit Service d328f3
			enable_ld_gc="no"
Packit Service d328f3
		fi
Packit Service d328f3
	fi
Packit Service d328f3
fi
Packit Service d328f3
])
Packit Service d328f3