Use local variables instead of those in *ir.
[gromacs.git] / CMakeLists.txt
blob4d8a8d4c197d60b36e029081bae340d6d938ad3d
2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2009,2010,2011,2012,2013,2014,2015, 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 cmake_minimum_required(VERSION 2.8.8)
36 # When we require cmake >= 2.8.12, it will provide
37 # CMAKE_MINIMUM_REQUIRED_VERSION automatically, but in the meantime we
38 # need to set a variable, and it must have a different name.
39 set(GMX_CMAKE_MINIMUM_REQUIRED_VERSION "2.8.8")
41 # CMake modules/macros are in a subdirectory to keep this file cleaner
42 # This needs to be set before project() in order to pick up toolchain files
43 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Platform)
45 project(Gromacs)
47 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
48 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
49 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
51 # Set up common version variables, as well as general information about
52 # the build tree (whether the build is from a source package or from a git
53 # repository).  Also declares a few functions that will be used for generating
54 # version info files later.
55 include(gmxVersionInfo)
57 if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND UNIX)
58     set(CMAKE_INSTALL_PREFIX "/usr/local/gromacs" CACHE STRING "Installation prefix (installation will need write permissions here)" FORCE)
59 endif()
60 if("${CMAKE_INSTALL_PREFIX}" STREQUAL "${CMAKE_BINARY_DIR}")
61     message(FATAL_ERROR "GROMACS cannot be installed into the build tree, choose a different location for CMAKE_INSTALL_PREFIX")
62 endif()
64 include(gmxBuildTypeReference)
65 include(gmxBuildTypeProfile)
66 include(gmxBuildTypeTSAN)
67 include(gmxBuildTypeASAN)
68 include(gmxBuildTypeMSAN)
69 include(gmxBuildTypeReleaseWithAssert)
71 if(NOT CMAKE_BUILD_TYPE)
72     set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel Reference RelWithAssert Profile." FORCE)
73     # There's no need to offer a user the choice of ThreadSanitizer
74     # Set the possible values of build type for cmake-gui
75     set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
76         "MinSizeRel" "RelWithDebInfo" "Reference" "RelWithAssert" "Profile")
77 endif()
78 if(CMAKE_CONFIGURATION_TYPES)
79     # Add appropriate GROMACS-specific build types for the Visual
80     # Studio generator (Debug, Release, MinSizeRel and RelWithDebInfo
81     # are already present by default).
82     list(APPEND CMAKE_CONFIGURATION_TYPES "RelWithAssert" "Reference")
83     list(REMOVE_DUPLICATES CMAKE_CONFIGURATION_TYPES)
84     set(CMAKE_CONFIGURATION_TYPES "${CMAKE_CONFIGURATION_TYPES}" CACHE STRING
85         "List of configuration types"
86         FORCE)
87 endif()
88 set(build_types_with_explicit_flags RELEASE DEBUG RELWITHDEBINFO RELWITHASSERT MINSIZEREL PROFILE)
90 set_property(GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS ON)
92 # Set a default valgrind suppression file.
93 # This unfortunately needs to duplicate information from CTest to work as
94 # expected...
95 set(MEMORYCHECK_SUPPRESSIONS_FILE
96     "${CMAKE_SOURCE_DIR}/cmake/legacy_and_external.supp"
97     CACHE FILEPATH
98     "File that contains suppressions for the memory checker")
99 include(CTest)
101 include(gmxCPackUtilities)
102 gmx_cpack_init()
104 # Variables that accumulate stuff influencing the installed headers
105 set(INSTALLED_HEADER_INCLUDE_DIRS "")
106 set(INSTALLED_HEADER_DEFINITIONS "")
108 ########################################################################
109 # Check and warn if cache generated on a different host is being reused
110 ########################################################################
111 if(CMAKE_HOST_UNIX)
112     execute_process(COMMAND hostname
113                     OUTPUT_VARIABLE TMP_HOSTNAME
114                     OUTPUT_STRIP_TRAILING_WHITESPACE)
115     if(GMX_BUILD_HOSTNAME AND NOT "${GMX_BUILD_HOSTNAME}" STREQUAL "${TMP_HOSTNAME}")
116         message(WARNING "
117             The CMake cache, probably generated on a different host (${GMX_BUILD_HOSTNAME}),
118             is being reused! This could lead to inconsistencies; therefore, it is
119             recommended to regenerate the cache!")
120     endif()
121     set(GMX_BUILD_HOSTNAME "${TMP_HOSTNAME}" CACHE INTERNAL
122             "Hostname of the machine where the cache was generated.")
123 endif()
125 ########################################################################
126 # Detect architecture before setting options so we can alter defaults
127 ########################################################################
128 # Detect the architecture the compiler is targetting, detect
129 # SIMD instructions possibilities on that hardware, suggest SIMD instruction set
130 # to use if none is specified, and populate the cache option for CPU
131 # SIMD.
132 include(gmxDetectTargetArchitecture)
133 gmx_detect_target_architecture()
135 ########################################################################
136 # User input options                                                   #
137 ########################################################################
138 include(gmxOptionUtilities)
140 set(CMAKE_PREFIX_PATH "" CACHE STRING "Extra locations to search for external libraries and tools (give directory without lib, bin, or include)")
142 # Fujitsu only has SIMD in double precision, so this will be faster
143 gmx_set_boolean(GMX_DOUBLE_DEFAULT GMX_TARGET_FUJITSU_SPARC64)
144 option(GMX_DOUBLE "Use double precision (much slower, use only if you really need it)" ${GMX_DOUBLE_DEFAULT})
145 option(GMX_RELAXED_DOUBLE_PRECISION "Accept single precision 1/sqrt(x) when using Fujitsu HPC-ACE SIMD" OFF)
146 mark_as_advanced(GMX_RELAXED_DOUBLE_PRECISION)
148 option(GMX_MPI    "Build a parallel (message-passing) version of GROMACS" OFF)
149 option(GMX_THREAD_MPI  "Build a thread-MPI-based multithreaded version of GROMACS (not compatible with MPI)" ON)
150 gmx_dependent_option(
151     GMX_MPI_IN_PLACE
152     "Enable MPI_IN_PLACE for MPIs that have it defined"
153     ON
154     GMX_MPI)
155 mark_as_advanced(GMX_MPI_IN_PLACE)
156 option(GMX_SOFTWARE_INVSQRT "Use GROMACS software 1/sqrt" ON)
157 mark_as_advanced(GMX_SOFTWARE_INVSQRT)
158 option(GMX_FAHCORE "Build a library with mdrun functionality" OFF)
159 mark_as_advanced(GMX_FAHCORE)
161 option(GMX_COOL_QUOTES "Enable GROMACS cool quotes" ON)
162 mark_as_advanced(GMX_COOL_QUOTES)
163 gmx_add_cache_dependency(GMX_COOL_QUOTES BOOL "NOT GMX_FAHCORE" OFF)
165 option(GMX_USE_OPENCL "Enable OpenCL acceleration" OFF)
167 # Decide on GPU settings based on user-settings and GPU/CUDA detection.
168 # GCC 4.6 requires CUDA 5.0 and VS2013 requires CUDA 6.5
169 if(MSVC)
170     set(REQUIRED_CUDA_VERSION 6.5)
171 else()
172     set(REQUIRED_CUDA_VERSION 5.0)
173 endif()
174 set(REQUIRED_CUDA_COMPUTE_CAPABILITY 2.0)
176 # OpenCL required version: 1.1 or newer
177 set(REQUIRED_OPENCL_MIN_VERSION 1.1)
179 if(NOT GMX_USE_OPENCL)
180     # CUDA detection is done only if GMX_USE_OPENCL is OFF
181     include(gmxManageGPU)
182 else()
183     #Now the OpenCL path
184     if(GMX_GPU)
185         include(gmxManageOpenCL)
186     else(GMX_GPU)
187         message(FATAL_ERROR "OpenCL requested but GPU option is not enabled (try -DGMX_GPU=on) ")
188     endif(GMX_GPU)
189 endif()
191 include(gmxDetectSimd)
192 gmx_detect_simd(GMX_SUGGESTED_SIMD)
194 gmx_option_multichoice(
195     GMX_SIMD
196     "SIMD instruction set for CPU kernels and compiler optimization"
197     "${GMX_SUGGESTED_SIMD}"
198     None SSE2 SSE4.1 AVX_128_FMA AVX_256 AVX2_256 AVX_512F AVX_512ER MIC ARM_NEON ARM_NEON_ASIMD IBM_QPX IBM_VMX IBM_VSX Sparc64_HPC_ACE Reference)
200 if(GMX_TARGET_MIC)
201     set(GMX_FFT_LIBRARY_DEFAULT "mkl")
202 else()
203     set(GMX_FFT_LIBRARY_DEFAULT "fftw3")
204 endif()
206 gmx_option_multichoice(
207     GMX_FFT_LIBRARY
208     "FFT library"
209     "${GMX_FFT_LIBRARY_DEFAULT}"
210     fftw3 mkl "fftpack[built-in]")
211 gmx_dependent_option(
212     GMX_BUILD_OWN_FFTW
213     "Download and build FFTW 3 during the GROMACS build process, rather than fall back on the really slow fftpack."
214     OFF
215     "GMX_FFT_LIBRARY STREQUAL FFTW3")
216 gmx_dependent_option(
217     GMX_DISABLE_FFTW_MEASURE
218     "Do not optimize FFTW setups (not needed with SSE)"
219     OFF
220     "GMX_FFT_LIBRARY STREQUAL FFTW3")
221 mark_as_advanced(GMX_BUILD_OWN_FFTW)
222 mark_as_advanced(GMX_DISABLE_FFTW_MEASURE)
224 gmx_option_multichoice(
225     GMX_QMMM_PROGRAM
226     "QM package for QM/MM"
227     None
228     none gaussian mopac gamess orca)
230 gmx_dependent_cache_variable(GMX_SIMD_REF_FLOAT_WIDTH  "Reference SIMD single precision width" STRING "4" "GMX_SIMD STREQUAL REFERENCE")
231 gmx_dependent_cache_variable(GMX_SIMD_REF_DOUBLE_WIDTH "Reference SIMD double precision width" STRING "2" "GMX_SIMD STREQUAL REFERENCE")
233 # This should be moved to a separate NBNXN cmake module when that code is cleaned up and modularized
235 option(GMX_BROKEN_CALLOC "Work around broken calloc()" OFF)
236 mark_as_advanced(GMX_BROKEN_CALLOC)
237 option(GMX_LOAD_PLUGINS "Compile with plugin support, needed to read VMD supported file formats" ON)
238 mark_as_advanced(GMX_LOAD_PLUGINS)
240 option(GMX_GPU  "Enable GPU acceleration" ON)
241 option(GMX_OPENMP "Enable OpenMP-based multithreading" ON)
243 option(GMX_USE_TNG "Use the TNG library for trajectory I/O" ON)
245 option(GMX_BUILD_MDRUN_ONLY "Build and install only the mdrun binary" OFF)
247 option(GMX_CYCLE_SUBCOUNTERS "Enable cycle subcounters to get a more detailed cycle timings" OFF)
248 mark_as_advanced(GMX_CYCLE_SUBCOUNTERS)
250 option(GMX_SKIP_DEFAULT_CFLAGS "Don't automatically add suggested/required Compiler flags." OFF)
251 mark_as_advanced(GMX_SKIP_DEFAULT_CFLAGS)
253 option(GMX_BUILD_FOR_COVERAGE
254        "Tune build for better code coverage metrics (e.g., disable asserts)"
255        OFF)
256 mark_as_advanced(GMX_BUILD_FOR_COVERAGE)
258 option(GMX_DEVELOPER_BUILD
259     "Enable Developer convenience features: always build unit-tests"
260     OFF)
261 mark_as_advanced(GMX_DEVELOPER_BUILD)
263 gmx_set_boolean(GMX_COMPILER_WARNINGS_DEFAULT "NOT SOURCE_IS_SOURCE_DISTRIBUTION")
264 option(GMX_COMPILER_WARNINGS
265     "Enable a default set of compiler warnings"
266     ${GMX_COMPILER_WARNINGS_DEFAULT})
267 mark_as_advanced(GMX_COMPILER_WARNINGS)
268 # Always turn on compiler warnings with a developer build.
269 gmx_add_cache_dependency(GMX_COMPILER_WARNINGS BOOL "NOT GMX_DEVELOPER_BUILD" ON)
271 option(GMX_BUILD_SHARED_EXE
272     "Build exectuables as shared binaries. If not set, this disables rpath and dynamic linker flags in an attempt to build a static binary, but this may require setting up the toolchain properly and making appropriate libraries available."
273     ON)
274 mark_as_advanced(GMX_BUILD_SHARED_EXE)
276 ######################################################################
277 # Detect OpenMP support
278 ######################################################################
279 # The OpenMP detection _must_ come before tests for other CFLAGS.
280 include(gmxManageOpenMP)
284 ######################################################################
285 # Compiler tests
286 # These need to be done early (before further tests).
287 #####################################################################
289 # The cmake/Check{C,CXX}CompilerFlag.cmake files in the GROMACS distribution
290 # are used with permission from CMake v3.0.0 so that GROMACS can detect
291 # invalid options with the Intel Compilers, and we have added a line
292 # to detect warnings with the Fujitsu compilers on K computer and ICC.
293 # CMake-3.0 also has a bug where the FAIL_REGEX pattern for AIX contains
294 # a semicolon. Since this is also used as a separator in lists inside CMake,
295 # that string ends up being split into two separate patterns, and the last
296 # part is just a single word that also matches other messages. We solved this
297 # by replacing the semicolon with a period that matches any character.
299 # These files should be removed from the source tree when a CMake version that
300 # includes the features in question becomes required for building GROMACS.
301 include(CheckCCompilerFlag)
302 include(CheckCXXCompilerFlag)
304 include(gmxCFlags)
305 gmx_c_flags()
307 # This variable should be used for additional compiler flags which are not
308 # generated in gmxCFlags nor are SIMD or MPI related.
309 set(EXTRA_C_FLAGS "")
310 set(EXTRA_CXX_FLAGS "")
312 # Run through a number of tests for buggy compilers and other issues
313 include(gmxTestCompilerProblems)
314 gmx_test_compiler_problems()
315 # GMX_SIMD will not be set automatically until the second
316 # pass (which is not strictly guaranteed to occur), so putting this
317 # check here among logically-related tests is inefficient, but the
318 # potential loss is likely zero.
319 if(GMX_SIMD STREQUAL "AVX_256"
320         AND CMAKE_COMPILER_IS_GNUCC
321         AND (C_COMPILER_VERSION VERSION_EQUAL "4.6.1"
322             OR CXX_COMPILER_VERSION VERSION_EQUAL "4.6.1"))
323     message(FATAL_ERROR "gcc 4.6.1 has buggy support for AVX, and GROMACS mdrun will not work. If you want simulation performance, use a more recent compiler. Otherwise, use GMX_SIMD=SSE4.1")
324     # See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49002
325 endif()
328 if(GMX_DOUBLE)
329     add_definitions(-DGMX_DOUBLE)
330     list(APPEND INSTALLED_HEADER_DEFINITIONS "-DGMX_DOUBLE")
331     if(GMX_RELAXED_DOUBLE_PRECISION)
332         add_definitions(-DGMX_RELAXED_DOUBLE_PRECISION)
333     endif()
334 endif()
335 if(GMX_SOFTWARE_INVSQRT)
336     list(APPEND INSTALLED_HEADER_DEFINITIONS "-DGMX_SOFTWARE_INVSQRT")
337 endif()
339 if(WIN32)
340     list(APPEND GMX_EXTRA_LIBRARIES "wsock32")
341     add_definitions(-DGMX_HAVE_WINSOCK)
342 endif()
346 ########################################################################
347 # Basic system tests (standard libraries, headers, functions, types)   #
348 ########################################################################
349 include(CheckIncludeFiles)
350 include(CheckIncludeFileCXX)
351 check_include_files(unistd.h     HAVE_UNISTD_H)
352 check_include_files(pwd.h        HAVE_PWD_H)
353 check_include_files(dirent.h     HAVE_DIRENT_H)
354 check_include_files(time.h       HAVE_TIME_H)
355 check_include_files(sys/time.h   HAVE_SYS_TIME_H)
356 check_include_files(io.h         HAVE_IO_H)
357 check_include_files(sched.h      HAVE_SCHED_H)
359 check_include_files(regex.h      HAVE_POSIX_REGEX)
360 # TODO: It could be nice to inform the user if no regex support is found,
361 # as selections won't be fully functional.
363 include(CheckCXXSymbolExists)
364 check_cxx_symbol_exists(gettimeofday      sys/time.h   HAVE_GETTIMEOFDAY)
365 check_cxx_symbol_exists(sysconf           unistd.h     HAVE_SYSCONF)
366 check_cxx_symbol_exists(nice              unistd.h     HAVE_NICE)
367 check_cxx_symbol_exists(fsync             unistd.h     HAVE_FSYNC)
368 check_cxx_symbol_exists(_fileno           stdio.h      HAVE__FILENO)
369 check_cxx_symbol_exists(fileno            stdio.h      HAVE_FILENO)
370 check_cxx_symbol_exists(_commit           io.h         HAVE__COMMIT)
371 check_cxx_symbol_exists(sigaction         signal.h     HAVE_SIGACTION)
372 check_cxx_symbol_exists(rsqrt             math.h       HAVE_RSQRT)
373 check_cxx_symbol_exists(rsqrtf            math.h       HAVE_RSQRTF)
374 check_cxx_symbol_exists(sqrtf             math.h       HAVE_SQRTF)
376 include(CheckLibraryExists)
377 find_library(HAVE_LIBM m)
378 check_library_exists(rt clock_gettime "" HAVE_CLOCK_GETTIME)
379 check_library_exists(m feenableexcept "" HAVE_FEENABLEEXCEPT)
381 include(TestSchedAffinity)
382 test_sched_affinity(HAVE_SCHED_AFFINITY)
384 # Aligned memory allocation. We need to check for both mm_malloc(),
385 # posix_memalign(), memalign(), and on windows also _aligned_malloc()
386 include(gmxTestMMMalloc)
387 gmx_test_mm_malloc(HAVE__MM_MALLOC)
388 check_cxx_symbol_exists(posix_memalign    stdlib.h     HAVE_POSIX_MEMALIGN)
389 check_cxx_symbol_exists(memalign          stdlib.h     HAVE_MEMALIGN)
390 if(MSVC)
391     # No need to waste time on this test on platforms where it will never be true
392     check_cxx_symbol_exists(_aligned_malloc   stdlib.h     HAVE__ALIGNED_MALLOC)
393 endif()
395 include(TestBigEndian)
396 test_big_endian(GMX_INTEGER_BIG_ENDIAN)
398 gmx_set_boolean(GMX_USE_NICE "HAVE_UNISTD_H AND HAVE_NICE")
400 # Management of GROMACS options for specific toolchains should go
401 # here. Because the initial settings for some of the main options have
402 # already happened, but things like library detection and MPI compiler
403 # feature detection have not, the docstrings for any over-rides of
404 # GROMACS defaults or user settings will make sense. Also, any
405 # toolchain-related reasons for choosing whether to detect various
406 # things can be sorted out now, before the detection takes place.
407 if(${CMAKE_SYSTEM_NAME} MATCHES BlueGene)
408     include(gmxManageBlueGene)
409 endif()
411 if(GMX_TARGET_FUJITSU_SPARC64)
412     include(gmxManageFujitsuSparc64)
413 endif()
415 ########################################################################
416 #Process MPI settings
417 ########################################################################
418 include(gmxManageMPI)
421 ########################################################################
422 #Process shared/static library settings
423 ########################################################################
424 include(gmxManageSharedLibraries)
427 ########################################################################
428 # Find external packages                                               #
429 ########################################################################
431 # TNG wants zlib if it is available. And static libxml2 might have a dependency
432 find_package(ZLIB QUIET)
433 include(gmxTestZLib)
434 gmx_test_zlib(HAVE_ZLIB)
436 # Unconditionally find the package, as it is also required for unit
437 # tests. This exports LIBXML2_FOUND, which we should not use because
438 # it does not tell us that linking will succeed. Instead, we test that
439 # next.
440 if(DEFINED LIBXML2_LIBRARIES)
441   set(LibXml2_FIND_QUIETLY TRUE)
442 endif()
443 find_package(LibXml2)
444 include(gmxTestLibXml2)
445 gmx_test_libxml2(HAVE_LIBXML2)
446 option(GMX_XML "Use libxml2 to parse xml files (currently has no effect)" ${HAVE_LIBXML2})
447 set(PKG_XML "")
448 mark_as_advanced(GMX_XML)
449 # Don't actually do anything, since libxml2 is currently not used by libgromacs
450 #if(GMX_XML AND NOT HAVE_LIBXML2)
451 #    message(FATAL_ERROR "libxml2 not found. Set GMX_XML=OFF to compile without XML support")
452 #endif()
453 #if(GMX_XML)
454 #    include_directories(${LIBXML2_INCLUDE_DIR})
455 #    set(PKG_XML libxml-2.0)
456 #    set(XML_LIBRARIES ${LIBXML2_LIBRARIES})
457 #endif()
459 option(GMX_EXTRAE "Add support for tracing using EXTRAE" OFF)
460 mark_as_advanced(GMX_EXTRAE)
462 if (GMX_EXTRAE)
463   find_package(EXTRAE)
464   if(EXTRAE_FOUND)
465     include_directories(SYSTEM ${EXTRAE_INCLUDE_DIR})
466     set(HAVE_EXTRAE 1)
467   else()
468     message(FATAL_ERROR "EXTRAE library was not found. Please add the correct path to CMAKE_PREFIX_PATH")
469   endif()
470 endif()
472 option(GMX_X11 "Use X window system" OFF)
473 if (GMX_X11)
474     find_package(X11)
475     # X11 includes/libraries are only set in the ngmx subdirectory!
476     if(NOT X11_FOUND)
477         message(FATAL_ERROR
478                 "X11 include files and/or libraries were not found. "
479                 "Set GMX_X11=OFF to compile without X11 support. "
480                 "gmx view will not be available.")
481     endif()
482     include_directories(SYSTEM ${X11_INCLUDE_DIR})
483 endif()
485 include(ThreadMPI)
486 # Enable core threading facilities
487 tmpi_enable_core("${CMAKE_SOURCE_DIR}/src/external/thread_mpi/include")
488 # Enable tMPI C++ support
489 tmpi_enable_cxx()
490 if(GMX_THREAD_MPI)
491     # enable MPI functions
492     tmpi_enable()
493     set(MPI_IN_PLACE_EXISTS 1)
494 endif()
495 # If atomics are manually disabled a define is needed because atomics.h doesn't depend on config.h
496 if (TMPI_ATOMICS_DISABLED)
497    add_definitions(-DTMPI_ATOMICS_DISABLED)
498 endif()
500 if(GMX_GPU)
501     # now that we have detected the dependencies, do the second configure pass
502     gmx_gpu_setup()
503 endif()
505 if(CYGWIN)
506     set(GMX_CYGWIN 1)
507 endif()
509 if(WIN32)
510     set(GMX_NATIVE_WINDOWS 1)
511     # This makes windows.h not declare min/max as macros that would break
512     # C++ code using std::min/std::max.
513     add_definitions(-DNOMINMAX)
514 endif()
516 if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") #Work-around for cmake bug #10837
517     if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Intel" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
518         set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
519     endif()
520     if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Intel" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
521         set(CMAKE_INCLUDE_SYSTEM_FLAG_CXX "-isystem ")
522     endif()
523 endif()
525 if(NOT DEFINED GMX_BUILD_UNITTESTS AND NOT HAVE_LIBXML2)
526     message(WARNING "libxml2 not found. Will build GROMACS without unit-tests. This is not recommended, because the unit-tests help to verify that GROMACS functions correctly. Most likely you are missing the libxml2-dev(el) package. After you installed it, set GMX_BUILD_UNITTESTS=ON.")
527 endif()
528 option(GMX_BUILD_UNITTESTS "Build unit tests with BUILD_TESTING (uses Google C++ Testing and Mocking Frameworks, requires libxml2)" ${HAVE_LIBXML2})
529 mark_as_advanced(GMX_BUILD_UNITTESTS)
530 gmx_add_cache_dependency(GMX_BUILD_UNITTESTS BOOL BUILD_TESTING OFF)
531 if (GMX_BUILD_UNITTESTS AND NOT HAVE_LIBXML2)
532     message(FATAL_ERROR
533         "Cannot build unit tests without libxml2. "
534         "Either set GMX_BUILD_UNITTESTS=OFF or tell CMake how to find a working version of libxml2.")
535 endif()
537 ########################################################################
538 # Our own GROMACS tests
539 ########################################################################
541 add_definitions( -DHAVE_CONFIG_H )
542 include_directories(BEFORE ${CMAKE_SOURCE_DIR}/src)
543 include_directories(BEFORE ${CMAKE_SOURCE_DIR}/src/external/thread_mpi/include)
544 # Required for config.h, maybe should only be set in src/CMakeLists.txt
545 include_directories(BEFORE ${CMAKE_BINARY_DIR}/src)
547 include(gmxTestInlineASM)
548 gmx_test_inline_asm_gcc_x86(GMX_X86_GCC_INLINE_ASM)
550 include(gmxSetBuildInformation)
551 gmx_set_build_information()
552 # Turn on RDTSCP if:
553 # - the build system's CPU supports it
554 # - the acceleration is set to AVX as all AVX-capable CPUs support AVX (which
555 #   at this point means that the user set it).
556 # Note: it's better to not use the later set value of GMX_SIMD because
557 # it reflects the system's capability of both compiling and running AVX code.
558 # TODO: After merge with 5.0 one could implement a cache variable dependency
559 # such that GMX_USE_RDTSCP can change if GMX_SIMD is changed to AVX
560 # after the first cmake pass.
561 if (BUILD_CPU_FEATURES MATCHES "rdtscp" OR GMX_SIMD MATCHES "AVX")
562     set(GMX_USE_RDTSCP_DEFAULT_VALUE ON)
563 else()
564     set(GMX_USE_RDTSCP_DEFAULT_VALUE OFF)
565 endif()
566 option(GMX_USE_RDTSCP "Use RDTSCP for better CPU-based timers (available on recent x86 CPUs; might need to be off when compiling for heterogeneous environments)" ${GMX_USE_RDTSCP_DEFAULT_VALUE})
567 mark_as_advanced(GMX_USE_RDTSCP)
568 if(GMX_USE_RDTSCP)
569     set(HAVE_RDTSCP 1)
570 endif()
572 include(gmxTestLargeFiles)
573 gmx_test_large_files(GMX_LARGEFILES)
575 include(gmxTestSignal)
576 gmx_test_sigusr1(HAVE_SIGUSR1)
578 include(gmxTestPipes)
579 gmx_test_pipes(HAVE_PIPES)
581 include(gmxTestCXX11)
582 gmx_test_cxx11(GMX_CXX11_SUPPORTED GMX_CXX11_FLAGS)
583 if(NOT GMX_CXX11_SUPPORTED)
584     message(FATAL_ERROR "This version of GROMACS requires C++11. Please use a newer compiler or use the GROMACS 5.1.x release. Note it might be sufficient to instruct the compiler to use a newer STL version. See the installation guide for details.")
585 endif()
586 set(EXTRA_CXX_FLAGS "${EXTRA_CXX_FLAGS} ${GMX_CXX11_FLAGS}")
588 # Now we can test for CXX11_REGEX include file
589 check_include_file_cxx(regex     HAVE_CXX11_REGEX)
591 include(gmxTestXDR)
592 gmx_test_xdr(GMX_SYSTEM_XDR)
593 if(NOT GMX_SYSTEM_XDR)
594     set(GMX_INTERNAL_XDR 1)
595 endif()
598 ##################################################
599 # Process SIMD instruction settings
600 ##################################################
601 # This checks what flags to add in order to
602 # support the SIMD instructions we need, it sets
603 # correct defines for the SIMD instructions supported,
604 # and adds advanced options to control accuracy
605 # for SIMD math operations.
606 include(gmxManageSimd)
607 gmx_manage_simd()
609 # Process QM/MM Settings
610 if(${GMX_QMMM_PROGRAM} STREQUAL "GAUSSIAN")
611     set(GMX_QMMM_GAUSSIAN 1)
612 elseif(${GMX_QMMM_PROGRAM} STREQUAL "MOPAC")
613     set(GMX_QMMM_MOPAC 1)
614 elseif(${GMX_QMMM_PROGRAM} STREQUAL "GAMESS")
615     set(GMX_QMMM_GAMESS 1)
616 elseif(${GMX_QMMM_PROGRAM} STREQUAL "ORCA")
617     set(GMX_QMMM_ORCA 1)
618 elseif(${GMX_QMMM_PROGRAM} STREQUAL "NONE")
619     # nothing to do
620 else()
621     gmx_invalid_option_value(GMX_QMMM_PROGRAM)
622 endif()
625 ##################################################
626 # Process FFT library settings
627 ##################################################
628 include(gmxManageFFTLibraries)
631 include(gmxManageLinearAlgebraLibraries)
633 # Whether GROMACS will really try to compile support for VMD plugins
634 set(GMX_USE_PLUGINS OFF)
636 if(GMX_LOAD_PLUGINS)
637   if(NOT WIN32)
638     # Native Windows does not have, nor need dlopen
639     include(gmxTestdlopen)
640     gmx_test_dlopen(HAVE_DLOPEN)
641   endif()
643   # so, should we use plug-ins?
644   if(WIN32 OR (HAVE_DLOPEN AND BUILD_SHARED_LIBS))
645     if(NOT VMD_QUIETLY)
646       MESSAGE(STATUS "Using dynamic plugins (e.g VMD-supported file formats)")
647     endif()
648     if(NOT GMX_VMD_PLUGIN_PATH)
649       find_package(VMD)
650     endif()
651     set(GMX_USE_PLUGINS ON)
652     list(APPEND GMX_EXTRA_LIBRARIES ${CMAKE_DL_LIBS}) # magic cross-platform pre-set variable for dlopen library
653     set(PKG_DL_LIBS "-l${CMAKE_DL_LIBS}")
654   else()
655     set(PKG_DL_LIBS)
656   endif()
657 endif()
658 set(VMD_QUIETLY TRUE CACHE INTERNAL "")
660 # Link real-time library for POSIX timers. The check for clock_gettime
661 # confirms the linkability of rt.
662 if(HAVE_TIME_H AND HAVE_UNISTD_H AND HAVE_CLOCK_GETTIME)
663     list(APPEND GMX_EXTRA_LIBRARIES rt)
664 endif()
666 # Math and thread libraries must often come after all others when linking...
667 if(HAVE_LIBM)
668     list(APPEND GMX_EXTRA_LIBRARIES m)
669 endif()
671 option(GMX_NACL "Configure for Native Client builds" OFF)
672 if (GMX_NACL)
673   list(APPEND GMX_EXTRA_LIBRARIES nosys)
674   set(GMX_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lnosys")
675   # TODO: Is this still necessary with the check for its presence?
676   set(GMX_USE_NICE 0)
677   set(GMX_NO_RENAME 1)
678 endif()
679 mark_as_advanced(GMX_NACL)
681 if(GMX_FAHCORE)
682   set(COREWRAP_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/../corewrap" CACHE STRING
683       "Path to swindirect.h")
684   include_directories(${COREWRAP_INCLUDE_DIR})
685 endif()
687 # Value of GMX_BUILD_HELP=AUTO tries to generate things, but will only
688 # produce warnings if that fails.
689 set(build_help_default AUTO)
690 if (SOURCE_IS_SOURCE_DISTRIBUTION OR CMAKE_CROSSCOMPILING)
691     set(build_help_default OFF)
692 endif()
693 gmx_option_trivalue(GMX_BUILD_HELP "Build completions automatically (requires that compiled binaries can be executed on the build host) and install man pages if built (requires building the 'man' target manually)" ${build_help_default})
694 mark_as_advanced(GMX_BUILD_HELP)
695 if (GMX_BUILD_HELP AND SOURCE_IS_SOURCE_DISTRIBUTION AND BUILD_IS_INSOURCE)
696     message(FATAL_ERROR
697         "Rebuilding shell completions or man pages is not supported for "
698         "in-source builds from a source distribution. "
699         "Set GMX_BUILD_HELP=OFF or do an out-of-source build to proceed.")
700 endif()
702 # # # # # # # # # # NO MORE TESTS AFTER THIS LINE! # # # # # # # # # # #
703 # these are set after everything else
704 if (NOT GMX_SKIP_DEFAULT_CFLAGS)
705     set(CMAKE_C_FLAGS "${SIMD_C_FLAGS} ${MPI_COMPILE_FLAGS} ${EXTRA_C_FLAGS} ${CMAKE_C_FLAGS}")
706     set(CMAKE_CXX_FLAGS "${SIMD_CXX_FLAGS} ${MPI_COMPILE_FLAGS} ${EXTRA_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
707     set(CMAKE_EXE_LINKER_FLAGS "${FFT_LINKER_FLAGS} ${MPI_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}")
708     set(CMAKE_SHARED_LINKER_FLAGS "${FFT_LINKER_FLAGS} ${MPI_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS}")
709 else()
710     message("Recommended flags which are not added because GMX_SKIP_DEFAULT_CFLAGS=yes:")
711     message("CMAKE_C_FLAGS: ${SIMD_C_FLAGS} ${MPI_COMPILE_FLAGS} ${EXTRA_C_FLAGS} ${GMXC_CFLAGS}")
712     message("CMAKE_C_FLAGS_RELEASE: ${GMXC_CFLAGS_RELEASE}")
713     message("CMAKE_C_FLAGS_DEBUG: ${GMXC_CFLAGS_DEBUG}")
714     message("CMAKE_CXX_FLAGS: ${SIMD_CXX_FLAGS} ${MPI_COMPILE_FLAGS} ${EXTRA_CXX_FLAGS} ${GMXC_CXXFLAGS}")
715     message("CMAKE_CXX_FLAGS_RELEASE: ${GMXC_CXXFLAGS_RELEASE}")
716     message("CMAKE_CXX_FLAGS_DEBUG: ${GMXC_CXXFLAGS_DEBUG}")
717     message("CMAKE_EXE_LINKER_FLAGS: ${FFT_LINKER_FLAGS} ${MPI_LINKER_FLAGS}")
718     message("CMAKE_SHARED_LINKER_FLAGS: ${MPI_LINKER_FLAGS}")
719 endif()
721 if(NOT GMX_OPENMP)
722     #Unset all OpenMP flags in case OpenMP was disabled either by the user
723     #or because it was only partially detected (e.g. only for C but not C++ compiler)
724     unset(OpenMP_C_FLAGS CACHE)
725     unset(OpenMP_CXX_FLAGS CACHE)
726 else()
727     set(GMX_EXE_LINKER_FLAGS ${GMX_EXE_LINKER_FLAGS} ${OpenMP_LINKER_FLAGS})
728     set(GMX_SHARED_LINKER_FLAGS ${GMX_SHARED_LINKER_FLAGS} ${OpenMP_SHARED_LINKER_FLAGS})
729 endif()
731 ########################################################################
732 # Specify install locations
733 ########################################################################
734 # Use GNUInstallDirs to set paths on multiarch systems.
735 include(GNUInstallDirs)
737 # Customization for the installation tree paths.
738 set(GMX_LIB_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR} CACHE STRING
739     "Library installation directory (default: ${CMAKE_INSTALL_LIBDIR})")
740 set(GMX_DATA_INSTALL_DIR gromacs CACHE STRING
741     "Data installation directory under share/ (default: gromacs)")
742 mark_as_advanced(GMX_LIB_INSTALL_DIR GMX_DATA_INSTALL_DIR)
744 # These variables are used internally to provide a central location for
745 # customizing the install locations.
746 set(LIB_INSTALL_DIR       ${GMX_LIB_INSTALL_DIR})
747 set(BIN_INSTALL_DIR       bin)
748 # This variable also gets written into config.h for use in finding the data
749 # directories.
750 set(DATA_INSTALL_DIR      share/${GMX_DATA_INSTALL_DIR})
751 set(MAN_INSTALL_DIR       share/man)
752 # If the nesting level wrt. the installation root is changed,
753 # gromacs-config.cmake.cmakein needs to be adapted.
754 set(CMAKE_INSTALL_DIR     share/cmake)
755 # TODO: Make GMXRC adapt if this is changed
756 set(PKGCONFIG_INSTALL_DIR ${LIB_INSTALL_DIR}/pkgconfig)
757 set(OCL_INSTALL_DIR       ${DATA_INSTALL_DIR}/opencl)
758 set(INCL_INSTALL_DIR      include)
760 list(APPEND INSTALLED_HEADER_INCLUDE_DIRS ${INCL_INSTALL_DIR})
762 # Binary and library suffix options
763 include(gmxManageSuffixes)
765 ################################################################
766 # Shared library load path settings
767 ################################################################
768 # CMake supports RPATH on OS X only from 2.8.12 upwards.
769 # CMAKE_SYSTEM_VERSION > 8.0 matches OS X 10.5 and above, where RPATH support
770 # was added.
772 if(NOT GMX_BUILD_SHARED_EXE)
773     # No rpath
774     set(CMAKE_SKIP_RPATH TRUE)
775     set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) # remove -Wl,-Bdynamic
776     set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS)
777 elseif((NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") OR
778    ((CMAKE_SYSTEM_VERSION VERSION_GREATER 8.0) AND (NOT CMAKE_VERSION VERSION_LESS 2.8.12)))
779     # The build folder always has bin/ and lib/; if we are also going to
780     # install to lib/, then the installation RPATH works also in the build
781     # tree.  This makes installation slightly faster (no need to rewrite the
782     # RPATHs), and makes the binaries in the build tree relocatable.
783     if(GMX_LIB_INSTALL_DIR STREQUAL "lib")
784         set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
785     endif()
786     # Set the RPATH as relative to the executable location to make the
787     # binaries relocatable.
788     if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
789         set(CMAKE_INSTALL_RPATH "\$ORIGIN/../${GMX_LIB_INSTALL_DIR}")
790     else()
791         set(CMAKE_INSTALL_RPATH "@executable_path/../${GMX_LIB_INSTALL_DIR}")
792     endif()
793     set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
794     set(CMAKE_MACOSX_RPATH 1)
795 else()
796     # We are on Darwin/OSX, and CMake cannot handle RPATHs automatically.
797     if(CMAKE_SYSTEM_VERSION VERSION_GREATER 8.0)
798         # Set the RPATH options manually.
799         set(CMAKE_INSTALL_NAME_DIR "@rpath")
800         set(GMX_EXE_LINKER_FLAGS ${GMX_EXE_LINKER_FLAGS} "-Wl,-rpath,@executable_path/../${GMX_LIB_INSTALL_DIR}")
801     else()
802         # Use the old INSTALL_NAME_DIR mechanism if RPATH is not supported.
803         set(CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/${LIB_INSTALL_DIR}")
804     endif()
805 endif()
807 #COPYING file: Only necessary for binary distributions.
808 #Simpler to always install.
809 install(FILES COPYING DESTINATION ${DATA_INSTALL_DIR} COMPONENT data)
811 if (GMX_BUILD_FOR_COVERAGE)
812     # Code heavy with asserts makes conditional coverage close to useless metric,
813     # as by design most of the false branches are impossible to trigger in
814     # correctly functioning code.  And the benefit of testing those that could
815     # be triggered by using an API against its specification isn't usually
816     # worth the effort.
817     add_definitions(-DNDEBUG -DGMX_DISABLE_ASSERTS)
818 endif()
820 if (BUILD_TESTING)
821     # "tests" target builds all the separate test binaries.
822     add_custom_target(tests)
823     # "run-ctest" is an internal target that actually runs the tests.
824     # This is necessary to be able to add separate targets that execute as part
825     # of 'make check', but are ensured to be executed after the actual tests.
826     add_custom_target(run-ctest
827                       COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
828                       COMMENT "Running all tests"
829                       VERBATIM)
830     add_dependencies(run-ctest tests)
831     # "check" target builds and runs all tests.
832     add_custom_target(check DEPENDS run-ctest)
833 endif()
835 if (NOT GMX_BUILD_MDRUN_ONLY)
836     add_subdirectory(docs)
837     add_subdirectory(share)
838     add_subdirectory(scripts)
839 endif()
840 add_subdirectory(src)
842 if (BUILD_TESTING)
843     add_subdirectory(tests)
844 endif()
846 gmx_cpack_write_config()
848 # Issue a warning if NVIDIA GPUs were detected, but CUDA was not found.
849 # Don't bother the user after the first configure pass.
850 if ((CUDA_NOTFOUND_AUTO AND GMX_DETECT_GPU_AVAILABLE) AND NOT GMX_GPU_DETECTION_DONE)
851     message(WARNING "${CUDA_NOTFOUND_MESSAGE}")
852 endif()
853 set(GMX_GPU_DETECTION_DONE TRUE CACHE INTERNAL "Whether GPU detection has already been done")
855 #######################
856 ## uninstall target
857 #######################
858 CONFIGURE_FILE(   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
859                   "${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake"
860                   IMMEDIATE @ONLY)
861 ###########################
862 ADD_CUSTOM_TARGET(uninstall
863                   "${CMAKE_COMMAND}" -P
864                   "${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake")
865 ###########################
866 set_directory_properties(PROPERTIES
867             ADDITIONAL_MAKE_CLEAN_FILES "install_manifest.txt")