# - Find FFTW
# Find the native FFTW includes and library
# This module defines
#  FFTW_INCLUDE_DIR, where to find fftw3.h, etc.
#  FFTW_LIBRARIES, the libraries needed to use FFTW.
#  FFTW_FOUND, If false, do not try to use FFTW.
# also defined, but not for general use are
#  FFTW_LIBRARY, where to find the FFTW library.

FIND_PATH(FFTW_INCLUDE_DIR fftw3.h)

SET(FFTW_NAMES ${FFTW_NAMES} fftw3 fftw3 fftw3-3)
FIND_LIBRARY(FFTW_LIBRARY
  NAMES ${FFTW_NAMES}
)

# Find threads part of FFTW

SET(FFTW_THREADS_NAMES ${FFTW_THREADS_NAMES} fftw3_threads fftw3-3_threads)
FIND_LIBRARY(FFTW_THREADS_LIBRARY
  NAMES ${FFTW_THREADS_NAMES}
)

SET(FFTW_OMP_NAMES ${FFTW_OMP_NAMES} fftw3_omp fftw3-3_omp)
FIND_LIBRARY(FFTW_OMP_LIBRARY
  NAMES ${FFTW_OMP_NAMES}
)

SET(FFTW_MPI_NAMES ${FFTW_MPI_NAMES} fftw3_mpi fftw3-3_mpi)
FIND_LIBRARY(FFTW_MPI_LIBRARY
  NAMES ${FFTW_MPI_NAMES}
)


IF (FFTW_THREADS_LIBRARY AND FFTW_INCLUDE_DIR)
    SET(FFTW_THREADS_LIBRARIES ${FFTW_THREADS_LIBRARY})
    SET(FFTW_THREADS_FOUND "YES")
ELSE (FFTW_THREADS_LIBRARY AND FFTW_INCLUDE_DIR)
  SET(FFTW_THREADS_FOUND "NO")
ENDIF (FFTW_THREADS_LIBRARY AND FFTW_INCLUDE_DIR)

IF (FFTW_MPI_LIBRARY AND FFTW_INCLUDE_DIR)
  SET(FFTW_MPI_LIBRARIES ${FFTW_MPI_LIBRARY})
  SET(FFTW_MPI_FOUND "YES")
ELSE()
  SET(FFTW_MPI_FOUND "NO")
ENDIF ()

IF (FFTW_THREADS_FOUND)
   IF (NOT FFTW_THREADS_FIND_QUIETLY)
      MESSAGE(STATUS "Found FFTW threads: ${FFTW_THREADS_LIBRARIES}")
   ENDIF (NOT FFTW_THREADS_FIND_QUIETLY)
ELSE (FFTW_THREADS_FOUND)
   IF (FFTW_THREADS_FIND_REQUIRED)
      MESSAGE(FATAL_ERROR "Could not find FFTW threads library")
   ENDIF (FFTW_THREADS_FIND_REQUIRED)
ENDIF (FFTW_THREADS_FOUND)

IF (FFTW_MPI_FOUND)
   IF (NOT FFTW_MPI_FIND_QUIETLY)
     MESSAGE(STATUS "Found FFTW MPI: ${FFTW_MPI_LIBRARIES}")
   ENDIF()
ELSE()
  IF (FFTW_MPI_FIND_REQUIRED)
    MESSAGE(FATAL_ERROR "Could not find FFTW MPI library")
  ENDIF()
ENDIF ()



IF (FFTW_OMP_LIBRARY AND FFTW_INCLUDE_DIR)
    SET(FFTW_OMP_LIBRARIES ${FFTW_OMP_LIBRARY})
    SET(FFTW_OMP_FOUND "YES")
ELSE (FFTW_OMP_LIBRARY AND FFTW_INCLUDE_DIR)
  SET(FFTW_OMP_FOUND "NO")
ENDIF (FFTW_OMP_LIBRARY AND FFTW_INCLUDE_DIR)


IF (FFTW_OMP_FOUND)
   IF (NOT FFTW_OMP_FIND_QUIETLY)
      MESSAGE(STATUS "Found FFTW threads: ${FFTW_OMP_LIBRARIES}")
   ENDIF (NOT FFTW_OMP_FIND_QUIETLY)
ELSE (FFTW_OMP_FOUND)
   IF (FFTW_OMP_FIND_REQUIRED)
      MESSAGE(FATAL_ERROR "Could not find FFTW OpenMP library")
   ENDIF (FFTW_OMP_FIND_REQUIRED)
ENDIF (FFTW_OMP_FOUND)


IF (FFTW_LIBRARY AND FFTW_INCLUDE_DIR)
    SET(FFTW_LIBRARIES ${FFTW_LIBRARY})
    SET(FFTW_FOUND "YES")
ELSE (FFTW_LIBRARY AND FFTW_INCLUDE_DIR)
  SET(FFTW_FOUND "NO")
ENDIF (FFTW_LIBRARY AND FFTW_INCLUDE_DIR)


IF (FFTW_FOUND)
   IF (NOT FFTW_FIND_QUIETLY)
      MESSAGE(STATUS "Found FFTW: ${FFTW_LIBRARIES}")
   ENDIF (NOT FFTW_FIND_QUIETLY)
ELSE (FFTW_FOUND)
   IF (FFTW_FIND_REQUIRED)
      MESSAGE(FATAL_ERROR "Could not find FFTW library")
   ENDIF (FFTW_FIND_REQUIRED)
ENDIF (FFTW_FOUND)

SET (ON_UNIX ${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR
             ${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
IF (${ON_UNIX})
   SET (FFTW_EXECUTABLE_LIBRARIES fftw3 fftw3_threads)
ENDIF (${ON_UNIX})
