Reimplement compiler options checking.

This commit is contained in:
Andrew Borodin 2021-09-19 17:13:56 +03:00
parent c32c4b316b
commit 62d2580302
7 changed files with 264 additions and 90 deletions

View File

@ -7,6 +7,10 @@ m4_include([m4.include/gnulib/windows-stat-inodes.m4])
m4_include([m4.include/gnulib/sys_types_h.m4])
m4_include([m4.include/ax_path_lib_pcre.m4])
m4_include([m4.include/dx_doxygen.m4])
m4_include([m4.include/ax_require_defined.m4])
m4_include([m4.include/ax_check_compile_flag.m4])
m4_include([m4.include/ax_append_flag.m4])
m4_include([m4.include/ax_append_compile_flags.m4])
m4_include([m4.include/mc-cflags.m4])
m4_include([m4.include/ax_gcc_func_attribute.m4])
m4_include([m4.include/mc-check-search-type.m4])

View File

@ -53,7 +53,7 @@ fi
AC_ARG_ENABLE([werror],
AS_HELP_STRING([--enable-werror], [Handle all compiler warnings as errors]))
if test "x$enable_werror" = xyes; then
mc_CHECK_ONE_CFLAG([-Werror])
AX_APPEND_COMPILE_FLAGS([-Werror], [mc_configured_cflags])
fi
dnl Compiler can generate warnings for unrecognized flags added to CFLAGS

View File

@ -0,0 +1,46 @@
# ============================================================================
# https://www.gnu.org/software/autoconf-archive/ax_append_compile_flags.html
# ============================================================================
#
# SYNOPSIS
#
# AX_APPEND_COMPILE_FLAGS([FLAG1 FLAG2 ...], [FLAGS-VARIABLE], [EXTRA-FLAGS], [INPUT])
#
# DESCRIPTION
#
# For every FLAG1, FLAG2 it is checked whether the compiler works with the
# flag. If it does, the flag is added FLAGS-VARIABLE
#
# If FLAGS-VARIABLE is not specified, the current language's flags (e.g.
# CFLAGS) is used. During the check the flag is always added to the
# current language's flags.
#
# If EXTRA-FLAGS is defined, it is added to the current language's default
# flags (e.g. CFLAGS) when the check is done. The check is thus made with
# the flags: "CFLAGS EXTRA-FLAGS FLAG". This can for example be used to
# force the compiler to issue an error when a bad flag is given.
#
# INPUT gives an alternative input source to AC_COMPILE_IFELSE.
#
# NOTE: This macro depends on the AX_APPEND_FLAG and
# AX_CHECK_COMPILE_FLAG. Please keep this macro in sync with
# AX_APPEND_LINK_FLAGS.
#
# LICENSE
#
# Copyright (c) 2011 Maarten Bosmans <mkbosmans@gmail.com>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.
#serial 7
AC_DEFUN([AX_APPEND_COMPILE_FLAGS],
[AX_REQUIRE_DEFINED([AX_CHECK_COMPILE_FLAG])
AX_REQUIRE_DEFINED([AX_APPEND_FLAG])
for flag in $1; do
AX_CHECK_COMPILE_FLAG([$flag], [AX_APPEND_FLAG([$flag], [$2])], [], [$3], [$4])
done
])dnl AX_APPEND_COMPILE_FLAGS

View File

@ -0,0 +1,50 @@
# ===========================================================================
# https://www.gnu.org/software/autoconf-archive/ax_append_flag.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_APPEND_FLAG(FLAG, [FLAGS-VARIABLE])
#
# DESCRIPTION
#
# FLAG is appended to the FLAGS-VARIABLE shell variable, with a space
# added in between.
#
# If FLAGS-VARIABLE is not specified, the current language's flags (e.g.
# CFLAGS) is used. FLAGS-VARIABLE is not changed if it already contains
# FLAG. If FLAGS-VARIABLE is unset in the shell, it is set to exactly
# FLAG.
#
# NOTE: Implementation based on AX_CFLAGS_GCC_OPTION.
#
# LICENSE
#
# Copyright (c) 2008 Guido U. Draheim <guidod@gmx.de>
# Copyright (c) 2011 Maarten Bosmans <mkbosmans@gmail.com>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.
#serial 8
AC_DEFUN([AX_APPEND_FLAG],
[dnl
AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_SET_IF
AS_VAR_PUSHDEF([FLAGS], [m4_default($2,_AC_LANG_PREFIX[FLAGS])])
AS_VAR_SET_IF(FLAGS,[
AS_CASE([" AS_VAR_GET(FLAGS) "],
[*" $1 "*], [AC_RUN_LOG([: FLAGS already contains $1])],
[
AS_VAR_APPEND(FLAGS,[" $1"])
AC_RUN_LOG([: FLAGS="$FLAGS"])
])
],
[
AS_VAR_SET(FLAGS,[$1])
AC_RUN_LOG([: FLAGS="$FLAGS"])
])
AS_VAR_POPDEF([FLAGS])dnl
])dnl AX_APPEND_FLAG

View File

@ -0,0 +1,53 @@
# ===========================================================================
# https://www.gnu.org/software/autoconf-archive/ax_check_compile_flag.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_CHECK_COMPILE_FLAG(FLAG, [ACTION-SUCCESS], [ACTION-FAILURE], [EXTRA-FLAGS], [INPUT])
#
# DESCRIPTION
#
# Check whether the given FLAG works with the current language's compiler
# or gives an error. (Warnings, however, are ignored)
#
# ACTION-SUCCESS/ACTION-FAILURE are shell commands to execute on
# success/failure.
#
# If EXTRA-FLAGS is defined, it is added to the current language's default
# flags (e.g. CFLAGS) when the check is done. The check is thus made with
# the flags: "CFLAGS EXTRA-FLAGS FLAG". This can for example be used to
# force the compiler to issue an error when a bad flag is given.
#
# INPUT gives an alternative input source to AC_COMPILE_IFELSE.
#
# NOTE: Implementation based on AX_CFLAGS_GCC_OPTION. Please keep this
# macro in sync with AX_CHECK_{PREPROC,LINK}_FLAG.
#
# LICENSE
#
# Copyright (c) 2008 Guido U. Draheim <guidod@gmx.de>
# Copyright (c) 2011 Maarten Bosmans <mkbosmans@gmail.com>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.
#serial 6
AC_DEFUN([AX_CHECK_COMPILE_FLAG],
[AC_PREREQ(2.64)dnl for _AC_LANG_PREFIX and AS_VAR_IF
AS_VAR_PUSHDEF([CACHEVAR],[ax_cv_check_[]_AC_LANG_ABBREV[]flags_$4_$1])dnl
AC_CACHE_CHECK([whether _AC_LANG compiler accepts $1], CACHEVAR, [
ax_check_save_flags=$[]_AC_LANG_PREFIX[]FLAGS
_AC_LANG_PREFIX[]FLAGS="$[]_AC_LANG_PREFIX[]FLAGS $4 $1"
AC_COMPILE_IFELSE([m4_default([$5],[AC_LANG_PROGRAM()])],
[AS_VAR_SET(CACHEVAR,[yes])],
[AS_VAR_SET(CACHEVAR,[no])])
_AC_LANG_PREFIX[]FLAGS=$ax_check_save_flags])
AS_VAR_IF(CACHEVAR,yes,
[m4_default([$2], :)],
[m4_default([$3], :)])
AS_VAR_POPDEF([CACHEVAR])dnl
])dnl AX_CHECK_COMPILE_FLAGS

View File

@ -0,0 +1,37 @@
# ===========================================================================
# https://www.gnu.org/software/autoconf-archive/ax_require_defined.html
# ===========================================================================
#
# SYNOPSIS
#
# AX_REQUIRE_DEFINED(MACRO)
#
# DESCRIPTION
#
# AX_REQUIRE_DEFINED is a simple helper for making sure other macros have
# been defined and thus are available for use. This avoids random issues
# where a macro isn't expanded. Instead the configure script emits a
# non-fatal:
#
# ./configure: line 1673: AX_CFLAGS_WARN_ALL: command not found
#
# It's like AC_REQUIRE except it doesn't expand the required macro.
#
# Here's an example:
#
# AX_REQUIRE_DEFINED([AX_CHECK_LINK_FLAG])
#
# LICENSE
#
# Copyright (c) 2014 Mike Frysinger <vapier@gentoo.org>
#
# Copying and distribution of this file, with or without modification, are
# permitted in any medium without royalty provided the copyright notice
# and this notice are preserved. This file is offered as-is, without any
# warranty.
#serial 2
AC_DEFUN([AX_REQUIRE_DEFINED], [dnl
m4_ifndef([$1], [m4_fatal([macro ]$1[ is not defined; is a m4 file missing?])])
])dnl AX_REQUIRE_DEFINED

View File

@ -3,104 +3,88 @@ dnl
dnl Check flags supported by C compiler
dnl
dnl @author Slava Zanko <slavazanko@gmail.com>
dnl @version 2013-01-16
dnl @modified by Andrew Borodin <aborodin@vmail.ru>
dnl @version 2021-09-19
dnl @license GPL
dnl @copyright Free Software Foundation, Inc.
AC_DEFUN([mc_CHECK_ONE_CFLAG],[
AC_MSG_CHECKING([whether ${CC} accepts $1])
safe_CFLAGS=$CFLAGS
case "$CC" in
clang*)
CFLAGS="-Werror $1"
;;
*)
CFLAGS="$1"
;;
esac
AC_COMPILE_IFELSE(
[AC_LANG_PROGRAM([], [[return 0;]])],
[mc_check_one_cflag=yes],
[mc_check_one_cflag=no])
CFLAGS=$safe_CFLAGS
AC_MSG_RESULT([$mc_check_one_cflag])
if test x$mc_check_one_cflag = xyes; then
mc_configured_cflags="$mc_configured_cflags $1"
fi
])
AC_DEFUN([mc_CHECK_CFLAGS],[
AC_LANG_PUSH(C)
mc_configured_cflags=""
dnl Sorted -f options:
dnl AC_MSG_CHECKING([CC is $CC])
case "$CC" in
gcc*)
mc_CHECK_ONE_CFLAG([-fdiagnostics-show-option])
dnl mc_CHECK_ONE_CFLAG([-fno-stack-protector])
;;
*)
;;
esac
dnl AC_MSG_CHECKING([CC is $CC])
dnl Sorted -W options:
mc_CHECK_ONE_CFLAG([-Wassign-enum])
mc_CHECK_ONE_CFLAG([-Wbad-function-cast])
mc_CHECK_ONE_CFLAG([-Wcomment])
mc_CHECK_ONE_CFLAG([-Wconditional-uninitialized])
mc_CHECK_ONE_CFLAG([-Wdeclaration-after-statement])
mc_CHECK_ONE_CFLAG([-Wfloat-conversion])
mc_CHECK_ONE_CFLAG([-Wfloat-equal])
mc_CHECK_ONE_CFLAG([-Wformat])
mc_CHECK_ONE_CFLAG([-Wformat-security])
mc_CHECK_ONE_CFLAG([-Wformat-signedness])
mc_CHECK_ONE_CFLAG([-Wimplicit])
mc_CHECK_ONE_CFLAG([-Wimplicit-fallthrough])
mc_CHECK_ONE_CFLAG([-Wignored-qualifiers])
mc_CHECK_ONE_CFLAG([-Wlogical-not-parentheses])
mc_CHECK_ONE_CFLAG([-Wmaybe-uninitialized])
mc_CHECK_ONE_CFLAG([-Wmissing-braces])
mc_CHECK_ONE_CFLAG([-Wmissing-declarations])
mc_CHECK_ONE_CFLAG([-Wmissing-field-initializers])
mc_CHECK_ONE_CFLAG([-Wmissing-format-attribute])
mc_CHECK_ONE_CFLAG([-Wmissing-parameter-type])
mc_CHECK_ONE_CFLAG([-Wmissing-prototypes])
mc_CHECK_ONE_CFLAG([-Wmissing-variable-declarations])
mc_CHECK_ONE_CFLAG([-Wnested-externs])
mc_CHECK_ONE_CFLAG([-Wno-long-long])
mc_CHECK_ONE_CFLAG([-Wno-unreachable-code])
mc_CHECK_ONE_CFLAG([-Wparentheses])
mc_CHECK_ONE_CFLAG([-Wpointer-arith])
mc_CHECK_ONE_CFLAG([-Wpointer-sign])
mc_CHECK_ONE_CFLAG([-Wredundant-decls])
mc_CHECK_ONE_CFLAG([-Wreturn-type])
mc_CHECK_ONE_CFLAG([-Wsequence-point])
mc_CHECK_ONE_CFLAG([-Wshadow])
mc_CHECK_ONE_CFLAG([-Wsign-compare])
dnl mc_CHECK_ONE_CFLAG([-Wstrict-aliasing])
mc_CHECK_ONE_CFLAG([-Wstrict-prototypes])
mc_CHECK_ONE_CFLAG([-Wswitch])
mc_CHECK_ONE_CFLAG([-Wswitch-default])
mc_CHECK_ONE_CFLAG([-Wtype-limits])
mc_CHECK_ONE_CFLAG([-Wundef])
mc_CHECK_ONE_CFLAG([-Wuninitialized])
mc_CHECK_ONE_CFLAG([-Wunreachable-code])
mc_CHECK_ONE_CFLAG([-Wunused-but-set-variable])
mc_CHECK_ONE_CFLAG([-Wunused-function])
mc_CHECK_ONE_CFLAG([-Wunused-label])
mc_CHECK_ONE_CFLAG([-Wunused-parameter])
mc_CHECK_ONE_CFLAG([-Wunused-result])
mc_CHECK_ONE_CFLAG([-Wunused-value])
mc_CHECK_ONE_CFLAG([-Wunused-variable])
mc_CHECK_ONE_CFLAG([-Wwrite-strings])
dnl https://stackoverflow.com/questions/52557417/how-to-check-support-compile-flag-in-autoconf-for-clang
case "$CC" in
clang*)
EXTRA_OPTION="-Werror"
;;
*)
EXTRA_OPTION=""
;;
esac
dnl Sorted -f options:
case "$CC" in
gcc*)
AX_APPEND_COMPILE_FLAGS([-fdiagnostics-show-option], [mc_configured_cflags])
dnl AX_APPEND_COMPILE_FLAGS([-fno-stack-protector], [mc_configured_cflags])
;;
*)
;;
esac
dnl Sorted -W options:
AX_APPEND_COMPILE_FLAGS([-Wassign-enum], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wbad-function-cast], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wcomment], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wconditional-uninitialized], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wdeclaration-after-statement], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wfloat-conversion], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wfloat-equal], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wformat], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wformat-security], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wformat-signedness], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wimplicit], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wimplicit-fallthrough], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wignored-qualifiers], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wlogical-not-parentheses], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wmaybe-uninitialized], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wmissing-braces], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wmissing-declarations], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wmissing-field-initializers], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wmissing-format-attribute], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wmissing-parameter-type], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wmissing-prototypes], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wmissing-variable-declarations], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wnested-externs], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wno-long-long], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wno-unreachable-code], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wparentheses], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wpointer-arith], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wpointer-sign], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wredundant-decls], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wreturn-type], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wsequence-point], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wshadow], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wsign-compare], [mc_configured_cflags], [$EXTRA_OPTION])
dnl AX_APPEND_COMPILE_FLAGS([-Wstrict-aliasing], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wstrict-prototypes], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wswitch], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wswitch-default], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wtype-limits], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wundef], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wuninitialized], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wunreachable-code], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wunused-but-set-variable], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wunused-function], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wunused-label], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wunused-parameter], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wunused-result], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wunused-value], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wunused-variable], [mc_configured_cflags], [$EXTRA_OPTION])
AX_APPEND_COMPILE_FLAGS([-Wwrite-strings], [mc_configured_cflags], [$EXTRA_OPTION])
AC_LANG_POP()
])