Skip to content

Commit d449ea0

Browse files
authored
Merge pull request egpbos#10 from zhao-shihan/master
Add VERSION_VAR to find_package_handle_standard_args to handle FFTW version
2 parents 32b86f1 + 0229b2f commit d449ea0

4 files changed

Lines changed: 79 additions & 6 deletions

File tree

.github/workflows/test.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ jobs:
2424
cmake -S test -B build \
2525
-DFFTW_TEST_COMPONENTS="${fftw_components}"
2626
27+
- name: Configure test with version 3
28+
run: |
29+
cmake -S test -B build \
30+
-DFFTW_TEST_COMPONENTS="${fftw_components}" \
31+
-DFFTW_TEST_VERSION=3
32+
33+
- name: Configure test with invalid version
34+
run: |
35+
set +e
36+
cmake -S test -B build_version_fail -DFFTW_TEST_VERSION=999.99.9 2>/dev/null
37+
STATUS=$?
38+
if [ $STATUS -ne 0 ]; then
39+
echo "PASS: find_package correctly rejected version 999.99.9"
40+
else
41+
echo "FAIL: find_package should have rejected version 999.99.9 but succeeded"
42+
exit 1
43+
fi
44+
2745
test-source:
2846
name: Test source install
2947
runs-on: ubuntu-latest
@@ -79,6 +97,13 @@ jobs:
7997
-DFFTW_ROOT="$HOME/fftw_install" \
8098
-DFFTW_TEST_COMPONENTS="${fftw_components}"
8199
100+
- name: Configure test with version 3
101+
run: |
102+
cmake -S test -B build \
103+
-DFFTW_ROOT="$HOME/fftw_install" \
104+
-DFFTW_TEST_COMPONENTS="${fftw_components}" \
105+
-DFFTW_TEST_VERSION=3
106+
82107
test-conda:
83108
name: Test conda-forge install
84109
runs-on: ubuntu-latest
@@ -99,3 +124,11 @@ jobs:
99124
cmake -S test -B build \
100125
-DFFTW_ROOT="$CONDA_PREFIX" \
101126
-DFFTW_TEST_COMPONENTS="${fftw_components}"
127+
128+
- name: Configure test with version 3
129+
shell: micromamba-shell {0}
130+
run: |
131+
cmake -S test -B build \
132+
-DFFTW_ROOT="$CONDA_PREFIX" \
133+
-DFFTW_TEST_COMPONENTS="${fftw_components}" \
134+
-DFFTW_TEST_VERSION=3

FindFFTW.cmake

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
# Copyright (c) 2017, Patrick Bos
88
#
99
# Usage:
10-
# find_package(FFTW [REQUIRED] [QUIET] [COMPONENTS component1 ... componentX] )
10+
# find_package(FFTW [<version>] [REQUIRED] [QUIET] [COMPONENTS component1 ... componentX] )
1111
#
1212
# It sets the following variables:
1313
# FFTW_FOUND ... true if fftw is found on the system
1414
# FFTW_[component]_LIB_FOUND ... true if the component is found on the system (see components below)
1515
# FFTW_LIBRARIES ... full paths to all found fftw libraries
1616
# FFTW_[component]_LIB ... full path to one of the components (see below)
1717
# FFTW_INCLUDE_DIRS ... fftw include directory paths
18+
# FFTW_VERSION ... version of the found fftw library
1819
#
1920
# The following variables will be checked by the function
2021
# FFTW_USE_STATIC_LIBS ... if true, only static libraries are found, otherwise both static and shared.
@@ -44,9 +45,31 @@ endif()
4445
# Check if we can use PkgConfig
4546
find_package(PkgConfig)
4647

47-
#Determine from PKG
48+
# Determine from PKG
4849
if( PKG_CONFIG_FOUND AND NOT FFTW_ROOT )
4950
pkg_check_modules( PKG_FFTW QUIET "fftw3" )
51+
set( FFTW_VERSION ${PKG_FFTW_VERSION} )
52+
else()
53+
# If pkg-config was skipped, there seems no way to get the version directly.
54+
# Try to deduce the version from fftw-wisdom-to-conf instead.
55+
# (From @kprussing. See https://github.com/egpbos/findFFTW/pull/8)
56+
execute_process(COMMAND ${FFTW_ROOT}/bin/fftw-wisdom-to-conf -V
57+
RESULT_VARIABLE _fftw_wtc_success
58+
OUTPUT_VARIABLE _fftw_wtc_stdout
59+
ERROR_VARIABLE _fftw_wtc_stderr)
60+
if (_fftw_wtc_success EQUAL 0)
61+
string(REGEX MATCH "FFTW *version *([0-9]+([.][0-9]+([.][0-9]+)?)?)"
62+
_fftw_wtc_version ${_fftw_wtc_stdout})
63+
string(REPLACE " " ";" _fftw_wtc_version_list ${_fftw_wtc_version})
64+
list(GET _fftw_wtc_version_list -1 FFTW_VERSION)
65+
else()
66+
message(WARNING "Error running ${FFTW_ROOT}/bin/fftw-wisdom-to-conf. "
67+
"Could not determine FFTW version.
68+
Output:
69+
${_fftw_wtc_stdout}
70+
Error:
71+
${_fftw_wtc_stderr}")
72+
endif()
5073
endif()
5174

5275
#Check whether to search static or dynamic libs
@@ -398,6 +421,7 @@ include(FindPackageHandleStandardArgs)
398421

399422
find_package_handle_standard_args(FFTW
400423
REQUIRED_VARS FFTW_INCLUDE_DIRS
424+
VERSION_VAR FFTW_VERSION
401425
HANDLE_COMPONENTS
402426
)
403427

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CMake module for finding FFTW 3 using find_package
55
Once added to your project, this module allows you to find FFTW libraries and headers using the CMake `find_package` command:
66

77
```cmake
8-
find_package(FFTW [REQUIRED] [QUIET] [COMPONENTS component1 ... componentX] )
8+
find_package(FFTW [<version>] [REQUIRED] [QUIET] [COMPONENTS component1 ... componentX] )
99
```
1010

1111
This module sets the following variables:
@@ -14,6 +14,7 @@ This module sets the following variables:
1414
- `FFTW_LIBRARIES` ... full paths to all found fftw libraries
1515
- `FFTW_[component]_LIB` ... full path to one of the components (see below)
1616
- `FFTW_INCLUDE_DIRS` ... fftw include directory paths
17+
- `FFTW_VERSION` ... version of the found fftw library
1718

1819
The following variables will be checked by the module:
1920
- `FFTW_USE_STATIC_LIBS` ... if true, only static libraries are found, otherwise both static and shared.

test/CMakeLists.txt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/..")
66

77
# Find FFTW. If FFTW_TEST_COMPONENTS is given (semicolon-separated list), those
88
# components are required; otherwise we just require that FFTW itself is found.
9-
if(DEFINED FFTW_TEST_COMPONENTS)
10-
find_package(FFTW REQUIRED COMPONENTS ${FFTW_TEST_COMPONENTS})
9+
if(FFTW_TEST_COMPONENTS)
10+
find_package(FFTW ${FFTW_TEST_VERSION} REQUIRED COMPONENTS ${FFTW_TEST_COMPONENTS})
1111
else()
12-
find_package(FFTW REQUIRED)
12+
find_package(FFTW ${FFTW_TEST_VERSION} REQUIRED)
1313
endif()
1414

1515
# ---------------------------------------------------------------------------
@@ -27,6 +27,20 @@ if(NOT FFTW_LIBRARIES)
2727
message(FATAL_ERROR "FFTW_LIBRARIES is empty after find_package(FFTW REQUIRED)")
2828
endif()
2929

30+
if(NOT FFTW_VERSION)
31+
message(FATAL_ERROR "FFTW_VERSION is empty after find_package(FFTW REQUIRED)")
32+
endif()
33+
34+
# ---------------------------------------------------------------------------
35+
# Version checks
36+
# ---------------------------------------------------------------------------
37+
if(FFTW_TEST_VERSION)
38+
if(NOT "${FFTW_VERSION}" VERSION_GREATER_EQUAL "${FFTW_TEST_VERSION}")
39+
message(FATAL_ERROR
40+
"Expected FFTW_VERSION >= ${FFTW_TEST_VERSION} but got ${FFTW_VERSION}")
41+
endif()
42+
endif()
43+
3044
# ---------------------------------------------------------------------------
3145
# Map component names to their CMake imported target names
3246
# ---------------------------------------------------------------------------
@@ -71,6 +85,7 @@ endforeach()
7185
# ---------------------------------------------------------------------------
7286
message(STATUS "FindFFTW test passed!")
7387
message(STATUS " FFTW_FOUND: ${FFTW_FOUND}")
88+
message(STATUS " FFTW_VERSION: ${FFTW_VERSION}")
7489
message(STATUS " FFTW_INCLUDE_DIRS: ${FFTW_INCLUDE_DIRS}")
7590
message(STATUS " FFTW_LIBRARIES: ${FFTW_LIBRARIES}")
7691

0 commit comments

Comments
 (0)