+# Detect C and LD warning flags supported by the compiler.
+
+# Detect warning flags supported by the C and C++ compilers and append them to
+# WARN_CFLAGS and WARN_CXXFLAGS.
+#
+m4_define([WARN_FLAGS_LIST], [ dnl
+ -Wall dnl
+ -Wextra dnl
+ -Wmissing-prototypes dnl
+ -Wmissing-declarations dnl
+ -Wnull-dereference dnl
+ -Wundef dnl
+ -Wredundant-decls dnl
+ -Wshadow dnl
+ dnl GCC's -Wshadow warns about constructor parameters shadowing fields, but
+ dnl Clang's does not. Enable Clang's `-Wshadow-field-in-constructor`, to make
+ dnl Clang warn about that.
+ -Wshadow-field-in-constructor dnl
+ -Wshadow-field dnl
+ -Wjump-misses-init dnl
+ -Wsuggest-attribute=format dnl
+ -Wtautological-constant-out-of-range-compare dnl
+ -Wnested-externs dnl
+ -Wwrite-strings dnl
+ -Wformat=2 dnl
+ -Wstrict-aliasing dnl
+ -Wmissing-noreturn dnl
+ -Winit-self dnl
+ -Wduplicated-cond dnl
+ -Wduplicated-branches dnl
+ -Wlogical-op dnl
+ -Wsuggest-override dnl
+ -Wno-unused-parameter dnl
+ -Wno-sign-compare dnl
+ dnl
+ dnl Some versions of SWIG (like 3.0.12) generate code that produces
+ dnl -Wcast-function-type warnings. This warning is present in gcc >= 8. This
+ dnl combo happens on RHEL/Centos 8, for example. Later versions of SWIG (like
+ dnl 4.0.1) have the correct function signatures to not produce this warning.
+ dnl It's simpler to just disable the warning globally.
+ dnl
+ dnl Note that the Debian/Ubuntu SWIG package 3.0.12-2 contains a local patch to
+ dnl fix this (python-fix-function-cast-warnings.patch), so you won't be able to
+ dnl reproduce the warning using that package.
+ dnl
+ dnl Ref: https://github.com/swig/swig/issues/1259
+ -Wno-cast-function-type dnl
+ -Wno-missing-field-initializers dnl
+ -Wno-maybe-uninitialized dnl
+])
+
+# Pass -Werror as an extra flag during the test: this is needed to make the
+# -Wunknown-warning-option diagnostic fatal with clang.
+AC_LANG([C++])
+AX_APPEND_COMPILE_FLAGS([WARN_FLAGS_LIST], [WARN_CXXFLAGS], [-Werror])
+AC_LANG([C])
+AX_APPEND_COMPILE_FLAGS([WARN_FLAGS_LIST], [WARN_CFLAGS], [-Werror])
+
+AE_IF_FEATURE_ENABLED([Werror], [WARN_CXXFLAGS="${WARN_CXXFLAGS} -Werror"])
+AE_IF_FEATURE_ENABLED([Werror], [WARN_CFLAGS="${WARN_CFLAGS} -Werror"])
+
+# The test used in AX_APPEND_COMPILE_FLAGS, generated using AC_LANG_PROGRAM, is
+# written in such a way that it triggers warnings with the following warning
+# flags. So they would always end up disabled if we put them there, because
+# the test program would not build.
+#
+# Enable them here unconditionally. They are supported by GCC >= 4.8 and by
+# Clang >= 3.3 (required by the project) and are only valid for C code.
+WARN_CFLAGS="${WARN_CFLAGS} -Wold-style-definition -Wstrict-prototypes"
+
+# CFLAGS from AX_APPEND_COMPILE_FLAGS.
+AM_CXXFLAGS="${AM_CXXFLAGS} ${WARN_CXXFLAGS}"
+AM_CFLAGS="${AM_CFLAGS} ${WARN_CFLAGS}"
+
+# Done for AM_CXXFLAGS, AM_CFLAGS and AM_LDFLAGS.
+AC_SUBST(AM_CXXFLAGS)
+AC_SUBST(AM_CFLAGS)
+AC_SUBST(AM_LDFLAGS)
+
+# Set global CPPFLAGS in AM_CPPFLAGS
+AM_CPPFLAGS="-I\$(top_srcdir)/include -I\$(top_builddir)/src -I\$(top_srcdir)/src -include common/config.h"
+AC_SUBST(AM_CPPFLAGS)
+
+# Add glib to global link libs
+LIBS="$LIBS $GLIB_LIBS"
+