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