a0ba03b1f1441c3675ed7108d0b172479bf8999c
[alexxy/gromacs.git] / cmake / FindFFTW.cmake
1 # - Find FFTW 3
2 # Find the native FFTW headers and libraries.
3 #
4 #  ${FFTW}_INCLUDE_DIRS - where to find FFTW headers
5 #  ${FFTW}_LIBRARIES    - List of libraries when using FFTW.
6 #  ${FFTW}_PKG          - The name of the pkg-config package needed
7 #  ${FFTW}_HAVE_SIMD    - True if FFTW was built with SIMD support
8 #  ${FFTW}_HAVE_AVX     - True if FFTW was built with AVX support
9 #  ${FFTW}_FOUND        - True if FFTW was found
10 #  where ${FFTW} is FFTW or FFTWF
11 #
12 #  This file is part of Gromacs        Copyright (c) 2012
13
14 list(LENGTH FFTW_FIND_COMPONENTS FFTW_NUM_COMPONENTS_WANTED)
15 if(${FFTW_NUM_COMPONENTS_WANTED} LESS 1)
16   message(FATAL_ERROR "No FFTW component to search given")
17 elseif(${FFTW_NUM_COMPONENTS_WANTED} GREATER 1)
18   message(FATAL_ERROR "We only support finding one FFTW component at the time, go and implement it ;-)")
19 elseif(${FFTW_FIND_COMPONENTS} MATCHES "^fftw(f)?$")
20   if (NOT FFTW_FIND_VERSION OR FFTW_FIND_VERSION EQUAL 3) #find FFTW3 by default
21     string(TOUPPER "${FFTW_FIND_COMPONENTS}" FFTW)
22     string(REGEX REPLACE "fftw" "fftw3" ${FFTW}_PKG "${FFTW_FIND_COMPONENTS}")
23     set(${FFTW}_FUNCTION_PREFIX "${FFTW_FIND_COMPONENTS}")
24   else()
25     message(FATAL_ERROR "We only support finding FFTW version 3, go and implement it ;-)")
26   endif()
27 else()
28   message(FATAL_ERROR "We do not support finding ${FFTW_FIND_COMPONENTS}, go and implement it ;-)")
29 endif()
30
31 find_package(PkgConfig)
32 if(NOT __pkg_config_checked_PC_${FFTW} OR NOT ${FFTW}_LIBRARY)
33   pkg_check_modules(PC_${FFTW} "${${FFTW}_PKG}")
34   if(NOT PC_${FFTW}_FOUND)
35     message(STATUS "pkg-config could not detect ${${FFTW}_PKG}, trying generic detection")
36   endif()
37 endif(NOT __pkg_config_checked_PC_${FFTW} OR NOT ${FFTW}_LIBRARY)
38
39 find_path(${FFTW}_INCLUDE_DIR "fftw3.h" HINTS ${PC_${FFTW}_INCLUDE_DIRS})
40 find_library(${FFTW}_LIBRARY NAMES "${${FFTW}_PKG}" HINTS ${PC_${FFTW}_LIBRARY_DIRS})
41
42 set(${FFTW}_LIBRARIES "${${FFTW}_LIBRARY}")
43 set(${FFTW}_INCLUDE_DIRS "${${FFTW}_INCLUDE_DIR}")
44
45 #better error message than find_package_handle_standard_args
46 if (${FFTW}_LIBRARY AND ${FFTW}_INCLUDE_DIR)
47   set(${FFTW}_FOUND TRUE)
48 elseif (NOT ${FFTW}_LIBRARY)
49   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')")
50 elseif (NOT ${FFTW}_INCLUDE_DIR)
51   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')")
52 endif()
53
54 if (${FFTW}_FOUND)
55   #The user could specify trash in ${FFTW}_LIBRARY, so test if we can link it
56   include(CheckLibraryExists)
57   if (HAVE_LIBM)
58     #adding MATH_LIBRARIES here to allow static libs, this does not harm us as we are anyway using it
59     set(CMAKE_REQUIRED_LIBRARIES m)
60   endif (HAVE_LIBM)
61   check_library_exists("${${FFTW}_LIBRARIES}" "${${FFTW}_FUNCTION_PREFIX}_plan_r2r_1d" "" FOUND_${FFTW}_PLAN)
62   if(NOT FOUND_${FFTW}_PLAN)
63     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') !")
64   endif(NOT FOUND_${FFTW}_PLAN)
65
66   # Check for FFTW3 compiled with --enable-avx, which is slower for GROMACS than --enable-sse or --enable-sse2
67   foreach(AVX_FUNCTION ${${FFTW}_FUNCTION_PREFIX}_have_simd_avx)
68     check_library_exists("${${FFTW}_LIBRARIES}" "${AVX_FUNCTION}" "" ${FFTW}_HAVE_${AVX_FUNCTION})
69     if(${FFTW}_HAVE_${AVX_FUNCTION})
70       set(${FFTW}_HAVE_AVX TRUE)
71       break()
72     endif(${FFTW}_HAVE_${AVX_FUNCTION})
73   endforeach()
74
75   #in 3.3 sse function name has changed
76   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)
77     check_library_exists("${${FFTW}_LIBRARIES}" "${SIMD_FCT}" "" ${FFTW}_HAVE_${SIMD_FCT})
78     if(${FFTW}_HAVE_${SIMD_FCT})
79       set(${FFTW}_HAVE_SIMD TRUE)
80       break()
81     endif(${FFTW}_HAVE_${SIMD_FCT})
82   endforeach()
83   #Verify FFTW is compiled with fPIC (necessary for shared libraries)
84   if (CMAKE_OBJDUMP AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" AND BUILD_SHARED_LIBS)
85       execute_process(COMMAND ${CMAKE_OBJDUMP} --reloc ${${FFTW}_LIBRARY} OUTPUT_VARIABLE ${FFTW}_OBJDUMP)
86       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
87               AND NOT ${${FFTW}_OBJDUMP} MATCHES "R_X86_64_PLT32")
88           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.")
89       endif ()
90   endif ()
91   set(CMAKE_REQUIRED_LIBRARIES)
92 endif (${FFTW}_FOUND)
93
94 mark_as_advanced(${FFTW}_INCLUDE_DIR ${FFTW}_LIBRARY)