2 # This file is part of the GROMACS molecular simulation package.
4 # Copyright (c) 2012,2013,2014,2015, by the GROMACS development team, led by
5 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 # and including many others, as listed in the AUTHORS file in the
7 # top-level source directory and at http://www.gromacs.org.
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.
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.
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.
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.
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.
35 # Download and build a suitable copy of FFTW.
36 # The GROMACS team won't distribute source or binaries linked to FFTW
37 # because we are choosing to be very clear about distributing only
38 # LGPL-licensed code, to suit requirements from our funding source.
40 # Input: FFTW variable contains the FFTW component to build,
41 # either fftw or fftwf for double or single precision
43 string(TOUPPER "${FFTW}" UPPERFFTW)
44 string(TOLOWER "${FFTW}" LOWERFFTW)
46 set(GMX_BUILD_OWN_FFTW_OPTIMIZATION_CONFIGURATION "" CACHE INTERNAL "Optimization flags for FFTW compilation")
47 if(${CMAKE_CURRENT_BINARY_DIR} MATCHES ".*[[:space:]].*")
48 message(FATAL_ERROR "An internal limitation of FFTW means GROMACS cannot build FFTW in a directory with whitespace in its name. Either use a system FFTW, build it yourself, or build GROMACS in a different location.")
52 set(GMX_BUILD_OWN_FFTW_PREC --enable-float)
55 # Always build a static lib, so it gets added to libmd and doesn't need to be installed
56 set(GMX_BUILD_OWN_FFTW_SHARED_FLAG --disable-shared --enable-static)
57 if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" AND BUILD_SHARED_LIBS) # FFTW doesn't use -DPIC by default
58 set(GMX_BUILD_OWN_FFTW_SHARED_FLAG ${GMX_BUILD_OWN_FFTW_SHARED_FLAG} --with-pic)
61 # Testing shows FFTW configured with --enable-avx is never better than --enable-sse2, so we do the latter always.
62 if(${GMX_SIMD} MATCHES "^(SSE|AVX)")
63 set(GMX_BUILD_OWN_FFTW_OPTIMIZATION_CONFIGURATION --enable-sse2 CACHE INTERNAL "Optimization flags for FFTW compilation")
64 # Fake the result of the SIMD test, so the checks for x86 SIMD
66 set(${UPPERFFTW}_HAVE_SSE2 TRUE PARENT_SCOPE)
69 # Allow cross-compiles
71 set(GMX_BUILD_OWN_FFTW_TARGET_HOST --host=${TARGET_HOST})
74 # Machinery for running the external project
75 set(EXTERNAL_FFTW_VERSION 3.3.3)
76 # cmake make eats slashes //// -> //
77 set(GMX_BUILD_OWN_FFTW_URL
78 "http:////www.fftw.org/fftw-${EXTERNAL_FFTW_VERSION}.tar.gz" CACHE PATH
79 "URL from where to download fftw (use an absolute path when offline, adjust GMX_BUILD_OWN_FFTW_MD5 if downloading other version than ${EXTERNAL_FFTW_VERSION})")
80 set(GMX_BUILD_OWN_FFTW_MD5 0a05ca9c7b3bfddc8278e7c40791a1c2 CACHE STRING
81 "Expected MD5 hash for the file at GMX_BUILD_OWN_FFTW_URL")
82 mark_as_advanced(GMX_BUILD_OWN_FFTW_URL GMX_BUILD_OWN_FFTW_MD5)
84 # ExternalProject at least up to CMake 3.0 prints a confusing error message if
85 # download fails when MD5 verification is enabled. So we manage the download
86 # ourselves so that MD5 sum is not verified there, and then pass a local file
87 # as the URL to ExternalProject. This way, ExternalProject still verifies the
88 # MD5 sum with a proper message if that fails.
89 set(url "${GMX_BUILD_OWN_FFTW_URL}")
90 # Determine whether we are actually downloading (this matches the conditions in
91 # ExternalProject). ExternalProject works as expected if passed a local file.
93 if (IS_DIRECTORY "${url}" OR "${url}" MATCHES "^file://" OR NOT "${url}" MATCHES "^[a-z]+://")
94 set(is_download FALSE)
97 # For simplicity, don't try to extract the file name from the URL, but use
99 set(remote_url "${GMX_BUILD_OWN_FFTW_URL}")
100 set(local_path "${CMAKE_CURRENT_BINARY_DIR}/fftw.tar.gz")
101 set(url ${local_path})
102 # Write a script to do our own download step (mimics what ExternalProject
103 # would do, but without MD5 sum verification at this step).
104 set(download_script ${CMAKE_CURRENT_BINARY_DIR}/fftw-download.cmake)
105 configure_file(fftw-download.cmake.cmakein ${download_script} @ONLY)
108 # The actual build target.
109 set(EXTERNAL_FFTW_BUILD_TARGET fftwBuild)
110 include(ExternalProject)
111 ExternalProject_add(${EXTERNAL_FFTW_BUILD_TARGET}
112 URL "${url}" URL_MD5 ${GMX_BUILD_OWN_FFTW_MD5}
113 CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR> --libdir=<INSTALL_DIR>/lib --disable-fortran
114 ${GMX_BUILD_OWN_FFTW_SHARED_FLAG} ${GMX_BUILD_OWN_FFTW_OPTIMIZATION_CONFIGURATION}
115 ${GMX_BUILD_OWN_FFTW_PREC}
116 ${GMX_BUILD_OWN_FFTW_TARGET_HOST})
117 # Add a custom step to do our own download if that is necessary.
119 ExternalProject_add_step(${EXTERNAL_FFTW_BUILD_TARGET} pre-download
120 COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/fftw-download.cmake
124 ExternalProject_get_property(${EXTERNAL_FFTW_BUILD_TARGET} INSTALL_DIR)
126 string(REGEX REPLACE "fftw" "fftw3" FFTW_LIBNAME ${LOWERFFTW})
127 set(${UPPERFFTW}_LIBRARIES ${INSTALL_DIR}/lib/lib${FFTW_LIBNAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
128 set(${UPPERFFTW}_INCLUDE_DIRS ${INSTALL_DIR}/include PARENT_SCOPE)
129 set(${UPPERFFTW}_FOUND TRUE PARENT_SCOPE)
130 # TODO This is only true on x86, but does not actually cause a problem
131 set(${UPPERFFTW}_HAVE_SIMD TRUE PARENT_SCOPE)
133 add_library(gmxfftw STATIC IMPORTED GLOBAL)
134 set_target_properties(gmxfftw PROPERTIES IMPORTED_LOCATION ${${UPPERFFTW}_LIBRARIES})
135 set(${UPPERFFTW}_LIBRARIES gmxfftw PARENT_SCOPE)
136 add_dependencies(gmxfftw ${EXTERNAL_FFTW_BUILD_TARGET})
138 message(STATUS "The GROMACS-managed build of FFTW 3 will configure with the following optimizations: ${GMX_BUILD_OWN_FFTW_OPTIMIZATION_CONFIGURATION}")