build: Add compiler warnings support Detect as many warnings as possible during configure and enable them if the user did not supply any, so that any such problem can be spotted and fixed.
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
diff --git a/configure.ac b/configure.ac
index 2c22c1f..11db6c7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -18,6 +18,9 @@ LIBMD_ABI_PATCH=4
LIBMD_ABI="$LIBMD_ABI_MAJOR:$LIBMD_ABI_MINOR:$LIBMD_ABI_PATCH"
AC_SUBST([LIBMD_ABI])
+# Check and store if we got user supplied variables.
+user_CFLAGS=${CFLAGS-unset}
+
# Checks for operating system services and capabilities.
AC_USE_SYSTEM_EXTENSIONS
AC_SYS_LARGEFILE
@@ -29,6 +32,46 @@ LIBMD_LINKER_VERSION_SCRIPT
# Checks for programs.
AC_PROG_CC
+# Checks for the build machinery.
+AS_IF([test "$user_CFLAGS" = unset], [
+ LIBMD_CHECK_COMPILER_FLAG([-Wall])
+ LIBMD_CHECK_COMPILER_FLAG([-Wextra])
+
+ LIBMD_CHECK_COMPILER_FLAG([-Wbad-function-cast])
+ LIBMD_CHECK_COMPILER_FLAG([-Wc99-c11-compat])
+ LIBMD_CHECK_COMPILER_FLAG([-Wcast-align])
+ LIBMD_CHECK_COMPILER_FLAG([-Wdeclaration-after-statement])
+ LIBMD_CHECK_COMPILER_FLAG([-Wdocumentation])
+ LIBMD_CHECK_COMPILER_FLAG([-Wduplicated-branches])
+ LIBMD_CHECK_COMPILER_FLAG([-Wduplicated-cond])
+ LIBMD_CHECK_COMPILER_FLAG([-Wformat -Wformat-security])
+ LIBMD_CHECK_COMPILER_FLAG([-Wformat=2])
+ LIBMD_CHECK_COMPILER_FLAG([-Winit-self])
+ LIBMD_CHECK_COMPILER_FLAG([-Wlogical-not-parentheses])
+ LIBMD_CHECK_COMPILER_FLAG([-Wlogical-op])
+ LIBMD_CHECK_COMPILER_FLAG([-Wmissing-declarations])
+ LIBMD_CHECK_COMPILER_FLAG([-Wmissing-format-attribute])
+ LIBMD_CHECK_COMPILER_FLAG([-Wmissing-prototypes])
+ LIBMD_CHECK_COMPILER_FLAG([-Wnested-externs])
+ LIBMD_CHECK_COMPILER_FLAG([-Wno-tautological-constant-out-of-range-compare])
+ LIBMD_CHECK_COMPILER_FLAG([-Wno-unused-parameter])
+ LIBMD_CHECK_COMPILER_FLAG([-Wnull-dereference])
+ LIBMD_CHECK_COMPILER_FLAG([-Wold-style-definition])
+ LIBMD_CHECK_COMPILER_FLAG([-Wpointer-arith])
+ LIBMD_CHECK_COMPILER_FLAG([-Wredundant-decls])
+ LIBMD_CHECK_COMPILER_FLAG([-Wregister])
+ LIBMD_CHECK_COMPILER_FLAG([-Wrestrict])
+ LIBMD_CHECK_COMPILER_FLAG([-Wshadow])
+ LIBMD_CHECK_COMPILER_FLAG([-Wshift-negative-value])
+ LIBMD_CHECK_COMPILER_FLAG([-Wsizeof-array-argument])
+ LIBMD_CHECK_COMPILER_FLAG([-Wstrict-prototypes])
+ LIBMD_CHECK_COMPILER_FLAG([-Wswitch-bool])
+ LIBMD_CHECK_COMPILER_FLAG([-Wvla])
+ LIBMD_CHECK_COMPILER_FLAG([-Wwrite-strings])
+
+ CFLAGS="$LIBMD_COMPILER_FLAGS $CFLAGS"
+])
+
# Checks for libraries.
# Checks for header files.
diff --git a/m4/libmd-compiler.m4 b/m4/libmd-compiler.m4
new file mode 100644
index 0000000..373b1c3
--- /dev/null
+++ b/m4/libmd-compiler.m4
@@ -0,0 +1,24 @@
+# Copyright © 2021 Guillem Jover <guillem@hadrons.org>
+
+# LIBMD_CHECK_COMPILER_FLAG
+# -------------------------
+AC_DEFUN([LIBMD_CHECK_COMPILER_FLAG], [
+ AS_VAR_PUSHDEF([libmd_varname_cache], [libmd_cv_cflags_$1])
+ AC_CACHE_CHECK([whether $CC accepts $1], [libmd_varname_cache], [
+ m4_define([libmd_check_flag], m4_bpatsubst([$1], [^-Wno-], [-W]))
+ AS_VAR_COPY([libmd_save_CFLAGS], [CFLAGS])
+ AS_VAR_SET([CFLAGS], ["-Werror libmd_check_flag"])
+ AC_COMPILE_IFELSE([
+ AC_LANG_SOURCE([[]])
+ ], [
+ AS_VAR_SET([libmd_varname_cache], [yes])
+ ], [
+ AS_VAR_SET([libmd_varname_cache], [no])
+ ])
+ AS_VAR_COPY([CFLAGS], [libmd_save_CFLAGS])
+ ])
+ AS_VAR_IF([libmd_varname_cache], [yes], [
+ AS_VAR_APPEND([LIBMD_COMPILER_FLAGS], [" $1"])
+ ])
+ AS_VAR_POPDEF([libmd_varname_cache])
+])