From 7b9c5e124ba8e5ca4a44fa57e90f291b043a0eb2 Mon Sep 17 00:00:00 2001 From: Dana Robinson <43805+derobins@users.noreply.github.com> Date: Tue, 24 Nov 2020 19:57:48 -0800 Subject: Adds a configure/CMake option to control -Werror behavior (#119) * Works in both Autotools and CMake * OFF by default * Reverts "always on" -Werror behavior released in 1.10.7 --- config/cmake/HDFCXXCompilerFlags.cmake | 12 ++++++--- config/cmake/HDFCompilerFlags.cmake | 29 ++++++++++++++++++---- config/commence.am | 4 +-- configure.ac | 45 ++++++++++++++++++++++++++++------ release_docs/RELEASE.txt | 22 +++++++++++++++++ src/libhdf5.settings.in | 2 +- 6 files changed, 95 insertions(+), 19 deletions(-) diff --git a/config/cmake/HDFCXXCompilerFlags.cmake b/config/cmake/HDFCXXCompilerFlags.cmake index 7bbebbc..8443bbd 100644 --- a/config/cmake/HDFCXXCompilerFlags.cmake +++ b/config/cmake/HDFCXXCompilerFlags.cmake @@ -89,7 +89,9 @@ if (NOT MSVC AND NOT MINGW) AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 4.8) # add the general CXX flags for g++ compiler versions 4.8 and above. ADD_H5_FLAGS (HDF5_CMAKE_CXX_FLAGS "${HDF5_SOURCE_DIR}/config/gnu-warnings/cxx-general") - ADD_H5_FLAGS (H5_CXXFLAGS0 "${HDF5_SOURCE_DIR}/config/gnu-warnings/cxx-error-general") + if (HDF5_ENABLE_WARNINGS_AS_ERRORS) + ADD_H5_FLAGS (H5_CXXFLAGS0 "${HDF5_SOURCE_DIR}/config/gnu-warnings/cxx-error-general") + endif () endif () elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") ADD_H5_FLAGS (HDF5_CMAKE_CXX_FLAGS "${HDF5_SOURCE_DIR}/config/clang-warnings/general") @@ -152,7 +154,9 @@ if (NOT MSVC AND NOT MINGW) if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0) # autotools always add the C flags with the CXX flags ADD_H5_FLAGS (H5_CXXFLAGS1 "${HDF5_SOURCE_DIR}/config/gnu-warnings/cxx-5") - ADD_H5_FLAGS (H5_CXXFLAGS1 "${HDF5_SOURCE_DIR}/config/gnu-warnings/cxx-error-5") + if (HDF5_ENABLE_WARNINGS_AS_ERRORS) + ADD_H5_FLAGS (H5_CXXFLAGS1 "${HDF5_SOURCE_DIR}/config/gnu-warnings/cxx-error-5") + endif () endif () # Append more extra warning flags that only gcc 6.x+ know about @@ -177,7 +181,9 @@ if (NOT MSVC AND NOT MINGW) if (NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0) # autotools always add the C flags with the CXX flags ADD_H5_FLAGS (H5_CXXFLAGS3 "${HDF5_SOURCE_DIR}/config/gnu-warnings/8") - #ADD_H5_FLAGS (H5_CXXFLAGS3 "${HDF5_SOURCE_DIR}/config/gnu-warnings/error-8") + #if (HDF5_ENABLE_WARNINGS_AS_ERRORS) + # ADD_H5_FLAGS (H5_CXXFLAGS3 "${HDF5_SOURCE_DIR}/config/gnu-warnings/error-8") + #endif () if (HDF5_ENABLE_DEV_WARNINGS) # autotools always add the C flags with the CXX flags ADD_H5_FLAGS (H5_CXXFLAGS3 "${HDF5_SOURCE_DIR}/config/gnu-warnings/developer-8") diff --git a/config/cmake/HDFCompilerFlags.cmake b/config/cmake/HDFCompilerFlags.cmake index 31af2f8..456db36 100644 --- a/config/cmake/HDFCompilerFlags.cmake +++ b/config/cmake/HDFCompilerFlags.cmake @@ -69,6 +69,18 @@ endif () # break into groups (from the config/gnu-flags file) #----------------------------------------------------------------------------- if (NOT MSVC AND NOT MINGW) + #----------------------------------------------------------------------------- + # Option to allow the user to interpret certain warnings as errors + # + # This should NOT be on by default as it can cause a lot of conflicts with + # new operating systems and compiler versions. Header files that are out of + # our control (MPI, HDFS, etc.) can also raise warnings. + #----------------------------------------------------------------------------- + option (HDF5_ENABLE_WARNINGS_AS_ERRORS "Interpret some warnings as errors" OFF) + if (HDF5_ENABLE_WARNINGS_AS_ERRORS) + message (STATUS "...some warnings will be interpreted as errors") + endif () + if (${CMAKE_SYSTEM_NAME} MATCHES "SunOS") list (APPEND HDF5_CMAKE_C_FLAGS "-erroff=%none -DBSD_COMP") else () @@ -96,14 +108,18 @@ if (NOT MSVC AND NOT MINGW) # Add general CFlags for GCC versions 4.8 and above if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 4.8) ADD_H5_FLAGS (HDF5_CMAKE_C_FLAGS "${HDF5_SOURCE_DIR}/config/gnu-warnings/general") - ADD_H5_FLAGS (H5_CFLAGS0 "${HDF5_SOURCE_DIR}/config/gnu-warnings/error-general") + if (HDF5_ENABLE_WARNINGS_AS_ERRORS) + ADD_H5_FLAGS (H5_CFLAGS0 "${HDF5_SOURCE_DIR}/config/gnu-warnings/error-general") + endif () endif () # gcc automatically inlines based on the optimization level # this is just a failsafe list (APPEND H5_CFLAGS0 "-finline-functions") elseif (CMAKE_C_COMPILER_ID STREQUAL "Clang") ADD_H5_FLAGS (HDF5_CMAKE_C_FLAGS "${HDF5_SOURCE_DIR}/config/clang-warnings/general") - ADD_H5_FLAGS (H5_CFLAGS0 "${HDF5_SOURCE_DIR}/config/clang-warnings/error-general") + if (HDF5_ENABLE_WARNINGS_AS_ERRORS) + ADD_H5_FLAGS (H5_CFLAGS0 "${HDF5_SOURCE_DIR}/config/clang-warnings/error-general") + endif () elseif (CMAKE_C_COMPILER_ID STREQUAL "PGI") list (APPEND HDF5_CMAKE_C_FLAGS "-Minform=inform") endif () @@ -132,7 +148,6 @@ if (NOT MSVC AND NOT MINGW) endif () endif () - if (CMAKE_C_COMPILER_ID STREQUAL "GNU") # Technically, variable-length arrays are part of the C99 standard, but # we should approach them a bit cautiously... Only needed for gcc 4.X @@ -158,7 +173,9 @@ if (NOT MSVC AND NOT MINGW) # Append more extra warning flags that only gcc 5.x+ know about if (NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 5.0) ADD_H5_FLAGS (H5_CFLAGS1 "${HDF5_SOURCE_DIR}/config/gnu-warnings/5") - ADD_H5_FLAGS (H5_CFLAGS1 "${HDF5_SOURCE_DIR}/config/gnu-warnings/error-5") + if (HDF5_ENABLE_WARNINGS_AS_ERRORS) + ADD_H5_FLAGS (H5_CFLAGS1 "${HDF5_SOURCE_DIR}/config/gnu-warnings/error-5") + endif () endif () # Append more extra warning flags that only gcc 6.x+ know about @@ -179,7 +196,9 @@ if (NOT MSVC AND NOT MINGW) # Append more extra warning flags that only gcc 8.x+ know about if (NOT CMAKE_C_COMPILER_VERSION VERSION_LESS 8.0) ADD_H5_FLAGS (H5_CFLAGS3 "${HDF5_SOURCE_DIR}/config/gnu-warnings/8") - ADD_H5_FLAGS (H5_CFLAGS3 "${HDF5_SOURCE_DIR}/config/gnu-warnings/error-8") + if (HDF5_ENABLE_WARNINGS_AS_ERRORS) + ADD_H5_FLAGS (H5_CFLAGS3 "${HDF5_SOURCE_DIR}/config/gnu-warnings/error-8") + endif () if (HDF5_ENABLE_DEV_WARNINGS) ADD_H5_FLAGS (H5_CFLAGS3 "${HDF5_SOURCE_DIR}/config/gnu-warnings/developer-8") else () diff --git a/config/commence.am b/config/commence.am index e73352c..f47d704 100644 --- a/config/commence.am +++ b/config/commence.am @@ -70,9 +70,9 @@ H5CPP=${DESTDIR}$(bindir)/h5c++ # instead of CFLAGS, as CFLAGS is reserved solely for the user to define. # This applies to FCFLAGS, CXXFLAGS, CPPFLAGS, and LDFLAGS as well. -AM_CFLAGS=@AM_CFLAGS@ @H5_CFLAGS@ @H5_ECFLAGS@ +AM_CFLAGS=@AM_CFLAGS@ @H5_CFLAGS@ AM_FCFLAGS=@AM_FCFLAGS@ @H5_FCFLAGS@ -AM_CXXFLAGS=@AM_CXXFLAGS@ @H5_CXXFLAGS@ @H5_ECXXFLAGS@ +AM_CXXFLAGS=@AM_CXXFLAGS@ @H5_CXXFLAGS@ AM_CPPFLAGS=@AM_CPPFLAGS@ @H5_CPPFLAGS@ AM_LDFLAGS=@AM_LDFLAGS@ @H5_LDFLAGS@ diff --git a/configure.ac b/configure.ac index f6b8423..2a02109 100644 --- a/configure.ac +++ b/configure.ac @@ -104,14 +104,10 @@ AC_SUBST([AR_FLAGS]) ## H5_CFLAGS (and company) are for CFLAGS that should be used on HDF5, but ## not exported to h5cc (or h5fc, etc.) ## -## H5_ECFLAGS (and company) are for warnings that should be treated as errors. -## AC_SUBST([H5_CFLAGS]) -AC_SUBST([H5_ECFLAGS]) AC_SUBST([H5_CPPFLAGS]) AC_SUBST([H5_FCFLAGS]) AC_SUBST([H5_CXXFLAGS]) -AC_SUBST([H5_ECXXFLAGS]) AC_SUBST([H5_JNIFLAGS]) AC_SUBST([H5_JAVACFLAGS]) AC_SUBST([H5_JAVAFLAGS]) @@ -2248,10 +2244,6 @@ if test "X-$DEV_WARNINGS" = X- ; then DEV_WARNINGS=no fi -## Allow this variable to be substituted in -## other files (src/libhdf5.settings.in, etc.) -AC_SUBST([DEV_WARNINGS]) - case "X-$DEV_WARNINGS" in X-yes) H5_CFLAGS="$H5_CFLAGS $DEVELOPER_WARNING_CFLAGS" @@ -2267,6 +2259,43 @@ case "X-$DEV_WARNINGS" in esac ## ---------------------------------------------------------------------- +## Check if we should consider certain compiler warnings as errors +## +## These should NOT be on by default as the risk of breakage is high +## when compiling HDF5 on new (or new versions) of platforms and +## compilers. It can also cause failures when header files we have no +## control over (e.g. MPI, HDFS) raise warnings. +## +AC_MSG_CHECKING([enable warnings as errors]) +AC_ARG_ENABLE([warnings-as-errors], + [AS_HELP_STRING([--enable-warnings-as-errors], + [Determines whether certain warnings will be + considered errors. This is mainly for use + by HDF5 library developers. + [default=no] + ])], + [WARNINGS_AS_ERRORS=$enableval]) + +## Set default +if test "X-$WARNINGS_AS_ERRORS" = X- ; then + WARNINGS_AS_ERRORS=no +fi + +case "X-$WARNINGS_AS_ERRORS" in + X-yes) + H5_CFLAGS="$H5_CFLAGS $H5_ECFLAGS" + H5_CXXFLAGS="$H5_CXXFLAGS $H5_ECXXFLAGS" + AC_MSG_RESULT([yes]) + ;; + X-no) + AC_MSG_RESULT([no]) + ;; + *) + AC_MSG_ERROR([Unrecognized value: $WARNINGS_AS_ERRORS]) + ;; +esac + +## ---------------------------------------------------------------------- ## Check if the compiler should use profiling flags/settings ## AC_MSG_CHECKING([profiling]) diff --git a/release_docs/RELEASE.txt b/release_docs/RELEASE.txt index 1b6a097..67fbe51 100644 --- a/release_docs/RELEASE.txt +++ b/release_docs/RELEASE.txt @@ -47,6 +47,28 @@ New Features Configuration: ------------- + - Added a configure-time option to consider certain compiler warnings + as errors + + A new configure-time option was added that converts some compiler warnings + to errors. This is mainly intended for library developers and currently + only works for gcc and clang. The warnings that are considered errors + will appear in the generated libhdf5.settings file. These warnings apply + to C and C++ code and will appear in "H5 C Flags" and H5 C++ Flags", + respectively. They will NOT be exported to h5cc, etc. + + The default is OFF. Building with this option may fail when compiling + on operating systems and with compiler versions not commonly used by + the library developers. Compilation may also fail when headers not + under the control of the library developers (e.g., mpi.h, hdfs.h) raise + warnings. + + Autotools: --enable-warnings-as-errors + + CMake: HDF5_ENABLE_WARNINGS_AS_ERRORS + + (DER - 2020/11/23, HDFFV-11189) + - Autotools and CMake target added to produce doxygen generated documentation The default is OFF or disabled. diff --git a/src/libhdf5.settings.in b/src/libhdf5.settings.in index 98390b8..4fddbc8 100644 --- a/src/libhdf5.settings.in +++ b/src/libhdf5.settings.in @@ -39,7 +39,7 @@ Languages: H5_CPPFLAGS: @H5_CPPFLAGS@ AM_CPPFLAGS: @AM_CPPFLAGS@ C Flags: @CFLAGS@ - H5 C Flags: @H5_CFLAGS@ @H5_ECFLAGS@ + H5 C Flags: @H5_CFLAGS@ AM C Flags: @AM_CFLAGS@ Shared C Library: @enable_shared@ Static C Library: @enable_static@ -- cgit v0.12