Split up compare.*
[gromacs.git] / cmake / gmxTestCXX11.cmake
blobe32b00b99ddeb801021a438c3c523cd100cf3281
2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2012,2013,2014,2015,2016, 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)
37 # Check whether both a suitable C++11-compatible compiler and standard
38 # library is available, and give a fatal error if not.
40 # Any required compiler flag for C++11 support is returned in
41 # ${FLAG}. The other parameters are only inputs, naming variables that
42 # contain flags that may have been detected, or set by the user.
43 function(GMX_TEST_CXX11 CXX11_CXX_FLAG_NAME STDLIB_CXX_FLAG_NAME STDLIB_LIBRARIES_NAME)
45     # First check that the compiler is OK, and find the appropriate flag.
47     if(WIN32 AND NOT MINGW)
48         set(CXX11_CXX_FLAG "/Qstd=c++0x")
49     elseif(CYGWIN)
50         set(CXX11_CXX_FLAG "-std=gnu++0x") #required for strdup
51     else()
52         set(CXX11_CXX_FLAG "-std=c++0x")
53     endif()
54     CHECK_CXX_COMPILER_FLAG("${CXX11_CXX_FLAG}" CXXFLAG_STD_CXX0X)
55     if(NOT CXXFLAG_STD_CXX0X)
56         set(CXX11_CXX_FLAG "")
57     endif()
58     set(CMAKE_REQUIRED_FLAGS "${CXX11_CXX_FLAG}")
59     check_cxx_source_compiles(
60 "// Test that a subclass has a proper copy constructor
61 struct a {
62   a() {};
63   a(const a&) {};
64   a(a&&) = delete;
66 class b: public a
69 b bTest() {
70   return b();
72 // Early patch versions of icc 16 (and perhaps earlier versions)
73 // have an issue with this test, but the GROMACS tests pass,
74 // so we disable this test in that sub-case.
75 #if (defined __INTEL_COMPILER && __INTEL_COMPILER >= 1700) || (defined __ICL && __ICL >= 1700) || (defined __INTEL_COMPILER_UDPATE && __INTEL_COMPILER_UPDATE >= 3)
76 // Test that a subclass has a proper move constructor
77 struct c {
78   c() {};
79   c(const c&) = delete;
80   c(c&&) {};
82 struct d : public c {
84 d dTest() {
85   return d();
87 #endif
88 // Test that operator bool() works
89 struct e {
90   explicit operator bool() {return true;}
92 // Test that constexpr works
93 constexpr int factorial(int n)
95     return n <= 1? 1 : (n * factorial(n - 1));
97 // Test that r-value references work
98 void checkRvalueReference(int &&);
99 // Test that extern templates work
100 template <typename T> void someFunction();
101 extern template void someFunction<int>();
102 int main() {
103   // Test nullptr
104   double *x = nullptr;
105   // Test range-based for loops
106   int array[5] = { 1, 2, 3, 4, 5 };
107   for (int& x : array)
108     x *= 2;
109 }" CXX11_SUPPORTED)
110     if(CXX11_SUPPORTED)
111         set(${CXX11_CXX_FLAG_NAME} ${CXX11_CXX_FLAG} PARENT_SCOPE)
112     else()
113         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.")
114     endif()
116     # Now check the standard library is OK
118     set(CMAKE_REQUIRED_FLAGS "${CXX11_CXX_FLAG} ${${STDLIB_CXX_FLAG_NAME}}")
119     set(CMAKE_REQUIRED_LIBRARIES "${${STDLIB_LIBRARIES_NAME}}")
120     check_cxx_source_compiles(
121 "#include <map>
122 #include <memory>
123 #include <utility>
124 int main() {
125   typedef std::unique_ptr<int> intPointer;
126   intPointer p(new int(10));
127   std::map<int, std::unique_ptr<int>> m;
128   m.insert(std::make_pair(5, std::move(p)));
129 }" CXX11_STDLIB_PRESENT)
130     if(NOT CXX11_STDLIB_PRESENT)
131         message(FATAL_ERROR "This version of GROMACS requires C++11-compatible standard library. Please use a newer compiler, or a newer standard library, or use the GROMACS 5.1.x release. See the installation guide for details.")
132     endif()
133 endfunction()