Merge release-4-6 into master
[alexxy/gromacs.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 2.8)
2 # Keep CMake suitably quiet on Cygwin
3 set(CMAKE_LEGACY_CYGWIN_WIN32 0) # Remove when CMake >= 2.8.4 is required
4
5 # CMake modules/macros are in a subdirectory to keep this file cleaner
6 # This needs to be set before project() in order to pick up toolchain files
7 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Platform)
8
9 project(Gromacs)
10 include(Dart)
11 mark_as_advanced(DART_ROOT)
12
13 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
14 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
15 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
16
17 # PROJECT_VERSION should have the following structure: 
18 # VERSION-dev[-SUFFIX] where the VERSION should have the for: vMajor.vMinor.vPatch
19 #
20 # The "-dev" suffix is important to keep because it makes possible to distinguish 
21 # between a build from official release and a build from git release branch on a 
22 # machine with no git. 
23 #
24 # NOTE: when releasing the "-dev" suffix needs to be stripped off!
25 set(PROJECT_VERSION "5.0-dev")
26 set(CUSTOM_VERSION_STRING ""
27     CACHE STRING "Custom version string (if empty, use hard-coded default)")
28 mark_as_advanced(CUSTOM_VERSION_STRING)
29 if (CUSTOM_VERSION_STRING)
30     set(PROJECT_VERSION ${CUSTOM_VERSION_STRING})
31 endif (CUSTOM_VERSION_STRING)
32 set(SOVERSION 6)
33 # It is a bit irritating, but this has to be set separately for now!
34 SET(CPACK_PACKAGE_VERSION_MAJOR "5")
35 SET(CPACK_PACKAGE_VERSION_MINOR "0")
36 #SET(CPACK_PACKAGE_VERSION_PATCH "0")
37
38 # The numerical gromacs version. It is 40600 for 4.6.0.
39 # The #define GMX_VERSION in gmx_header_config_h is set to this value.
40 math(EXPR NUM_VERSION 
41     "${CPACK_PACKAGE_VERSION_MAJOR}*10000 + ${CPACK_PACKAGE_VERSION_MINOR}*100")
42 if(CPACK_PACKAGE_VERSION_PATCH)
43     math(EXPR NUM_VERSION 
44          "${NUM_VERSION} + ${CPACK_PACKAGE_VERSION_PATCH}")
45 endif()
46
47 # The API version tracks the numerical Gromacs version (for now).
48 # It is potentially different from the Gromacs version in the future, if
49 # the programs/libraries diverge from the presumably more stable API.
50 # The #define GMX_API_VERSION in version.h is set to this value to
51 # provide backward compatibility of software written against the Gromacs API.
52 set(API_VERSION ${NUM_VERSION})
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
58 set(GMX_INSTALL_PREFIX "" CACHE STRING "Prefix gets appended to CMAKE_INSTALL_PREFIX. For cpack it sets the root folder of the archive.")
59 mark_as_advanced(GMX_INSTALL_PREFIX)
60
61 include(gmxBuildTypeReference)
62
63 if(NOT CMAKE_BUILD_TYPE)
64     set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel Reference." FORCE)
65 endif(NOT CMAKE_BUILD_TYPE)
66
67 enable_language(C)
68 enable_language(CXX)
69
70 set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
71 set(CPACK_PACKAGE_VENDOR "gromacs.org")
72 set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Gromacs - a toolkit for high-performance molecular simulation")
73 set(CPACK_RESOURCE_FILE_WELCOME "${CMAKE_SOURCE_DIR}/admin/InstallWelcome.txt")
74 # Its GPL/LGPL, so they do not have to agree to a license for mere usage, but some installers require this...
75 set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/COPYING")
76 set(CPACK_RESOURCE_FILE_README "${CMAKE_SOURCE_DIR}/admin/InstallInfo.txt")
77 set(CPACK_SOURCE_IGNORE_FILES "\\\\.isreposource$;\\\\.git/;\\\\.gitignore$")
78 set(CPACK_PROJECT_CONFIG_FILE "${CMAKE_SOURCE_DIR}/CPackInit.cmake")
79 SET(CPACK_SOURCE_INSTALLED_DIRECTORIES "${CMAKE_SOURCE_DIR};/;${CMAKE_BINARY_DIR}/man;man")
80 set(CPACK_PACKAGE_CONTACT "gmx-users@gromacs.org")
81
82 #must come after all cpack settings!
83 include(CPack)
84
85 set(SOURCE_IS_GIT_REPOSITORY OFF)
86 if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
87     set(SOURCE_IS_GIT_REPOSITORY ON)
88 endif()
89
90 ########################################################################
91 # Check and warn if cache generated on a different host is being reused
92 ########################################################################
93 if(CMAKE_HOST_UNIX)
94     execute_process(COMMAND hostname
95                     OUTPUT_VARIABLE TMP_HOSTNAME
96                     OUTPUT_STRIP_TRAILING_WHITESPACE)
97     if(GMX_BUILD_HOSTNAME AND NOT "${GMX_BUILD_HOSTNAME}" STREQUAL "${TMP_HOSTNAME}")
98         message(WARNING "
99             The CMake cache, probably generated on a different host (${GMX_BUILD_HOSTNAME}),
100             is being reused! This could lead to inconsitencies; therefore, it is
101             recommended to regenerate the cache!")
102     endif()
103     set(GMX_BUILD_HOSTNAME "${TMP_HOSTNAME}" CACHE INTERNAL
104             "Hostname of the machine where the cache was generated.")
105 endif()
106
107 ########################################################################
108 option(GMX_COOL_QUOTES "Enable Gromacs cool quotes" ON)
109 mark_as_advanced(GMX_COOL_QUOTES)
110
111 set(CMAKE_PREFIX_PATH "" CACHE STRING "Extra locations to search for external libraries and tools (give directory without lib, bin, or include)")
112 # User input options                                                   #
113 ########################################################################
114 option(GMX_DOUBLE "Use double precision (much slower, use only if you really need it)" OFF)
115 option(GMX_MPI    "Build a parallel (message-passing) version of GROMACS" OFF)
116 option(GMX_THREAD_MPI  "Build a thread-MPI-based multithreaded version of GROMACS (not compatible with MPI)" ON)
117 option(GMX_SOFTWARE_INVSQRT "Use GROMACS software 1/sqrt" ON)
118 mark_as_advanced(GMX_SOFTWARE_INVSQRT)
119 option(GMX_FAHCORE "Build a library with mdrun functionality" OFF)
120 mark_as_advanced(GMX_FAHCORE)
121
122 # decide on GPU settings based on user-settings and GPU/CUDA detection
123 include(gmxManageGPU)
124
125 # TODO: move OpenMM to contrib
126 option(GMX_OPENMM "Accelerated execution on GPUs through the OpenMM library (rerun cmake after changing to see relevant options)" OFF)
127 mark_as_advanced(GMX_OPENMM)
128
129 include(gmxDetectAcceleration)
130 if(NOT DEFINED GMX_CPU_ACCELERATION)
131     if(CMAKE_CROSSCOMPILING)
132         if("${CMAKE_SYSTEM_NAME}" MATCHES "BlueGeneQ")
133             set(GMX_SUGGESTED_CPU_ACCELERATION "IBM_QPX")
134         else()
135             set(GMX_SUGGESTED_CPU_ACCELERATION "None")
136         endif()
137     else(CMAKE_CROSSCOMPILING)
138         gmx_detect_acceleration(GMX_SUGGESTED_CPU_ACCELERATION)
139     endif(CMAKE_CROSSCOMPILING)
140 endif(NOT DEFINED GMX_CPU_ACCELERATION)
141
142 set(GMX_CPU_ACCELERATION "@GMX_SUGGESTED_CPU_ACCELERATION@"
143     CACHE STRING "Accelerated CPU kernels. Pick one of: None, SSE2, SSE4.1, AVX_128_FMA, AVX_256, IBM_QPX")
144
145 set(GMX_FFT_LIBRARY "fftw3" 
146     CACHE STRING "FFT library choices: fftw3,mkl,fftpack[built-in]")
147 option(GMX_BUILD_OWN_FFTW "Download and build FFTW 3 during the GROMACS build process, rather than fall back on the really slow fftpack." OFF)
148 mark_as_advanced(GMX_BUILD_OWN_FFTW)
149 option(GMX_DISABLE_FFTW_MEASURE 
150        "Do not optimize FFTW setups (not needed with SSE)" OFF)
151 mark_as_advanced(GMX_DISABLE_FFTW_MEASURE)
152 set(GMX_QMMM_PROGRAM "none" 
153     CACHE STRING "QM package choices: none,gaussian,mopac,gamess,orca")
154 option(GMX_BROKEN_CALLOC "Work around broken calloc()" OFF)
155 mark_as_advanced(GMX_BROKEN_CALLOC)
156 option(GMX_MPI_IN_PLACE "Enable MPI_IN_PLACE for MPIs that have it defined" ON)
157 mark_as_advanced(GMX_MPI_IN_PLACE)
158 option(GMX_LOAD_PLUGINS "Compile with plugin support, needed to read VMD supported file formats" ON)
159 mark_as_advanced(GMX_LOAD_PLUGINS)
160
161 option(GMX_GPU  "Enable GPU acceleration" ON)
162 option(GMX_OPENMP "Enable OpenMP-based multithreading" ON)
163
164 option(GMX_GIT_VERSION_INFO "Generate git version information" ${SOURCE_IS_GIT_REPOSITORY})
165 mark_as_advanced(GMX_GIT_VERSION_INFO)
166
167 option(GMX_DEFAULT_SUFFIX "Use default suffixes for GROMACS binaries and libs (_d for double, _mpi for MPI; rerun cmake after changing to see relevant options)" ON)
168 if(UNIX)
169     option(GMX_SYMLINK_OLD_BINARY_NAMES "Create symbolic links for pre-5.0 binary names" ON)
170 endif()
171
172 if(UNIX)
173     option(GMX_PREFER_STATIC_LIBS "When finding libraries prefer static archives (not available on non-*nix platforms and it will only work if static versions of external dependencies are available and found)!" OFF)
174     mark_as_advanced(GMX_PREFER_STATIC_LIBS)
175 endif()
176
177 option(GMX_CYCLE_SUBCOUNTERS "Enable cycle subcounters to get a more detailed cycle timings" OFF)
178 mark_as_advanced(GMX_CYCLE_SUBCOUNTERS)
179
180 option(GMX_SKIP_DEFAULT_CFLAGS "Don't automatically add suggested/required Compiler flags." OFF)
181 mark_as_advanced(GMX_SKIP_DEFAULT_CFLAGS)
182
183 ######################################################################
184 # Compiler tests
185 # These need to be done early (before further tests).
186 #####################################################################
187
188 # The cmake/Check{C,CXX}CompilerFlag.cmake files in the GROMACS distribution
189 # are used with permission from CMake v2.8.9 so that GROMACS can detect
190 # invalid options with the Intel Compilers.
191 # These files should be removed from the source tree when a CMake version that
192 # includes the features in question becomes required for building GROMACS.
193 include(CheckCCompilerFlag)
194 include(CheckCXXCompilerFlag)
195
196 # Get compiler version information, needs to be done early as check that depend
197 # on compiler verison follow below.
198 include(gmxGetCompilerInfo)
199 get_compiler_version()
200
201 # First exclude compilers known to not work with OpenMP although claim to support it:
202 # gcc 4.2.1 and gcc-llvm 4.2.1 (also claims to be 4.2.1) on Mac OS X
203 # This fixes redmine 900 and needs to run before OpenMP flags are set below.
204 if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND
205     (CMAKE_COMPILER_IS_GNUCC AND C_COMPILER_VERSION AND C_COMPILER_VERSION VERSION_LESS 4.3))
206     message(STATUS "OpenMP multithreading not supported with gcc/llvm-gcc 4.2 on Mac OS X, disabled")
207     set(GMX_OPENMP OFF CACHE BOOL
208         "OpenMP multithreading not not supported with gcc/llvm-gcc 4.2 on Mac OS X, disabled!" FORCE)
209 endif()
210
211 # OpenMP check must come before other CFLAGS!
212 if(GMX_OPENMP)
213     find_package(OpenMP)
214     if(OPENMP_FOUND)
215         # CMake on Windows doesn't support linker flags passed to target_link_libraries
216         # (i.e. it treats /openmp as \openmp library file). Also, no OpenMP linker flags are needed.
217         if(NOT (WIN32 AND NOT CYGWIN))
218             if(CMAKE_COMPILER_IS_GNUCC AND GMX_PREFER_STATIC_OPENMP AND NOT APPLE)
219                 set(OpenMP_LINKER_FLAGS "-Wl,-static -lgomp -lrt -Wl,-Bdynamic -lpthread")
220                 set(OpenMP_SHARED_LINKER_FLAGS "")
221             else()
222                 # Only set a linker flag if the user didn't set them manually
223                 if(NOT DEFINED OpenMP_LINKER_FLAGS)
224                     set(OpenMP_LINKER_FLAGS "${OpenMP_C_FLAGS}")
225                 endif()
226                 if(NOT DEFINED OpenMP_SHARED_LINKER_FLAGS)
227                     set(OpenMP_SHARED_LINKER_FLAGS "${OpenMP_C_FLAGS}")
228                 endif()
229             endif()
230         endif()
231     else(OPENMP_FOUND)
232         message(WARNING
233                 "The compiler you are using does not support OpenMP parallelism. This might hurt your performance a lot, in particular with GPUs. Try using a more recent version, or a different compiler. For now, we are proceeding by turning off OpenMP.")
234         set(GMX_OPENMP OFF CACHE STRING "Whether GROMACS will use OpenMP parallelism." FORCE)
235     endif(OPENMP_FOUND)
236 endif()
237
238
239 include(gmxCFlags)
240 gmx_c_flags()
241
242 # gcc 4.4.x is buggy and crashes when compiling some files with O3 and OpenMP on.
243 # Detect here whether applying a workaround is needed and will apply it later
244 # on the affected files.
245 include(gmxGCC44O3BugWorkaround)
246 gmx_check_gcc44_bug_workaround_needed(GMX_USE_GCC44_BUG_WORKAROUND)
247
248 # clang 3.0 is buggy for some unknown reason detected during adding
249 # the SSE2 group kernels for GROMACS 4.6. If we ever work out what
250 # that is, we should replace these tests with a compiler feature test,
251 # update GROMACS Redmine task #1039 and perhaps report a clang bug.
252 #
253 # In the meantime, until we require CMake 2.8.10 we cannot rely on it to detect
254 # the compiler version for us. So we need a manual check for clang 3.0.
255 include(gmxDetectClang30)
256 gmx_detect_clang_3_0(COMPILER_IS_CLANG_3_0)
257 if(COMPILER_IS_CLANG_3_0)
258     message(FATAL_ERROR "Your compiler is clang version 3.0, which is known to be buggy for GROMACS. Use a different compiler.")
259 endif()
260
261 if (CMAKE_C_COMPILER_ID STREQUAL "PGI")
262     message(WARNING "All tested PGI compiler versions (up to 12.9.0) generate binaries which produce incorrect results, or even fail to compile Gromacs. Highly recommended to use a different compiler. If you choose to use PGI, make sure to run the regressiontests.")
263 endif()
264
265 ########################################################################
266 # Set up binary and library suffixing 
267 ########################################################################
268 set(GMX_BINARY_SUFFIX "" CACHE STRING "Suffix for GROMACS binaries (default: _d for double, _mpi for MPI, _mpi_d for MPI and double).")
269 set(GMX_LIBS_SUFFIX "" 
270   CACHE STRING "Suffix for GROMACS libs (default: _d for double, _mpi for MPI, _mpi_d for MPI and double).")
271 if (GMX_DEFAULT_SUFFIX)
272   set(GMX_BINARY_SUFFIX "")
273   set(GMX_LIBS_SUFFIX "")
274   if (GMX_MPI)
275     set(GMX_BINARY_SUFFIX "_mpi")
276     set(GMX_LIBS_SUFFIX "_mpi")
277   endif(GMX_MPI)
278   if (GMX_DOUBLE)
279     set (GMX_BINARY_SUFFIX "${GMX_BINARY_SUFFIX}_d")
280     set (GMX_LIBS_SUFFIX "${GMX_LIBS_SUFFIX}_d")
281   endif(GMX_DOUBLE)
282   mark_as_advanced(FORCE GMX_BINARY_SUFFIX GMX_LIBS_SUFFIX)
283   if (NOT SUFFIX_QUIETLY)
284     message(STATUS "Using default binary suffix: \"${GMX_BINARY_SUFFIX}\"")
285     message(STATUS "Using default library suffix: \"${GMX_LIBS_SUFFIX}\"")
286   endif (NOT SUFFIX_QUIETLY)
287 else(GMX_DEFAULT_SUFFIX)
288   mark_as_advanced(CLEAR GMX_BINARY_SUFFIX GMX_LIBS_SUFFIX)
289   if (NOT SUFFIX_QUIETLY)
290     message(STATUS "Using manually set binary suffix: \"${GMX_BINARY_SUFFIX}\"")
291     message(STATUS "Using manually set library suffix: \"${GMX_LIBS_SUFFIX}\"")
292   endif (NOT SUFFIX_QUIETLY)
293 endif(GMX_DEFAULT_SUFFIX)
294 set(SUFFIX_QUIETLY TRUE CACHE INTERNAL "")
295
296 set(PKG_CFLAGS "")
297 if(GMX_DOUBLE)
298     set(PKG_CFLAGS "${PKG_CFLAGS} -DGMX_DOUBLE")
299 endif(GMX_DOUBLE)
300 if(GMX_SOFTWARE_INVSQRT)
301   set(PKG_CFLAGS "${PKG_CFLAGS} -DGMX_SOFTWARE_INVSQRT")
302 endif(GMX_SOFTWARE_INVSQRT)
303
304
305
306 ########################################################################
307 # Basic system tests (standard libraries, headers, functions, types)   #
308 ########################################################################
309 include(CheckIncludeFiles)
310 include(CheckIncludeFileCXX)
311 check_include_files(unistd.h     HAVE_UNISTD_H)
312 check_include_files(pwd.h        HAVE_PWD_H)
313 check_include_files(pthread.h    HAVE_PTHREAD_H)
314 check_include_files(dirent.h     HAVE_DIRENT_H)
315 check_include_files(sys/time.h   HAVE_SYS_TIME_H)
316 check_include_files(io.h                 HAVE_IO_H)
317 check_include_files(sched.h      HAVE_SCHED_H)
318
319 check_include_files(regex.h      HAVE_POSIX_REGEX)
320 check_include_file_cxx(regex     HAVE_CXX11_REGEX)
321 # TODO: It could be nice to inform the user if no regex support is found,
322 # as selections won't be fully functional.
323
324 include(CheckFunctionExists)
325 check_function_exists(strdup            HAVE_STRDUP)
326 check_function_exists(posix_memalign    HAVE_POSIX_MEMALIGN)
327 check_function_exists(memalign          HAVE_MEMALIGN)
328 check_function_exists(_aligned_malloc   HAVE__ALIGNED_MALLOC)
329 check_function_exists(gettimeofday      HAVE_GETTIMEOFDAY)
330 check_function_exists(fsync             HAVE_FSYNC)
331 check_function_exists(_fileno           HAVE__FILENO)
332 check_function_exists(fileno            HAVE_FILENO)
333 check_function_exists(_commit           HAVE__COMMIT)
334 check_function_exists(sigaction         HAVE_SIGACTION)
335 check_function_exists(sysconf           HAVE_SYSCONF)
336 check_function_exists(sched_setaffinity HAVE_SCHED_SETAFFINITY)
337 check_function_exists(sched_getaffinity HAVE_SCHED_GETAFFINITY)
338 check_function_exists(rsqrt             HAVE_RSQRT)
339 check_function_exists(rsqrtf            HAVE_RSQRTF)
340 check_function_exists(sqrtf             HAVE_SQRTF)
341
342 include(CheckLibraryExists)
343 check_library_exists(m sqrt "" HAVE_LIBM)
344 check_library_exists(m cbrt "" HAVE_CBRT)
345
346
347 include(CheckTypeSize)
348
349 check_type_size("int"           SIZEOF_INT) 
350 check_type_size("long int"      SIZEOF_LONG_INT) 
351 check_type_size("long long int" SIZEOF_LONG_LONG_INT) 
352 check_type_size("off_t"         SIZEOF_OFF_T)
353 check_type_size("void *"        SIZEOF_VOIDP)
354
355 if (CMAKE_C_SIZEOF_DATA_PTR EQUAL 8)
356     set(GMX_64_BIT TRUE)
357 else (CMAKE_C_SIZEOF_DATA_PTR EQUAL 8)
358     set(GMX_64_BIT FALSE)
359 endif (CMAKE_C_SIZEOF_DATA_PTR EQUAL 8)
360
361 include(TestBigEndian)
362 test_big_endian(GMX_INTEGER_BIG_ENDIAN)
363
364
365 if(APPLE OR CYGWIN OR ${CMAKE_SYSTEM_NAME} MATCHES "Linux|.*BSD")
366     # Maybe Solaris should be here? Patch this if you know!
367     SET(SHARED_LIBS_DEFAULT ON)
368 elseif(WIN32 OR ${CMAKE_SYSTEM_NAME} MATCHES "BlueGene")
369     # Support for shared libs on native Windows is a bit new. Its
370     # default might change later if/when we sort things out. Also,
371     # Cray should go here. What variable value can detect it?
372     SET(SHARED_LIBS_DEFAULT OFF)
373 else()
374     message(STATUS "Defaulting to building static libraries")
375     SET(SHARED_LIBS_DEFAULT OFF)
376 endif()
377
378 # Management of GROMACS options for specific toolchains should go
379 # here. Because the initial settings for some of the main options have
380 # already happened, but things like library detection and MPI compiler
381 # feature detection have not, the docstrings for any over-rides of
382 # GROMACS defaults or user settings will make sense. Also, any
383 # toolchain-related reasons for choosing whether to detect various
384 # things can be sorted out now, before the detection takes place.
385 if(${CMAKE_SYSTEM_NAME} MATCHES BlueGene)
386     include(gmxManageBlueGene)
387 endif()
388
389 if(UNIX AND GMX_PREFER_STATIC_LIBS AND SHARED_LIBS_DEFAULT)
390     if(BUILD_SHARED_LIBS)
391         # Warn the user about the combination. But don't overwrite the request.
392         message(WARNING "Searching for static libraries requested, and building shared Gromacs libraries requested. This might cause problems linking later.")
393     elseif(NOT DEFINED BUILD_SHARED_LIBS)
394         # Change default to OFF. Don't warn if it's already off.
395         message(WARNING "Searching for static libraries requested, so the GROMACS libraries will also be built statically (BUILD_SHARED_LIBS=OFF)")
396         set(SHARED_LIBS_DEFAULT OFF)
397     endif()
398 endif()
399
400 # By now, all tool chains should have spoken up if they care about
401 # the setting of SHARED_LIBS_DEFAULT.
402 option(BUILD_SHARED_LIBS "Enable shared libraries (can be problematic e.g. with MPI, or on some HPC systems)" ${SHARED_LIBS_DEFAULT})
403
404 ########################################################################
405 #Process MPI settings
406 ########################################################################
407 include(gmxManageMPI)
408
409 ########################################################################
410 # Find external packages                                               #
411 ########################################################################
412 if(UNIX AND GMX_PREFER_STATIC_LIBS)
413     # On Linux .a is the static library suffix, on Mac OS X .lib can also
414     # be used, so we'll add both to the preference list.
415     SET(CMAKE_FIND_LIBRARY_SUFFIXES ".lib;.a" ${CMAKE_FIND_LIBRARY_SUFFIXES})
416 endif()
417
418 IF( WIN32 AND NOT CYGWIN)
419   # This makes windows.h not declare min/max as macros that would break
420   # C++ code using std::min/std::max.
421   add_definitions(-DNOMINMAX)
422
423   if (NOT BUILD_SHARED_LIBS)
424       option(GMX_PREFER_STATIC_LIBS "When finding libraries prefer static system libraries (MT instead of MD)!" ON)
425       if(NOT GMX_PREFER_STATIC_LIBS)
426           message(WARNING "Shared system libraries requested, and static Gromacs libraries requested.")
427       endif()
428   else()
429       message(FATAL_ERROR "BUILD_SHARED_LIBS not yet working for Windows in the master branch")
430       option(GMX_PREFER_STATIC_LIBS "When finding libraries prefer static system libraries (MT instead of MD)!" OFF)
431       if(GMX_PREFER_STATIC_LIBS)
432           #this combination segfaults (illigal passing of file handles)
433           message(FATAL_ERROR "Static system libraries requested, and shared Gromacs libraries requested.")
434       endif()
435       add_definitions(-DUSE_VISIBILITY -DTMPI_USE_VISIBILITY)
436       set(PKG_CFLAGS "$PKG_CFLAGS -DUSE_VISIBILITY -DTMPI_USE_VISIBILITY")
437   endif()
438   mark_as_advanced(GMX_PREFER_STATIC_LIBS)
439
440   IF (GMX_PREFER_STATIC_LIBS)
441       #Only setting Debug and Release flags. Others configurations are current not used.
442       STRING(REPLACE /MD /MT CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
443       STRING(REPLACE /MD /MT CMAKE_C_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})
444       if(CMAKE_CXX_COMPILER_LOADED)
445           STRING(REPLACE /MD /MT CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
446           STRING(REPLACE /MD /MT CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
447       endif()
448   ENDIF()
449   IF( CMAKE_C_COMPILER_ID MATCHES "Intel" )
450     if(BUILD_SHARED_LIBS) #not sure why incremental building with shared libs doesn't work
451         STRING(REPLACE "/INCREMENTAL:YES" "" CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS})
452     endif()
453   ENDIF()
454 ENDIF()
455
456 option(GMX_XML "Use libxml2 to parse xml files" ON)
457 if (GMX_XML)
458   find_package(LibXml2)
459   set(PKG_XML "")
460   if(LIBXML2_FOUND)
461     include_directories(${LIBXML2_INCLUDE_DIR})
462     set(PKG_XML libxml-2.0)
463     set(XML_LIBRARIES ${LIBXML2_LIBRARIES})
464   endif(LIBXML2_FOUND)
465 endif(GMX_XML)
466
467 option(GMX_GSL "Add support for gsl" OFF)
468 if (GMX_GSL)
469   find_package(gsl)
470   set(PKG_GSL "")
471   if(GSL_FOUND)
472     include_directories(${GSL_INCLUDE_DIR})
473     set(PKG_GSL gsl)
474     set(HAVE_LIBGSL 1)
475   endif(GSL_FOUND)
476 endif (GMX_GSL)
477
478 option(GMX_X11 "Use X window system" OFF)
479 if (GMX_X11)
480         find_package(X11)
481         # X11 includes/libraries are only set in the ngmx subdirectory!
482         if(X11_FOUND)
483         set(HAVE_X11 1)
484         endif(X11_FOUND)
485 endif(GMX_X11)
486
487 include(ThreadMPI)
488 set(THREAD_MPI_LIB thread_mpi)
489 if(GMX_THREAD_MPI)
490     tmpi_get_source_list(THREAD_MPI_SRC CXX)
491     set(PKG_CFLAGS "${PKG_CFLAGS} -DGMX_THREAD_MPI")
492     set(GMX_MPI 1)
493 else(GMX_THREAD_MPI)
494     tmpi_get_source_list(THREAD_MPI_SRC CXX NOMPI)
495 endif(GMX_THREAD_MPI)
496
497 if(GMX_GPU)
498     # now that we have detected the dependencies, do the second configure pass
499     gmx_gpu_setup()
500 endif(GMX_GPU)
501
502 if(APPLE)
503    find_library(ACCELERATE_FRAMEWORK Accelerate)
504    list(APPEND GMX_EXTRA_LIBRARIES ${ACCELERATE_FRAMEWORK})
505 endif(APPLE)
506
507 if(CYGWIN)
508     set(GMX_CYGWIN 1)
509 endif(CYGWIN)
510
511 if(WIN32 AND NOT CYGWIN)
512     set(GMX_NATIVE_WINDOWS 1)
513 endif()
514
515 # only bother with finding git and using version.h if the source is a git repo
516 if(GMX_GIT_VERSION_INFO)
517     if (NOT SOURCE_IS_GIT_REPOSITORY)
518         message(FATAL_ERROR
519             "Cannot generate git version information from source tree not under git. "
520             "Set GMX_GIT_VERSION_INFO=OFF to proceed.")
521     endif ()
522     # We need at least git v1.5.3 be able to parse git's date output. If not
523     # found or the version is too small, we can't generate version information.
524     find_package(Git)
525
526     # Find out the git version
527     if(GIT_FOUND AND NOT GIT_VERSION)
528       execute_process(COMMAND ${GIT_EXECUTABLE} "--version"
529         OUTPUT_VARIABLE _exec_out
530         OUTPUT_STRIP_TRAILING_WHITESPACE)
531       string(REGEX REPLACE "git version (.*)" "\\1" GIT_VERSION ${_exec_out})
532       set(GIT_VERSION ${GIT_VERSION} CACHE STRING "Git version")
533       mark_as_advanced(GIT_VERSION)
534     endif()
535
536     if(NOT GIT_FOUND OR GIT_VERSION VERSION_LESS "1.5.3")
537         message(FATAL_ERROR
538             "No compatible git version found (>= 1.5.3 required). "
539             "Won't be able to generate development version information. "
540             "Set GMX_GIT_VERSION_INFO=OFF to proceed.")
541     endif()
542 endif()
543
544 option(GMX_INTERNAL_BOOST "Use minimal internal version of boost" OFF)
545 if ( NOT GMX_INTERNAL_BOOST )
546   find_package( Boost 1.44.0 )
547 else ( NOT GMX_INTERNAL_BOOST )
548   set (Boost_FOUND FALSE)
549 endif( NOT GMX_INTERNAL_BOOST )
550
551 if(Boost_FOUND AND NOT Boost_VERSION VERSION_LESS "104400" )
552   include_directories(${Boost_INCLUDE_DIRS})
553 else()
554   if(NOT BOOST_QUIETLY)
555     message("Boost >= 1.44 not found. Using minimal internal version. Not recommended if GROMACS is used as library!")
556   endif()
557   include_directories(${CMAKE_SOURCE_DIR}/src/external/boost)
558   add_definitions( -DBOOST_NO_TYPEID )  #TYPEID not supported for minimal internal version (would add significant more code)
559   # TODO: Propagate the above settings to the installed CMakeFiles.txt template
560   # (from share/template/)
561   # TODO: Reorder stuff such that INCL_INSTALL_DIR could be used here
562   set(PKG_CFLAGS "${PKG_CFLAGS} -DBOOST_NO_TYPEID -I${GMX_INSTALL_PREFIX}include/gromacs/external/boost")
563   install(DIRECTORY ${CMAKE_SOURCE_DIR}/src/external/boost/boost
564           DESTINATION ${GMX_INSTALL_PREFIX}include/gromacs/external/boost
565           COMPONENT development)
566 endif()
567 set(BOOST_QUIETLY TRUE CACHE INTERNAL "")
568
569 if (LIBXML2_FOUND)
570     option(GMX_BUILD_UNITTESTS "Build unit tests with BUILD_TESTING (uses Google C++ Testing and Mocking Frameworks, requires libxml2)" ON)
571 else (LIBXML2_FOUND)
572     set(GMX_BUILD_UNITTESTS OFF)
573 endif (LIBXML2_FOUND)
574 mark_as_advanced(GMX_BUILD_UNITTESTS)
575 set(MEMORYCHECK_SUPPRESSIONS_FILE ${CMAKE_SOURCE_DIR}/cmake/legacy_and_external.supp)
576
577 ########################################################################
578 # Generate development version info for cache
579 ########################################################################
580 # set(GEN_VERSION_INFO_INTERNAL "ON")
581 # include(gmxGenerateVersionString)
582
583 ########################################################################
584 # Our own GROMACS tests
585 ########################################################################
586
587 add_definitions( -DHAVE_CONFIG_H )
588 include_directories(${CMAKE_SOURCE_DIR}/src)
589 # Required for config.h, maybe should only be set in src/CMakeLists.txt
590 include_directories(${CMAKE_BINARY_DIR}/src)
591 # Required for gmx_header_config_gen.h to be found before installation
592 include_directories(${CMAKE_BINARY_DIR}/src/gromacs/utility)
593 # Required for now to make old code compile
594 include_directories(${CMAKE_SOURCE_DIR}/src/gromacs/legacyheaders)
595
596 include(gmxTestInlineASM)
597 gmx_test_inline_asm_gcc_x86(GMX_X86_GCC_INLINE_ASM)
598
599 include(gmxSetBuildInformation)
600 gmx_set_build_information()
601 if(BUILD_CPU_FEATURES MATCHES "rdtscp" AND NOT GMX_DISTRIBUTABLE_BUILD)
602     # The timestep counter headers do not include config.h
603     add_definitions(-DHAVE_RDTSCP)
604 endif(BUILD_CPU_FEATURES MATCHES "rdtscp" AND NOT GMX_DISTRIBUTABLE_BUILD)
605
606 include(gmxTestFloatFormat)
607 gmx_test_float_format(GMX_FLOAT_FORMAT_IEEE754 
608                       GMX_IEEE754_BIG_ENDIAN_BYTE_ORDER
609                       GMX_IEEE754_BIG_ENDIAN_WORD_ORDER)
610
611 include(gmxTestLargeFiles)
612 gmx_test_large_files(GMX_LARGEFILES)
613
614 include(gmxTestSignal)
615 gmx_test_sigusr1(HAVE_SIGUSR1)
616
617 include(gmxTestInline)
618 gmx_test_inline(INLINE_KEYWORD)
619
620 include(gmxTestRestrict)
621 gmx_test_restrict(RESTRICT_KEYWORD)
622
623 include(gmxTestPipes)
624 gmx_test_pipes(HAVE_PIPES)
625
626 include(gmxTestIsfinite)
627 gmx_test_isfinite(HAVE_ISFINITE)
628 gmx_test__isfinite(HAVE__ISFINITE)
629 gmx_test__finite(HAVE__FINITE)
630
631 include(gmxTestCXX11)
632 gmx_test_cxx11(GMX_CXX11 GMX_CXX11_FLAGS)
633 if(CXX11_FLAG AND GMX_GPU)
634     #FIXME: add proper solution for progate all but cxx11 flag
635     set(CUDA_PROPAGATE_HOST_FLAGS no)
636     message(WARNING "Please manually add compiler flags to CUDA_NVCC_FLAGS. Automatic propogation temporary not working.")
637 endif()
638
639 include(gmxTestXDR)
640 gmx_test_xdr(GMX_SYSTEM_XDR)
641 if(NOT GMX_SYSTEM_XDR)
642     set(GMX_INTERNAL_XDR 1)
643     set(PKG_CFLAGS "${PKG_CFLAGS} -DGMX_INTERNAL_XDR")
644 endif(NOT GMX_SYSTEM_XDR)
645
646 # include avx test source, used if the AVX flags are set below
647 include(gmxTestAVXMaskload)
648
649 # Process nonbonded accelerated kernels settings
650 string(TOUPPER ${GMX_CPU_ACCELERATION} ${GMX_CPU_ACCELERATION})
651 if(${GMX_CPU_ACCELERATION} STREQUAL "NONE")
652     # nothing to do
653 elseif(${GMX_CPU_ACCELERATION} STREQUAL "SSE2")
654
655     GMX_TEST_CFLAG(GNU_SSE2_CFLAG "-msse2" ACCELERATION_C_FLAGS)
656     if(NOT GNU_SSE2_CFLAG AND GMX_NATIVE_WINDOWS)
657         GMX_TEST_CFLAG(MSVC_SSE2_CFLAG "/arch:SSE2" ACCELERATION_C_FLAGS)
658     endif(NOT GNU_SSE2_CFLAG AND GMX_NATIVE_WINDOWS)
659
660     GMX_TEST_CXXFLAG(GNU_SSE2_CXXFLAG "-msse2" ACCELERATION_CXX_FLAGS)
661     if(NOT GNU_SSE2_CXXFLAG AND GMX_NATIVE_WINDOWS)
662         GMX_TEST_CXXFLAG(MSVC_SSE2_CXXFLAG "/arch:SSE2" ACCELERATION_CXX_FLAGS)
663     endif(NOT GNU_SSE2_CXXFLAG AND GMX_NATIVE_WINDOWS)
664
665     # We dont warn for lacking SSE2 flag support, since that is probably standard today.
666
667     # Only test the include after we have tried to add the correct flag for SSE2 support
668     check_include_file(emmintrin.h  HAVE_EMMINTRIN_H ${ACCELERATION_C_FLAGS})
669
670     if(NOT HAVE_EMMINTRIN_H)
671         message(FATAL_ERROR "Cannot find emmintrin.h, which is required for SSE2 intrinsics support.")
672     endif(NOT HAVE_EMMINTRIN_H)
673
674     set(GMX_CPU_ACCELERATION_X86_SSE2 1)
675     # The user should not be able to set this orthogonally to the acceleration
676     set(GMX_X86_SSE2 1)
677     if (NOT ACCELERATION_QUIETLY)
678       message(STATUS "Enabling SSE2 Gromacs acceleration, and it will help compiler optimization.")
679     endif()
680
681 elseif(${GMX_CPU_ACCELERATION} STREQUAL "SSE4.1")
682
683     GMX_TEST_CFLAG(GNU_SSE4_CFLAG "-msse4.1" ACCELERATION_C_FLAGS)
684     if (NOT GNU_SSE4_CFLAG AND GMX_NATIVE_WINDOWS)
685         GMX_TEST_CFLAG(MSVC_SSE4_CFLAG "/arch:SSE4.1" ACCELERATION_C_FLAGS)
686     endif(NOT GNU_SSE4_CFLAG AND GMX_NATIVE_WINDOWS)
687     if (NOT GNU_SSE4_CFLAG AND NOT MSVC_SSE4_CFLAG)
688         # Not surprising if we end up here! MSVC current does not support the SSE4.1 flag. However, it appears to accept SSE4.1
689         # intrinsics when SSE2 support is enabled, so we try that instead first.
690         if (GMX_NATIVE_WINDOWS)
691             GMX_TEST_CFLAG(MSVC_SSE2_CFLAG "/arch:SSE2" ACCELERATION_C_FLAGS)
692             message(WARNING "Neither SSE4.1 or SSE2 seems to be supported by your Windows compiler. Something is likely broken.")
693         else()
694             message(WARNING "No C SSE4.1 flag found. Consider a newer compiler, or use SSE2 for slightly lower performance")
695         endif()
696     endif(NOT GNU_SSE4_CFLAG AND NOT MSVC_SSE4_CFLAG)
697
698     GMX_TEST_CXXFLAG(GNU_SSE4_CXXFLAG "-msse4.1" ACCELERATION_CXX_FLAGS)
699     if (NOT GNU_SSE4_CXXFLAG AND GMX_NATIVE_WINDOWS)
700         GMX_TEST_CXXFLAG(MSVC_SSE4_CXXFLAG "/arch:SSE4.1" ACCELERATION_CXX_FLAGS)
701     endif(NOT GNU_SSE4_CXXFLAG AND GMX_NATIVE_WINDOWS)
702     if (NOT GNU_SSE4_CXXFLAG AND NOT MSVC_SSE4_CXXFLAG)
703         message(WARNING "No C++ SSE4.1 flag found. Consider a newer compiler, or use SSE2 for slightly lower performance.")
704         # Not surprising if we end up here! MSVC current does not support the SSE4.1 flag. However, it appears to accept SSE4.1
705         # intrinsics when SSE2 support is enabled, so we try that instead.
706         if (GMX_NATIVE_WINDOWS)
707             GMX_TEST_CXXFLAG(MSVC_SSE2_CXXFLAG "/arch:SSE2" ACCELERATION_CXX_FLAGS)
708         endif()
709     endif(NOT GNU_SSE4_CXXFLAG AND NOT MSVC_SSE4_CXXFLAG)
710
711     # This must come after we have added the -msse4.1 flag on some platforms.
712     check_include_file(smmintrin.h  HAVE_SMMINTRIN_H ${ACCELERATION_C_FLAGS})
713
714     if(NOT HAVE_SMMINTRIN_H)
715         message(FATAL_ERROR "Cannot find smmintrin.h, which is required for SSE4.1 intrinsics support.")
716     endif(NOT HAVE_SMMINTRIN_H)
717
718     set(GMX_CPU_ACCELERATION_X86_SSE4_1 1)
719     # The user should not be able to set this orthogonally to the acceleration
720     set(GMX_X86_SSE4_1 1)
721     set(GMX_X86_SSE2   1)
722     if (NOT ACCELERATION_QUIETLY)
723       message(STATUS "Enabling SSE4.1 Gromacs acceleration, and it will help compiler optimization.")
724     endif()
725
726 elseif(${GMX_CPU_ACCELERATION} STREQUAL "AVX_128_FMA" OR ${GMX_CPU_ACCELERATION} STREQUAL "AVX_256")
727
728     # Set the AVX compiler flag for both these choices!
729
730     GMX_TEST_CFLAG(GNU_AVX_CFLAG "-mavx" ACCELERATION_C_FLAGS)
731     if (NOT GNU_AVX_CFLAG AND GMX_NATIVE_WINDOWS)
732         GMX_TEST_CFLAG(MSVC_AVX_CFLAG "/arch:AVX" ACCELERATION_C_FLAGS)
733     endif (NOT GNU_AVX_CFLAG AND GMX_NATIVE_WINDOWS)
734     if (NOT GNU_AVX_CFLAG AND NOT MSVC_AVX_CFLAG)
735         message(WARNING "No C AVX flag found. Consider a newer compiler, or try SSE4.1 (lower performance).")
736     endif (NOT GNU_AVX_CFLAG AND NOT MSVC_AVX_CFLAG)
737
738     GMX_TEST_CXXFLAG(GNU_AVX_CXXFLAG "-mavx" ACCELERATION_CXX_FLAGS)
739     if (NOT GNU_AVX_CXXFLAG AND GMX_NATIVE_WINDOWS)
740         GMX_TEST_CXXFLAG(MSVC_AVX_CXXFLAG "/arch:AVX" ACCELERATION_CXX_FLAGS)
741     endif (NOT GNU_AVX_CXXFLAG AND GMX_NATIVE_WINDOWS)
742     if (NOT GNU_AVX_CXXFLAG AND NOT MSVC_AVX_CXXFLAG)
743        message(WARNING "No C++ AVX flag found. Consider a newer compiler, or try SSE4.1 (lower performance).")
744     endif (NOT GNU_AVX_CXXFLAG AND NOT MSVC_AVX_CXXFLAG)
745
746     # Set the FMA4 flags (MSVC doesn't require any)
747     if(${GMX_CPU_ACCELERATION} STREQUAL "AVX_128_FMA" AND NOT MSVC)
748         if (${CMAKE_COMPILER_ID} MATCHES "Clang")
749             message(FATAL_ERROR "Clang up to at least version 3.2 produces incorrect code for AVX_128_FMA. Sorry, but you will have to select a different compiler or acceleration.")
750         endif()
751         GMX_TEST_CFLAG(GNU_FMA_CFLAG "-mfma4" ACCELERATION_C_FLAGS)
752         if (NOT GNU_FMA_CFLAG)
753             message(WARNING "No C FMA4 flag found. Consider a newer compiler, or try SSE4.1 (lower performance).")
754         endif(NOT GNU_FMA_CFLAG)
755         GMX_TEST_CFLAG(GNU_XOP_CFLAG "-mxop" ACCELERATION_C_FLAGS)
756         # No big deal if we do not have xop, so no point yelling warnings about it.
757         if (CMAKE_CXX_COMPILER_LOADED)
758             GMX_TEST_CXXFLAG(GNU_FMA_CXXFLAG "-mfma4" ACCELERATION_CXX_FLAGS)
759             if (NOT GNU_FMA_CXXFLAG)
760                 message(WARNING "No C++ FMA flag found. Consider a newer compiler, or try SSE4.1 (lower performance).")
761             endif (NOT GNU_FMA_CXXFLAG)
762             GMX_TEST_CXXFLAG(GNU_XOP_CXXFLAG "-mxop" ACCELERATION_CXX_FLAGS)
763             # No big deal if we do not have xop, so no point yelling warnings about it.
764         endif()
765     endif()
766
767     # Only test the header after we have tried to add the flag for AVX support
768     check_include_file(immintrin.h  HAVE_IMMINTRIN_H ${ACCELERATION_C_FLAGS})
769
770     if(NOT HAVE_IMMINTRIN_H)
771         message(FATAL_ERROR "Cannot find immintrin.h, which is required for AVX intrinsics support. Consider switching compiler.")
772     endif(NOT HAVE_IMMINTRIN_H)
773
774     if(${GMX_CPU_ACCELERATION} STREQUAL "AVX_256")
775         try_compile(TEST_AVX ${CMAKE_BINARY_DIR}
776             "${CMAKE_SOURCE_DIR}/cmake/TestAVX.c"
777             COMPILE_DEFINITIONS "${ACCELERATION_C_FLAGS}")
778         if(NOT TEST_AVX)
779             message(FATAL_ERROR "Cannot compile AVX intrinsics. Consider switching compiler.")
780         endif()
781     endif()
782
783     # GCC requires x86intrin.h for FMA support. MSVC 2010 requires intrin.h for FMA support.
784     check_include_file(x86intrin.h HAVE_X86INTRIN_H ${ACCELERATION_C_FLAGS})
785     check_include_file(intrin.h HAVE_INTRIN_H ${ACCELERATION_C_FLAGS})
786
787     # The user should not be able to set this orthogonally to the acceleration
788     set(GMX_X86_SSE4_1 1)
789     set(GMX_X86_SSE2   1)
790
791     # But just enable one of the choices internally...
792     if(${GMX_CPU_ACCELERATION} STREQUAL "AVX_128_FMA")
793         set(GMX_CPU_ACCELERATION_X86_AVX_128_FMA 1)
794         set(GMX_X86_AVX_128_FMA 1)
795         if (NOT ACCELERATION_QUIETLY)
796           message(STATUS "Enabling 128-bit AVX Gromacs acceleration (with fused-multiply add), and it will help compiler optimization.")
797         endif()
798     else()
799         # If we are not doing AVX_128, it must be AVX_256...
800         set(GMX_CPU_ACCELERATION_X86_AVX_256 1)
801         set(GMX_X86_AVX_256 1)
802         if (NOT ACCELERATION_QUIETLY)
803           message(STATUS "Enabling 256-bit AVX Gromacs acceleration, and it will help compiler optimization.")
804         endif()
805     endif()
806
807     # Unfortunately gcc-4.5.2 and gcc-4.6.0 has a bug where they use the wrong datatype for the formal
808     # parameter of the mask for maskload/maskstore arguments. Check if this is present, since we can work around it.
809     gmx_test_avx_gcc_maskload_bug(${ACCELERATION_C_FLAGS} GMX_X86_AVX_GCC_MASKLOAD_BUG)
810
811 else(${GMX_CPU_ACCELERATION} STREQUAL "IBM_QPX")
812     # Used on BlueGene/Q
813     if (CMAKE_C_COMPILER_ID MATCHES "XL")
814         GMX_TEST_CFLAG(XLC_BLUEGENEQ_CFLAG "-qarch=qp -qtune=qp" ACCELERATION_C_FLAGS)
815         try_compile(TEST_QPX ${CMAKE_BINARY_DIR}
816             "${CMAKE_SOURCE_DIR}/cmake/TestQPX.c"
817             COMPILE_DEFINITIONS "${ACCELERATION_C_FLAGS}")
818         if(NOT TEST_QPX)
819             message(FATAL_ERROR "Cannot compile the requested IBM QPX intrinsics.")
820         endif()
821     endif()
822     if (CMAKE_CXX_COMPILER_ID MATCHES "XL" AND CMAKE_CXX_COMPILER_LOADED)
823         GMX_TEST_CXXFLAG(XLC_BLUEGENEQ_CXXFLAG "-qarch=qp -qtune=qp" ACCELERATION_CXX_FLAGS)
824         try_compile(TEST_QPX ${CMAKE_BINARY_DIR}
825             "cmake/TestQPX.c"
826             COMPILE_DEFINITIONS "${ACCELERATION_CXX_FLAGS")
827         if(NOT TEST_QPX)
828             message(FATAL_ERROR "Cannot compile the requested IBM QPX intrinsics.")
829         endif()
830     endif()
831
832     if (TEST_QPX)
833         message(WARNING "IBM QPX acceleration was selected and could be compiled, but the accelerated kernels are not yet available.")
834         set(GMX_CPU_ACCELERATION_IBM_QPX 1)
835     else()
836         message(FATAL_ERROR "Cannot compile IBM QPX intrinsics without the XL compiler. If you are compiling for BlueGene/Q, use 'cmake .. -DCMAKE_TOOLCHAIN_FILE=BlueGeneQ-static-XL-C' to set up the tool chain.")
837     endif()
838
839 else(${GMX_CPU_ACCELERATION} STREQUAL "NONE")
840     MESSAGE(FATAL_ERROR "Unrecognized option for accelerated kernels: ${GMX_CPU_ACCELERATION}. Pick one of None, SSE2, SSE4.1, AVX_128_FMA, AVX_256, IBM_QPX")
841 endif(${GMX_CPU_ACCELERATION} STREQUAL "NONE")
842 set(ACCELERATION_QUIETLY TRUE CACHE INTERNAL "")
843
844 # Process QM/MM Settings
845 string(TOUPPER ${GMX_QMMM_PROGRAM} ${GMX_QMMM_PROGRAM})
846 if(${GMX_QMMM_PROGRAM} STREQUAL "GAUSSIAN")
847     set(GMX_QMMM_GAUSSIAN 1)
848 elseif(${GMX_QMMM_PROGRAM} STREQUAL "MOPAC")
849     set(GMX_QMMM_MOPAC 1)
850 elseif(${GMX_QMMM_PROGRAM} STREQUAL "GAMESS")
851     set(GMX_QMMM_GAMESS 1)
852 elseif(${GMX_QMMM_PROGRAM} STREQUAL "ORCA")
853     set(GMX_QMMM_ORCA 1)
854 elseif(${GMX_QMMM_PROGRAM} STREQUAL "NONE")
855     # nothing to do
856 else(${GMX_QMMM_PROGRAM} STREQUAL "GAUSSIAN")
857     MESSAGE(FATAL_ERROR "Invalid QM/MM program option: ${GMX_QMMM_PROGRAM}. Choose one of: Gaussian, Mopac, Gamess, Orca, None")
858 endif(${GMX_QMMM_PROGRAM} STREQUAL "GAUSSIAN")
859
860 # Process FFT library settings
861 string(TOUPPER ${GMX_FFT_LIBRARY} ${GMX_FFT_LIBRARY})
862 set(PKG_FFT "")
863 set(PKG_FFT_LIBS "")
864 if(${GMX_FFT_LIBRARY} STREQUAL "FFTW3")
865     if(GMX_DOUBLE)
866         set(FFTW fftw)
867     else()
868         set(FFTW fftwf)
869     endif()
870
871     if(GMX_BUILD_OWN_FFTW)
872       add_subdirectory(src/contrib/fftw)
873     else()
874       find_package(FFTW COMPONENTS ${FFTW})
875     endif()
876
877     string(TOUPPER "${FFTW}" FFTW)
878     set(PKG_FFT "${${FFTW}_PKG}")
879     include_directories(${${FFTW}_INCLUDE_DIRS})
880     set(FFT_LIBRARIES ${${FFTW}_LIBRARIES})
881     if(NOT ${FFTW}_FOUND)
882       MESSAGE(FATAL_ERROR "Cannot find FFTW 3 (with correct precision - libfftw3f for single-precision GROMACS or libfftw3 for double-precision GROMACS). Either choose the right precision, choose another FFT(W) library, enable the advanced option to let GROMACS build FFTW 3 for you, or use the really slow GROMACS built-in fftpack library.")
883     endif()
884
885     set(GMX_FFT_FFTW3 1)
886
887     if (NOT ${GMX_CPU_ACCELERATION} STREQUAL "NONE" AND NOT ${FFTW}_HAVE_SIMD) 
888       message(WARNING "The fftw library found is compiled without SIMD support, which makes it slow. Consider recompiling it or contact your admin")
889     endif()
890
891     if(NOT ${GMX_CPU_ACCELERATION} STREQUAL "NONE" AND ${FFTW}_HAVE_AVX)
892         # If we're not doing CPU acceleration, we don't care about FFTW performance on x86 either
893         message(WARNING "The FFTW library was compiled with --enable-avx to enable AVX SIMD instructions. That might sound like a good idea for your processor, but for FFTW versions up to 3.3.3, these are slower than the SSE/SSE2 SIMD instructions for the way GROMACS uses FFTs. Limitations in the way FFTW allows GROMACS to measure performance make it awkward for either GROMACS or FFTW to make the decision for you based on runtime performance. You should compile a different FFTW library with --enable-sse or --enable-sse2. If you have a more recent FFTW, you may like to compare the performance of GROMACS with FFTW libraries compiled with and without --enable-avx. However, the GROMACS developers do not really expect the FFTW AVX optimization to help, because the performance is limited by memory access, not computation.")
894     endif()
895
896 elseif(${GMX_FFT_LIBRARY} STREQUAL "MKL")
897 #    MESSAGE(STATUS "Using external FFT library - Intel MKL")
898     find_package(MKL REQUIRED)
899     include_directories(${MKL_INCLUDE_DIR})
900     set(FFT_LIBRARIES ${MKL_LIBRARIES})
901     set(PKG_FFT_LIBS ${MKL_LIBRARIES})
902
903     set(GMX_FFT_MKL 1)
904     set(HAVE_MKL 1)
905
906 #elseif(${GMX_FFT_LIBRARY} STREQUAL "ACML")
907 #    MESSAGE(STATUS "Using external FFT library - AMD core math library")
908 #    set(GMX_FFT_ACML 1)
909 elseif(${GMX_FFT_LIBRARY} STREQUAL "FFTPACK")
910     MESSAGE(STATUS "Using internal FFT library - fftpack")
911     set(GMX_FFT_FFTPACK 1)
912 else(${GMX_FFT_LIBRARY} STREQUAL "FFTW3")
913     MESSAGE(FATAL_ERROR "Invalid FFT library setting: ${GMX_FFT_LIBRARY}. Choose one of: fftw3, mkl, fftpack")
914 endif(${GMX_FFT_LIBRARY} STREQUAL "FFTW3")
915
916 # enable threaded fftw3 if we've found it 
917 if(FFTW3_THREADS OR FFTW3F_THREADS)
918     add_definitions(-DFFT5D_FFTW_THREADS)
919 endif()
920
921 set(GMX_EXTERNAL_BLAS TRUE CACHE BOOL "Use external BLAS instead of built-in")
922 set(GMX_EXTERNAL_LAPACK TRUE CACHE BOOL "Use external LAPACK instead of built-in")
923 # MKL has BLAS/LAPACK routines
924 if(NOT HAVE_MKL AND NOT ACCELERATE_FRAMEWORK)
925   if(GMX_EXTERNAL_BLAS)
926     if (GMX_BLAS_USER)
927         list(APPEND GMX_EXTRA_LIBRARIES ${GMX_BLAS_USER})
928     else(GMX_BLAS_USER)
929         set(BLAS_FIND_QUIETLY ON)
930         find_package(BLAS)
931         if (BLAS_FOUND)
932           list(APPEND GMX_EXTRA_LIBRARIES ${BLAS_LIBRARIES})
933         else()
934           MESSAGE(STATUS "Using internal BLAS library")
935           set(GMX_EXTERNAL_BLAS FALSE CACHE BOOL "Use external BLAS instead of built-in" FORCE)
936         endif()
937     endif(GMX_BLAS_USER)
938   endif(GMX_EXTERNAL_BLAS)
939   if(GMX_EXTERNAL_LAPACK)
940     if (GMX_LAPACK_USER)
941         list(APPEND GMX_EXTRA_LIBRARIES ${GMX_LAPACK_USER})
942     else(GMX_LAPACK_USER)
943         set(LAPACK_FIND_QUIETLY ON)
944         find_package(LAPACK)
945         if (LAPACK_FOUND)
946           list(APPEND GMX_EXTRA_LIBRARIES ${LAPACK_LIBRARIES})
947         else()
948           MESSAGE(STATUS "Using internal LAPACK library")
949           set(GMX_EXTERNAL_LAPACK FALSE CACHE BOOL "Use external LAPACK instead of built-in" FORCE)
950         endif()
951     endif(GMX_LAPACK_USER)
952   endif(GMX_EXTERNAL_LAPACK)
953 endif()
954 mark_as_advanced(GMX_EXTERNAL_LAPACK)
955 mark_as_advanced(GMX_EXTERNAL_BLAS)
956
957 set(GMX_USE_PLUGINS OFF CACHE INTERNAL "Whether GROMACS will really try to compile support for VMD plugins")
958
959 if(GMX_LOAD_PLUGINS)
960   if(CYGWIN OR NOT WIN32)
961     # Native Windows does not have, nor need dlopen
962     # Note that WIN32 is set with Cygwin, but Cygwin needs dlopen to use plug-ins
963     include(gmxTestdlopen)
964     gmx_test_dlopen(HAVE_DLOPEN)
965   endif()
966
967   # so, should we use plug-ins?
968   if((WIN32 AND NOT CYGWIN) OR (HAVE_DLOPEN AND BUILD_SHARED_LIBS))
969     if(NOT VMD_QUIETLY)
970       MESSAGE(STATUS
971           "Found the ability to use plug-ins when building shared libaries, "
972           "so will compile to use plug-ins (e.g. to read VMD-supported file "
973           "formats).")
974     endif()
975     if(NOT GMX_VMD_PLUGIN_PATH)
976       find_package(VMD)
977     endif()
978     set(GMX_USE_PLUGINS ON)
979     list(APPEND GMX_EXTRA_LIBRARIES ${CMAKE_DL_LIBS}) # magic cross-platform pre-set variable for dlopen library
980     set(PKG_DL_LIBS "-l${CMAKE_DL_LIBS}")
981   else()
982     set(PKG_DL_LIBS)
983   endif()
984 endif(GMX_LOAD_PLUGINS)
985 set(VMD_QUIETLY TRUE CACHE INTERNAL "")
986
987 if(EXISTS "${CMAKE_SOURCE_DIR}/admin/.isreposource")
988     if(NOT CMAKE_CROSSCOMPILING)
989         option(GMX_BUILD_MANPAGES "Build man pages" ON)
990     else()
991         message(STATUS "Building the man pages is not available when "
992             "cross-compiling the developer version from git")
993     endif()
994 else()
995     #make sure source package contains all man pages
996     if(NOT EXISTS "${CMAKE_SOURCE_DIR}/man/man1/ngmx.1")
997         message(FATAL_ERROR "Man pages are missing from source package.")
998     endif()
999 endif()
1000 mark_as_advanced(GMX_BUILD_MANPAGES)
1001
1002 # Math and thread libraries must often come after all others when linking...
1003 if(HAVE_LIBM)
1004     list(APPEND GMX_EXTRA_LIBRARIES m)
1005 endif(HAVE_LIBM)
1006
1007 if(GMX_FAHCORE)
1008   set(COREWRAP_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/../corewrap" CACHE STRING 
1009       "Path to swindirect.h")
1010   include_directories(${COREWRAP_INCLUDE_DIR})
1011   set_property(CACHE GMX_COOL_QUOTES VALUE OFF)
1012 endif(GMX_FAHCORE)
1013
1014 # # # # # # # # # # NO MORE TESTS AFTER THIS LINE! # # # # # # # # # # #
1015 # these are set after everything else
1016 if (NOT GMX_SKIP_DEFAULT_CFLAGS)
1017     set(CMAKE_C_FLAGS "${ACCELERATION_C_FLAGS} ${MPI_COMPILE_FLAGS} ${CMAKE_C_FLAGS}")
1018     set(CMAKE_CXX_FLAGS "${ACCELERATION_CXX_FLAGS} ${MPI_COMPILE_FLAGS} ${GMX_CXX11_FLAGS} ${CMAKE_CXX_FLAGS}")
1019     set(CMAKE_EXE_LINKER_FLAGS "${MPI_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}")
1020     set(CMAKE_SHARED_LINKER_FLAGS "${MPI_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS}")
1021 else()
1022     message("Recommended flags which are not added because GMX_SKIP_DEFAULT_CFLAGS=yes:")
1023     message("CMAKE_C_FLAGS: ${ACCELERATION_C_FLAGS} ${MPI_COMPILE_FLAGS} ${GMXC_CFLAGS}")
1024     message("CMAKE_C_FLAGS_RELEASE: ${GMXC_CFLAGS_RELEASE}")
1025     message("CMAKE_C_FLAGS_DEBUG: ${GMXC_CFLAGS_DEBUG}")
1026     message("CMAKE_CXX_FLAGS: ${ACCELERATION_CXX_FLAGS} ${MPI_COMPILE_FLAGS} ${GMX_CXX11_FLAGS} ${GMXC_CXXFLAGS}")
1027     message("CMAKE_CXX_FLAGS_RELEASE: ${GMXC_CXXFLAGS_RELEASE}")
1028     message("CMAKE_CXX_FLAGS_DEBUG: ${GMXC_CXXFLAGS_DEBUG}")
1029     message("CMAKE_EXE_LINKER_FLAGS: ${MPI_LINKER_FLAGS}")
1030     message("CMAKE_SHARED_LINKER_FLAGS: ${MPI_LINKER_FLAGS}")
1031 endif()
1032
1033 if(NOT GMX_OPENMP)
1034     #Unset all OpenMP flags in case OpenMP was disabled either by the user
1035     #or because it was only partially detected (e.g. only for C but not C++ compiler)
1036     unset(OpenMP_C_FLAGS CACHE) 
1037     unset(OpenMP_CXX_FLAGS CACHE)
1038 else()
1039     set(GMX_EXE_LINKER_FLAGS ${GMX_EXE_LINKER_FLAGS} ${OpenMP_LINKER_FLAGS})
1040     set(GMX_SHARED_LINKER_FLAGS ${GMX_SHARED_LINKER_FLAGS} ${OpenMP_SHARED_LINKER_FLAGS})
1041 endif()
1042 set(PKG_CFLAGS "${PKG_CFLAGS} ${OpenMP_C_FLAGS}")
1043
1044 ######################################
1045 # Output compiler and CFLAGS used
1046 ######################################
1047 get_compiler_info(C BUILD_C_COMPILER BUILD_CFLAGS)
1048 if (CMAKE_CXX_COMPILER_LOADED)
1049     get_compiler_info(CXX BUILD_CXX_COMPILER BUILD_CXXFLAGS)
1050 endif ()
1051
1052 ########################################################################
1053 # Specify install locations and which subdirectories to process        #
1054 ########################################################################
1055 if ( NOT DEFINED GMXLIB )
1056     set(GMXLIB lib)
1057 endif()
1058 set(LIB_INSTALL_DIR "${GMX_INSTALL_PREFIX}${GMXLIB}")
1059 set(BIN_INSTALL_DIR  ${GMX_INSTALL_PREFIX}bin)
1060 set(DATA_INSTALL_DIR ${GMX_INSTALL_PREFIX}share/gromacs)
1061 set(MAN_INSTALL_DIR  ${GMX_INSTALL_PREFIX}share/man)
1062 set(INCL_INSTALL_DIR ${GMX_INSTALL_PREFIX}include)
1063
1064 set(GMXLIBDIR        ${DATA_INSTALL_DIR}/top)
1065
1066 ##################################################################
1067 # Shared library settings
1068 ##################################################################
1069 if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
1070     set(CMAKE_SKIP_BUILD_RPATH  FALSE)
1071     set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
1072     set(CMAKE_INSTALL_RPATH "\\\$ORIGIN/../${GMXLIB}")
1073     set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
1074 else()
1075     if(CMAKE_SYSTEM_VERSION VERSION_GREATER 8.0) #rpath supported for >10.4
1076         set(CMAKE_INSTALL_NAME_DIR "@rpath")
1077         set(GMX_EXE_LINKER_FLAGS ${GMX_EXE_LINKER_FLAGS} "-Wl,-rpath,@executable_path/../lib")
1078     else()
1079         set(CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/${LIB_INSTALL_DIR}")
1080     endif()
1081 endif()
1082
1083 #COPYING file: Only necessary for binary distributions.
1084 #Simpler to always install.
1085 install(FILES COPYING DESTINATION ${DATA_INSTALL_DIR} COMPONENT data)
1086
1087 add_subdirectory(doxygen)
1088 add_subdirectory(share)
1089 add_subdirectory(src)
1090 add_subdirectory(scripts)
1091
1092 # Issue a warning if NVIDIA GPUs were detected, but CUDA was not found.
1093 # Don't bother the user after the first configure pass.
1094 if ((CUDA_NOTFOUND_AUTO AND GMX_DETECT_GPU_AVAILABLE) AND NOT GMX_GPU_DETECTION_DONE)
1095     message(WARNING "${CUDA_NOTFOUND_MESSAGE}")
1096     unset(CUDA_NOTFOUND_AUTO)
1097     unset(CUDA_NOTFOUND_MESSAGE)
1098 endif()
1099 set(GMX_GPU_DETECTION_DONE TRUE CACHE INTERNAL "Whether GPU detection has already been done")
1100
1101 #######################
1102 ## uninstall target
1103 #######################
1104     CONFIGURE_FILE(
1105                    "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
1106                    "${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake"
1107                    IMMEDIATE @ONLY)
1108 ###########################
1109 ADD_CUSTOM_TARGET(uninstall
1110                   "${CMAKE_COMMAND}" -P 
1111                   "${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake")
1112 ###########################
1113
1114 ########################################################################
1115 # Tests                                                                #
1116 ########################################################################
1117
1118 include(CTest)
1119 mark_as_advanced(BUILD_TESTING)
1120 #gmxtests target builds all binaries required for running gmxtest
1121 add_custom_target(gmxtests DEPENDS grompp mdrun pdb2gmx gmxcheck gmx)
1122 IF(BUILD_TESTING)
1123     enable_testing()
1124     add_subdirectory(tests)
1125     if(REGRESSIONTEST_PATH)
1126         #check target builds all to run tests and the runs tests
1127         add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure)
1128         add_dependencies(check gmxtests)
1129     else()
1130         add_custom_target(check COMMAND ${CMAKE_COMMAND} -E echo "WARNING: No tests are run. Running the tests requires either of the cmake variables REGRESSIONTEST_PATH or REGRESSIONTEST_DOWNLOAD to be set.")
1131     endif()
1132 ENDIF()
1133