Building own fftw now uses version 3.3.4
[alexxy/gromacs.git] / src / contrib / fftw / CMakeLists.txt
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
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.
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 # 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.
39 #
40 # Input: FFTW variable contains the FFTW component to build,
41 #        either fftw or fftwf for double or single precision
42
43 string(TOUPPER "${FFTW}" UPPERFFTW)
44 string(TOLOWER "${FFTW}" LOWERFFTW)
45
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.")
49 endif()
50
51 if(NOT GMX_DOUBLE)
52     set(GMX_BUILD_OWN_FFTW_PREC --enable-float)
53 endif()
54
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)
59 endif()
60
61 # Set library optimizations
62 set(_fftw_simd_support_level "")
63 if(${GMX_SIMD} MATCHES "^(SSE|AVX)" AND APPLE AND "${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
64     # OS X with gcc and --enable-avx causes compilation issues. It's
65     # not an important enough performance loss to bother warning the
66     # user about.
67     set(_fftw_simd_support_level "--enable-sse2")
68 elseif(${GMX_SIMD} MATCHES "^(SSE)")
69     set(_fftw_simd_support_level "--enable-sse2")
70 elseif(${GMX_SIMD} MATCHES "^(AVX)")
71     # Testing shows FFTW configured with --enable-sse2 --enable-avx is
72     # slightly faster on most architectures than --enable-sse2 alone
73     set(_fftw_simd_support_level "--enable-sse2 --enable-avx")
74 endif()
75 set(GMX_BUILD_OWN_FFTW_OPTIMIZATION_CONFIGURATION ${_fftw_simd_support_level} CACHE INTERNAL "Optimization flags for FFTW compilation")
76
77 # Allow cross-compiles
78 if (TARGET_HOST)
79     set(GMX_BUILD_OWN_FFTW_TARGET_HOST --host=${TARGET_HOST})
80 endif()
81
82 # Machinery for running the external project
83 set(EXTERNAL_FFTW_VERSION 3.3.4)
84 # cmake make eats slashes //// -> //
85 set(GMX_BUILD_OWN_FFTW_URL
86     "http:////www.fftw.org/fftw-${EXTERNAL_FFTW_VERSION}.tar.gz" CACHE PATH
87     "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})")
88 set(GMX_BUILD_OWN_FFTW_MD5 2edab8c06b24feeb3b82bbb3ebf3e7b3 CACHE STRING
89     "Expected MD5 hash for the file at GMX_BUILD_OWN_FFTW_URL")
90 mark_as_advanced(GMX_BUILD_OWN_FFTW_URL GMX_BUILD_OWN_FFTW_MD5)
91
92 # ExternalProject at least up to CMake 3.0 prints a confusing error message if
93 # download fails when MD5 verification is enabled.  So we manage the download
94 # ourselves so that MD5 sum is not verified there, and then pass a local file
95 # as the URL to ExternalProject.  This way, ExternalProject still verifies the
96 # MD5 sum with a proper message if that fails.
97 set(url "${GMX_BUILD_OWN_FFTW_URL}")
98 # Determine whether we are actually downloading (this matches the conditions in
99 # ExternalProject).  ExternalProject works as expected if passed a local file.
100 set(is_download TRUE)
101 if (IS_DIRECTORY "${url}" OR "${url}" MATCHES "^file://" OR NOT "${url}" MATCHES "^[a-z]+://")
102     set(is_download FALSE)
103 endif()
104 if (is_download)
105     # For simplicity, don't try to extract the file name from the URL, but use
106     # a hard-coded value.
107     set(remote_url "${GMX_BUILD_OWN_FFTW_URL}")
108     set(local_path "${CMAKE_CURRENT_BINARY_DIR}/fftw.tar.gz")
109     set(url ${local_path})
110     # Write a script to do our own download step (mimics what ExternalProject
111     # would do, but without MD5 sum verification at this step).
112     set(download_script ${CMAKE_CURRENT_BINARY_DIR}/fftw-download.cmake)
113     configure_file(fftw-download.cmake.cmakein ${download_script} @ONLY)
114 endif()
115
116 # The actual build target.
117 set(EXTERNAL_FFTW_BUILD_TARGET fftwBuild)
118 include(ExternalProject)
119 ExternalProject_add(${EXTERNAL_FFTW_BUILD_TARGET}
120         URL "${url}" URL_MD5 ${GMX_BUILD_OWN_FFTW_MD5}
121         CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR> --libdir=<INSTALL_DIR>/lib --disable-fortran
122         ${GMX_BUILD_OWN_FFTW_SHARED_FLAG} ${GMX_BUILD_OWN_FFTW_OPTIMIZATION_CONFIGURATION}
123         ${GMX_BUILD_OWN_FFTW_PREC}
124         ${GMX_BUILD_OWN_FFTW_TARGET_HOST})
125 # Add a custom step to do our own download if that is necessary.
126 if (is_download)
127     ExternalProject_add_step(${EXTERNAL_FFTW_BUILD_TARGET} pre-download
128             COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/fftw-download.cmake
129             DEPENDERS download)
130 endif()
131
132 ExternalProject_get_property(${EXTERNAL_FFTW_BUILD_TARGET} INSTALL_DIR)
133
134 string(REGEX REPLACE "fftw" "fftw3" FFTW_LIBNAME ${LOWERFFTW})
135 set(${UPPERFFTW}_LIBRARIES ${INSTALL_DIR}/lib/lib${FFTW_LIBNAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
136 set(${UPPERFFTW}_INCLUDE_DIRS ${INSTALL_DIR}/include PARENT_SCOPE)
137
138 add_library(gmxfftw STATIC IMPORTED GLOBAL)
139 set_target_properties(gmxfftw PROPERTIES IMPORTED_LOCATION ${${UPPERFFTW}_LIBRARIES})
140 set(${UPPERFFTW}_LIBRARIES gmxfftw PARENT_SCOPE)
141 add_dependencies(gmxfftw ${EXTERNAL_FFTW_BUILD_TARGET})
142
143 message(STATUS "The GROMACS-managed build of FFTW 3 will configure with the following optimizations: ${GMX_BUILD_OWN_FFTW_OPTIMIZATION_CONFIGURATION}")