538e0dd2ded9bf158e65d9fe11635f6ecec88b49
[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 # 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 endif()
65
66 # Allow cross-compiles
67 if (TARGET_HOST)
68     set(GMX_BUILD_OWN_FFTW_TARGET_HOST --host=${TARGET_HOST})
69 endif()
70
71 # Machinery for running the external project
72 set(EXTERNAL_FFTW_VERSION 3.3.3)
73 # cmake make eats slashes //// -> //
74 set(GMX_BUILD_OWN_FFTW_URL
75     "http:////www.fftw.org/fftw-${EXTERNAL_FFTW_VERSION}.tar.gz" CACHE PATH
76     "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})")
77 set(GMX_BUILD_OWN_FFTW_MD5 0a05ca9c7b3bfddc8278e7c40791a1c2 CACHE STRING
78     "Expected MD5 hash for the file at GMX_BUILD_OWN_FFTW_URL")
79 mark_as_advanced(GMX_BUILD_OWN_FFTW_URL GMX_BUILD_OWN_FFTW_MD5)
80
81 # ExternalProject at least up to CMake 3.0 prints a confusing error message if
82 # download fails when MD5 verification is enabled.  So we manage the download
83 # ourselves so that MD5 sum is not verified there, and then pass a local file
84 # as the URL to ExternalProject.  This way, ExternalProject still verifies the
85 # MD5 sum with a proper message if that fails.
86 set(url "${GMX_BUILD_OWN_FFTW_URL}")
87 # Determine whether we are actually downloading (this matches the conditions in
88 # ExternalProject).  ExternalProject works as expected if passed a local file.
89 set(is_download TRUE)
90 if (IS_DIRECTORY "${url}" OR "${url}" MATCHES "^file://" OR NOT "${url}" MATCHES "^[a-z]+://")
91     set(is_download FALSE)
92 endif()
93 if (is_download)
94     # For simplicity, don't try to extract the file name from the URL, but use
95     # a hard-coded value.
96     set(remote_url "${GMX_BUILD_OWN_FFTW_URL}")
97     set(local_path "${CMAKE_CURRENT_BINARY_DIR}/fftw.tar.gz")
98     set(url ${local_path})
99     # Write a script to do our own download step (mimics what ExternalProject
100     # would do, but without MD5 sum verification at this step).
101     set(download_script ${CMAKE_CURRENT_BINARY_DIR}/fftw-download.cmake)
102     configure_file(fftw-download.cmake.cmakein ${download_script} @ONLY)
103 endif()
104
105 # The actual build target.
106 set(EXTERNAL_FFTW_BUILD_TARGET fftwBuild)
107 include(ExternalProject)
108 ExternalProject_add(${EXTERNAL_FFTW_BUILD_TARGET}
109         URL "${url}" URL_MD5 ${GMX_BUILD_OWN_FFTW_MD5}
110         CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=<INSTALL_DIR> --libdir=<INSTALL_DIR>/lib --disable-fortran
111         ${GMX_BUILD_OWN_FFTW_SHARED_FLAG} ${GMX_BUILD_OWN_FFTW_OPTIMIZATION_CONFIGURATION}
112         ${GMX_BUILD_OWN_FFTW_PREC}
113         ${GMX_BUILD_OWN_FFTW_TARGET_HOST})
114 # Add a custom step to do our own download if that is necessary.
115 if (is_download)
116     ExternalProject_add_step(${EXTERNAL_FFTW_BUILD_TARGET} pre-download
117             COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/fftw-download.cmake
118             DEPENDERS download)
119 endif()
120
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}")