Merge branch release-2016
[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,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.
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 3.4.3)
36
37 # CMake modules/macros are in a subdirectory to keep this file cleaner
38 # This needs to be set before project() in order to pick up toolchain files
39 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Platform)
40
41 project(Gromacs)
42
43 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
44 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
45 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
46
47 # Set up common version variables, as well as general information about
48 # the build tree (whether the build is from a source package or from a git
49 # repository).  Also declares a few functions that will be used for generating
50 # version info files later.
51 include(gmxBuildTreeInfo)
52 include(gmxVersionInfo)
53
54 if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND UNIX)
55     set(CMAKE_INSTALL_PREFIX "/usr/local/gromacs" CACHE STRING "Installation prefix (installation will need write permissions here)" FORCE)
56 endif()
57 if("${CMAKE_INSTALL_PREFIX}" STREQUAL "${CMAKE_BINARY_DIR}")
58     message(FATAL_ERROR "GROMACS cannot be installed into the build tree, choose a different location for CMAKE_INSTALL_PREFIX")
59 endif()
60
61 include(gmxBuildTypeReference)
62 include(gmxBuildTypeProfile)
63 include(gmxBuildTypeTSAN)
64 include(gmxBuildTypeASAN)
65 include(gmxBuildTypeMSAN)
66 include(gmxBuildTypeReleaseWithAssert)
67
68 if(NOT CMAKE_BUILD_TYPE)
69     set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel Reference RelWithAssert Profile TSAN ASAN MSAN." FORCE)
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" "TSAN" "ASAN" "MSAN")
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 include(gmxCTestUtilities)
89 gmx_ctest_init()
90
91 include(gmxCPackUtilities)
92 gmx_cpack_init()
93
94 # Variables that accumulate stuff influencing the installed headers
95 set(INSTALLED_HEADER_INCLUDE_DIRS "")
96 set(INSTALLED_HEADER_DEFINITIONS "")
97
98 ########################################################################
99 # Check and warn if cache generated on a different host is being reused
100 ########################################################################
101 if(CMAKE_HOST_UNIX)
102     execute_process(COMMAND hostname
103                     OUTPUT_VARIABLE TMP_HOSTNAME
104                     OUTPUT_STRIP_TRAILING_WHITESPACE)
105     if(GMX_BUILD_HOSTNAME AND NOT "${GMX_BUILD_HOSTNAME}" STREQUAL "${TMP_HOSTNAME}")
106         message(WARNING "
107             The CMake cache, probably generated on a different host (${GMX_BUILD_HOSTNAME}),
108             is being reused! This could lead to inconsistencies; therefore, it is
109             recommended to regenerate the cache!")
110     endif()
111     set(GMX_BUILD_HOSTNAME "${TMP_HOSTNAME}" CACHE INTERNAL
112             "Hostname of the machine where the cache was generated.")
113 endif()
114
115 ########################################################################
116 # Detect architecture before setting options so we can alter defaults
117 ########################################################################
118 # Detect the architecture the compiler is targetting, detect
119 # SIMD instructions possibilities on that hardware, suggest SIMD instruction set
120 # to use if none is specified, and populate the cache option for CPU
121 # SIMD.
122 include(gmxDetectTargetArchitecture)
123 gmx_detect_target_architecture()
124
125 # Permit the user to specify a particular standard library, e.g. compiling
126 # with "-stdlib=libc++" and linking with "-lc++abi -lc++" to get clang's libcxx.
127 set(GMX_STDLIB_CXX_FLAGS "" CACHE STRING "Compiler flag for a C++ standard library flavour")
128 set(GMX_STDLIB_LIBRARIES "" CACHE STRING "Linker libraries for a particular C++ standard library")
129 mark_as_advanced(GMX_STDLIB_CXX_FLAGS)
130 mark_as_advanced(GMX_STDLIB_LIBRARIES)
131
132 ########################################################################
133 # Detect CXX11 support and flags
134 ########################################################################
135 # The cmake/Check{C,CXX}CompilerFlag.cmake files in the GROMACS distribution
136 # are used with permission from CMake v3.0.0 so that GROMACS can detect
137 # invalid options with the Intel Compilers, and we have added a line
138 # to detect warnings with the Fujitsu compilers on K computer and ICC.
139 # CMake-3.0 also has a bug where the FAIL_REGEX pattern for AIX contains
140 # a semicolon. Since this is also used as a separator in lists inside CMake,
141 # that string ends up being split into two separate patterns, and the last
142 # part is just a single word that also matches other messages. We solved this
143 # by replacing the semicolon with a period that matches any character.
144 #
145 # These files should be removed from the source tree when a CMake version that
146 # includes the features in question becomes required for building GROMACS.
147 include(CheckCCompilerFlag)
148 include(CheckCXXCompilerFlag)
149
150 # This must come early, since some of our configuration flag tests
151 # depend on being able to compile C++11 source files.
152 include(gmxTestCXX11)
153 gmx_test_cxx11(GMX_CXX11_FLAGS GMX_STDLIB_CXX_FLAGS GMX_STDLIB_LIBRARIES)
154
155 # Make sure all C++ code will be compiled in C++11 mode, with the
156 # expected standard library.
157 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GMX_CXX11_FLAGS} ${GMX_STDLIB_CXX_FLAGS}")
158
159 ########################################################################
160 # User input options                                                   #
161 ########################################################################
162 include(gmxOptionUtilities)
163
164 set(CMAKE_PREFIX_PATH "" CACHE STRING "Extra locations to search for external libraries and tools (give directory without lib, bin, or include)")
165
166 # Fujitsu only has SIMD in double precision, so this will be faster
167 gmx_set_boolean(GMX_DOUBLE_DEFAULT GMX_TARGET_FUJITSU_SPARC64)
168 option(GMX_DOUBLE "Use double precision (much slower, use only if you really need it)" ${GMX_DOUBLE_DEFAULT})
169 option(GMX_RELAXED_DOUBLE_PRECISION "Accept single precision 1/sqrt(x) when using Fujitsu HPC-ACE SIMD" OFF)
170 mark_as_advanced(GMX_RELAXED_DOUBLE_PRECISION)
171
172 option(GMX_MPI    "Build a parallel (message-passing) version of GROMACS" OFF)
173 option(GMX_THREAD_MPI  "Build a thread-MPI-based multithreaded version of GROMACS (not compatible with MPI)" ON)
174 gmx_dependent_option(
175     GMX_MPI_IN_PLACE
176     "Enable MPI_IN_PLACE for MPIs that have it defined"
177     ON
178     GMX_MPI)
179 mark_as_advanced(GMX_MPI_IN_PLACE)
180 option(GMX_FAHCORE "Build a library with mdrun functionality" OFF)
181 mark_as_advanced(GMX_FAHCORE)
182
183 option(GMX_COOL_QUOTES "Enable GROMACS cool quotes" ON)
184 mark_as_advanced(GMX_COOL_QUOTES)
185 gmx_add_cache_dependency(GMX_COOL_QUOTES BOOL "NOT GMX_FAHCORE" OFF)
186
187 option(GMX_USE_OPENCL "Enable OpenCL acceleration" OFF)
188
189 # Decide on GPU settings based on user-settings and GPU/CUDA
190 # detection.  GCC 4.8 requires CUDA 6.0 (but we choose 6.5 for the
191 # preliminary C++11 support), icc 15 requires CUDA 7.0, and VS2015
192 # requires CUDA 8.0
193 if(MSVC)
194     set(REQUIRED_CUDA_VERSION 8.0)
195 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Intel")
196     set(REQUIRED_CUDA_VERSION 7.0)
197 else()
198     set(REQUIRED_CUDA_VERSION 6.5)
199 endif()
200 set(REQUIRED_CUDA_COMPUTE_CAPABILITY 2.0)
201
202 # OpenCL required version: 1.1 or newer
203 set(REQUIRED_OPENCL_MIN_VERSION 1.1)
204
205 if(NOT GMX_USE_OPENCL)
206     # CUDA detection is done only if GMX_USE_OPENCL is OFF.
207     include(gmxManageGPU)
208     set(GMX_USE_CUDA ${GMX_GPU})
209     if(GMX_GPU)
210         set(GMX_GPU_ACCELERATION_FRAMEWORK "GMX_GPU_CUDA")
211     else()
212         set(GMX_GPU_ACCELERATION_FRAMEWORK "GMX_GPU_NONE")
213     endif()
214 else()
215     #Now the OpenCL path (for both AMD and NVIDIA)
216     if(GMX_GPU)
217         include(gmxManageOpenCL)
218         set(GMX_GPU_ACCELERATION_FRAMEWORK "GMX_GPU_OPENCL")
219     else()
220         message(FATAL_ERROR "OpenCL requested but GPU option is not enabled (try -DGMX_GPU=on) ")
221     endif()
222 endif()
223
224 include(gmxDetectSimd)
225 gmx_detect_simd(GMX_SUGGESTED_SIMD)
226
227 gmx_option_multichoice(
228     GMX_SIMD
229     "SIMD instruction set for CPU kernels and compiler optimization"
230     "${GMX_SUGGESTED_SIMD}"
231     None SSE2 SSE4.1 AVX_128_FMA AVX_256 AVX2_256 AVX_512 AVX_512_KNL MIC ARM_NEON ARM_NEON_ASIMD IBM_QPX IBM_VMX IBM_VSX Sparc64_HPC_ACE Reference)
232
233 if(GMX_TARGET_MIC)
234     set(GMX_FFT_LIBRARY_DEFAULT "mkl")
235 else()
236     set(GMX_FFT_LIBRARY_DEFAULT "fftw3")
237 endif()
238
239 gmx_option_multichoice(
240     GMX_FFT_LIBRARY
241     "FFT library"
242     "${GMX_FFT_LIBRARY_DEFAULT}"
243     fftw3 mkl "fftpack[built-in]")
244 gmx_dependent_option(
245     GMX_BUILD_OWN_FFTW
246     "Download and build FFTW 3 during the GROMACS build process, rather than fall back on the really slow fftpack."
247     OFF
248     "GMX_FFT_LIBRARY STREQUAL FFTW3")
249 gmx_dependent_option(
250     GMX_DISABLE_FFTW_MEASURE
251     "Do not optimize FFTW setups (not needed with SSE)"
252     OFF
253     "GMX_FFT_LIBRARY STREQUAL FFTW3")
254 mark_as_advanced(GMX_BUILD_OWN_FFTW)
255 mark_as_advanced(GMX_DISABLE_FFTW_MEASURE)
256
257 gmx_option_multichoice(
258     GMX_QMMM_PROGRAM
259     "QM package for QM/MM"
260     None
261     none gaussian mopac gamess orca)
262
263 gmx_dependent_cache_variable(GMX_SIMD_REF_FLOAT_WIDTH  "Reference SIMD single precision width" STRING "4" "GMX_SIMD STREQUAL REFERENCE")
264 gmx_dependent_cache_variable(GMX_SIMD_REF_DOUBLE_WIDTH "Reference SIMD double precision width" STRING "2" "GMX_SIMD STREQUAL REFERENCE")
265
266 # This should be moved to a separate NBNXN cmake module when that code is cleaned up and modularized
267
268 option(GMX_BROKEN_CALLOC "Work around broken calloc()" OFF)
269 mark_as_advanced(GMX_BROKEN_CALLOC)
270
271 option(GMX_OPENMP "Enable OpenMP-based multithreading" ON)
272
273 option(GMX_USE_TNG "Use the TNG library for trajectory I/O" ON)
274
275 option(GMX_BUILD_MDRUN_ONLY "Build and install only the mdrun binary" OFF)
276
277 option(GMX_CYCLE_SUBCOUNTERS "Enable cycle subcounters to get a more detailed cycle timings" OFF)
278 mark_as_advanced(GMX_CYCLE_SUBCOUNTERS)
279
280 option(GMX_SKIP_DEFAULT_CFLAGS "Don't automatically add suggested/required Compiler flags." OFF)
281 mark_as_advanced(GMX_SKIP_DEFAULT_CFLAGS)
282
283 option(GMX_BUILD_FOR_COVERAGE
284        "Tune build for better code coverage metrics (e.g., disable asserts)"
285        OFF)
286 mark_as_advanced(GMX_BUILD_FOR_COVERAGE)
287
288 option(GMX_DEVELOPER_BUILD
289     "Enable Developer convenience features: always build unit-tests"
290     OFF)
291 mark_as_advanced(GMX_DEVELOPER_BUILD)
292
293 gmx_set_boolean(GMX_COMPILER_WARNINGS_DEFAULT "NOT SOURCE_IS_SOURCE_DISTRIBUTION")
294 option(GMX_COMPILER_WARNINGS
295     "Enable a default set of compiler warnings"
296     ${GMX_COMPILER_WARNINGS_DEFAULT})
297 mark_as_advanced(GMX_COMPILER_WARNINGS)
298 # Always turn on compiler warnings with a developer build.
299 gmx_add_cache_dependency(GMX_COMPILER_WARNINGS BOOL "NOT GMX_DEVELOPER_BUILD" ON)
300
301 option(GMX_BUILD_SHARED_EXE
302     "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."
303     ON)
304 mark_as_advanced(GMX_BUILD_SHARED_EXE)
305
306 ######################################################################
307 # Detect OpenMP support
308 ######################################################################
309 # The OpenMP detection _must_ come before tests for other CFLAGS.
310 include(gmxManageOpenMP)
311
312
313
314 ######################################################################
315 # Compiler tests
316 # These need to be done early (before further tests).
317 #####################################################################
318
319 include(gmxCFlags)
320 gmx_c_flags()
321
322 # These collect libraries that Gromacs requires for linking:
323 #  - Libraries that are required for libgromacs (only)
324 set(GMX_EXTRA_LIBRARIES "")
325 #  - Libraries that are required for all code in the repository
326 set(GMX_COMMON_LIBRARIES "")
327 #  - Libraries that all code linked against libgromacs needs
328 #    (i.e., something that is exposed in installed headers).
329 set(GMX_PUBLIC_LIBRARIES "")
330
331 # This variable should be used for additional compiler flags which are not
332 # generated in gmxCFlags nor are SIMD or MPI related.
333 set(EXTRA_C_FLAGS "")
334 set(EXTRA_CXX_FLAGS "")
335
336 # Run through a number of tests for buggy compilers and other issues
337 include(gmxTestCompilerProblems)
338 gmx_test_compiler_problems()
339
340 # Implement double-precision option. This is complicated because we
341 # need installed headers to use the precision mode of the build that
342 # produced the library, but cannot use config.h in that case. We also
343 # want such variables to always have a definition, because #if is more
344 # robust than #ifdef. So, we put this value on the compiler command
345 # line in all cases.
346 #
347 # GMX_RELAXED_DOUBLE_PRECISION does not need to be handled here,
348 # because no installed header needs it
349 if(GMX_DOUBLE)
350     set(GMX_DOUBLE_VALUE 1)
351 else()
352     set(GMX_DOUBLE_VALUE 0)
353 endif()
354 add_definitions(-DGMX_DOUBLE=${GMX_DOUBLE_VALUE})
355 list(APPEND INSTALLED_HEADER_DEFINITIONS "-DGMX_DOUBLE=${GMX_DOUBLE_VALUE}")
356
357 if(WIN32)
358     list(APPEND GMX_EXTRA_LIBRARIES "wsock32")
359     add_definitions(-DGMX_HAVE_WINSOCK)
360 endif()
361
362
363
364 ########################################################################
365 # Basic system tests (standard libraries, headers, functions, types)   #
366 ########################################################################
367 include(CheckIncludeFiles)
368 include(CheckIncludeFileCXX)
369 check_include_files(unistd.h     HAVE_UNISTD_H)
370 check_include_files(pwd.h        HAVE_PWD_H)
371 check_include_files(dirent.h     HAVE_DIRENT_H)
372 check_include_files(time.h       HAVE_TIME_H)
373 check_include_files(sys/time.h   HAVE_SYS_TIME_H)
374 check_include_files(io.h         HAVE_IO_H)
375 check_include_files(sched.h      HAVE_SCHED_H)
376
377 check_include_files(regex.h      HAVE_POSIX_REGEX)
378 # TODO: It could be nice to inform the user if no regex support is found,
379 # as selections won't be fully functional.
380
381 include(CheckCXXSymbolExists)
382 check_cxx_symbol_exists(gettimeofday      sys/time.h   HAVE_GETTIMEOFDAY)
383 check_cxx_symbol_exists(sysconf           unistd.h     HAVE_SYSCONF)
384 check_cxx_symbol_exists(nice              unistd.h     HAVE_NICE)
385 check_cxx_symbol_exists(fsync             unistd.h     HAVE_FSYNC)
386 check_cxx_symbol_exists(_fileno           stdio.h      HAVE__FILENO)
387 check_cxx_symbol_exists(fileno            stdio.h      HAVE_FILENO)
388 check_cxx_symbol_exists(_commit           io.h         HAVE__COMMIT)
389 check_cxx_symbol_exists(sigaction         signal.h     HAVE_SIGACTION)
390
391 # We cannot check for the __builtins as symbols, but check if code compiles
392 check_cxx_source_compiles("int main(){ return __builtin_clz(1);}"   HAVE_BUILTIN_CLZ)
393 check_cxx_source_compiles("int main(){ return __builtin_clzll(1);}" HAVE_BUILTIN_CLZLL)
394 if(MSVC)
395     check_cxx_source_compiles("#include <intrin.h>\n int main(){unsigned long r;unsigned long i=1;_BitScanReverse(&r,i);return r;}" HAVE_BITSCANREVERSE)
396     check_cxx_source_compiles("#include <intrin.h>\n int main(){unsigned long r;unsigned __int64 i=1;_BitScanReverse(&r,i);return r;}" HAVE_BITSCANREVERSE64)
397 elseif(CMAKE_CXX_COMPILER_ID MATCHES "XL")
398     check_cxx_source_compiles("int main(){ return __cntlz4(1);}" HAVE_CNTLZ4)
399     check_cxx_source_compiles("int main(){ return __cntlz8(1);}" HAVE_CNTLZ8)
400 endif()
401
402 include(CheckLibraryExists)
403 find_library(HAVE_LIBM m)
404 mark_as_advanced(HAVE_LIBM)
405 check_library_exists(rt clock_gettime "" HAVE_CLOCK_GETTIME)
406 check_library_exists(m feenableexcept "" HAVE_FEENABLEEXCEPT)
407
408 include(TestSchedAffinity)
409 test_sched_affinity(HAVE_SCHED_AFFINITY)
410
411 # Aligned memory allocation. We need to check for both mm_malloc(),
412 # posix_memalign(), memalign(), and on windows also _aligned_malloc()
413 include(gmxTestMMMalloc)
414 gmx_test_mm_malloc(HAVE__MM_MALLOC)
415 check_cxx_symbol_exists(posix_memalign    stdlib.h     HAVE_POSIX_MEMALIGN)
416 check_cxx_symbol_exists(memalign          stdlib.h     HAVE_MEMALIGN)
417 if(MSVC)
418     # No need to waste time on this test on platforms where it will never be true
419     check_cxx_symbol_exists(_aligned_malloc   stdlib.h     HAVE__ALIGNED_MALLOC)
420 endif()
421
422 include(TestBigEndian)
423 test_big_endian(GMX_INTEGER_BIG_ENDIAN)
424
425 gmx_set_boolean(GMX_USE_NICE "HAVE_UNISTD_H AND HAVE_NICE")
426
427 # Management of GROMACS options for specific toolchains should go
428 # here. Because the initial settings for some of the main options have
429 # already happened, but things like library detection and MPI compiler
430 # feature detection have not, the docstrings for any over-rides of
431 # GROMACS defaults or user settings will make sense. Also, any
432 # toolchain-related reasons for choosing whether to detect various
433 # things can be sorted out now, before the detection takes place.
434 if(${CMAKE_SYSTEM_NAME} MATCHES BlueGene)
435     include(gmxManageBlueGene)
436 endif()
437
438 if(GMX_TARGET_FUJITSU_SPARC64)
439     include(gmxManageFujitsuSparc64)
440 endif()
441
442 ########################################################################
443 #Process MPI settings
444 ########################################################################
445 include(gmxManageMPI)
446
447
448 ########################################################################
449 #Process shared/static library settings
450 ########################################################################
451 include(gmxManageSharedLibraries)
452
453
454 ########################################################################
455 # Find external packages                                               #
456 ########################################################################
457
458 # Unconditionally find the package, as it is also required for unit
459 # tests. This exports LIBXML2_FOUND, which we should not use because
460 # it does not tell us that linking will succeed. Instead, we test that
461 # next.
462 #if(DEFINED LIBXML2_LIBRARIES)
463 #  set(LibXml2_FIND_QUIETLY TRUE)
464 #endif()
465 #find_package(LibXml2)
466 #include(gmxTestLibXml2)
467 #gmx_test_libxml2(HAVE_LIBXML2)
468 #option(GMX_XML "Use libxml2 to parse xml files (currently has no effect)" ${HAVE_LIBXML2})
469 #set(PKG_XML "")
470 #mark_as_advanced(GMX_XML)
471 # Don't actually do anything, since libxml2 is currently not used by libgromacs
472 #if(GMX_XML AND NOT HAVE_LIBXML2)
473 #    message(FATAL_ERROR "libxml2 not found. Set GMX_XML=OFF to compile without XML support")
474 #endif()
475 #if(GMX_XML)
476 #    include_directories(${LIBXML2_INCLUDE_DIR})
477 #    set(PKG_XML libxml-2.0)
478 #    set(XML_LIBRARIES ${LIBXML2_LIBRARIES})
479 #endif()
480
481 if(DEFINED HWLOC_LIBRARIES)
482   set(Hwloc_FIND_QUIETLY TRUE)
483 endif()
484 find_package(Hwloc 1.5)
485 if (HWLOC_FOUND)
486     if (HWLOC_LIBRARIES MATCHES ".a$")
487         set(_STATIC_HWLOC TRUE)
488     endif()
489
490     gmx_check_if_changed(HWLOC_FOUND_CHANGED HWLOC_FOUND)
491     if (_STATIC_HWLOC AND HWLOC_FOUND_CHANGED)
492         message(STATUS "Static hwloc library found, will not attempt using it as it could lead to link-time errors. To use the detected library, manually set GMX_HWLOC=ON and you will likely have to pass appropriate linker flags too to satisfy the link-time dependencies of your hwloc library. Try \"pkg-config --libs --static hwloc\" for suggestions on what you will need.")
493         set(GMX_HWLOC_DEFAULT OFF)
494     else()
495         set(GMX_HWLOC_DEFAULT ON)
496     endif()
497 else()
498     set(GMX_HWLOC_DEFAULT OFF)
499 endif()
500 option(GMX_HWLOC "Add support for hwloc Portable Hardware locality library" ${GMX_HWLOC_DEFAULT})
501 if(GMX_HWLOC)
502     if(HWLOC_FOUND)
503         include_directories(SYSTEM ${HWLOC_INCLUDE_DIRS})
504         list(APPEND GMX_EXTRA_LIBRARIES ${HWLOC_LIBRARIES})
505     else()
506         message(FATAL_ERROR "Hwloc package support requested, but not found.")
507     endif()
508 endif()
509
510 option(GMX_EXTERNAL_TINYXML2 "Use external TinyXML-2 instead of compiling the version bundled with GROMACS." OFF)
511 mark_as_advanced(GMX_EXTERNAL_TINYXML2)
512 if(GMX_EXTERNAL_TINYXML2)
513     # Find an external TinyXML-2 library.
514     find_package(TinyXML-2 3.0.0)
515     set(HAVE_TINYXML2 ${TinyXML2_FOUND})
516     if(NOT HAVE_TINYXML2)
517         message(FATAL_ERROR "External TinyXML-2 could not be found, please adjust your search paths")
518     endif()
519 endif()
520
521 option(GMX_EXTRAE "Add support for tracing using EXTRAE" OFF)
522 mark_as_advanced(GMX_EXTRAE)
523
524 if (GMX_EXTRAE)
525   find_package(EXTRAE)
526   if(EXTRAE_FOUND)
527     include_directories(SYSTEM ${EXTRAE_INCLUDE_DIR})
528     set(HAVE_EXTRAE 1)
529   else()
530     message(FATAL_ERROR "EXTRAE library was not found. Please add the correct path to CMAKE_PREFIX_PATH")
531   endif()
532 endif()
533
534 option(GMX_X11 "Use X window system" OFF)
535 if (GMX_X11)
536     find_package(X11)
537     # X11 includes/libraries are only set in the ngmx subdirectory!
538     if(NOT X11_FOUND)
539         message(FATAL_ERROR
540                 "X11 include files and/or libraries were not found. "
541                 "Set GMX_X11=OFF to compile without X11 support. "
542                 "gmx view will not be available.")
543     endif()
544     include_directories(SYSTEM ${X11_INCLUDE_DIR})
545 endif()
546
547 include(ThreadMPI)
548 # Enable core threading facilities
549 tmpi_enable_core("${CMAKE_SOURCE_DIR}/src/external/thread_mpi/include")
550 # Enable tMPI C++ support
551 tmpi_enable_cxx()
552 if(GMX_THREAD_MPI)
553     # enable MPI functions
554     tmpi_enable()
555     set(MPI_IN_PLACE_EXISTS 1)
556 endif()
557 # If atomics are manually disabled a define is needed because atomics.h doesn't depend on config.h
558 if (TMPI_ATOMICS_DISABLED)
559    add_definitions(-DTMPI_ATOMICS_DISABLED)
560 endif()
561
562 include(gmxManageTNG)
563
564 include(gmxManageLmfit)
565
566 if(GMX_GPU)
567     # now that we have detected the dependencies, do the second configure pass
568     gmx_gpu_setup()
569 endif()
570
571 if(CYGWIN)
572     set(GMX_CYGWIN 1)
573 endif()
574
575 if(WIN32)
576     set(GMX_NATIVE_WINDOWS 1)
577     # This makes windows.h not declare min/max as macros that would break
578     # C++ code using std::min/std::max.
579     add_definitions(-DNOMINMAX)
580 endif()
581
582 if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") #Work-around for cmake bug #10837
583     if (CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Intel" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
584         set(CMAKE_INCLUDE_SYSTEM_FLAG_C "-isystem ")
585     endif()
586     if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Intel" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
587         set(CMAKE_INCLUDE_SYSTEM_FLAG_CXX "-isystem ")
588     endif()
589 endif()
590
591 option(GMX_BUILD_UNITTESTS "Build unit tests with BUILD_TESTING" ON)
592 mark_as_advanced(GMX_BUILD_UNITTESTS)
593 gmx_add_cache_dependency(GMX_BUILD_UNITTESTS BOOL BUILD_TESTING OFF)
594
595 ########################################################################
596 # Our own GROMACS tests
597 ########################################################################
598
599 add_definitions( -DHAVE_CONFIG_H )
600 include_directories(BEFORE ${CMAKE_SOURCE_DIR}/src)
601 # TODO required at high level because both libgromacs and progs/mdrun
602 # require it, both for thread-MPI and its atomics and mutexes.
603 include_directories(BEFORE SYSTEM ${CMAKE_SOURCE_DIR}/src/external/thread_mpi/include)
604 # Required for config.h, maybe should only be set in src/CMakeLists.txt
605 include_directories(BEFORE ${CMAKE_BINARY_DIR}/src)
606
607 include(gmxTestInlineASM)
608 gmx_test_inline_asm_gcc_x86(GMX_X86_GCC_INLINE_ASM)
609
610 include(gmxSetBuildInformation)
611 gmx_set_build_information()
612 # Turn on RDTSCP if:
613 # - the build system's CPU supports it
614 # - the acceleration is set to AVX as all AVX-capable CPUs support AVX (which
615 #   at this point means that the user set it).
616 # Note: it's better to not use the later set value of GMX_SIMD because
617 # it reflects the system's capability of both compiling and running AVX code.
618 # TODO: After merge with 5.0 one could implement a cache variable dependency
619 # such that GMX_USE_RDTSCP can change if GMX_SIMD is changed to AVX
620 # after the first cmake pass.
621 if (BUILD_CPU_FEATURES MATCHES "rdtscp" OR GMX_SIMD MATCHES "AVX")
622     set(GMX_USE_RDTSCP_DEFAULT_VALUE ON)
623 else()
624     set(GMX_USE_RDTSCP_DEFAULT_VALUE OFF)
625 endif()
626 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})
627 mark_as_advanced(GMX_USE_RDTSCP)
628 if(GMX_USE_RDTSCP)
629     set(HAVE_RDTSCP 1)
630 endif()
631
632 include(gmxTestLargeFiles)
633 gmx_test_large_files(GMX_LARGEFILES)
634
635 include(gmxTestSignal)
636 gmx_test_sigusr1(HAVE_SIGUSR1)
637
638 include(gmxTestPipes)
639 gmx_test_pipes(HAVE_PIPES)
640
641 check_include_file_cxx(regex     HAVE_CXX11_REGEX)
642
643 include(gmxTestXDR)
644 gmx_test_xdr(GMX_SYSTEM_XDR)
645 if(NOT GMX_SYSTEM_XDR)
646     set(GMX_INTERNAL_XDR 1)
647 endif()
648
649
650 ##################################################
651 # Process SIMD instruction settings
652 ##################################################
653 # This checks what flags to add in order to
654 # support the SIMD instructions we need, it sets
655 # correct defines for the SIMD instructions supported,
656 # and adds advanced options to control accuracy
657 # for SIMD math operations.
658 include(gmxManageSimd)
659 gmx_manage_simd()
660
661 include(gmxManageCycleCounters)
662 gmx_manage_cycle_counters()
663
664 # Process QM/MM Settings
665 if(${GMX_QMMM_PROGRAM} STREQUAL "GAUSSIAN")
666     set(GMX_QMMM_GAUSSIAN 1)
667 elseif(${GMX_QMMM_PROGRAM} STREQUAL "MOPAC")
668     set(GMX_QMMM_MOPAC 1)
669 elseif(${GMX_QMMM_PROGRAM} STREQUAL "GAMESS")
670     set(GMX_QMMM_GAMESS 1)
671 elseif(${GMX_QMMM_PROGRAM} STREQUAL "ORCA")
672     set(GMX_QMMM_ORCA 1)
673 elseif(${GMX_QMMM_PROGRAM} STREQUAL "NONE")
674     # nothing to do
675 else()
676     gmx_invalid_option_value(GMX_QMMM_PROGRAM)
677 endif()
678
679
680 ##################################################
681 # Process FFT library settings
682 ##################################################
683 include(gmxManageFFTLibraries)
684
685
686 include(gmxManageLinearAlgebraLibraries)
687
688 include(gmxManagePluginSupport)
689
690 if (GMX_USE_PLUGINS)
691     if(NOT GMX_VMD_PLUGIN_PATH)
692         find_package(VMD)
693     endif()
694 endif()
695
696 # Link real-time library for POSIX timers. The check for clock_gettime
697 # confirms the linkability of rt.
698 if(HAVE_TIME_H AND HAVE_UNISTD_H AND HAVE_CLOCK_GETTIME)
699     list(APPEND GMX_EXTRA_LIBRARIES rt)
700 endif()
701
702 # Math and thread libraries must often come after all others when linking...
703 if (HAVE_LIBM)
704     list(APPEND GMX_PUBLIC_LIBRARIES m)
705 endif()
706
707 option(GMX_NACL "Configure for Native Client builds" OFF)
708 if (GMX_NACL)
709   list(APPEND GMX_EXTRA_LIBRARIES nosys)
710   set(GMX_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lnosys")
711   # TODO: Is this still necessary with the check for its presence?
712   set(GMX_USE_NICE 0)
713   set(GMX_NO_RENAME 1)
714 endif()
715 mark_as_advanced(GMX_NACL)
716
717 if(GMX_FAHCORE)
718   set(COREWRAP_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/../corewrap" CACHE STRING
719       "Path to swindirect.h")
720   include_directories(${COREWRAP_INCLUDE_DIR})
721 endif()
722
723 # Value of GMX_BUILD_HELP=AUTO tries to generate things, but will only
724 # produce warnings if that fails.
725 set(build_help_default AUTO)
726 if (SOURCE_IS_SOURCE_DISTRIBUTION OR CMAKE_CROSSCOMPILING)
727     set(build_help_default OFF)
728 endif()
729 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})
730 mark_as_advanced(GMX_BUILD_HELP)
731 if (GMX_BUILD_HELP AND SOURCE_IS_SOURCE_DISTRIBUTION AND BUILD_IS_INSOURCE)
732     message(FATAL_ERROR
733         "Rebuilding shell completions or man pages is not supported for "
734         "in-source builds from a source distribution. "
735         "Set GMX_BUILD_HELP=OFF or do an out-of-source build to proceed.")
736 endif()
737
738 # # # # # # # # # # NO MORE TESTS AFTER THIS LINE! # # # # # # # # # # #
739 # these are set after everything else
740 if (NOT GMX_SKIP_DEFAULT_CFLAGS)
741     set(CMAKE_C_FLAGS "${SIMD_C_FLAGS} ${MPI_COMPILE_FLAGS} ${EXTRA_C_FLAGS} ${CMAKE_C_FLAGS}")
742     set(CMAKE_CXX_FLAGS "${SIMD_CXX_FLAGS} ${MPI_COMPILE_FLAGS} ${EXTRA_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
743     set(CMAKE_EXE_LINKER_FLAGS "${FFT_LINKER_FLAGS} ${MPI_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}")
744     set(CMAKE_SHARED_LINKER_FLAGS "${FFT_LINKER_FLAGS} ${MPI_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS}")
745 else()
746     message("Recommended flags which are not added because GMX_SKIP_DEFAULT_CFLAGS=yes:")
747     message("CMAKE_C_FLAGS: ${SIMD_C_FLAGS} ${MPI_COMPILE_FLAGS} ${EXTRA_C_FLAGS} ${GMXC_CFLAGS}")
748     message("CMAKE_C_FLAGS_RELEASE: ${GMXC_CFLAGS_RELEASE}")
749     message("CMAKE_C_FLAGS_DEBUG: ${GMXC_CFLAGS_DEBUG}")
750     message("CMAKE_CXX_FLAGS: ${SIMD_CXX_FLAGS} ${MPI_COMPILE_FLAGS} ${EXTRA_CXX_FLAGS} ${GMXC_CXXFLAGS}")
751     message("CMAKE_CXX_FLAGS_RELEASE: ${GMXC_CXXFLAGS_RELEASE}")
752     message("CMAKE_CXX_FLAGS_DEBUG: ${GMXC_CXXFLAGS_DEBUG}")
753     message("CMAKE_EXE_LINKER_FLAGS: ${FFT_LINKER_FLAGS} ${MPI_LINKER_FLAGS}")
754     message("CMAKE_SHARED_LINKER_FLAGS: ${FFT_LINKER_FLAGS} ${MPI_LINKER_FLAGS}")
755 endif()
756
757 if(NOT GMX_OPENMP)
758     #Unset all OpenMP flags in case OpenMP was disabled either by the user
759     #or because it was only partially detected (e.g. only for C but not C++ compiler)
760     unset(OpenMP_C_FLAGS CACHE)
761     unset(OpenMP_CXX_FLAGS CACHE)
762 else()
763     set(GMX_EXE_LINKER_FLAGS ${GMX_EXE_LINKER_FLAGS} ${OpenMP_LINKER_FLAGS})
764     set(GMX_SHARED_LINKER_FLAGS ${GMX_SHARED_LINKER_FLAGS} ${OpenMP_SHARED_LINKER_FLAGS})
765 endif()
766
767 ########################################################################
768 # Specify install locations
769 ########################################################################
770 # Use GNUInstallDirs to set paths on multiarch systems.
771 include(GNUInstallDirs)
772
773 # Customization for the installation tree paths.
774 set(GMX_LIB_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR} CACHE STRING
775     "Library installation directory (default: ${CMAKE_INSTALL_LIBDIR})")
776 set(GMX_DATA_INSTALL_DIR gromacs CACHE STRING
777     "Data installation directory under share/ (default: gromacs)")
778 mark_as_advanced(GMX_LIB_INSTALL_DIR GMX_DATA_INSTALL_DIR)
779
780 # These variables are used internally to provide a central location for
781 # customizing the install locations.
782 set(LIB_INSTALL_DIR       ${GMX_LIB_INSTALL_DIR})
783 set(BIN_INSTALL_DIR       bin)
784 # This variable also gets written into config.h for use in finding the data
785 # directories.
786 set(DATA_INSTALL_DIR      share/${GMX_DATA_INSTALL_DIR})
787 set(MAN_INSTALL_DIR       share/man)
788 # If the nesting level wrt. the installation root is changed,
789 # gromacs-config.cmake.cmakein needs to be adapted.
790 set(CMAKE_INSTALL_DIR     share/cmake)
791 # TODO: Make GMXRC adapt if this is changed
792 set(PKGCONFIG_INSTALL_DIR ${LIB_INSTALL_DIR}/pkgconfig)
793 set(OCL_INSTALL_DIR       ${DATA_INSTALL_DIR}/opencl)
794 set(INCL_INSTALL_DIR      include)
795
796 list(APPEND INSTALLED_HEADER_INCLUDE_DIRS ${INCL_INSTALL_DIR})
797
798 # Binary and library suffix options
799 include(gmxManageSuffixes)
800
801 ################################################################
802 # Shared library load path settings
803 ################################################################
804 if(NOT GMX_BUILD_SHARED_EXE)
805     # No rpath
806     set(CMAKE_SKIP_RPATH TRUE)
807     set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) # remove -Wl,-Bdynamic
808     set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS)
809 else()
810     # The build folder always has bin/ and lib/; if we are also going to
811     # install to lib/, then the installation RPATH works also in the build
812     # tree.  This makes installation slightly faster (no need to rewrite the
813     # RPATHs), and makes the binaries in the build tree relocatable.
814     if(GMX_LIB_INSTALL_DIR STREQUAL "lib")
815         set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
816     endif()
817     # Set the RPATH as relative to the executable location to make the
818     # binaries relocatable.
819     if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") #Assume OS X >=10.5
820         set(CMAKE_INSTALL_RPATH "\$ORIGIN/../${GMX_LIB_INSTALL_DIR}")
821     else()
822         set(CMAKE_INSTALL_RPATH "@executable_path/../${GMX_LIB_INSTALL_DIR}")
823     endif()
824     set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
825     set(CMAKE_MACOSX_RPATH 1)
826 endif()
827
828 #COPYING file: Only necessary for binary distributions.
829 #Simpler to always install.
830 install(FILES COPYING DESTINATION ${DATA_INSTALL_DIR} COMPONENT data)
831
832 if (GMX_BUILD_FOR_COVERAGE)
833     # Code heavy with asserts makes conditional coverage close to useless metric,
834     # as by design most of the false branches are impossible to trigger in
835     # correctly functioning code.  And the benefit of testing those that could
836     # be triggered by using an API against its specification isn't usually
837     # worth the effort.
838     add_definitions(-DNDEBUG -DGMX_DISABLE_ASSERTS)
839 endif()
840
841 if (BUILD_TESTING)
842     include(tests/CheckTarget.cmake)
843 endif()
844
845 if (NOT GMX_BUILD_MDRUN_ONLY)
846     add_subdirectory(docs)
847     add_subdirectory(share)
848     add_subdirectory(scripts)
849 endif()
850 add_subdirectory(src)
851
852 if (BUILD_TESTING)
853     add_subdirectory(tests)
854 endif()
855
856 gmx_cpack_write_config()
857
858 # Issue a warning if NVIDIA GPUs were detected, but CUDA was not found.
859 # Don't bother the user after the first configure pass.
860 if ((CUDA_NOTFOUND_AUTO AND GMX_DETECT_GPU_AVAILABLE) AND NOT GMX_GPU_DETECTION_DONE)
861     message(WARNING "${CUDA_NOTFOUND_MESSAGE}")
862 endif()
863 set(GMX_GPU_DETECTION_DONE TRUE CACHE INTERNAL "Whether GPU detection has already been done")
864
865 #######################
866 ## uninstall target
867 #######################
868 CONFIGURE_FILE(   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
869                   "${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake"
870                   IMMEDIATE @ONLY)
871 ###########################
872 ADD_CUSTOM_TARGET(uninstall
873                   "${CMAKE_COMMAND}" -P
874                   "${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake")
875 ###########################
876 set_directory_properties(PROPERTIES
877             ADDITIONAL_MAKE_CLEAN_FILES "install_manifest.txt")