Add initial support for python bindings
[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, 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 # - Find FFTW 3
36 # Find the native FFTW headers and libraries.
37 #
38 #  ${FFTW}_INCLUDE_DIRS - where to find FFTW headers
39 #  ${FFTW}_LIBRARIES    - List of libraries when using FFTW.
40 #  ${FFTW}_PKG          - The name of the pkg-config package needed
41 #  ${FFTW}_HAVE_SIMD    - True if FFTW was built with SIMD support
42 #  ${FFTW}_HAVE_AVX     - True if FFTW was built with AVX support
43 #  ${FFTW}_FOUND        - True if FFTW was found
44 #  where ${FFTW} is FFTW or FFTWF
45
46 list(LENGTH FFTW_FIND_COMPONENTS FFTW_NUM_COMPONENTS_WANTED)
47 if(${FFTW_NUM_COMPONENTS_WANTED} LESS 1)
48   message(FATAL_ERROR "No FFTW component to search given")
49 elseif(${FFTW_NUM_COMPONENTS_WANTED} GREATER 1)
50   message(FATAL_ERROR "We only support finding one FFTW component at the time, go and implement it ;-)")
51 elseif(${FFTW_FIND_COMPONENTS} MATCHES "^fftw(f)?$")
52   if (NOT FFTW_FIND_VERSION OR FFTW_FIND_VERSION EQUAL 3) #find FFTW3 by default
53     string(TOUPPER "${FFTW_FIND_COMPONENTS}" FFTW)
54     string(REGEX REPLACE "fftw" "fftw3" ${FFTW}_PKG "${FFTW_FIND_COMPONENTS}")
55     set(${FFTW}_FUNCTION_PREFIX "${FFTW_FIND_COMPONENTS}")
56   else()
57     message(FATAL_ERROR "We only support finding FFTW version 3, go and implement it ;-)")
58   endif()
59 else()
60   message(FATAL_ERROR "We do not support finding ${FFTW_FIND_COMPONENTS}, go and implement it ;-)")
61 endif()
62
63 find_package(PkgConfig QUIET)
64 if(NOT __pkg_config_checked_PC_${FFTW} OR NOT ${FFTW}_LIBRARY)
65   pkg_check_modules(PC_${FFTW} "${${FFTW}_PKG}")
66   if(NOT PC_${FFTW}_FOUND)
67     message(STATUS "pkg-config could not detect ${${FFTW}_PKG}, trying generic detection")
68   endif()
69 endif()
70
71 find_path(${FFTW}_INCLUDE_DIR "fftw3.h" HINTS ${PC_${FFTW}_INCLUDE_DIRS})
72 find_library(${FFTW}_LIBRARY NAMES "${${FFTW}_PKG}" HINTS ${PC_${FFTW}_LIBRARY_DIRS})
73
74 set(${FFTW}_LIBRARIES "${${FFTW}_LIBRARY}")
75 set(${FFTW}_INCLUDE_DIRS "${${FFTW}_INCLUDE_DIR}")
76
77 #better error message than find_package_handle_standard_args
78 if (${FFTW}_LIBRARY AND ${FFTW}_INCLUDE_DIR)
79   set(${FFTW}_FOUND TRUE)
80 elseif (NOT ${FFTW}_LIBRARY)
81   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')")
82 elseif (NOT ${FFTW}_INCLUDE_DIR)
83   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')")
84 endif()
85
86 if (${FFTW}_FOUND)
87   #The user could specify trash in ${FFTW}_LIBRARY, so test if we can link it
88   include(CheckLibraryExists)
89   include(gmxOptionUtilities)
90   if (HAVE_LIBM)
91     #adding MATH_LIBRARIES here to allow static libs, this does not harm us as we are anyway using it
92     set(CMAKE_REQUIRED_LIBRARIES m)
93   endif ()
94   gmx_check_if_changed(FFTW_LIBRARY_CHANGED ${FFTW}_LIBRARIES)
95   if (FFTW_LIBRARY_CHANGED)
96     unset(FOUND_${FFTW}_PLAN CACHE)
97   endif()
98   check_library_exists("${${FFTW}_LIBRARIES}" "${${FFTW}_FUNCTION_PREFIX}_plan_r2r_1d" "" FOUND_${FFTW}_PLAN)
99   if(NOT FOUND_${FFTW}_PLAN)
100     message(FATAL_ERROR "Could not find ${${FFTW}_FUNCTION_PREFIX}_plan_r2r_1d 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') !")
101   endif()
102
103   # Check for FFTW3 compiled with --enable-avx, which is slower for GROMACS than --enable-sse or --enable-sse2
104   foreach(AVX_FUNCTION ${${FFTW}_FUNCTION_PREFIX}_have_simd_avx)
105     if (FFTW_LIBRARY_CHANGED)
106       unset(${FFTW}_HAVE_${AVX_FUNCTION} CACHE)
107     endif()
108     check_library_exists("${${FFTW}_LIBRARIES}" "${AVX_FUNCTION}" "" ${FFTW}_HAVE_${AVX_FUNCTION})
109     if(${FFTW}_HAVE_${AVX_FUNCTION})
110       set(${FFTW}_HAVE_AVX TRUE)
111       break()
112     endif()
113   endforeach()
114
115   #in 3.3 sse function name has changed
116   foreach(SIMD_FCT ${${FFTW}_FUNCTION_PREFIX}_have_simd_sse2;${${FFTW}_FUNCTION_PREFIX}_have_simd_avx;${${FFTW}_FUNCTION_PREFIX}_have_simd_altivec;${${FFTW}_FUNCTION_PREFIX}_have_simd_neon;${${FFTW}_FUNCTION_PREFIX}_have_sse2;${${FFTW}_FUNCTION_PREFIX}_have_sse;${${FFTW}_FUNCTION_PREFIX}_have_altivec)
117     if (FFTW_LIBRARY_CHANGED)
118       unset(${FFTW}_HAVE_${SIMD_FCT} CACHE)
119     endif()
120     check_library_exists("${${FFTW}_LIBRARIES}" "${SIMD_FCT}" "" ${FFTW}_HAVE_${SIMD_FCT})
121     if(${FFTW}_HAVE_${SIMD_FCT})
122       set(${FFTW}_HAVE_SIMD TRUE)
123       break()
124     endif()
125   endforeach()
126   #Verify FFTW is compiled with fPIC (necessary for shared libraries)
127   if (CMAKE_OBJDUMP AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" AND BUILD_SHARED_LIBS AND NOT CYGWIN)
128       execute_process(COMMAND ${CMAKE_OBJDUMP} --reloc ${${FFTW}_LIBRARY} OUTPUT_VARIABLE ${FFTW}_OBJDUMP)
129       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
130               AND NOT ${${FFTW}_OBJDUMP} MATCHES "R_X86_64_PLT32")
131           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.")
132       endif()
133   endif()
134   set(CMAKE_REQUIRED_LIBRARIES)
135 endif ()
136
137 mark_as_advanced(${FFTW}_INCLUDE_DIR ${FFTW}_LIBRARY)