f46fe2c21b2797d3a789853de951904a5410a8a6
[alexxy/gromacs.git] / cmake / FindFFTW.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5 # Copyright (c) 2017,2018,2020, by the GROMACS development team, led by
6 # Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 # and including many others, as listed in the AUTHORS file in the
8 # top-level source directory and at http://www.gromacs.org.
9 #
10 # GROMACS is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public License
12 # as published by the Free Software Foundation; either version 2.1
13 # of the License, or (at your option) any later version.
14 #
15 # GROMACS is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # Lesser General Public License for more details.
19 #
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with GROMACS; if not, see
22 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
24 #
25 # If you want to redistribute modifications to GROMACS, please
26 # consider that scientific software is very special. Version
27 # control is crucial - bugs must be traceable. We will be happy to
28 # consider code for inclusion in the official distribution, but
29 # derived work must not be called official GROMACS. Details are found
30 # in the README & COPYING files - if they are missing, get the
31 # official version at http://www.gromacs.org.
32 #
33 # To help us fund GROMACS development, we humbly ask that you cite
34 # the research papers on the package. Check out http://www.gromacs.org.
35
36 # - Find FFTW 3
37 # Find the native FFTW headers and libraries.
38 #
39 #  ${FFTW}_INCLUDE_DIRS - where to find FFTW headers
40 #  ${FFTW}_LIBRARIES    - List of libraries when using FFTW.
41 #  ${FFTW}_PKG          - The name of the pkg-config package needed
42 #  ${FFTW}_HAVE_SIMD    - True if FFTW was built with SIMD support
43 #  ${FFTW}_HAVE_SSE     - True if FFTW was built with SSE support
44 #  ${FFTW}_HAVE_SSE2    - True if FFTW was built with SSE2 support
45 #  ${FFTW}_FOUND        - True if FFTW was found
46 #  where ${FFTW} is FFTW or FFTWF
47
48 list(LENGTH FFTW_FIND_COMPONENTS FFTW_NUM_COMPONENTS_WANTED)
49 if(${FFTW_NUM_COMPONENTS_WANTED} LESS 1)
50   message(FATAL_ERROR "No FFTW component to search given")
51 elseif(${FFTW_NUM_COMPONENTS_WANTED} GREATER 1)
52   message(FATAL_ERROR "We only support finding one FFTW component at the time, go and implement it ;-)")
53 elseif(${FFTW_FIND_COMPONENTS} MATCHES "^fftw(f)?$")
54   if (NOT FFTW_FIND_VERSION OR FFTW_FIND_VERSION EQUAL 3) #find FFTW3 by default
55     string(TOUPPER "${FFTW_FIND_COMPONENTS}" FFTW)
56     string(REGEX REPLACE "fftw" "fftw3" ${FFTW}_PKG "${FFTW_FIND_COMPONENTS}")
57     set(${FFTW}_FUNCTION_PREFIX "${FFTW_FIND_COMPONENTS}")
58   else()
59     message(FATAL_ERROR "We only support finding FFTW version 3, go and implement it ;-)")
60   endif()
61 else()
62   message(FATAL_ERROR "We do not support finding ${FFTW_FIND_COMPONENTS}, go and implement it ;-)")
63 endif()
64
65 find_package(PkgConfig QUIET)
66 if(NOT __pkg_config_checked_PC_${FFTW} OR NOT ${FFTW}_LIBRARY)
67   pkg_check_modules(PC_${FFTW} "${${FFTW}_PKG}")
68   if(NOT PC_${FFTW}_FOUND)
69     message(STATUS "pkg-config could not detect ${${FFTW}_PKG}, trying generic detection")
70   endif()
71 endif()
72
73 find_path(${FFTW}_INCLUDE_DIR "fftw3.h" HINTS ${PC_${FFTW}_INCLUDE_DIRS})
74 find_library(${FFTW}_LIBRARY NAMES "${${FFTW}_PKG}" HINTS ${PC_${FFTW}_LIBRARY_DIRS})
75
76 set(${FFTW}_LIBRARIES "${${FFTW}_LIBRARY}")
77 set(${FFTW}_INCLUDE_DIRS "${${FFTW}_INCLUDE_DIR}")
78
79 #better error message than find_package_handle_standard_args
80 if (${FFTW}_LIBRARY AND ${FFTW}_INCLUDE_DIR)
81   set(${FFTW}_FOUND TRUE)
82 elseif (NOT ${FFTW}_LIBRARY)
83   message("Could not find ${${FFTW}_PKG} library named lib${${FFTW}_PKG}, please specify its location in CMAKE_PREFIX_PATH or ${FFTW}_LIBRARY by hand (e.g. -D${FFTW}_LIBRARY='/path/to/lib${${FFTW}_PKG}.so')")
84 elseif (NOT ${FFTW}_INCLUDE_DIR)
85   message("Could not the ${${FFTW}_PKG} header fftw3.h, please specify its path in ${FFTW}_INCLUDE_DIR by hand (e.g. -D${FFTW}_INCLUDE_DIR='/path/to/include')")
86 endif()
87
88 if (${FFTW}_FOUND)
89   #The user could specify trash in ${FFTW}_LIBRARY, so test if we can link it
90   include(CheckLibraryExists)
91   include(gmxOptionUtilities)
92   if (HAVE_LIBM)
93     #adding MATH_LIBRARIES here to allow static libs, this does not harm us as we are anyway using it
94     set(CMAKE_REQUIRED_LIBRARIES m)
95   endif ()
96   gmx_check_if_changed(FFTW_LIBRARY_CHANGED ${FFTW}_LIBRARIES)
97   if (FFTW_LIBRARY_CHANGED)
98     unset(FOUND_${FFTW}_PLAN CACHE)
99   endif()
100   check_library_exists("${${FFTW}_LIBRARIES}" "${${FFTW}_FUNCTION_PREFIX}_plan_many_dft"     "" FOUND_${FFTW}_PLAN_MANY)
101   check_library_exists("${${FFTW}_LIBRARIES}" "${${FFTW}_FUNCTION_PREFIX}_plan_many_dft_r2c" "" FOUND_${FFTW}_PLAN_MANY_R2C)
102   check_library_exists("${${FFTW}_LIBRARIES}" "${${FFTW}_FUNCTION_PREFIX}_plan_many_dft_c2r" "" FOUND_${FFTW}_PLAN_MANY_C2R)
103   if(NOT (FOUND_${FFTW}_PLAN_MANY AND
104       FOUND_${FFTW}_PLAN_MANY_R2C AND
105       FOUND_${FFTW}_PLAN_MANY_C2R))
106       message(FATAL_ERROR "Could not find ${${FFTW}_FUNCTION_PREFIX}_plan_many_[r2c|c2r] in ${${FFTW}_LIBRARY}, take a look at the error message in ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log to find out what went wrong. If you are using a static lib (.a) make sure you have specified all dependencies of ${${FFTW}_PKG} in ${FFTW}_LIBRARY by hand (e.g. -D${FFTW}_LIBRARY='/path/to/lib${${FFTW}_PKG}.so;/path/to/libm.so') !")
107   endif()
108
109   # Check for FFTW3 compiled with --enable-sse
110   foreach(SSE_FUNCTION ${${FFTW}_FUNCTION_PREFIX}_have_simd_sse ${${FFTW}_FUNCTION_PREFIX}_have_sse)
111     if (FFTW_LIBRARY_CHANGED)
112       unset(${FFTW}_HAVE_${SSE_FUNCTION} CACHE)
113     endif()
114     check_library_exists("${${FFTW}_LIBRARIES}" "${SSE_FUNCTION}" "" ${FFTW}_HAVE_${SSE_FUNCTION})
115     if(${FFTW}_HAVE_${SSE_FUNCTION})
116       set(${FFTW}_HAVE_SSE TRUE)
117       set(${FFTW}_HAVE_SIMD TRUE)
118       break()
119     endif()
120   endforeach()
121
122   # Check for FFTW3 compiled with --enable-sse2
123   foreach(SSE2_FUNCTION ${${FFTW}_FUNCTION_PREFIX}_have_simd_sse2 ${${FFTW}_FUNCTION_PREFIX}_have_sse2)
124     if (FFTW_LIBRARY_CHANGED)
125       unset(${FFTW}_HAVE_${SSE2_FUNCTION} CACHE)
126     endif()
127     check_library_exists("${${FFTW}_LIBRARIES}" "${SSE2_FUNCTION}" "" ${FFTW}_HAVE_${SSE2_FUNCTION})
128     if(${FFTW}_HAVE_${SSE2_FUNCTION})
129       set(${FFTW}_HAVE_SSE2 TRUE)
130       set(${FFTW}_HAVE_SIMD TRUE)
131       break()
132     endif()
133   endforeach()
134
135   # Check for any other SIMD support in FFTW
136   if (NOT ${FFTW}_HAVE_SIMD)
137       foreach(SIMD_FCT
138               ${${FFTW}_FUNCTION_PREFIX}_have_simd_avx
139               ${${FFTW}_FUNCTION_PREFIX}_have_simd_avx2
140               ${${FFTW}_FUNCTION_PREFIX}_have_simd_avx2_128
141               ${${FFTW}_FUNCTION_PREFIX}_have_simd_avx512
142               ${${FFTW}_FUNCTION_PREFIX}_have_simd_avx_128_fma
143               ${${FFTW}_FUNCTION_PREFIX}_have_simd_avx_512
144               ${${FFTW}_FUNCTION_PREFIX}_have_simd_kcvi
145               ${${FFTW}_FUNCTION_PREFIX}_have_simd_altivec
146               ${${FFTW}_FUNCTION_PREFIX}_have_simd_neon
147               ${${FFTW}_FUNCTION_PREFIX}_have_simd_vsx
148               ${${FFTW}_FUNCTION_PREFIX}_have_simd_altivec
149               ${${FFTW}_FUNCTION_PREFIX}_have_altivec) # Name used before FFTW 3.3
150           if (FFTW_LIBRARY_CHANGED)
151               unset(${FFTW}_HAVE_${SIMD_FCT} CACHE)
152           endif()
153           check_library_exists("${${FFTW}_LIBRARIES}" "${SIMD_FCT}" "" ${FFTW}_HAVE_${SIMD_FCT})
154           if(${FFTW}_HAVE_${SIMD_FCT})
155               set(${FFTW}_HAVE_SIMD TRUE)
156               break()
157           endif()
158       endforeach()
159   endif()
160
161   #Verify FFTW is compiled with fPIC (necessary for shared libraries)
162   if (CMAKE_OBJDUMP AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" AND BUILD_SHARED_LIBS AND NOT CYGWIN AND NOT APPLE)
163       execute_process(COMMAND ${CMAKE_OBJDUMP} --reloc ${${FFTW}_LIBRARY} OUTPUT_VARIABLE ${FFTW}_OBJDUMP)
164       if (${${FFTW}_OBJDUMP} MATCHES "R_X86_64" #Should always be true for static libraries. Checks that objdump works properly and that the library isn't dynamic
165               AND NOT ${${FFTW}_OBJDUMP} MATCHES "R_X86_64_PLT32")
166           message(FATAL_ERROR "The FFTW library ${${FFTW}_LIBRARY} cannot be used with shared libraries. Provide a different FFTW library by setting ${FFTW}_LIBRARY. If you don't have a different one, recompile FFTW with \"--enable-shared\" or \"--with-pic\". Or disable shared libraries for GROMACS by setting BUILD_SHARED_LIBS to \"no\". Note: Disabling shared libraries requires up to 10x as much disk space.")
167       endif()
168   endif()
169   set(CMAKE_REQUIRED_LIBRARIES)
170 endif ()
171
172 mark_as_advanced(${FFTW}_INCLUDE_DIR ${FFTW}_LIBRARY)