a0a1379a0124f0e41e0f42a6bb10c4f95e7b0fe9
[alexxy/gromacs.git] / src / external / build-fftw / CMakeLists.txt
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012,2013,2014,2015,2016,2017,2018, 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_ACTIVE} MATCHES "^(SSE|AVX)" AND APPLE)
64     # OS X and --enable-avx causes compilation issues (fftw always picks gcc by default). 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_ACTIVE} MATCHES "^(SSE)")
69     set(_fftw_simd_support_level "--enable-sse2")
70 elseif(${GMX_SIMD_ACTIVE} MATCHES "^(AVX)" AND NOT ${GMX_SIMD_ACTIVE} MATCHES "^(AVX_512)")
71     # Testing shows FFTW configured with --enable-sse2 --enable-avx is
72     # slightly faster on most architectures than --enable-sse2 alone.
73     # Support for --enable-avx2 was only added in 3.3.5, but
74     # configuring with it is at worst a warning, even on an earlier
75     # version.
76     # On platforms capable of AVX512 where we are building with AVX2,
77     # enabling AVX512 risks clock-throttling the entire mdrun if
78     # fftw happens to pick up an AVX512 kernel (which is not unlikely
79     # as fftw tuning is known to produce highly varying results).
80     set(_fftw_simd_support_level --enable-sse2;--enable-avx;--enable-avx2)
81 elseif(${GMX_SIMD_ACTIVE} MATCHES "^(AVX_512)")
82     # MSVC, GCC < 4.9, Clang < 3.9 do not support AVX-512, so
83     # we should not enable it there. FFTW does not support clang with
84     # AVX-512, so we should not enable that either.
85     if(MSVC OR (CMAKE_COMPILER_IS_GNUCC AND CMAKE_C_COMPILER_VERSION VERSION_LESS 4.9.0) OR
86         (CMAKE_C_COMPILER_ID MATCHES "Clang" AND CMAKE_C_COMPILER_VERSION VERSION_LESS 3.9.0) OR
87         (CMAKE_C_COMPILER_ID MATCHES "Clang" AND ${GMX_SIMD_ACTIVE} MATCHES "^(AVX_512)"))
88         set(_fftw_simd_support_level --enable-sse2;--enable-avx;--enable-avx2)
89     else()
90         set(_fftw_simd_support_level --enable-sse2;--enable-avx;--enable-avx2;--enable-avx512)
91     endif()
92 elseif(${GMX_SIMD_ACTIVE} MATCHES "^(VSX)")
93     set(_fftw_simd_support_level --enable-vsx)
94 endif()
95 set(GMX_BUILD_OWN_FFTW_OPTIMIZATION_CONFIGURATION ${_fftw_simd_support_level} CACHE INTERNAL "Optimization flags for FFTW compilation")
96
97 # Allow cross-compiles
98 if (TARGET_HOST)
99     set(GMX_BUILD_OWN_FFTW_TARGET_HOST --host=${TARGET_HOST})
100 endif()
101
102 # Machinery for running the external project
103 set(EXTERNAL_FFTW_VERSION 3.3.8)
104 set(GMX_BUILD_OWN_FFTW_URL
105     "http://www.fftw.org/fftw-${EXTERNAL_FFTW_VERSION}.tar.gz" CACHE STRING
106     "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})")
107 set(GMX_BUILD_OWN_FFTW_MD5 8aac833c943d8e90d51b697b27d4384d CACHE STRING
108     "Expected MD5 hash for the file at GMX_BUILD_OWN_FFTW_URL")
109 mark_as_advanced(GMX_BUILD_OWN_FFTW_URL GMX_BUILD_OWN_FFTW_MD5)
110
111 # The actual build target.
112 set(EXTERNAL_FFTW_BUILD_TARGET fftwBuild)
113 include(ExternalProject)
114 ExternalProject_add(${EXTERNAL_FFTW_BUILD_TARGET}
115         URL "${GMX_BUILD_OWN_FFTW_URL}"
116         URL_MD5 ${GMX_BUILD_OWN_FFTW_MD5}
117         CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR> --libdir=<INSTALL_DIR>/lib --disable-fortran
118         ${GMX_BUILD_OWN_FFTW_SHARED_FLAG} ${GMX_BUILD_OWN_FFTW_OPTIMIZATION_CONFIGURATION}
119         ${GMX_BUILD_OWN_FFTW_PREC}
120         ${GMX_BUILD_OWN_FFTW_TARGET_HOST})
121 ExternalProject_get_property(${EXTERNAL_FFTW_BUILD_TARGET} INSTALL_DIR)
122
123 string(REGEX REPLACE "fftw" "fftw3" FFTW_LIBNAME ${LOWERFFTW})
124 set(${UPPERFFTW}_LIBRARIES ${INSTALL_DIR}/lib/lib${FFTW_LIBNAME}${CMAKE_STATIC_LIBRARY_SUFFIX})
125 set(${UPPERFFTW}_INCLUDE_DIRS ${INSTALL_DIR}/include PARENT_SCOPE)
126
127 add_library(gmxfftw STATIC IMPORTED GLOBAL)
128 set_target_properties(gmxfftw PROPERTIES IMPORTED_LOCATION ${${UPPERFFTW}_LIBRARIES})
129 set(${UPPERFFTW}_LIBRARIES gmxfftw PARENT_SCOPE)
130 add_dependencies(gmxfftw ${EXTERNAL_FFTW_BUILD_TARGET})
131
132 message(STATUS "The GROMACS-managed build of FFTW 3 will configure with the following optimizations: ${GMX_BUILD_OWN_FFTW_OPTIMIZATION_CONFIGURATION}")