Fix warnings with Intel 2021.4
[alexxy/gromacs.git] / cmake / gmxTestCompilerProblems.cmake
index 8fa5836ae74669e0d4ebe43ab49e1281af6e407d..c382cc85bbf57d59fe9ee3c69b7e35b8dcccb7c6 100644 (file)
@@ -1,7 +1,8 @@
 #
 # This file is part of the GROMACS molecular simulation package.
 #
-# Copyright (c) 2012,2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
+# Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
+# Copyright (c) 2017,2018,2019,2020,2021, by the GROMACS development team, led by
 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
 # and including many others, as listed in the AUTHORS file in the
 # top-level source directory and at http://www.gromacs.org.
@@ -32,6 +33,8 @@
 # To help us fund GROMACS development, we humbly ask that you cite
 # the research papers on the package. Check out http://www.gromacs.org.
 
+include(CheckCXXSourceCompiles)
+
 # Macro that runs through a number of tests for buggy compiler
 # versions, or other potential problems.
 macro(gmx_test_compiler_problems)
@@ -44,13 +47,58 @@ macro(gmx_test_compiler_problems)
         message(WARNING "The versions of the C and C++ compilers do not match (${CMAKE_C_COMPILER_VERSION} and ${CMAKE_CXX_COMPILER_VERSION}, respectively). Mixing different C/C++ compilers can cause problems.")
     endif()
 
-    # Note that we've already tested that the compiler works with C++11
-    if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
-        if(WIN32 AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "3.5.0")
-            message(WARNING "Using Clang on Windows requires Clang 3.5.0")
+    # Error if compiler doesn't support required C++17 features.
+    # cmake feature detection is currently inconsistent: gitlab.kitware.com/cmake/cmake/issues/18869
+    # We might want to switch to using feature test macros some time.
+    if(CMAKE_COMPILER_IS_GNUCXX)
+        if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7)
+            set(cxx_required_version "GCC version 7")
+        endif()
+    elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
+        if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19.15)
+            set(cxx_required_version "Visual Studio 2017")
+        endif()
+    elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+        if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5)
+            set(cxx_required_version "Clang 5")
+        endif()
+    elseif(CMAKE_CXX_COMPILER_ID MATCHES "IntelLLVM")
+        # All versions of IntelLLVM (a.k.a. DPCPP) compiler so far support C++17
+    else()
+        message(WARNING "You are using an unsupported compiler. Please make sure it fully supports C++17.")
+    endif()
+    if (cxx_required_version)
+        message(FATAL_ERROR "${cxx_required_version} or later required. "
+                            "Earlier versions don't have full C++17 support.")
+    endif()
+
+    if (CMAKE_CXX_COMPILER_ID MATCHES "Intel" AND NOT CMAKE_CXX_COMPILER_ID MATCHES "IntelLLVM")
+        message(WARNING "The Intel classic compiler is no longer supported. It may pass the tests, but is not tested by the GROMACS developers. Use the clang-based compiler from oneAPI, or gcc")
+    endif()
+    # Intel LLVM 2021.2 defaults to no-finite-math which isn't OK for GROMACS and its dependencies (muParser and GTest).
+    # This is why we set the flags globally via CMAKE_CXX_FLAGS
+    if(GMX_INTEL_LLVM AND GMX_INTEL_LLVM_VERSION GREATER_EQUAL 2021020)
+        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-finite-math-only")
+    endif()
+
+
+    if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "XL")
+        check_cxx_source_compiles(
+"// Test in-class array initalizers used with constructor initializer lists
+struct TestStruct
+{
+    float a[3][3] = {{0}}; // in-class initializer
+    float b; // not initialized until constructor initializer list
+    TestStruct();
+};
+TestStruct::TestStruct() : b(0) {}
+}" XLC_COMPILES_CORRECTLY)
+        if (NOT XLC_COMPILES_CORRECTLY)
+            message(FATAL_ERROR "No known version of xlC can compile the normal C++11 code in GROMACS, highest version checked is 16.1.0")
         endif()
-    elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "PGI")
-        message(WARNING "Currently tested PGI compiler versions (up to 15.7) generate binaries that do not pass all regression test, and the generated binaries are significantly slower than with GCC, ICC or Clang. For now we do not recommend PGI beyond development testing - make sure to run the regressiontests.")
+    endif()
+    if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "PGI")
+        message(WARNING "Currently tested PGI compiler versions (up to 15.7) generate binaries that do not pass all regression test, and the generated binaries are significantly slower than with GCC or Clang. For now we do not recommend PGI beyond development testing - make sure to run the regressiontests.")
     endif()
 
 endmacro(gmx_test_compiler_problems)