File: FindFFTW.cmake

package info (click to toggle)
ecbuild 3.7.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,564 kB
  • sloc: sh: 908; perl: 734; cpp: 454; f90: 430; python: 383; ansic: 297; fortran: 43; makefile: 15
file content (286 lines) | stat: -rw-r--r-- 9,489 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# (C) Copyright 2011- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation nor
# does it submit to any jurisdiction.

##############################################################################
#.rst:
#
# FindFFTW
# ========
#
# Find the FFTW library. ::
#
#   find_package(FFTW [REQUIRED] [QUIET]
#                [COMPONENTS [single] [double] [long_double] [quad]])
#
# By default, search for the double precision library ``fftw3``
#
# Search procedure
# ----------------
#
# 1) FFTW_LIBRARIES and FFTW_INCLUDE_DIRS set by user
#    --> Nothing is searched and these variables are used instead
#
# 2) Find MKL implementation via FFTW_ENABLE_MKL
#    --> If FFTW_ENABLE_MKL is explicitely set to ON, only MKL is considered
#        If FFTW_ENABLE_MKL is explictely set to OFF, MKL will not be considered
#        If FFTW_ENABLE_MKL is undefined, MKL is preferred unless ENABLE_MKL is explicitely set to OFF
#    --> MKLROOT environment variable helps to detect MKL (See FindMKL.cmake)
#
# 3) Find official FFTW impelementation
#    --> FFTW_ROOT variable / environment variable helps to detect FFTW
#
# Components
# ----------
#
# If a different version or multiple versions of the library are required,
# these need to be specified as ``COMPONENTS``. Note that double must be given
# explicitly if any ``COMPONENTS`` are specified.
#
# The libraries corresponding to each of the ``COMPONENTS`` are:
#
# :single:      ``FFTW::fftw3f``
# :double:      ``FFTW::fftw3``
# :long_double: ``FFTW::fftw3l``
# :quad:        ``FFTW::fftw3q``
#
# Output variables
# ----------------
#
# The following CMake variables are set on completion:
#
# :FFTW_FOUND:            true if FFTW is found on the system
# :FFTW_LIBRARIES:        full paths to requested FFTW libraries
# :FFTW_INCLUDE_DIRS:     FFTW include directory
#
# Input variables
# ---------------
#
# The following CMake variables are checked by the function:
#
# :FFTW_USE_STATIC_LIBS:  if true, only static libraries are found
# :FFTW_ROOT:             if set, this path is exclusively searched
# :FFTW_DIR:              equivalent to FFTW_ROOT (deprecated)
# :FFTW_PATH:             equivalent to FFTW_ROOT (deprecated)
# :FFTW_LIBRARIES:        User overriden FFTW libraries
# :FFTW_INCLUDE_DIRS:     User overriden FFTW includes directories
# :FFTW_ENABLE_MKL:       User requests use of MKL implementation
#
##############################################################################

list( APPEND _possible_components double single long_double quad )

if( NOT FFTW_FIND_COMPONENTS )
  set( FFTW_FIND_COMPONENTS double )
endif()

set( FFTW_double_LIBRARY_NAME fftw3 )
set( FFTW_single_LIBRARY_NAME fftw3f )
set( FFTW_long_double_LIBRARY_NAME fftw3l )
set( FFTW_quad_LIBRARY_NAME fftw3q )

macro( FFTW_CHECK_ALL_COMPONENTS )
    set( FFTW_FOUND_ALL_COMPONENTS TRUE )
    foreach( _component ${FFTW_FIND_COMPONENTS} )
        if( NOT FFTW_${_component}_FOUND )
            set( FFTW_FOUND_ALL_COMPONENTS false )
        endif()
    endforeach()
endmacro()

# Command line override
foreach( _component ${FFTW_FIND_COMPONENTS} )
    if( NOT FFTW_${_component}_LIBRARIES AND FFTW_LIBRARIES )
        set( FFTW_${_component}_LIBRARIES ${FFTW_LIBRARIES} )
    endif()
    if( FFTW_${_component}_LIBRARIES )
        set( FFTW_${_component}_FOUND TRUE )
    endif()
endforeach()

### Check MKL
FFTW_CHECK_ALL_COMPONENTS()
if( NOT FFTW_FOUND_ALL_COMPONENTS )

    if( NOT DEFINED FFTW_ENABLE_MKL AND NOT DEFINED ENABLE_MKL )
        set( FFTW_ENABLE_MKL ON )
        set( FFTW_FindMKL_OPTIONS QUIET )
    elseif( FFTW_ENABLE_MKL )
        set( FFTW_MKL_REQUIRED TRUE )
    elseif( ENABLE_MKL AND NOT DEFINED FFTW_ENABLE_MKL )
        set( FFTW_ENABLE_MKL ON )
    endif()

    if( FFTW_ENABLE_MKL )
        if( NOT MKL_FOUND )
            find_package( MKL ${FFTW_FindMKL_OPTIONS} )
        endif()
        if( MKL_FOUND )
            if( NOT FFTW_INCLUDE_DIRS )
                set( FFTW_INCLUDE_DIRS ${MKL_INCLUDE_DIRS}/fftw )
            endif()
            if( NOT FFTW_LIBRARIES )
                set( FFTW_LIBRARIES ${MKL_LIBRARIES} )
            endif()

            foreach( _component ${FFTW_FIND_COMPONENTS} )
                set( FFTW_${_component}_FOUND TRUE )
                set( FFTW_${_component}_LIBRARIES ${MKL_LIBRARIES} )
            endforeach()
        else()
            if( FFTW_MKL_REQUIRED )
                if( FFTW_FIND_REQUIRED )
                    message(CRITICAL "FindFFTW: MKL required, but MKL was not found" )
                else()
                    if( NOT FFTW_MKL_FIND_QUIETLY )
                        message(STATUS "FindFFTW: MKL required, but MKL was not found" )
                    endif()
                    set( FFTW_FOUND FALSE )
                    return()
                endif()
            endif()
        endif()
    endif()
endif()

### Standard FFTW
if( (NOT FFTW_ROOT) AND EXISTS $ENV{FFTW_ROOT} )
    set( FFTW_ROOT $ENV{FFTW_ROOT} )
endif()
if( (NOT FFTW_ROOT) AND FFTW_DIR )
    set( FFTW_ROOT ${FFTW_DIR} )
endif()
if( (NOT FFTW_ROOT) AND EXISTS $ENV{FFTW_DIR} )
    set( FFTW_ROOT $ENV{FFTW_DIR} )
endif()
if( (NOT FFTW_ROOT) AND FFTWDIR )
    set( FFTW_ROOT ${FFTWDIR} )
endif()
if( (NOT FFTW_ROOT) AND EXISTS $ENV{FFTWDIR} )
    set( FFTW_ROOT $ENV{FFTWDIR} )
endif()
if( (NOT FFTW_ROOT) AND FFTW_PATH )
    set( FFTW_ROOT ${FFTW_PATH} )
endif()
if( (NOT FFTW_ROOT) AND EXISTS $ENV{FFTW_PATH})
    set( FFTW_ROOT $ENV{FFTW_PATH} )
endif()

if( FFTW_ROOT ) # On cc[a|b|t] FFTW_DIR is set to the lib directory :(
    get_filename_component(_dirname ${FFTW_ROOT} NAME)
    if( _dirname MATCHES "lib" )
        set( FFTW_ROOT "${FFTW_ROOT}/.." )
    endif()
endif()

if( NOT FFTW_ROOT )
    # Check if we can use PkgConfig
    find_package(PkgConfig)

    #Determine from PKG
    if( PKG_CONFIG_FOUND AND NOT FFTW_ROOT )
        pkg_check_modules( PKG_FFTW QUIET "fftw3" )
    endif()
endif()

#Check whether to search static or dynamic libs
set( CMAKE_FIND_LIBRARY_SUFFIXES_SAV ${CMAKE_FIND_LIBRARY_SUFFIXES} )

if( ${FFTW_USE_STATIC_LIBS} )
    set( CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX} )
else()
    set( CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_SHARED_LIBRARY_SUFFIX} )
endif()


if( FFTW_ROOT )
    set( _default_paths NO_DEFAULT_PATH )
    set( _lib_paths ${FFTW_ROOT} )
    set( _include_paths ${FFTW_ROOT} )
else()
    set( _lib_paths ${PKG_FFTW_LIBRARY_DIRS} ${LIB_INSTALL_DIR} )
    set( _include_paths ${PKG_FFTW_INCLUDE_DIRS} ${INCLUDE_INSTALL_DIR} )
endif()

# find includes
if( NOT FFTW_INCLUDE_DIRS )
    find_path(
        FFTW_INCLUDE_DIR
        NAMES "fftw3.h"
        PATHS ${_include_paths}
        PATH_SUFFIXES "include"
        ${_default_paths}
        )
    if( NOT FFTW_INCLUDE_DIR )
        if( NOT FFTW_FIND_QUIETLY OR FFTW_FIND_REQUIRED )
            message( STATUS "FindFFTW: fftw include headers not found")
        endif()
    endif()

    set( FFTW_INCLUDE_DIRS ${FFTW_INCLUDE_DIR} )
endif()

# find libs
foreach( _component ${FFTW_FIND_COMPONENTS} )
    if( NOT FFTW_${_component}_LIBRARIES )
        find_library(
            FFTW_${_component}_LIB
            NAMES ${FFTW_${_component}_LIBRARY_NAME}
            PATHS ${_lib_paths}
            PATH_SUFFIXES "lib" "lib64"
            ${_default_paths}
            )
        set( FFTW_${_component}_LIBRARIES ${FFTW_${_component}_LIB} )
        if( FFTW_${_component}_LIBRARIES )
            set( FFTW_${_component}_FOUND TRUE )
        else()
            if( NOT FFTW_FIND_QUIETLY OR FFTW_FIND_REQUIRED )
                message(STATUS "FindFFTW: ${_component} precision required, but ${FFTW_${_component}_LIBRARY_NAME} was not found")
            endif()
            set( FFTW_${_component}_FOUND FALSE )
        endif()
    endif()
endforeach()

# Assemble FFTW_LIBRARIES
if( NOT FFTW_LIBRARIES )
    foreach( _component ${FFTW_FIND_COMPONENTS} )
        list( APPEND FFTW_LIBRARIES ${FFTW_${_component}_LIBRARIES} )
    endforeach()
    list( REMOVE_DUPLICATES FFTW_LIBRARIES )
endif()

# FFTW CREATE_INTERFACE_TARGETS
foreach( _component ${FFTW_FIND_COMPONENTS} )
    set( _target FFTW::${FFTW_${_component}_LIBRARY_NAME} )

    if( FFTW_${_component}_FOUND AND NOT TARGET ${_target} )
        add_library( ${_target} INTERFACE IMPORTED )
        target_link_libraries( ${_target} INTERFACE ${FFTW_${_component}_LIBRARIES} )
        target_include_directories( ${_target} INTERFACE ${FFTW_INCLUDE_DIRS} )
    endif()
endforeach()

if( NOT FFTW_FIND_QUIETLY AND FFTW_LIBRARIES )
  message( STATUS "FFTW targets:" )
  foreach( _component ${FFTW_FIND_COMPONENTS} )
    set( _target FFTW::${FFTW_${_component}_LIBRARY_NAME} )
    message( STATUS "    ${_target} (${_component} precision)  [${FFTW_${_component}_LIBRARIES}]")
  endforeach()
endif()


set( CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_SAV} )

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args( FFTW
                                   REQUIRED_VARS FFTW_INCLUDE_DIRS FFTW_LIBRARIES
                                   HANDLE_COMPONENTS )

set( FFTW_INCLUDES ${FFTW_INCLUDE_DIRS} ) # deprecated
set( FFTW_LIB ${FFTW_double_LIBRARIES} ) # deprecated
mark_as_advanced(FFTW_INCLUDE_DIRS FFTW_LIBRARIES FFTW_LIB)