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