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