From 16c3e1868a15a5946c0f0f7df6e107e9b3c6389d Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 10 Jun 2021 21:26:53 +0200 Subject: [PATCH] Clarify that compiler warnings in the Gnulib tests can be ignored. * gnulib-tool (func_emit_tests_Makefile_am): Emit overrides for CFLAGS and CXXFLAGS. Emit a dependency of 'all' on 'all-notice' that prints a notice. (func_emit_initmacro_start): Add a second argument. If it is true, emit code to require gl_CC_ALLOW_WARNINGS and gl_CXX_ALLOW_WARNINGS. (func_import, func_create_testdir): All callers updated. * m4/gnulib-common.m4 (gl_CC_ALLOW_WARNINGS, gl_CXX_ALLOW_WARNINGS): New macros. --- ChangeLog | 12 ++++++++ gnulib-tool | 45 ++++++++++++++++++++++++++---- m4/gnulib-common.m4 | 68 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 118 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 0ae76be1e7..f95d389028 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2021-06-10 Bruno Haible + + Clarify that compiler warnings in the Gnulib tests can be ignored. + * gnulib-tool (func_emit_tests_Makefile_am): Emit overrides for CFLAGS + and CXXFLAGS. Emit a dependency of 'all' on 'all-notice' that prints a + notice. + (func_emit_initmacro_start): Add a second argument. If it is true, emit + code to require gl_CC_ALLOW_WARNINGS and gl_CXX_ALLOW_WARNINGS. + (func_import, func_create_testdir): All callers updated. + * m4/gnulib-common.m4 (gl_CC_ALLOW_WARNINGS, gl_CXX_ALLOW_WARNINGS): New + macros. + 2021-06-09 Bruno Haible sigsegv tests: Hide a null pointer from the compiler's optimizations. diff --git a/gnulib-tool b/gnulib-tool index 237693a883..d41d58ce84 100755 --- a/gnulib-tool +++ b/gnulib-tool @@ -4201,6 +4201,25 @@ func_emit_tests_Makefile_am () fi done echo + # Insert a '-Wno-error' option in the compilation commands emitted by + # Automake, between $(AM_CPPFLAGS) and before the reference to @CFLAGS@. + # Why? + # 1) Because parts of the Gnulib tests exercise corner cases (invalid + # arguments, endless recursions, etc.) that a compiler may warn about, + # even with just the normal '-Wall' option. + # 2) Because every package maintainer has their preferred set of warnings + # that they may want to enforce in the main source code of their package. + # But Gnulib tests are maintained in Gnulib and don't end up in binaries + # that that package installs; therefore it does not make sense for + # package maintainers to enforce the absence of warnings on these tests. + # Why before @CFLAGS@? + # - Because "the user is always right": If a user adds '-Werror' to their + # CFLAGS, they have asked for errors, they will get errors. But they have + # no right to complain about these errors, because Gnulib does not support + # '-Werror'. + echo "CFLAGS = @GL_CFLAG_ALLOW_WARNINGS@ @CFLAGS@" + echo "CXXFLAGS = @GL_CXXFLAG_ALLOW_WARNINGS@ @CXXFLAGS@" + echo echo "AM_CPPFLAGS = \\" if $for_test; then echo " -DGNULIB_STRICT_CHECKING=1 \\" @@ -4248,6 +4267,14 @@ func_emit_tests_Makefile_am () echo cat "$tmp"/main_snippets "$tmp"/longrunning_snippets \ | sed -e 's|\$(top_srcdir)/build-aux/|$(top_srcdir)/'"$auxdir"'/|g' + # Arrange to print a message before compiling the files in this directory. + echo "all: all-notice" + echo "all-notice:" + echo " @echo '## ---------------------------------------------------- ##'" + echo " @echo '## ------------------- Gnulib tests ------------------- ##'" + echo " @echo '## You can ignore compiler warnings in this directory. ##'" + echo " @echo '## ---------------------------------------------------- ##'" + echo echo "# Clean up after Solaris cc." echo "clean-local:" echo " rm -rf SunWS_cache" @@ -4262,9 +4289,11 @@ func_emit_tests_Makefile_am () rm -f "$tmp"/main_snippets "$tmp"/longrunning_snippets } -# func_emit_initmacro_start macro_prefix +# func_emit_initmacro_start macro_prefix gentests # emits the first few statements of the gl_INIT macro to standard output. # - macro_prefix prefix of gl_EARLY, gl_INIT macros to use +# - gentests true if a tests Makefile.am is being generated, +# false otherwise # - module_indicator_prefix prefix of GNULIB_ variables to use func_emit_initmacro_start () { @@ -4298,6 +4327,10 @@ func_emit_initmacro_start () # Scope the GNULIB_ variables. echo " m4_pushdef([GL_MODULE_INDICATOR_PREFIX], [${module_indicator_prefix}])" echo " gl_COMMON" + if "$2"; then + echo " AC_REQUIRE([gl_CC_ALLOW_WARNINGS])" + echo " AC_REQUIRE([gl_CXX_ALLOW_WARNINGS])" + fi } # func_emit_initmacro_end macro_prefix @@ -5778,7 +5811,7 @@ s,//*$,/,' sed_replace_build_aux="$sed_noop" fi echo " gl_m4_base='$m4base'" - func_emit_initmacro_start $macro_prefix + func_emit_initmacro_start $macro_prefix false echo " gl_source_base='$sourcebase'" if test -n "$witness_c_macro"; then echo " m4_pushdef([gl_MODULE_INDICATOR_CONDITION], [$witness_c_macro])" @@ -5791,7 +5824,7 @@ s,//*$,/,' func_emit_initmacro_end $macro_prefix echo " gltests_libdeps=" echo " gltests_ltlibdeps=" - func_emit_initmacro_start ${macro_prefix}tests + func_emit_initmacro_start ${macro_prefix}tests $gentests echo " gl_source_base='$testsbase'" # Define a tests witness macro that depends on the package. # PACKAGE is defined by AM_INIT_AUTOMAKE, PACKAGE_TARNAME is defined by AC_INIT. @@ -6436,7 +6469,7 @@ func_create_testdir () ba }' echo "gl_m4_base='../$m4base'" - func_emit_initmacro_start $macro_prefix + func_emit_initmacro_start $macro_prefix true # We don't have explicit ordering constraints between the various # autoconf snippets. It's cleanest to put those of the library before # those of the tests. @@ -6552,7 +6585,7 @@ func_create_testdir () sed_replace_build_aux="$sed_noop" fi echo "gl_m4_base='$m4base'" - func_emit_initmacro_start $macro_prefix + func_emit_initmacro_start $macro_prefix false echo "gl_source_base='$sourcebase'" if $single_configure; then func_emit_autoconf_snippets "$main_modules" "$main_modules" func_verify_module true false false @@ -6563,7 +6596,7 @@ func_create_testdir () if $single_configure; then echo " gltests_libdeps=" echo " gltests_ltlibdeps=" - func_emit_initmacro_start ${macro_prefix}tests + func_emit_initmacro_start ${macro_prefix}tests true echo " gl_source_base='$testsbase'" # Define a tests witness macro. echo " ${macro_prefix}tests_WITNESS=IN_GNULIB_TESTS" diff --git a/m4/gnulib-common.m4 b/m4/gnulib-common.m4 index 7db4be1acb..bfa1645db2 100644 --- a/m4/gnulib-common.m4 +++ b/m4/gnulib-common.m4 @@ -1,4 +1,4 @@ -# gnulib-common.m4 serial 65 +# gnulib-common.m4 serial 66 dnl Copyright (C) 2007-2021 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -666,6 +666,72 @@ AC_DEFUN([gl_CACHE_VAL_SILENT], ]) ]) +# gl_CC_ALLOW_WARNINGS +# sets and substitutes a variable GL_CFLAG_ALLOW_WARNINGS, to a $(CC) option +# that reverts a preceding '-Werror' option, if available. +# This is expected to be '-Wno-error' on gcc, clang (except clang/MSVC), xlclang +# and empty otherwise. +AC_DEFUN([gl_CC_ALLOW_WARNINGS], +[ + AC_REQUIRE([AC_PROG_CC]) + AC_CACHE_CHECK([for C compiler option to allow warnings], + [gl_cv_cc_wallow], + [rm -f conftest* + echo 'int dummy;' > conftest.c + AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS -c conftest.c 2>conftest1.err]) >/dev/null + AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS -Wno-error -c conftest.c 2>conftest2.err]) >/dev/null + dnl Test the number of error output lines, because AIX xlc accepts the + dnl option '-Wno-error', just to produce a warning + dnl "Option -Wno-error was incorrectly specified. The option will be ignored." + dnl afterwards. + if test $? = 0 && test `wc -l < conftest1.err` = `wc -l < conftest2.err`; then + gl_cv_cc_wallow='-Wno-error' + else + gl_cv_cc_wallow=none + fi + rm -f conftest* + ]) + case "$gl_cv_cc_wallow" in + none) GL_CFLAG_ALLOW_WARNINGS='' ;; + *) GL_CFLAG_ALLOW_WARNINGS="$gl_cv_cc_wallow" ;; + esac + AC_SUBST([GL_CFLAG_ALLOW_WARNINGS]) +]) + +# gl_CXX_ALLOW_WARNINGS +# sets and substitutes a variable GL_CXXFLAG_ALLOW_WARNINGS, to a $(CC) option +# that reverts a preceding '-Werror' option, if available. +AC_DEFUN([gl_CXX_ALLOW_WARNINGS], +[ + dnl Requires AC_PROG_CXX or gl_PROG_ANSI_CXX. + if test -n "$CXX" && test "$CXX" != no; then + AC_CACHE_CHECK([for C++ compiler option to allow warnings], + [gl_cv_cxx_wallow], + [rm -f conftest* + echo 'int dummy;' > conftest.cc + AC_TRY_COMMAND([${CXX-c++} $CXXFLAGS $CPPFLAGS -c conftest.cc 2>conftest1.err]) >/dev/null + AC_TRY_COMMAND([${CXX-c++} $CXXFLAGS $CPPFLAGS -Wno-error -c conftest.cc 2>conftest2.err]) >/dev/null + dnl Test the number of error output lines, because AIX xlC accepts the + dnl option '-Wno-error', just to produce a warning + dnl "Option -Wno-error was incorrectly specified. The option will be ignored." + dnl afterwards. + if test $? = 0 && test `wc -l < conftest1.err` = `wc -l < conftest2.err`; then + gl_cv_cxx_wallow='-Wno-error' + else + gl_cv_cxx_wallow=none + fi + rm -f conftest* + ]) + case "$gl_cv_cxx_wallow" in + none) GL_CXXFLAG_ALLOW_WARNINGS='' ;; + *) GL_CXXFLAG_ALLOW_WARNINGS="$gl_cv_cxx_wallow" ;; + esac + else + GL_CXXFLAG_ALLOW_WARNINGS='' + fi + AC_SUBST([GL_CXXFLAG_ALLOW_WARNINGS]) +]) + dnl Expands to some code for use in .c programs that, on native Windows, defines dnl the Microsoft deprecated alias function names to the underscore-prefixed dnl actual function names. With this macro, these function names are available -- 2.39.5