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