Skip to content

Commit c92e335

Browse files
authored
Merge pull request #428 from GEOS-ESM/feature/mathomp4/v4-fix-f2py-detect
v4: Fixes to allow for odd Python setups and f2py
2 parents 73cbc95 + a45e845 commit c92e335

File tree

8 files changed

+135
-24
lines changed

8 files changed

+135
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717

1818
### Changed
1919

20+
- Change f2py detection for the odd case where there might be multiple Python installations. For now, if the path to the Python executable does not match the path to the f2py executable, we issue a `WARNING`. We use a `WARNING` since some installations (e.g., Spack) will have the Python executable in a different location than the f2py executable.
21+
- Removed warning that Baselibs is not supported, to a STATUS message.
22+
2023
### Deprecated
2124

2225
## [4.12.0] - 2025-02-11

external_libraries/FindBaselibs.cmake

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,7 @@ if (BASEDIR)
5959
endif ()
6060
set (BASEDIR "${BASEDIR}" CACHE PATH "Path to installed baselibs" FORCE)
6161
else ()
62-
ecbuild_warn(
63-
"BASEDIR not specified.\n"
64-
"If you wish to use Baselibs, please use:\n"
65-
" cmake ... -DBASEDIR=<path-to-Baselibs>\n"
66-
"or set BASEDIR in your environment.\n\n"
67-
"Note that building GEOS-ESM code without Baselibs is unsupported.")
62+
message(STATUS "BASEDIR not set. Baselibs not found. Assume we are using Spack or other methods to provide dependencies")
6863
endif ()
6964

7065
if (ESMA_SDF)

python/esma_python.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ cmake_minimum_required(VERSION 3.24)
66

77
# Find Python
88
set(Python_FIND_STRATEGY LOCATION)
9+
set(Python_FIND_UNVERSIONED_NAMES FIRST)
10+
set(Python_FIND_FRAMEWORK LAST)
911
find_package(Python COMPONENTS Interpreter)
1012

1113
# Find Python2
1214
set(Python2_FIND_STRATEGY LOCATION)
15+
set(Python2_FIND_UNVERSIONED_NAMES FIRST)
16+
set(Python2_FIND_FRAMEWORK LAST)
1317
find_package(Python2 COMPONENTS Interpreter)
1418
list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/f2py2")
1519
include (esma_find_python2_module)
@@ -18,6 +22,8 @@ include (esma_add_f2py2_module)
1822

1923
# Find Python3
2024
set(Python3_FIND_STRATEGY LOCATION)
25+
set(Python3_FIND_UNVERSIONED_NAMES FIRST)
26+
set(Python3_FIND_FRAMEWORK LAST)
2127
find_package(Python3 COMPONENTS Interpreter)
2228
list (APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/f2py3")
2329
include (esma_find_python3_module)

python/f2py/FindF2PY.cmake

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,38 @@
3131
#
3232

3333
# Path to the f2py executable
34-
find_program(F2PY_EXECUTABLE NAMES "f2py${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}"
35-
"f2py-${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}"
36-
"f2py${Python_VERSION_MAJOR}"
37-
"f2py"
38-
)
34+
35+
## We might have an odd circumstance where there are a couple f2py around. As such,
36+
## we need to find the one that matches the Python_EXECUTABLE. This is a bit of a
37+
## hack, but it should work for most cases.
38+
39+
## Find the directory where the Python_EXECUTABLE is located
40+
message(DEBUG "[F2PY]: Searching for f2py executable associated with Python_EXECUTABLE: ${Python_EXECUTABLE}")
41+
get_filename_component(PYTHON_EXECUTABLE_DIR ${Python_EXECUTABLE} DIRECTORY)
42+
message(DEBUG "[F2PY]: Python executable directory: ${PYTHON_EXECUTABLE_DIR}")
43+
44+
find_program(F2PY_EXECUTABLE
45+
NAMES "f2py"
46+
"f2py${Python_VERSION_MAJOR}"
47+
"f2py${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}"
48+
"f2py-${Python_VERSION_MAJOR}.${Python_VERSION_MINOR}"
49+
PATHS ${PYTHON_EXECUTABLE_DIR}
50+
)
51+
52+
message(DEBUG "[F2PY]: Found f2py executable: ${F2PY_EXECUTABLE}")
53+
54+
# Now as a sanity check, we need to make sure that the f2py executable is
55+
# actually the one that is associated with the Python_EXECUTABLE
56+
get_filename_component(F2PY_EXECUTABLE_DIR ${F2PY_EXECUTABLE} DIRECTORY)
57+
message(DEBUG "[F2PY]: f2py executable directory: ${F2PY_EXECUTABLE_DIR}")
58+
59+
# Now we issue a WARNING. We can't do more than that because of things like Spack
60+
# where f2py will be in a different location than python.
61+
if (NOT "${F2PY_EXECUTABLE_DIR}" STREQUAL "${PYTHON_EXECUTABLE_DIR}")
62+
message(WARNING
63+
"[F2PY]: The f2py executable [${F2PY_EXECUTABLE}] found is not the one associated with the Python_EXECUTABLE [${Python_EXECUTABLE}].\n"
64+
"Please check your Python environment if this is not expected (for example, not a Spack install) or build with -DUSE_F2PY=OFF.")
65+
endif ()
3966

4067
if(F2PY_EXECUTABLE)
4168
# extract the version string

python/f2py/try_f2py_compile.cmake

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,22 @@ macro (try_f2py_compile file var)
1919
else ()
2020
set(MESON_F2PY_FCOMPILER "${CMAKE_Fortran_COMPILER}")
2121
endif ()
22+
message(DEBUG "MESON_F2PY_FCOMPILER is set to ${MESON_F2PY_FCOMPILER}")
23+
message(DEBUG "F2PY_COMPILER is set to ${F2PY_COMPILER}")
24+
# hack for Macs. If the C compiler is clang and we are on Apple,
25+
# we need to set the CC environment to /usr/bin/clang
26+
if(APPLE AND CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
27+
set(MESON_CCOMPILER "/usr/bin/clang")
28+
message(STATUS "Detected AppleClang on macOS. Using ugly hack for meson by passing in CC=/usr/bin/clang")
29+
else()
30+
set(MESON_CCOMPILER "${CMAKE_C_COMPILER}")
31+
endif()
32+
message(DEBUG "MESON_CCOMPILER is set to ${MESON_CCOMPILER}")
33+
list(APPEND ENV_LIST FC=${MESON_F2PY_FCOMPILER})
34+
list(APPEND ENV_LIST CC=${MESON_CCOMPILER})
35+
message(DEBUG "ENV_LIST is set to ${ENV_LIST}")
2236
execute_process(
23-
COMMAND cmake -E env "FC=${MESON_F2PY_COMPILER}" ${F2PY_EXECUTABLE} -m test_ -c ${file} --fcompiler=${F2PY_FCOMPILER}
37+
COMMAND cmake -E env ${ENV_LIST} ${F2PY_EXECUTABLE} -m test_ -c ${file} --fcompiler=${F2PY_FCOMPILER}
2438
WORKING_DIRECTORY ${_f2py_check_bindir}
2539
RESULT_VARIABLE result
2640
OUTPUT_QUIET

python/f2py2/FindF2PY2.cmake

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,37 @@ if(NOT Python2_FOUND)
4545
return()
4646
endif()
4747

48-
# Path to the f2py2 executable
49-
find_program(F2PY2_EXECUTABLE NAMES "f2py${Python2_VERSION_MAJOR}.${Python2_VERSION_MINOR}"
50-
"f2py-${Python2_VERSION_MAJOR}.${Python2_VERSION_MINOR}"
51-
"f2py${Python2_VERSION_MAJOR}"
52-
"f2py"
53-
)
48+
## We might have an odd circumstance where there are a couple f2py around. As such,
49+
## we need to find the one that matches the Python2_EXECUTABLE. This is a bit of a
50+
## hack, but it should work for most cases.
51+
52+
## Find the directory where the Python2_EXECUTABLE is located
53+
message(DEBUG "[F2PY2]: Searching for f2py2 executable associated with Python2_EXECUTABLE: ${Python2_EXECUTABLE}")
54+
get_filename_component(PYTHON2_EXECUTABLE_DIR ${Python2_EXECUTABLE} DIRECTORY)
55+
message(DEBUG "[F2PY2]: Python2 executable directory: ${PYTHON2_EXECUTABLE_DIR}")
56+
57+
find_program(F2PY2_EXECUTABLE
58+
NAMES "f2py"
59+
"f2py${Python2_VERSION_MAJOR}"
60+
"f2py${Python2_VERSION_MAJOR}.${Python2_VERSION_MINOR}"
61+
"f2py-${Python2_VERSION_MAJOR}.${Python2_VERSION_MINOR}"
62+
PATHS ${PYTHON2_EXECUTABLE_DIR}
63+
)
64+
65+
message(DEBUG "[F2PY2]: Found f2py2 executable: ${F2PY2_EXECUTABLE}")
66+
67+
# Now as a sanity check, we need to make sure that the f2py2 executable is
68+
# actually the one that is associated with the Python2_EXECUTABLE
69+
get_filename_component(F2PY2_EXECUTABLE_DIR ${F2PY2_EXECUTABLE} DIRECTORY)
70+
message(DEBUG "[F2PY2]: f2py2 executable directory: ${F2PY2_EXECUTABLE_DIR}")
71+
72+
# Now we issue a WARNING. We can't do more than that because of things like Spack
73+
# where f2py will be in a different location than python.
74+
if (NOT "${F2PY2_EXECUTABLE_DIR}" STREQUAL "${PYTHON2_EXECUTABLE_DIR}")
75+
message(WARNING
76+
"[F2PY2]: The f2py2 executable [${F2PY2_EXECUTABLE}] found is not the one associated with the Python2_EXECUTABLE [${Python2_EXECUTABLE}].\n"
77+
"Please check your Python2 environment if this is not expected (for example, not a Spack install) or build with -DUSE_F2PY=OFF.")
78+
endif ()
5479

5580
if(F2PY2_EXECUTABLE)
5681
# extract the version string

python/f2py3/FindF2PY3.cmake

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,38 @@
3131
#
3232

3333
# Path to the f2py3 executable
34-
find_program(F2PY3_EXECUTABLE NAMES "f2py${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}"
35-
"f2py-${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}"
36-
"f2py${Python3_VERSION_MAJOR}"
37-
"f2py"
38-
)
34+
35+
## We might have an odd circumstance where there are a couple f2py around. As such,
36+
## we need to find the one that matches the Python3_EXECUTABLE. This is a bit of a
37+
## hack, but it should work for most cases.
38+
39+
## Find the directory where the Python3_EXECUTABLE is located
40+
message(DEBUG "[F2PY3]: Searching for f2py3 executable associated with Python3_EXECUTABLE: ${Python3_EXECUTABLE}")
41+
get_filename_component(PYTHON3_EXECUTABLE_DIR ${Python3_EXECUTABLE} DIRECTORY)
42+
message(DEBUG "[F2PY3]: Python3 executable directory: ${PYTHON3_EXECUTABLE_DIR}")
43+
44+
find_program(F2PY3_EXECUTABLE
45+
NAMES "f2py"
46+
"f2py${Python3_VERSION_MAJOR}"
47+
"f2py${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}"
48+
"f2py-${Python3_VERSION_MAJOR}.${Python3_VERSION_MINOR}"
49+
PATHS ${PYTHON3_EXECUTABLE_DIR}
50+
)
51+
52+
message(DEBUG "[F2PY3]: Found f2py3 executable: ${F2PY3_EXECUTABLE}")
53+
54+
# Now as a sanity check, we need to make sure that the f2py3 executable is
55+
# actually the one that is associated with the Python3_EXECUTABLE
56+
get_filename_component(F2PY3_EXECUTABLE_DIR ${F2PY3_EXECUTABLE} DIRECTORY)
57+
message(DEBUG "[F2PY3]: f2py3 executable directory: ${F2PY3_EXECUTABLE_DIR}")
58+
59+
# Now we issue a WARNING. We can't do more than that because of things like Spack
60+
# where f2py will be in a different location than python.
61+
if (NOT "${F2PY3_EXECUTABLE_DIR}" STREQUAL "${PYTHON3_EXECUTABLE_DIR}")
62+
message(WARNING
63+
"[F2PY3]: The f2py3 executable [${F2PY3_EXECUTABLE}] found is not the one associated with the Python3_EXECUTABLE [${Python3_EXECUTABLE}].\n"
64+
"Please check your Python3 environment if this is not expected (for example, not a Spack install) or build with -DUSE_F2PY=OFF.")
65+
endif ()
3966

4067
if(F2PY3_EXECUTABLE)
4168
# extract the version string

python/f2py3/try_f2py3_compile.cmake

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,22 @@ macro (try_f2py3_compile file var)
1919
else ()
2020
set(MESON_F2PY3_FCOMPILER "${CMAKE_Fortran_COMPILER}")
2121
endif ()
22+
message(DEBUG "MESON_F2PY3_FCOMPILER is set to ${MESON_F2PY3_FCOMPILER}")
23+
message(DEBUG "F2PY3_COMPILER is set to ${F2PY3_COMPILER}")
24+
# hack for Macs. If the C compiler is clang and we are on Apple,
25+
# we need to set the CC environment to /usr/bin/clang
26+
if(APPLE AND CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
27+
set(MESON_CCOMPILER "/usr/bin/clang")
28+
message(STATUS "Detected AppleClang on macOS. Using ugly hack for meson by passing in CC=/usr/bin/clang")
29+
else()
30+
set(MESON_CCOMPILER "${CMAKE_C_COMPILER}")
31+
endif()
32+
message(DEBUG "MESON_CCOMPILER is set to ${MESON_CCOMPILER}")
33+
list(APPEND ENV_LIST FC=${MESON_F2PY3_FCOMPILER})
34+
list(APPEND ENV_LIST CC=${MESON_CCOMPILER})
35+
message(DEBUG "ENV_LIST is set to ${ENV_LIST}")
2236
execute_process(
23-
COMMAND cmake -E env "FC=${MESON_F2PY3_COMPILER}" ${F2PY3_EXECUTABLE} -m test_ -c ${file} --fcompiler=${F2PY3_FCOMPILER}
37+
COMMAND cmake -E env ${ENV_LIST} ${F2PY3_EXECUTABLE} -m test_ -c ${file} --fcompiler=${F2PY3_FCOMPILER}
2438
WORKING_DIRECTORY ${_f2py3_check_bindir}
2539
RESULT_VARIABLE result
2640
OUTPUT_QUIET

0 commit comments

Comments
 (0)