Remove cycle suppression
[gromacs.git] / cmake / gmxTestCXX11.cmake
blob9b559c6e20da23a322158ed19e287eedd8fa9c41
2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2012,2013,2014,2015,2016,2017, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
35 include(CheckCXXSourceCompiles)
36 include(FindThreads)
38 # Check whether both a suitable C++11-compatible compiler and standard
39 # library is available, and give a fatal error if not.
41 # Any required compiler flag for C++11 support is returned in
42 # ${FLAG}. The other parameters are only inputs, naming variables that
43 # contain flags that may have been detected, or set by the user.
44 function(GMX_TEST_CXX11 CXX11_CXX_FLAG_NAME STDLIB_CXX_FLAG_NAME STDLIB_LIBRARIES_NAME)
46     # First check that the compiler is OK, and find the appropriate flag.
48     if(WIN32 AND NOT MINGW)
49         set(CXX11_CXX_FLAG "/Qstd=c++11")
50     elseif(CYGWIN)
51         set(CXX11_CXX_FLAG "-std=gnu++11") #required for strdup
52     else()
53         set(CXX11_CXX_FLAG "-std=c++11")
54     endif()
55     CHECK_CXX_COMPILER_FLAG("${CXX11_CXX_FLAG}" CXXFLAG_STD_CXX0X)
56     if(NOT CXXFLAG_STD_CXX0X)
57         set(CXX11_CXX_FLAG "")
58     endif()
59     set(CMAKE_REQUIRED_FLAGS "${CXX11_CXX_FLAG}")
60     check_cxx_source_compiles(
61 "// Permit testing typeid keyword
62 #include <typeinfo>
63 // Test that a subclass has a proper copy constructor
64 struct a {
65   a() {};
66   a(const a&) {};
67   a(a&&) = delete;
69 class b: public a
72 b bTest() {
73   return b();
75 // ICC requires that a suitable GCC is available. It is using its standard library and emulates
76 // GCC behaviour based on its version. Relevant here it emulates the implementation of the move
77 // constructor. This compiler check should only fail based on the compiler not GCC. The GCC version
78 // is checked by the following STL check. It is known that all ICC>=15 have the proper move
79 // constructor. Thus this check is disabled for ICC.
80 #if !((defined __INTEL_COMPILER && __INTEL_COMPILER >= 1500) || (defined __ICL && __ICL >= 1500))
81 // Test that a subclass has a proper move constructor
82 struct c {
83   c() {};
84   c(const c&) = delete;
85   c(c&&) {};
87 struct d : public c {
89 d dTest() {
90   return d();
92 #endif
93 struct e {
94   // Test that operator bool() works
95   explicit operator bool() {return true;}
96   // Test that an in-class initializer works
97   int x = 1;
98   // Test that a default constructor is generated
99   e() = default;
101 // Test that constexpr works
102 constexpr int factorial(int n)
104     return n <= 1? 1 : (n * factorial(n - 1));
106 // Test that r-value references work
107 void checkRvalueReference(int &&);
108 // Test that extern templates work
109 template <typename T> void someFunction();
110 extern template void someFunction<int>();
111 // Test using statement
112 using myInt = int;
113 // Test template using statement
114 template<class T> using myPointer = T*;
115 myPointer<int> x;
116 int main() {
117   // Test nullptr
118   double *x = nullptr;
119   (void)x; // Suppressing unused variable warning
120   // Test range-based for loops
121   int array[5] = { 1, 2, 3, 4, 5 };
122   for (int& x : array)
123     x *= 2;
124   // Test alignas
125   alignas(4*sizeof(int)) int y;
126   // Test typeid
127   const std::type_info &intType = typeid(int);
128   // Test static assertions do compile
129   static_assert(true, \"if you see this, true somehow isn't\");
130   // Test a lambda
131   [=]{};
132 }" CXX11_SUPPORTED)
133     if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
134         if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.8.1")
135             message(FATAL_ERROR "GROMACS requires version 4.8.1 or later of the GNU C++ compiler for complete C++11 support")
136         endif()
137     elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
138         if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "3.3")
139             message(FATAL_ERROR "GROMACS requires version 3.3 or later of the Clang C++ compiler for complete C++11 support")
140         endif()
141     elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
142         if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "15.0")
143             message(FATAL_ERROR "GROMACS requires version 15.0 or later of the Intel C++ compiler for complete C++11 support")
144         endif()
145     elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
146         if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "19.0.23026")
147             message(FATAL_ERROR "GROMACS requires version 2015 (19.0.23026) or later of the MSVC C++ compiler for complete C++11 support")
148         endif()
149     endif()
150     if(CXX11_SUPPORTED)
151         set(${CXX11_CXX_FLAG_NAME} ${CXX11_CXX_FLAG} PARENT_SCOPE)
152     else()
153         message(FATAL_ERROR "This version of GROMACS requires a C++11 compiler. Please use a newer compiler or use the GROMACS 5.1.x release. See the installation guide for details.")
154     endif()
156     # Now check the standard library is OK
158     set(CMAKE_REQUIRED_FLAGS "${CXX11_CXX_FLAG} ${${STDLIB_CXX_FLAG_NAME}}")
159     set(CMAKE_REQUIRED_LIBRARIES "${${STDLIB_LIBRARIES_NAME}} ${CMAKE_THREAD_LIBS_INIT}")
160     check_cxx_source_compiles(
161 "#include <algorithm>
162 #include <array>
163 #include <chrono>
164 #include <iterator>
165 #include <map>
166 #include <memory>
167 #include <thread>
168 #include <type_traits>
169 #include <tuple>
170 #include <utility>
171 #include <string>
172 #include <vector>
173 int main() {
174   // Test for std::vector
175   std::vector<double> doubles(100);
176   // Test for std::array
177   std::array<int, 3> someInts;
178   // Test std::for_each and a lambda
179   std::for_each(std::begin(doubles), std::end(doubles), [&](double &d) { d = 2.3; });
180   // Test std::unique_ptr
181   typedef std::unique_ptr<int> intPointer;
182   // Test using std::unique_ptr
183   intPointer p(new int(10));
184   // Test std::map
185   std::map<int, std::unique_ptr<int>> m;
186   // Test std::make_pair
187   m.insert(std::make_pair(5, std::move(p)));
188   // Test std::chrono (was missing before gcc 4.8.1)
189   auto start = std::chrono::steady_clock::now();
190   if (std::chrono::steady_clock::now() - start < std::chrono::seconds(2))
191   {
192       // Test std::thread
193       std::thread t;
194   }
195   // Test std::is_pod
196   static_assert(std::is_pod<int>::value, \"int isn't pod\");
197   // Test std::tuple
198   auto theTuple = std::make_tuple<int, double>(3, 4.2);
199   // Test std::tie
200   int tupleInt;
201   double tupleDouble;
202   std::tie(tupleInt, tupleDouble) = theTuple;
203   // Test std::string
204   std::string message(\"hello\");
205 }" CXX11_STDLIB_PRESENT)
206     if(NOT CXX11_STDLIB_PRESENT)
207         if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "16.0.3")
208             message(FATAL_ERROR "GROMACS requires that the Intel C++ compiler use a compatible C++ standard library, for complete C++11 support, however not all compiler versions support all possible standard library implementations. In particular, before icc version 16.0.3, the gcc version 5 standard library was not supported. If you are affected by such a case you should probably update your compiler version. Consult the GROMACS installation guide to check exactly what is supported and how to direct the use of a standard library from an older gcc version (but at least version 4.8.1 is needed).")
209         else()
210             message(FATAL_ERROR "This version of GROMACS requires C++11-compatible standard library. Please use a newer compiler, and/or a newer standard library, or use the GROMACS 5.1.x release. Consult the installation guide for details before upgrading components.")
211         endif()
212     endif()
213 endfunction()