2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2019,2020, 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 # For compilers which might require libstdc++ (Clang and Intel), find it and set CMAKE_CXX_FLAGS.
37 # Does nothing if compiler includes std-library (e.g. GCC) or compiler uses different std-library
38 # (either because of different defaults (e.g. on MacOS) or user flags (e.g. -stdlib=libc++)).
39 # The heuristic by the compiler of how to find libstdc++ is ignored. Any user-provided flags in
40 # e.g. CXXFLAGS for the location of libstdc++ are honored. The user can choose the libstdc++ by setting
41 # GMX_GPLUSPLUS_PATH, PATH or CMAKE_PREFIX_PATH to make sure the correct the g++ is found.
42 # Gives error if no g++ is found or the g++ found isn't new enough (5.1 is required).
43 # The location of g++ is cached as GMX_GPLUSPLUS_PATH making sure that the same libstdc++ is used
44 # for builds at different times using the same cache file (so that e.g. module loading is
45 # not required for a reproducible build).
47 if (NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND NOT CMAKE_CXX_COMPILER_ID MATCHES "Intel") # Compilers supported
51 # If the compiler isn't using libstdc++ (via its heuristics), then the
52 # compiler is set up to use some other standard library (e.g. libc++
53 # or the MSVC library). If so, then there is nothing to manage. Note
54 # that at this point we don't care *which* libstdc++ (or version) is
56 include(CheckCXXSourceCompiles)
57 check_cxx_source_compiles("#include <new>
58 int main() { return __GLIBCXX__; }" USING_LIBSTDCXX)
60 if (NOT USING_LIBSTDCXX)
64 if (DEFINED GMX_GPLUSGPLUS_PATH)
65 set(EXTRA_MESSAGE ", ignoring the value of GMX_GPLUSPLUS_PATH")
67 string(TOUPPER "${CMAKE_BUILD_TYPE}" _cmake_build_type)
68 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
69 if ("${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${_cmake_build_type}}" MATCHES "--gcc-toolchain")
70 message(STATUS "The --gcc-toolchain option is already present in the CMAKE_CXX_FLAGS "
71 "(or perhaps those specific to the CMAKE_BUILD_TYPE), and the GROMACS build "
72 "will use that one${EXTRA_MESSAGE}.")
74 set(NEED_TO_FIND_GPLUSPLUS TRUE)
77 if ("${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${_cmake_build_type}}" MATCHES "-gcc-name")
78 message(STATUS "The -gcc-name option is already present in the CMAKE_CXX_FLAGS "
79 "(or perhaps those specific to the CMAKE_BUILD_TYPE), and the GROMACS build "
80 "will use that one${EXTRA_MESSAGE}.")
82 set(NEED_TO_FIND_GPLUSPLUS TRUE)
86 if(NEED_TO_FIND_GPLUSPLUS)
87 # Find a gcc (perhaps already specified by the user in
88 # GMX_GPLUSPLUS_PATH) and prepare to reproducibly use its libstdc++.
89 find_program(GMX_GPLUSPLUS_PATH g++)
90 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
92 " Clang supports using libc++ with -DCMAKE_CXX_FLAGS=--stdlib=libc++, and if so there will be no need to find g++.")
94 if (NOT EXISTS "${GMX_GPLUSPLUS_PATH}")
95 message(FATAL_ERROR "Couldn't find g++. Please set GMX_GPLUSPLUS_PATH, PATH or CMAKE_PREFIX_PATH "
96 "accordingly for cmake to find it.${EXTRA_MESSAGE}")
99 # Ensure that a suitable version of g++ was found, caching the
100 # result for future configuration stages.
101 if (NOT GMX_GPLUSPLUS_VERSION)
102 execute_process(COMMAND ${GMX_GPLUSPLUS_PATH} -dumpfullversion -dumpversion OUTPUT_VARIABLE GMX_GPLUSPLUS_VERSION
103 ERROR_VARIABLE GMX_GPLUSPLUS_VERSION_ERROR
104 OUTPUT_STRIP_TRAILING_WHITESPACE)
105 if (NOT "${GMX_GPLUSPLUS_VERSION}" MATCHES "^[0-9]+\\.[0-9]+\\.?[0-9]?$") #Should never happen
106 message(FATAL_ERROR "Couldn't detect g++ version for ${GMX_GPLUSPLUS_PATH}. Version output: ${GMX_GPLUSPLUS_VERSION} "
107 ", error: ${GMX_GPLUSPLUS_VERSION_ERROR}. Please report to developers.${EXTRA_MESSAGE}")
109 # Cache this, so future configurations won't have to run g++ again.
110 set(GMX_GPLUSPLUS_VERSION ${GMX_GPLUSPLUS_VERSION} CACHE STRING "Version of g++ from which libstdc++ is obtained")
112 if (${GMX_GPLUSPLUS_VERSION} VERSION_LESS 5.1)
113 set(FATAL_ERROR_MESSAGE "Found g++ at ${GMX_GPLUSPLUS_PATH}. Its version is ${GMX_GPLUSPLUS_VERSION}. "
114 "GROMACS requires at least version 5.1. "
115 "Please specify a different g++ using GMX_GPLUSPLUS_PATH, PATH or CMAKE_PREFIX_PATH.${EXTRA_MESSAGE}")
116 # Be helpful and don't require the user to unset these manually.
117 unset(GMX_GPLUSPLUS_PATH CACHE)
118 unset(GMX_GPLUSPLUS_VERSION CACHE)
119 message(FATAL_ERROR ${FATAL_ERROR_MESSAGE})
122 # Now make some sanity checks on the compiler using libstdc++.
123 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
124 get_filename_component(GMX_GPLUSPLUS_PATH "${GMX_GPLUSPLUS_PATH}" REALPATH)
125 get_filename_component(GMX_GPLUSPLUS_PATH "${GMX_GPLUSPLUS_PATH}" DIRECTORY) #strip g++
126 get_filename_component(GMX_GPLUSPLUS_PATH "${GMX_GPLUSPLUS_PATH}" DIRECTORY) #strip bin
127 if (NOT EXISTS "${GMX_GPLUSPLUS_PATH}/include/c++")
128 message(FATAL_ERROR "${GMX_GPLUSPLUS_PATH}/include/c++ doesn't exist even though it should. "
129 "Please report to developers.")
132 if (${GMX_GPLUSPLUS_VERSION} VERSION_GREATER_EQUAL 7 AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 19)
133 message(FATAL_ERROR "ICC versions below 19 don't support GCC versions above 6.")
137 # Set up to use the libstdc++ from that g++. Note that we checked
138 # the existing contents of CMAKE_CXX_FLAGS* variables earlier, so
139 # we will not override any user settings here.
140 if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
141 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --gcc-toolchain=${GMX_GPLUSPLUS_PATH}")
143 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -gcc-name=${GMX_GPLUSPLUS_PATH}")
147 # Now run a sanity check on the compiler using libstdc++, regardless
148 # of how it was specified or found.
150 # Test required 2017 standard library features.
151 check_cxx_source_compiles("
152 #include <string_view>
154 int main() { std::string_view(); std::optional<int>(); }" CXX17_COMPILES)
156 if (NOT CXX17_COMPILES)
157 if (NEED_TO_FIND_GPLUSPLUS)
158 set (EXTRA_MESSAGE " The g++ found at ${GMX_GPLUSPLUS_PATH} had a suitable version, so "
159 "something else must be the problem")
161 set (EXTRA_MESSAGE " Check your toolchain documentation or environment flags so that "
162 "they will find a suitable C++17 standard library")
164 message(FATAL_ERROR "GROMACS requires C++17, but a test of such functionality in the C++ standard "
165 "library failed to compile.${EXTRA_MESSAGE}")