Skip to content

Commit d287680

Browse files
committed
cmake: Complete rewrite of FindFFTW3.cmake
1 parent 8d55d17 commit d287680

File tree

4 files changed

+103
-59
lines changed

4 files changed

+103
-59
lines changed

CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,13 @@ endif()
452452
#
453453

454454
if(ESPRESSO_BUILD_WITH_FFTW)
455-
find_package(FFTW3 REQUIRED)
455+
if(ESPRESSO_BUILD_WITH_SHARED_MEMORY_PARALLELISM)
456+
list(APPEND FFTW3_COMPONENTS omp)
457+
endif()
458+
if(ESPRESSO_BUILD_WITH_WALBERLA_FFT)
459+
list(APPEND FFTW3_COMPONENTS mpi)
460+
endif()
461+
find_package(fftw3 REQUIRED COMPONENTS ${FFTW3_COMPONENTS})
456462

457463
if(NOT EXISTS ${FETCHCONTENT_BASE_DIR}/heffte-src)
458464
find_package(Heffte 2.4.1 QUIET)

cmake/FindFFTW3.cmake

-57
This file was deleted.

cmake/Findfftw3.cmake

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#
2+
# Copyright (C) 2025 The ESPResSo project
3+
#
4+
# This file is part of ESPResSo.
5+
#
6+
# ESPResSo is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# ESPResSo is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
#
19+
20+
# Find the fftw3 libraries (with variants MPI, OpenMP, etc.) and header files.
21+
#
22+
# The following variables and imported targets are created:
23+
#
24+
# - `fftw3_INCLUDE_DIR` : folder containing `fftw3.h`
25+
# - `fftw3_MPI_INCLUDE_DIR` : folder containing `fftw3-mpi.h`
26+
# - `fftw3_FOUND` : true if `espresso::fftw3` exists and all required components were found
27+
# - `fftw3_mpi_FOUND` : true if `espresso::fftw3_mpi` exists
28+
# - `fftw3_omp_FOUND` : true if `espresso::fftw3_omp` exists
29+
# - `fftw3_threads_FOUND` : true if `espresso::fftw3_threads` exists
30+
31+
include(FindPackageHandleStandardArgs)
32+
33+
set(fftw3_PRECISIONS "" "f")
34+
set(fftw3_COMPONENTS "mpi" "omp" "threads")
35+
set(fftw3_MODULES ${fftw3_COMPONENTS})
36+
list(TRANSFORM fftw3_MODULES PREPEND "_")
37+
list(INSERT fftw3_MODULES 0 "")
38+
39+
find_path(fftw3_INCLUDE_DIR fftw3.h)
40+
find_path(fftw3_MPI_INCLUDE_DIR fftw3-mpi.h)
41+
42+
foreach(PRECISION IN LISTS fftw3_PRECISIONS)
43+
foreach(COMPONENT IN LISTS fftw3_COMPONENTS)
44+
find_library(fftw3${PRECISION}_${COMPONENT}_LIBRARIES
45+
fftw3${PRECISION}_${COMPONENT})
46+
mark_as_advanced(fftw3${PRECISION}_${COMPONENT}_LIBRARIES)
47+
endforeach()
48+
find_library(fftw3${PRECISION}_LIBRARIES fftw3${PRECISION})
49+
mark_as_advanced(fftw3${PRECISION}_LIBRARIES)
50+
endforeach()
51+
52+
# dependencies bookkeeping
53+
set(fftw3_LIBRARIES_REQUIRED fftw3_INCLUDE_DIR)
54+
foreach(PRECISION IN LISTS fftw3_PRECISIONS)
55+
list(APPEND fftw3_LIBRARIES_REQUIRED fftw3${PRECISION}_LIBRARIES)
56+
endforeach()
57+
58+
# find library components
59+
set(FPHSA_NAME_MISMATCHED 1)
60+
foreach(COMPONENT IN LISTS fftw3_COMPONENTS)
61+
if(${COMPONENT} IN_LIST fftw3_FIND_COMPONENTS)
62+
set(fftw3_COMPONENT_DEPS fftw3_INCLUDE_DIR)
63+
if(${COMPONENT} STREQUAL "mpi")
64+
list(APPEND fftw3_COMPONENT_DEPS fftw3_MPI_INCLUDE_DIR)
65+
endif()
66+
foreach(PRECISION IN LISTS fftw3_PRECISIONS)
67+
list(APPEND fftw3_COMPONENT_DEPS fftw3${PRECISION}_${COMPONENT}_LIBRARIES)
68+
endforeach()
69+
find_package_handle_standard_args(fftw3_${COMPONENT} DEFAULT_MSG ${fftw3_COMPONENT_DEPS})
70+
if(${fftw3_FIND_REQUIRED_${COMPONENT}})
71+
list(APPEND fftw3_LIBRARIES_REQUIRED ${fftw3_COMPONENT_DEPS})
72+
endif()
73+
endif()
74+
endforeach()
75+
list(REMOVE_DUPLICATES fftw3_LIBRARIES_REQUIRED)
76+
unset(FPHSA_NAME_MISMATCHED)
77+
78+
# find main library, but fail if required components are missing
79+
find_package_handle_standard_args(fftw3 DEFAULT_MSG ${fftw3_LIBRARIES_REQUIRED})
80+
81+
foreach(MODULE IN LISTS fftw3_MODULES)
82+
if(fftw3${MODULE}_FOUND AND NOT TARGET espresso::fftw3${MODULE})
83+
add_library(espresso::fftw3${MODULE} INTERFACE IMPORTED)
84+
target_include_directories(
85+
espresso::fftw3${MODULE}
86+
INTERFACE "${fftw3_INCLUDE_DIR}" "$<$<STREQUAL:${MODULE},_mpi>:${fftw3_MPI_INCLUDE_DIR}>")
87+
foreach(PRECISION IN LISTS fftw3_PRECISIONS)
88+
target_link_libraries(
89+
espresso::fftw3${MODULE}
90+
INTERFACE "${fftw3${PRECISION}${MODULE}_LIBRARIES}")
91+
endforeach()
92+
endif()
93+
endforeach()

src/core/fft/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#
1919

2020
if(ESPRESSO_BUILD_WITH_FFTW)
21-
target_link_libraries(espresso_p3m PRIVATE FFTW3::FFTW3)
21+
target_link_libraries(
22+
espresso_p3m PRIVATE $<$<BOOL:${fftw3_FOUND}>:espresso::fftw3>
23+
$<$<BOOL:${fftw3_omp_FOUND}>:espresso::fftw3_omp>)
2224
target_sources(espresso_p3m PRIVATE fft.cpp)
2325
endif()

0 commit comments

Comments
 (0)