Fix missing replacement/typo
[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         if("${CMAKE_SYSTEM_NAME}" MATCHES "BlueGeneQ")
175             set(GMX_SUGGESTED_CPU_ACCELERATION "IBM_QPX")
176         else()
177             set(GMX_SUGGESTED_CPU_ACCELERATION "None")
178         endif()
179     else(CMAKE_CROSSCOMPILING)
180         gmx_detect_acceleration(GMX_SUGGESTED_CPU_ACCELERATION)
181     endif(CMAKE_CROSSCOMPILING)
182 endif(NOT DEFINED GMX_CPU_ACCELERATION)
183
184 set(GMX_CPU_ACCELERATION "@GMX_SUGGESTED_CPU_ACCELERATION@"
185     CACHE STRING "Accelerated CPU kernels. Pick one of: None, SSE2, SSE4.1, AVX_128_FMA, AVX_256, IBM_QPX")
186
187 set(GMX_FFT_LIBRARY "fftw3" 
188     CACHE STRING "FFT library choices: fftw3,mkl,fftpack[built-in]")
189 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)
190 mark_as_advanced(GMX_BUILD_OWN_FFTW)
191 option(GMX_DISABLE_FFTW_MEASURE 
192        "Do not optimize FFTW setups (not needed with SSE)" OFF)
193 mark_as_advanced(GMX_DISABLE_FFTW_MEASURE)
194 set(GMX_QMMM_PROGRAM "none" 
195     CACHE STRING "QM package choices: none,gaussian,mopac,gamess,orca")
196 option(GMX_BROKEN_CALLOC "Work around broken calloc()" OFF)
197 mark_as_advanced(GMX_BROKEN_CALLOC)
198 option(GMX_MPI_IN_PLACE "Enable MPI_IN_PLACE for MPIs that have it defined" ON)
199 mark_as_advanced(GMX_MPI_IN_PLACE)
200 option(GMX_LOAD_PLUGINS "Compile with plugin support, needed to read VMD supported file formats" ON)
201 mark_as_advanced(GMX_LOAD_PLUGINS)
202
203 option(GMX_OPENMP "Enable OpenMP-based multithreading" ON)
204
205 option(USE_VERSION_H "Generate development version string/information" ON)
206 mark_as_advanced(USE_VERSION_H)
207
208 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)
209
210 if(UNIX)
211     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)
212     mark_as_advanced(GMX_PREFER_STATIC_LIBS)
213 endif()
214
215 option(GMX_CYCLE_SUBCOUNTERS "Enable cycle subcounters to get a more detailed cycle timings" OFF)
216 mark_as_advanced(GMX_CYCLE_SUBCOUNTERS)
217
218 option(GMX_SKIP_DEFAULT_CFLAGS "Don't automatically add suggested/required Compiler flags." OFF)
219 mark_as_advanced(GMX_SKIP_DEFAULT_CFLAGS)
220
221 ######################################################################
222 # Compiler tests
223 # These need to be done early (before further tests).
224 #####################################################################
225
226 # The cmake/Check{C,CXX}CompilerFlag.cmake files in the GROMACS distribution
227 # are used with permission from CMake v2.8.9 so that GROMACS can detect
228 # invalid options with the Intel Compilers.
229 # These files should be removed from the source tree when a CMake version that
230 # includes the features in question becomes required for building GROMACS.
231 include(CheckCCompilerFlag)
232 if(CMAKE_CXX_COMPILER_LOADED)
233     include(CheckCXXCompilerFlag)
234 endif()
235
236 # First exclude compilers known to not work with OpenMP although claim to support it:
237 # gcc 4.2.1 and gcc-llvm 4.2.1 (also claims to be 4.2.1) on Mac OS X
238 # This fixes redmine 900 and needs to run before OpenMP flags are set below.
239 message("CMAKE_COMPILER_IS_GNUCC: ${CMAKE_COMPILER_IS_GNUCC}")
240 if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND
241     CMAKE_COMPILER_IS_GNUCC AND C_COMPILER_VERSION VERSION_LESS 4.3)
242     message(STATUS "OpenMP multithreading not supported with gcc/llvm-gcc 4.2 on Mac OS X, disabled")
243     set(GMX_OPENMP OFF CACHE BOOL
244         "OpenMP multithreading not not supported with gcc/llvm-gcc 4.2 on Mac OS X, disabled!" FORCE)
245 endif()
246
247 # OpenMP check must come before other CFLAGS!
248 if(GMX_OPENMP)
249     find_package(OpenMP)
250     if(OPENMP_FOUND)
251         # CMake on Windows doesn't support linker flags passed to target_link_libraries
252         # (i.e. it treats /openmp as \openmp library file). Also, no OpenMP linker flags are needed.
253         if(NOT (WIN32 AND NOT CYGWIN))
254             if(CMAKE_COMPILER_IS_GNUCC AND GMX_PREFER_STATIC_OPENMP)
255                 set(OpenMP_LINKER_FLAGS "-Wl,-static -lgomp -lrt -Wl,-Bdynamic -lpthread")
256                 set(OpenMP_SHARED_LINKER_FLAGS "")
257             else()
258                 # Only set a linker flag if the user didn't set them manually
259                 if(NOT DEFINED OpenMP_LINKER_FLAGS)
260                     set(OpenMP_LINKER_FLAGS "${OpenMP_C_FLAGS}")
261                 endif()
262                 if(NOT DEFINED OpenMP_SHARED_LINKER_FLAGS)
263                     set(OpenMP_SHARED_LINKER_FLAGS "${OpenMP_C_FLAGS}")
264                 endif()
265             endif()
266         endif()
267     else(OPENMP_FOUND)
268         message(WARNING
269                 "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.")
270         set(GMX_OPENMP OFF CACHE STRING "Whether GROMACS will use OpenMP parallelism." FORCE)
271     endif(OPENMP_FOUND)
272 endif()
273
274
275 include(gmxCFlags)
276 gmx_c_flags()
277
278 include(gmxGetCompilerInfo)
279 get_compiler_version()
280
281 # gcc 4.4.x is buggy and crashes when compiling some files with O3 and OpenMP on.
282 # Detect here whether applying a workaround is needed and will apply it later
283 # on the affected files.
284 include(gmxGCC44O3BugWorkaround)
285 gmx_check_gcc44_bug_workaround_needed(GMX_USE_GCC44_BUG_WORKAROUND)
286
287 # clang 3.0 is buggy for some unknown reason detected during adding
288 # the SSE2 group kernels for GROMACS 4.6. If we ever work out what
289 # that is, we should replace these tests with a compiler feature test,
290 # update GROMACS Redmine task #1039 and perhaps report a clang bug.
291 #
292 # In the meantime, until we require CMake 2.8.10 we cannot rely on it to detect
293 # the compiler version for us. So we need a manual check for clang 3.0.
294 include(gmxDetectClang30)
295 gmx_detect_clang_3_0(COMPILER_IS_CLANG_3_0)
296 if(COMPILER_IS_CLANG_3_0)
297     message(FATAL_ERROR "Your compiler is clang version 3.0, which is known to be buggy for GROMACS. Use a different compiler.")
298 endif()
299
300 if (CMAKE_C_COMPILER_ID STREQUAL "PGI")
301     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.")
302 endif()
303
304 ########################################################################
305 # Set up binary and library suffixing 
306 ########################################################################
307 set(GMX_BINARY_SUFFIX "" CACHE STRING "Suffix for GROMACS binaries (default: _d for double, _mpi for MPI, _mpi_d for MPI and double).")
308 set(GMX_LIBS_SUFFIX "" 
309   CACHE STRING "Suffix for GROMACS libs (default: _d for double, _mpi for MPI, _mpi_d for MPI and double).")
310 if (GMX_DEFAULT_SUFFIX)
311   set(GMX_BINARY_SUFFIX "")
312   set(GMX_LIBS_SUFFIX "")
313   if (GMX_MPI)
314     set(GMX_BINARY_SUFFIX "_mpi")
315     set(GMX_LIBS_SUFFIX "_mpi")
316   endif(GMX_MPI)
317   if (GMX_DOUBLE)
318     set (GMX_BINARY_SUFFIX "${GMX_BINARY_SUFFIX}_d")
319     set (GMX_LIBS_SUFFIX "${GMX_LIBS_SUFFIX}_d")
320   endif(GMX_DOUBLE)
321   if (GMX_OPENMM)
322     set (GMX_BINARY_SUFFIX "-openmm")
323     set (GMX_LIBS_SUFFIX "_openmm")
324   endif(GMX_OPENMM)
325   mark_as_advanced(FORCE GMX_BINARY_SUFFIX GMX_LIBS_SUFFIX)
326   if (NOT SUFFIX_QUIETLY)
327     message(STATUS "Using default binary suffix: \"${GMX_BINARY_SUFFIX}\"")
328     message(STATUS "Using default library suffix: \"${GMX_LIBS_SUFFIX}\"")
329   endif (NOT SUFFIX_QUIETLY)
330 else(GMX_DEFAULT_SUFFIX)
331   mark_as_advanced(CLEAR GMX_BINARY_SUFFIX GMX_LIBS_SUFFIX)
332   if (NOT SUFFIX_QUIETLY)
333     message(STATUS "Using manually set binary suffix: \"${GMX_BINARY_SUFFIX}\"")
334     message(STATUS "Using manually set library suffix: \"${GMX_LIBS_SUFFIX}\"")
335   endif (NOT SUFFIX_QUIETLY)
336 endif(GMX_DEFAULT_SUFFIX)
337 set(SUFFIX_QUIETLY TRUE CACHE INTERNAL "")
338
339 set(PKG_CFLAGS "")
340 if(GMX_DOUBLE)
341     set(PKG_CFLAGS "${PKG_CFLAGS} -DGMX_DOUBLE")
342 endif(GMX_DOUBLE)
343 if(GMX_SOFTWARE_INVSQRT)
344   set(PKG_CFLAGS "${PKG_CFLAGS} -DGMX_SOFTWARE_INVSQRT")
345 endif(GMX_SOFTWARE_INVSQRT)
346
347 #######################################################################
348 # Check for options incompatible with OpenMM build                    #
349 #######################################################################
350 if(GMX_OPENMM)
351     # we'll use the built-in fft to avoid unnecessary dependencies
352     string(TOUPPER ${GMX_FFT_LIBRARY} GMX_FFT_LIBRARY)
353     if(NOT ${GMX_FFT_LIBRARY} STREQUAL "FFTPACK")
354         message(STATUS "No external FFT libraries needed for the OpenMM build, switching to fftpack!")
355         set(GMX_FFT_LIBRARY "fftpack" CACHE STRING 
356                 "No external FFT libraries needed for the OpenMM build, switching to  fftpack!" FORCE)
357     endif()
358     if(GMX_MPI)
359         message(FATAL_ERROR "The OpenMM build is not compatible with MPI!")
360     endif(GMX_MPI)
361     if(GMX_THREAD_MPI)
362         message(STATUS "Thread-MPI not compatible with OpenMM, disabled!")
363         set(GMX_THREAD_MPI OFF CACHE BOOL
364                 "Thread-MPI not compatible with OpenMM build, disabled!" FORCE)
365     endif(GMX_THREAD_MPI)
366     if(GMX_OPENMP)
367         message(STATUS "OpenMP multithreading not compatible with OpenMM, disabled")
368         set(GMX_OPENMP OFF CACHE BOOL
369             "OpenMP multithreading not compatible with OpenMM, disabled!" FORCE)
370     endif()
371     if(GMX_SOFTWARE_INVSQRT)
372         set(GMX_SOFTWARE_INVSQRT OFF CACHE STRING 
373                 "The OpenMM build does not need GROMACS software 1/sqrt!" FORCE)
374     endif(GMX_SOFTWARE_INVSQRT)
375     string(TOUPPER ${GMX_CPU_ACCELERATION} GMX_CPU_ACCELERATION)
376     if(NOT GMX_CPU_ACCELERATION STREQUAL "NONE")
377         message(STATUS "Switching off CPU-based acceleration, the OpenMM build does not support/need any!")
378         set(GMX_CPU_ACCELERATION "None" CACHE STRING
379             "Switching off CPU-based acceleration, the OpenMM build does not support/need any!" FORCE)
380     endif()
381     if(GMX_FAHCORE)
382         message(FATAL_ERROR "The OpenMM build does not support FAH build!")
383     endif(GMX_FAHCORE)
384     if(GMX_DOUBLE)
385         message(FATAL_ERROR  "The OpenMM-build does not support double precision calculations!")
386     endif()
387     # mark as advanced the unused variables
388     mark_as_advanced(FORCE GMX_CPU_ACCELERATION GMX_MPI GMX_FFT_LIBRARY 
389         GMX_QMMM_PROGRAM GMX_THREAD_MPI GMX_DOUBLE)
390 else(GMX_OPENMM)
391      mark_as_advanced(CLEAR GMX_CPU_ACCELERATION GMX_MPI GMX_FFT_LIBRARY 
392         GMX_QMMM_PROGRAM GMX_THREAD_MPI GMX_DOUBLE)
393 endif(GMX_OPENMM)
394
395
396 ########################################################################
397 # Basic system tests (standard libraries, headers, functions, types)   #
398 ########################################################################
399 include(CheckIncludeFiles)
400 check_include_files(string.h     HAVE_STRING_H)
401 check_include_files(math.h       HAVE_MATH_H)
402 check_include_files(limits.h     HAVE_LIMITS_H)
403 check_include_files(memory.h     HAVE_MEMORY_H)
404 check_include_files(unistd.h     HAVE_UNISTD_H)
405 check_include_files(direct.h     HAVE_DIRECT_H)
406 check_include_files(pwd.h        HAVE_PWD_H)
407 check_include_files(stdint.h     HAVE_STDINT_H)
408 check_include_files(stdlib.h     HAVE_STDLIB_H)
409 check_include_files(pthread.h    HAVE_PTHREAD_H)
410 check_include_files(dirent.h     HAVE_DIRENT_H)
411 check_include_files(inttypes.h   HAVE_INTTYPES_H)
412 check_include_files(regex.h      HAVE_REGEX_H)
413 check_include_files(sys/types.h  HAVE_SYS_TYPES_H)
414 check_include_files(sys/stat.h   HAVE_SYS_STAT_H)
415 check_include_files(sys/time.h   HAVE_SYS_TIME_H)
416 check_include_files(rpc/rpc.h    HAVE_RPC_RPC_H)
417 check_include_files("rpc/rpc.h;rpc/xdr.h"    HAVE_RPC_XDR_H)
418 check_include_files(io.h                 HAVE_IO_H)
419 check_include_files(sched.h      HAVE_SCHED_H)
420
421 include(CheckFunctionExists)
422 check_function_exists(strcasecmp        HAVE_STRCASECMP)
423 check_function_exists(strdup            HAVE_STRDUP)
424 check_function_exists(vprintf           HAVE_VPRINTF)
425 check_function_exists(memcmp            HAVE_MEMCMP)
426 check_function_exists(posix_memalign    HAVE_POSIX_MEMALIGN)
427 check_function_exists(memalign          HAVE_MEMALIGN)
428 check_function_exists(_aligned_malloc   HAVE__ALIGNED_MALLOC)
429 check_function_exists(gettimeofday      HAVE_GETTIMEOFDAY)
430 check_function_exists(isnan             HAVE_ISNAN)
431 check_function_exists(_isnan            HAVE__ISNAN)
432 check_function_exists(fsync             HAVE_FSYNC)
433 check_function_exists(_fileno           HAVE__FILENO)
434 check_function_exists(fileno            HAVE_FILENO)
435 check_function_exists(_commit           HAVE__COMMIT)
436 check_function_exists(sigaction         HAVE_SIGACTION)
437 check_function_exists(sysconf           HAVE_SYSCONF)
438 check_function_exists(sched_setaffinity HAVE_SCHED_SETAFFINITY)
439 check_function_exists(sched_getaffinity HAVE_SCHED_GETAFFINITY)
440 check_function_exists(rsqrt             HAVE_RSQRT)
441 check_function_exists(rsqrtf            HAVE_RSQRTF)
442 check_function_exists(sqrtf             HAVE_SQRTF)
443
444 include(CheckLibraryExists)
445 check_library_exists(m sqrt "" HAVE_LIBM)
446 check_library_exists(m cbrt "" HAVE_CBRT)
447
448
449 include(CheckTypeSize)
450
451 check_type_size("bool"          SIZEOF_BOOL) # will also set HAVE_BOOL
452 check_type_size("int"           SIZEOF_INT) 
453 check_type_size("long int"      SIZEOF_LONG_INT) 
454 check_type_size("long long int" SIZEOF_LONG_LONG_INT) 
455 check_type_size("off_t"         SIZEOF_OFF_T)
456 check_type_size("void *"        SIZEOF_VOIDP)
457
458 if (CMAKE_C_SIZEOF_DATA_PTR EQUAL 8)
459     set(GMX_64_BIT TRUE)
460 else (CMAKE_C_SIZEOF_DATA_PTR EQUAL 8)
461     set(GMX_64_BIT FALSE)
462 endif (CMAKE_C_SIZEOF_DATA_PTR EQUAL 8)
463
464 # Check for some basic types that we *need*, so set these to int if they are not present 
465 check_type_size(uid_t uid_t)
466 if(NOT uid_t)
467   set(uid_t int)
468 else(NOT uid_t)
469   set(uid_t 0)
470 endif(NOT uid_t)
471
472 check_type_size(gid_t gid_t)
473 if(NOT gid_t)
474   set(gid_t 1)
475 else(NOT gid_t)
476   set(gid_t 0)
477 endif(NOT gid_t)
478
479 check_type_size(size_t size_t)
480 if(NOT size_t)
481   set(size_t int)
482 else(NOT size_t)
483   set(size_t 0)
484 endif(NOT size_t)
485
486 check_type_size(off_t off_t)
487 if(NOT off_t)
488   set(off_t int)
489 else(NOT off_t)
490   set(off_t 0)
491 endif(NOT off_t)
492
493 include(TestBigEndian)
494 test_big_endian(GMX_INTEGER_BIG_ENDIAN)
495
496
497 if(APPLE OR CYGWIN OR ${CMAKE_SYSTEM_NAME} MATCHES "Linux|.*BSD")
498     # Maybe Solaris should be here? Patch this if you know!
499     SET(SHARED_LIBS_DEFAULT ON)
500 elseif(WIN32 OR ${CMAKE_SYSTEM_NAME} MATCHES "BlueGene")
501     # Support for shared libs on native Windows is a bit new. Its
502     # default might change later if/when we sort things out. Also,
503     # Cray should go here. What variable value can detect it?
504     SET(SHARED_LIBS_DEFAULT OFF)
505 else()
506     message(STATUS "Defaulting to building static libraries")
507     SET(SHARED_LIBS_DEFAULT OFF)
508 endif()
509
510 # Management of GROMACS options for specific toolchains should go
511 # here. Because the initial settings for some of the main options have
512 # already happened, but things like library detection and MPI compiler
513 # feature detection have not, the docstrings for any over-rides of
514 # GROMACS defaults or user settings will make sense. Also, any
515 # toolchain-related reasons for choosing whether to detect various
516 # things can be sorted out now, before the detection takes place.
517 if(${CMAKE_SYSTEM_NAME} MATCHES BlueGene)
518     include(gmxManageBlueGene)
519 endif()
520
521 if(UNIX AND GMX_PREFER_STATIC_LIBS AND SHARED_LIBS_DEFAULT)
522     if(BUILD_SHARED_LIBS)
523         # Warn the user about the combination. But don't overwrite the request.
524         message(WARNING "Searching for static libraries requested, and building shared Gromacs libraries requested. This might cause problems linking later.")
525     elseif(NOT DEFINED BUILD_SHARED_LIBS)
526         # Change default to OFF. Don't warn if it's already off.
527         message(WARNING "Searching for static libraries requested, so the GROMACS libraries will also be built statically (BUILD_SHARED_LIBS=OFF)")
528         set(SHARED_LIBS_DEFAULT OFF)
529     endif()
530 endif()
531
532 # By now, all tool chains should have spoken up if they care about
533 # the setting of SHARED_LIBS_DEFAULT.
534 option(BUILD_SHARED_LIBS "Enable shared libraries (can be problematic e.g. with MPI, or on some HPC systems)" ${SHARED_LIBS_DEFAULT})
535
536 ########################################################################
537 #Process MPI settings
538 ########################################################################
539 include(gmxManageMPI)
540
541 ########################################################################
542 # Find external packages                                               #
543 ########################################################################
544 if(UNIX AND GMX_PREFER_STATIC_LIBS)
545     # On Linux .a is the static library suffix, on Mac OS X .lib can also
546     # be used, so we'll add both to the preference list.
547     SET(CMAKE_FIND_LIBRARY_SUFFIXES ".lib;.a" ${CMAKE_FIND_LIBRARY_SUFFIXES})
548 endif()
549
550 IF( WIN32 AND NOT CYGWIN)
551   if (NOT BUILD_SHARED_LIBS)
552       option(GMX_PREFER_STATIC_LIBS "When finding libraries prefer static system libraries (MT instead of MD)!" ON)
553       if(NOT GMX_PREFER_STATIC_LIBS)
554           message(WARNING "Shared system libraries requested, and static Gromacs libraries requested.")
555       endif()
556   else()
557       option(GMX_PREFER_STATIC_LIBS "When finding libraries prefer static system libraries (MT instead of MD)!" OFF)
558       if(GMX_PREFER_STATIC_LIBS)
559           #this combination segfaults (illigal passing of file handles)
560           message(FATAL_ERROR "Static system libraries requested, and shared Gromacs libraries requested.")
561       endif()
562       add_definitions(-DUSE_VISIBILITY -DTMPI_USE_VISIBILITY)
563       set(PKG_CFLAGS "$PKG_CFLAGS -DUSE_VISIBILITY -DTMPI_USE_VISIBILITY")
564   endif()
565   mark_as_advanced(GMX_PREFER_STATIC_LIBS)
566
567   IF (GMX_PREFER_STATIC_LIBS)
568       #Only setting Debug and Release flags. Others configurations are current not used.
569       STRING(REPLACE /MD /MT CMAKE_C_FLAGS_RELEASE ${CMAKE_C_FLAGS_RELEASE})
570       STRING(REPLACE /MD /MT CMAKE_C_FLAGS_DEBUG ${CMAKE_C_FLAGS_DEBUG})
571       if(CMAKE_CXX_COMPILER_LOADED)
572           STRING(REPLACE /MD /MT CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
573           STRING(REPLACE /MD /MT CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
574       endif()
575   ENDIF()
576   IF( CMAKE_C_COMPILER_ID MATCHES "Intel" )
577     if(BUILD_SHARED_LIBS) #not sure why incremental building with shared libs doesn't work
578         STRING(REPLACE "/INCREMENTAL:YES" "" CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS})
579     endif()
580   ENDIF()
581 ENDIF()
582
583 option(GMX_GSL "Add support for gsl" OFF)
584 if (GMX_GSL)
585   find_package(gsl)
586   set(PKG_GSL "")
587   if(GSL_FOUND)
588     include_directories(${GSL_INCLUDE_DIR})
589     set(PKG_GSL gsl)
590     set(HAVE_LIBGSL 1)
591   endif(GSL_FOUND)
592 endif (GMX_GSL)
593
594 option(GMX_X11 "Use X window system" OFF)
595 if (GMX_X11)
596         find_package(X11)
597         # X11 includes/libraries are only set in the ngmx subdirectory!
598         if(X11_FOUND)
599         set(HAVE_X11 1)
600         endif(X11_FOUND)
601 endif(GMX_X11)
602
603 include(ThreadMPI)
604 set(THREAD_MPI_LIB thread_mpi)
605 if(GMX_THREAD_MPI)
606     tmpi_get_source_list(THREAD_MPI_SRC)
607     set(PKG_CFLAGS "${PKG_CFLAGS} -DGMX_THREAD_MPI")
608     set(GMX_MPI 1)
609 else(GMX_THREAD_MPI)
610     tmpi_get_source_list(THREAD_MPI_SRC NOMPI)
611 endif(GMX_THREAD_MPI)
612
613 if(GMX_OPENMM)
614     set(CUDA_BUILD_EMULATION OFF)
615     find_package(CUDA 3.1 REQUIRED)
616     add_definitions(-DGMX_OPENMM)
617     if(CMAKE_BUILD_TYPE STREQUAL "DEBUG")    
618         set(CUDA_VERBOSE_BUILD ON)
619     endif()
620     find_package(OpenMM) 
621 endif(GMX_OPENMM)
622
623 if(GMX_GPU)
624     # now that we have detected the dependencies, do the second configure pass
625     gmx_gpu_setup()
626 endif(GMX_GPU)
627
628 if(APPLE)
629    find_library(ACCELERATE_FRAMEWORK Accelerate)
630    list(APPEND GMX_EXTRA_LIBRARIES ${ACCELERATE_FRAMEWORK})
631 endif(APPLE)
632
633 if(CYGWIN)
634     set(GMX_CYGWIN 1)
635 endif(CYGWIN)
636
637 if(WIN32 AND NOT CYGWIN)
638     set(GMX_NATIVE_WINDOWS 1)
639 endif()
640
641 # only bother with finding git and using version.h if the source is a git repo
642 if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
643     if(USE_VERSION_H)
644         # We need at least git v1.5.3 be able to parse git's date output. If not
645         # found or the version is too small, we can't generate version information.
646         find_package(Git)
647
648         # Find out the git version
649         if(GIT_FOUND AND NOT GIT_VERSION)
650           execute_process(COMMAND ${GIT_EXECUTABLE} "--version"
651             OUTPUT_VARIABLE _exec_out
652             OUTPUT_STRIP_TRAILING_WHITESPACE)
653           string(REGEX REPLACE "git version (.*)" "\\1" GIT_VERSION ${_exec_out})
654           set(GIT_VERSION ${GIT_VERSION} CACHE STRING "Git version")
655           mark_as_advanced(GIT_VERSION)
656         endif()
657
658         if(NOT GIT_FOUND OR GIT_VERSION VERSION_LESS "1.5.3")
659           message("No compatible git version found, won't be able to generate proper development version information.")
660           set(USE_VERSION_H OFF)
661         endif()
662     endif()
663 else()
664     set(USE_VERSION_H OFF)
665 endif()
666
667 ########################################################################
668 # Generate development version info for cache
669 ########################################################################
670 # set(GEN_VERSION_INFO_INTERNAL "ON")
671 # include(gmxGenerateVersionString)
672
673 ########################################################################
674 # Our own GROMACS tests
675 ########################################################################
676
677 add_definitions( -DHAVE_CONFIG_H )
678 include_directories(${CMAKE_BINARY_DIR}/src)
679 include_directories(${CMAKE_BINARY_DIR}/include)
680 include_directories(${CMAKE_SOURCE_DIR}/include)
681
682 include(gmxTestInlineASM)
683 gmx_test_inline_asm_gcc_x86(GMX_X86_GCC_INLINE_ASM)
684
685 include(gmxSetBuildInformation)
686 gmx_set_build_information()
687 if(BUILD_CPU_FEATURES MATCHES "rdtscp" AND NOT GMX_DISTRIBUTABLE_BUILD)
688     # The timestep counter headers do not include config.h
689     add_definitions(-DHAVE_RDTSCP)
690 endif(BUILD_CPU_FEATURES MATCHES "rdtscp" AND NOT GMX_DISTRIBUTABLE_BUILD)
691
692 include(gmxTestFloatFormat)
693 gmx_test_float_format(GMX_FLOAT_FORMAT_IEEE754 
694                       GMX_IEEE754_BIG_ENDIAN_BYTE_ORDER
695                       GMX_IEEE754_BIG_ENDIAN_WORD_ORDER)
696
697 include(gmxTestLargeFiles)
698 gmx_test_large_files(GMX_LARGEFILES)
699
700 include(gmxTestSignal)
701 gmx_test_sigusr1(HAVE_SIGUSR1)
702
703 include(gmxTestInline)
704 gmx_test_inline(INLINE_KEYWORD)
705
706 include(gmxTestRestrict)
707 gmx_test_restrict(RESTRICT_KEYWORD)
708
709 include(gmxTestPipes)
710 gmx_test_pipes(HAVE_PIPES)
711
712 include(gmxTestIsfinite)
713 gmx_test_isfinite(HAVE_ISFINITE)
714 gmx_test__isfinite(HAVE__ISFINITE)
715 gmx_test__finite(HAVE__FINITE)
716
717 include(gmxTestXDR)
718 gmx_test_xdr(GMX_SYSTEM_XDR)
719 if(NOT GMX_SYSTEM_XDR)
720     set(GMX_INTERNAL_XDR 1)
721     set(PKG_CFLAGS "${PKG_CFLAGS} -DGMX_INTERNAL_XDR")
722 endif(NOT GMX_SYSTEM_XDR)
723
724 # include avx test source, used if the AVX flags are set below
725 include(gmxTestAVXMaskload)
726
727 # Process nonbonded accelerated kernels settings
728 string(TOUPPER ${GMX_CPU_ACCELERATION} ${GMX_CPU_ACCELERATION})
729 if(${GMX_CPU_ACCELERATION} STREQUAL "NONE")
730     # nothing to do
731 elseif(${GMX_CPU_ACCELERATION} STREQUAL "SSE2")
732
733     GMX_TEST_CFLAG(GNU_SSE2_CFLAG "-msse2" ACCELERATION_C_FLAGS)
734     if(NOT GNU_SSE2_CFLAG AND GMX_NATIVE_WINDOWS)
735         GMX_TEST_CFLAG(MSVC_SSE2_CFLAG "/arch:SSE2" ACCELERATION_C_FLAGS)
736     endif(NOT GNU_SSE2_CFLAG AND GMX_NATIVE_WINDOWS)
737
738     if (CMAKE_CXX_COMPILER_LOADED)
739         GMX_TEST_CXXFLAG(GNU_SSE2_CXXFLAG "-msse2" ACCELERATION_CXX_FLAGS)
740         if(NOT GNU_SSE2_CXXFLAG AND GMX_NATIVE_WINDOWS)
741             GMX_TEST_CXXFLAG(MSVC_SSE2_CXXFLAG "/arch:SSE2" ACCELERATION_CXX_FLAGS)
742         endif(NOT GNU_SSE2_CXXFLAG AND GMX_NATIVE_WINDOWS)
743     endif()
744
745     # We dont warn for lacking SSE2 flag support, since that is probably standard today.
746
747     # Only test the include after we have tried to add the correct flag for SSE2 support
748     check_include_file(emmintrin.h  HAVE_EMMINTRIN_H ${ACCELERATION_C_FLAGS})
749
750     if(NOT HAVE_EMMINTRIN_H)
751         message(FATAL_ERROR "Cannot find emmintrin.h, which is required for SSE2 intrinsics support.")
752     endif(NOT HAVE_EMMINTRIN_H)
753
754     set(GMX_CPU_ACCELERATION_X86_SSE2 1)
755     # The user should not be able to set this orthogonally to the acceleration
756     set(GMX_X86_SSE2 1)
757     if (NOT ACCELERATION_QUIETLY)
758       message(STATUS "Enabling SSE2 Gromacs acceleration, and it will help compiler optimization.")
759     endif()
760
761 elseif(${GMX_CPU_ACCELERATION} STREQUAL "SSE4.1")
762
763     GMX_TEST_CFLAG(GNU_SSE4_CFLAG "-msse4.1" ACCELERATION_C_FLAGS)
764     if (NOT GNU_SSE4_CFLAG AND GMX_NATIVE_WINDOWS)
765         GMX_TEST_CFLAG(MSVC_SSE4_CFLAG "/arch:SSE4.1" ACCELERATION_C_FLAGS)
766     endif(NOT GNU_SSE4_CFLAG AND GMX_NATIVE_WINDOWS)
767     if (NOT GNU_SSE4_CFLAG AND NOT MSVC_SSE4_CFLAG)
768         # Not surprising if we end up here! MSVC current does not support the SSE4.1 flag. However, it appears to accept SSE4.1
769         # intrinsics when SSE2 support is enabled, so we try that instead first.
770         if (GMX_NATIVE_WINDOWS)
771             GMX_TEST_CFLAG(MSVC_SSE2_CFLAG "/arch:SSE2" ACCELERATION_C_FLAGS)
772             message(WARNING "Neither SSE4.1 or SSE2 seems to be supported by your Windows compiler. Something is likely broken.")
773         else()
774             message(WARNING "No C SSE4.1 flag found. Consider a newer compiler, or use SSE2 for slightly lower performance")
775         endif()
776     endif(NOT GNU_SSE4_CFLAG AND NOT MSVC_SSE4_CFLAG)
777
778     if (CMAKE_CXX_COMPILER_LOADED)
779         GMX_TEST_CXXFLAG(GNU_SSE4_CXXFLAG "-msse4.1" ACCELERATION_CXX_FLAGS)
780         if (NOT GNU_SSE4_CXXFLAG AND GMX_NATIVE_WINDOWS)
781             GMX_TEST_CXXFLAG(MSVC_SSE4_CXXFLAG "/arch:SSE4.1" ACCELERATION_CXX_FLAGS)
782         endif(NOT GNU_SSE4_CXXFLAG AND GMX_NATIVE_WINDOWS)
783         if (NOT GNU_SSE4_CXXFLAG AND NOT MSVC_SSE4_CXXFLAG) 
784             message(WARNING "No C++ SSE4.1 flag found. Consider a newer compiler, or use SSE2 for slightly lower performance.")
785             # Not surprising if we end up here! MSVC current does not support the SSE4.1 flag. However, it appears to accept SSE4.1
786             # intrinsics when SSE2 support is enabled, so we try that instead.
787             if (GMX_NATIVE_WINDOWS)
788                 GMX_TEST_CXXFLAG(MSVC_SSE2_CXXFLAG "/arch:SSE2" ACCELERATION_CXX_FLAGS)
789             endif()
790         endif(NOT GNU_SSE4_CXXFLAG AND NOT MSVC_SSE4_CXXFLAG)
791     endif()
792
793     # This must come after we have added the -msse4.1 flag on some platforms.
794     check_include_file(smmintrin.h  HAVE_SMMINTRIN_H ${ACCELERATION_C_FLAGS})
795
796     if(NOT HAVE_SMMINTRIN_H)
797         message(FATAL_ERROR "Cannot find smmintrin.h, which is required for SSE4.1 intrinsics support.")
798     endif(NOT HAVE_SMMINTRIN_H)
799
800     set(GMX_CPU_ACCELERATION_X86_SSE4_1 1)
801     # The user should not be able to set this orthogonally to the acceleration
802     set(GMX_X86_SSE4_1 1)
803     set(GMX_X86_SSE2   1)
804     if (NOT ACCELERATION_QUIETLY)
805       message(STATUS "Enabling SSE4.1 Gromacs acceleration, and it will help compiler optimization.")
806     endif()
807
808 elseif(${GMX_CPU_ACCELERATION} STREQUAL "AVX_128_FMA" OR ${GMX_CPU_ACCELERATION} STREQUAL "AVX_256")
809
810     # Set the AVX compiler flag for both these choices!
811
812     GMX_TEST_CFLAG(GNU_AVX_CFLAG "-mavx" ACCELERATION_C_FLAGS)
813     if (NOT GNU_AVX_CFLAG AND GMX_NATIVE_WINDOWS)
814         GMX_TEST_CFLAG(MSVC_AVX_CFLAG "/arch:AVX" ACCELERATION_C_FLAGS)
815     endif (NOT GNU_AVX_CFLAG AND GMX_NATIVE_WINDOWS)
816     if (NOT GNU_AVX_CFLAG AND NOT MSVC_AVX_CFLAG)
817         message(WARNING "No C AVX flag found. Consider a newer compiler, or try SSE4.1 (lower performance).")
818     endif (NOT GNU_AVX_CFLAG AND NOT MSVC_AVX_CFLAG)
819
820     if (CMAKE_CXX_COMPILER_LOADED)
821         GMX_TEST_CXXFLAG(GNU_AVX_CXXFLAG "-mavx" ACCELERATION_CXX_FLAGS)
822         if (NOT GNU_AVX_CXXFLAG AND GMX_NATIVE_WINDOWS)
823             GMX_TEST_CXXFLAG(MSVC_AVX_CXXFLAG "/arch:AVX" ACCELERATION_CXX_FLAGS)
824         endif (NOT GNU_AVX_CXXFLAG AND GMX_NATIVE_WINDOWS)
825         if (NOT GNU_AVX_CXXFLAG AND NOT MSVC_AVX_CXXFLAG)
826             message(WARNING "No C++ AVX flag found. Consider a newer compiler, or try SSE4.1 (lower performance).")
827         endif (NOT GNU_AVX_CXXFLAG AND NOT MSVC_AVX_CXXFLAG)
828     endif()
829
830     # Set the FMA4 flags (MSVC doesn't require any)
831     if(${GMX_CPU_ACCELERATION} STREQUAL "AVX_128_FMA" AND NOT MSVC)
832         if (${CMAKE_COMPILER_ID} MATCHES "Clang")
833             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.")
834         endif()
835         GMX_TEST_CFLAG(GNU_FMA_CFLAG "-mfma4" ACCELERATION_C_FLAGS)
836         if (NOT GNU_FMA_CFLAG)
837             message(WARNING "No C FMA4 flag found. Consider a newer compiler, or try SSE4.1 (lower performance).")
838         endif(NOT GNU_FMA_CFLAG)
839         GMX_TEST_CFLAG(GNU_XOP_CFLAG "-mxop" ACCELERATION_C_FLAGS)
840         # No big deal if we do not have xop, so no point yelling warnings about it.
841         if (CMAKE_CXX_COMPILER_LOADED)
842             GMX_TEST_CXXFLAG(GNU_FMA_CXXFLAG "-mfma4" ACCELERATION_CXX_FLAGS)
843             if (NOT GNU_FMA_CXXFLAG)
844                 message(WARNING "No C++ FMA flag found. Consider a newer compiler, or try SSE4.1 (lower performance).")
845             endif (NOT GNU_FMA_CXXFLAG)
846             GMX_TEST_CXXFLAG(GNU_XOP_CXXFLAG "-mxop" ACCELERATION_CXX_FLAGS)
847             # No big deal if we do not have xop, so no point yelling warnings about it.
848         endif()
849     endif()
850
851     # Only test the header after we have tried to add the flag for AVX support
852     check_include_file(immintrin.h  HAVE_IMMINTRIN_H ${ACCELERATION_C_FLAGS})
853
854     if(NOT HAVE_IMMINTRIN_H)
855         message(FATAL_ERROR "Cannot find immintrin.h, which is required for AVX intrinsics support. Consider switching compiler.")
856     endif(NOT HAVE_IMMINTRIN_H)
857
858     if(${GMX_CPU_ACCELERATION} STREQUAL "AVX_256")
859         try_compile(TEST_AVX ${CMAKE_BINARY_DIR}
860             "${CMAKE_SOURCE_DIR}/cmake/TestAVX.c"
861             COMPILE_DEFINITIONS "${ACCELERATION_C_FLAGS}")
862         if(NOT TEST_AVX)
863             message(FATAL_ERROR "Cannot compile AVX intrinsics. Consider switching compiler.")
864         endif()
865     endif()
866
867     # GCC requires x86intrin.h for FMA support. MSVC 2010 requires intrin.h for FMA support.
868     check_include_file(x86intrin.h HAVE_X86INTRIN_H ${ACCELERATION_C_FLAGS})
869     check_include_file(intrin.h HAVE_INTRIN_H ${ACCELERATION_C_FLAGS})
870
871     # The user should not be able to set this orthogonally to the acceleration
872     set(GMX_X86_SSE4_1 1)
873     set(GMX_X86_SSE2   1)
874
875     # But just enable one of the choices internally...
876     if(${GMX_CPU_ACCELERATION} STREQUAL "AVX_128_FMA")
877         set(GMX_CPU_ACCELERATION_X86_AVX_128_FMA 1)
878         set(GMX_X86_AVX_128_FMA 1)
879         if (NOT ACCELERATION_QUIETLY)
880           message(STATUS "Enabling 128-bit AVX Gromacs acceleration (with fused-multiply add), and it will help compiler optimization.")
881         endif()
882     else()
883         # If we are not doing AVX_128, it must be AVX_256...
884         set(GMX_CPU_ACCELERATION_X86_AVX_256 1)
885         set(GMX_X86_AVX_256 1)
886         if (NOT ACCELERATION_QUIETLY)
887           message(STATUS "Enabling 256-bit AVX Gromacs acceleration, and it will help compiler optimization.")
888         endif()
889     endif()
890
891     # Unfortunately gcc-4.5.2 and gcc-4.6.0 has a bug where they use the wrong datatype for the formal
892     # parameter of the mask for maskload/maskstore arguments. Check if this is present, since we can work around it.
893     gmx_test_avx_gcc_maskload_bug(${ACCELERATION_C_FLAGS} GMX_X86_AVX_GCC_MASKLOAD_BUG)
894
895 else(${GMX_CPU_ACCELERATION} STREQUAL "IBM_QPX")
896     # Used on BlueGene/Q
897     if (CMAKE_C_COMPILER_ID MATCHES "XL")
898         GMX_TEST_CFLAG(XLC_BLUEGENEQ_CFLAG "-qarch=qp -qtune=qp" ACCELERATION_C_FLAGS)
899         try_compile(TEST_QPX ${CMAKE_BINARY_DIR}
900             "${CMAKE_SOURCE_DIR}/cmake/TestQPX.c"
901             COMPILE_DEFINITIONS "${ACCELERATION_C_FLAGS}")
902         if(NOT TEST_QPX)
903             message(FATAL_ERROR "Cannot compile the requested IBM QPX intrinsics.")
904         endif()
905     endif()
906     if (CMAKE_CXX_COMPILER_ID MATCHES "XL" AND CMAKE_CXX_COMPILER_LOADED)
907         GMX_TEST_CXXFLAG(XLC_BLUEGENEQ_CXXFLAG "-qarch=qp -qtune=qp" ACCELERATION_CXX_FLAGS)
908         try_compile(TEST_QPX ${CMAKE_BINARY_DIR}
909             "cmake/TestQPX.c"
910             COMPILE_DEFINITIONS "${ACCELERATION_CXX_FLAGS")
911         if(NOT TEST_QPX)
912             message(FATAL_ERROR "Cannot compile the requested IBM QPX intrinsics.")
913         endif()
914     endif()
915
916     if (TEST_QPX)
917         message(WARNING "IBM QPX acceleration was selected and could be compiled, but the accelerated kernels are not yet available.")
918         set(GMX_CPU_ACCELERATION_IBM_QPX 1)
919     else()
920         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.")
921     endif()
922
923 else(${GMX_CPU_ACCELERATION} STREQUAL "NONE")
924     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")
925 endif(${GMX_CPU_ACCELERATION} STREQUAL "NONE")
926 set(ACCELERATION_QUIETLY TRUE CACHE INTERNAL "")
927
928 # Process QM/MM Settings
929 string(TOUPPER ${GMX_QMMM_PROGRAM} ${GMX_QMMM_PROGRAM})
930 if(${GMX_QMMM_PROGRAM} STREQUAL "GAUSSIAN")
931     set(GMX_QMMM_GAUSSIAN 1)
932 elseif(${GMX_QMMM_PROGRAM} STREQUAL "MOPAC")
933     set(GMX_QMMM_MOPAC 1)
934 elseif(${GMX_QMMM_PROGRAM} STREQUAL "GAMESS")
935     set(GMX_QMMM_GAMESS 1)
936 elseif(${GMX_QMMM_PROGRAM} STREQUAL "ORCA")
937     set(GMX_QMMM_ORCA 1)
938 elseif(${GMX_QMMM_PROGRAM} STREQUAL "NONE")
939     # nothing to do
940 else(${GMX_QMMM_PROGRAM} STREQUAL "GAUSSIAN")
941     MESSAGE(FATAL_ERROR "Invalid QM/MM program option: ${GMX_QMMM_PROGRAM}. Choose one of: Gaussian, Mopac, Gamess, Orca, None")
942 endif(${GMX_QMMM_PROGRAM} STREQUAL "GAUSSIAN")
943
944 # Process FFT library settings - if not OpenMM build 
945 string(TOUPPER ${GMX_FFT_LIBRARY} ${GMX_FFT_LIBRARY})
946 set(PKG_FFT "")
947 set(PKG_FFT_LIBS "")
948 if(${GMX_FFT_LIBRARY} STREQUAL "FFTW3")
949     if(GMX_DOUBLE)
950         set(FFTW fftw)
951     else()
952         set(FFTW fftwf)
953     endif()
954
955     if(GMX_BUILD_OWN_FFTW)
956       add_subdirectory(src/contrib/fftw)
957     else()
958       find_package(FFTW COMPONENTS ${FFTW})
959     endif()
960
961     string(TOUPPER "${FFTW}" FFTW)
962     set(PKG_FFT "${${FFTW}_PKG}")
963     include_directories(${${FFTW}_INCLUDE_DIRS})
964     set(FFT_LIBRARIES ${${FFTW}_LIBRARIES})
965     if(NOT ${FFTW}_FOUND)
966       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.")
967     endif()
968
969     set(GMX_FFT_FFTW3 1)
970
971     if (NOT ${GMX_CPU_ACCELERATION} STREQUAL "NONE" AND NOT ${FFTW}_HAVE_SIMD) 
972       message(WARNING "The fftw library found is compiled without SIMD support, which makes it slow. Consider recompiling it or contact your admin")
973     endif()
974
975     if(NOT ${GMX_CPU_ACCELERATION} STREQUAL "NONE" AND ${FFTW}_HAVE_AVX)
976         # If we're not doing CPU acceleration, we don't care about FFTW performance on x86 either
977         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.")
978     endif()
979
980 elseif(${GMX_FFT_LIBRARY} STREQUAL "MKL")
981 #    MESSAGE(STATUS "Using external FFT library - Intel MKL")
982     find_package(MKL REQUIRED)
983     include_directories(${MKL_INCLUDE_DIR})
984     set(FFT_LIBRARIES ${MKL_LIBRARIES})
985     set(PKG_FFT_LIBS ${MKL_LIBRARIES})
986
987     set(GMX_FFT_MKL 1)
988     set(HAVE_MKL 1)
989
990 #elseif(${GMX_FFT_LIBRARY} STREQUAL "ACML")
991 #    MESSAGE(STATUS "Using external FFT library - AMD core math library")
992 #    set(GMX_FFT_ACML 1)
993 elseif(${GMX_FFT_LIBRARY} STREQUAL "FFTPACK")
994     MESSAGE(STATUS "Using internal FFT library - fftpack")
995     set(GMX_FFT_FFTPACK 1)
996 else(${GMX_FFT_LIBRARY} STREQUAL "FFTW3")
997     MESSAGE(FATAL_ERROR "Invalid FFT library setting: ${GMX_FFT_LIBRARY}. Choose one of: fftw3, mkl, fftpack")
998 endif(${GMX_FFT_LIBRARY} STREQUAL "FFTW3")
999
1000 # enable threaded fftw3 if we've found it 
1001 if(FFTW3_THREADS OR FFTW3F_THREADS)
1002     add_definitions(-DFFT5D_FFTW_THREADS)
1003 endif()
1004
1005 set(GMX_EXTERNAL_BLAS TRUE CACHE BOOL "Use external BLAS instead of built-in")
1006 set(GMX_EXTERNAL_LAPACK TRUE CACHE BOOL "Use external LAPACK instead of built-in")
1007 # MKL has BLAS/LAPACK routines
1008 if(NOT HAVE_MKL AND NOT ACCELERATE_FRAMEWORK)
1009   if(GMX_EXTERNAL_BLAS)
1010     if (GMX_BLAS_USER)
1011         list(APPEND GMX_EXTRA_LIBRARIES ${GMX_BLAS_USER})
1012     else(GMX_BLAS_USER)
1013         set(BLAS_FIND_QUIETLY ON)
1014         find_package(BLAS)
1015         if (BLAS_FOUND)
1016           list(APPEND GMX_EXTRA_LIBRARIES ${BLAS_LIBRARIES})
1017         else()
1018           MESSAGE(STATUS "Using internal BLAS library")
1019           set(GMX_EXTERNAL_BLAS FALSE CACHE BOOL "Use external BLAS instead of built-in" FORCE)
1020         endif()
1021     endif(GMX_BLAS_USER)
1022   endif(GMX_EXTERNAL_BLAS)
1023   if(GMX_EXTERNAL_LAPACK)
1024     if (GMX_LAPACK_USER)
1025         list(APPEND GMX_EXTRA_LIBRARIES ${GMX_LAPACK_USER})
1026     else(GMX_LAPACK_USER)
1027         set(LAPACK_FIND_QUIETLY ON)
1028         find_package(LAPACK)
1029         if (LAPACK_FOUND)
1030           list(APPEND GMX_EXTRA_LIBRARIES ${LAPACK_LIBRARIES})
1031         else()
1032           MESSAGE(STATUS "Using internal LAPACK library")
1033           set(GMX_EXTERNAL_LAPACK FALSE CACHE BOOL "Use external LAPACK instead of built-in" FORCE)
1034         endif()
1035     endif(GMX_LAPACK_USER)
1036   endif(GMX_EXTERNAL_LAPACK)
1037 endif()
1038 mark_as_advanced(GMX_EXTERNAL_LAPACK)
1039 mark_as_advanced(GMX_EXTERNAL_BLAS)
1040
1041 set(GMX_USE_PLUGINS OFF CACHE INTERNAL "Whether GROMACS will really try to compile support for VMD plugins")
1042
1043 if(GMX_LOAD_PLUGINS)
1044   if(CYGWIN OR NOT WIN32)
1045     # Native Windows does not have, nor need dlopen
1046     # Note that WIN32 is set with Cygwin, but Cygwin needs dlopen to use plug-ins
1047     include(gmxTestdlopen)
1048     gmx_test_dlopen(HAVE_DLOPEN)
1049   endif()
1050
1051   # so, should we use plug-ins?
1052   if((WIN32 AND NOT CYGWIN) OR (HAVE_DLOPEN AND BUILD_SHARED_LIBS))
1053     if(NOT VMD_QUIETLY)
1054       MESSAGE(STATUS
1055           "Found the ability to use plug-ins when building shared libaries, "
1056           "so will compile to use plug-ins (e.g. to read VMD-supported file "
1057           "formats).")
1058     endif()
1059     if(NOT GMX_VMD_PLUGIN_PATH)
1060       find_package(VMD)
1061     endif()
1062     set(GMX_USE_PLUGINS ON)
1063     list(APPEND GMX_EXTRA_LIBRARIES ${CMAKE_DL_LIBS}) # magic cross-platform pre-set variable for dlopen library
1064     set(PKG_DL_LIBS "-l${CMAKE_DL_LIBS}")
1065   else()
1066     set(PKG_DL_LIBS)
1067   endif()
1068 endif(GMX_LOAD_PLUGINS)
1069 set(VMD_QUIETLY TRUE CACHE INTERNAL "")
1070
1071 if(EXISTS "${CMAKE_SOURCE_DIR}/admin/.isreposource")
1072     if(NOT CMAKE_CROSSCOMPILING)
1073         option(GMX_BUILD_MANPAGES "Build man pages" ON)
1074     else()
1075         message(STATUS "Building the man pages is not available when "
1076             "cross-compiling the developer version from git")
1077     endif()
1078 else()
1079     #make sure source package contains all man pages
1080     if(NOT EXISTS "${CMAKE_SOURCE_DIR}/man/man1/ngmx.1")
1081         message(FATAL_ERROR "Man pages are missing from source package.")
1082     endif()
1083 endif()
1084 mark_as_advanced(GMX_BUILD_MANPAGES)
1085
1086 # Math and thread libraries must often come after all others when linking...
1087 if(HAVE_LIBM)
1088     list(APPEND GMX_EXTRA_LIBRARIES m)
1089 endif(HAVE_LIBM)
1090
1091 if(GMX_FAHCORE)
1092   set(COREWRAP_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/../corewrap" CACHE STRING 
1093       "Path to swindirect.h")
1094   include_directories(${COREWRAP_INCLUDE_DIR})
1095   set_property(CACHE GMX_COOL_QUOTES VALUE OFF)
1096 endif(GMX_FAHCORE)
1097
1098 # # # # # # # # # # NO MORE TESTS AFTER THIS LINE! # # # # # # # # # # #
1099 # these are set after everything else
1100 if (NOT GMX_SKIP_DEFAULT_CFLAGS)
1101     set(CMAKE_C_FLAGS "${ACCELERATION_C_FLAGS} ${MPI_COMPILE_FLAGS} ${CMAKE_C_FLAGS}")
1102     set(CMAKE_CXX_FLAGS "${ACCELERATION_CXX_FLAGS} ${MPI_COMPILE_FLAGS} ${CMAKE_CXX_FLAGS}")
1103     set(CMAKE_EXE_LINKER_FLAGS "${MPI_LINKER_FLAGS} ${CMAKE_EXE_LINKER_FLAGS}")
1104     set(CMAKE_SHARED_LINKER_FLAGS "${MPI_LINKER_FLAGS} ${CMAKE_SHARED_LINKER_FLAGS}")
1105 else()
1106     message("Recommended flags which are not added because GMX_SKIP_DEFAULT_CFLAGS=yes:")
1107     message("CMAKE_C_FLAGS: ${ACCELERATION_C_FLAGS} ${MPI_COMPILE_FLAGS} ${GMXC_CFLAGS}")
1108     message("CMAKE_C_FLAGS_RELEASE: ${GMXC_CFLAGS_RELEASE}")
1109     message("CMAKE_C_FLAGS_DEBUG: ${GMXC_CFLAGS_DEBUG}")
1110     if(CMAKE_CXX_COMPILER_LOADED)
1111         message("CMAKE_CXX_FLAGS: ${ACCELERATION_CXX_FLAGS} ${MPI_COMPILE_FLAGS} ${GMXC_CXXFLAGS}")
1112         message("CMAKE_CXX_FLAGS_RELEASE: ${GMXC_CXXFLAGS_RELEASE}")
1113         message("CMAKE_CXX_FLAGS_DEBUG: ${GMXC_CXXFLAGS_DEBUG}")
1114     endif()
1115     message("CMAKE_EXE_LINKER_FLAGS: ${MPI_LINKER_FLAGS}")
1116     message("CMAKE_SHARED_LINKER_FLAGS: ${MPI_LINKER_FLAGS}")
1117 endif()
1118
1119 if(NOT GMX_OPENMP)
1120     #Unset all OpenMP flags in case OpenMP was disabled either by the user
1121     #or because it was only partially detected (e.g. only for C but not C++ compiler)
1122     unset(OpenMP_C_FLAGS CACHE) 
1123     unset(OpenMP_CXX_FLAGS CACHE)
1124     unset(OpenMP_LINKER_FLAGS CACHE)
1125     unset(OpenMP_SHARED_LINKER_FLAGS)
1126 endif()
1127 set(PKG_CFLAGS "${PKG_CFLAGS} ${OpenMP_C_FLAGS}")
1128
1129 ######################################
1130 # Output compiler and CFLAGS used
1131 ######################################
1132 get_compiler_info(C BUILD_C_COMPILER BUILD_CFLAGS)
1133 if (CMAKE_CXX_COMPILER_LOADED)
1134     get_compiler_info(CXX BUILD_CXX_COMPILER BUILD_CXXFLAGS)
1135 endif ()
1136
1137 ########################################################################
1138 # Specify install locations and which subdirectories to process        #
1139 ########################################################################
1140 if (GMX_USE_RELATIVE_INSTALL_PATH)
1141     set(GMX_INSTALL_PREFIX "" CACHE STRING "Prefix gets appended to CMAKE_INSTALL_PREFIX. For cpack it sets the root folder of the archive.")
1142     mark_as_advanced(GMX_INSTALL_PREFIX)
1143 else()
1144     set(GMX_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}/")
1145 endif()
1146
1147 if ( NOT DEFINED GMXLIB )
1148     set(GMXLIB lib)
1149 endif()
1150 set(LIB_INSTALL_DIR "${GMX_INSTALL_PREFIX}${GMXLIB}")
1151 set(BIN_INSTALL_DIR  ${GMX_INSTALL_PREFIX}bin)
1152 set(DATA_INSTALL_DIR ${GMX_INSTALL_PREFIX}share/gromacs)
1153 set(MAN_INSTALL_DIR  ${GMX_INSTALL_PREFIX}share/man)
1154 set(INCL_INSTALL_DIR ${GMX_INSTALL_PREFIX}include)
1155
1156 set(GMXLIBDIR        ${DATA_INSTALL_DIR}/top)
1157
1158 ##################################################################
1159 # Shared library settings - Darwin uses INSTALL_NAME_DIR instead!
1160 ##################################################################
1161 if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
1162     set(CMAKE_SKIP_BUILD_RPATH  FALSE)
1163     set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
1164     set(CMAKE_INSTALL_RPATH "\\\$ORIGIN/../${GMXLIB}")
1165     set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
1166 endif()
1167
1168 #COPYING file: Only necessary for binary distributions.
1169 #Simpler to always install.
1170 install(FILES COPYING DESTINATION ${DATA_INSTALL_DIR} COMPONENT data)
1171
1172 add_subdirectory(share)
1173 add_subdirectory(include)
1174 add_subdirectory(src)
1175 add_subdirectory(scripts)
1176
1177 # Issue a warning if NVIDIA GPUs were detected, but CUDA was not found.
1178 # Don't bother the user after the first configure pass.
1179 if ((CUDA_NOTFOUND_AUTO AND GMX_DETECT_GPU_AVAILABLE) AND NOT GMX_GPU_DETECTION_DONE)
1180     message(WARNING "${CUDA_NOTFOUND_MESSAGE}")
1181     unset(CUDA_NOTFOUND_AUTO)
1182     unset(CUDA_NOTFOUND_MESSAGE)
1183 endif()
1184 set(GMX_GPU_DETECTION_DONE TRUE CACHE INTERNAL "Whether GPU detection has already been done")
1185
1186 #######################
1187 ## uninstall target
1188 #######################
1189     CONFIGURE_FILE(
1190                    "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
1191                    "${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake"
1192                    IMMEDIATE @ONLY)
1193 ###########################
1194 ADD_CUSTOM_TARGET(uninstall
1195                   "${CMAKE_COMMAND}" -P 
1196                   "${CMAKE_CURRENT_BINARY_DIR}/cmake/cmake_uninstall.cmake")
1197 ###########################
1198
1199 ########################################################################
1200 # Tests                                                                #
1201 ########################################################################
1202
1203 include(CTest)
1204 mark_as_advanced(BUILD_TESTING)
1205 IF(BUILD_TESTING)
1206         enable_testing()
1207         add_subdirectory(tests)
1208 ENDIF()
1209