Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic help with f2py-cmake: am I doing this right? #20

Open
mathomp4 opened this issue Dec 2, 2024 · 0 comments
Open

Basic help with f2py-cmake: am I doing this right? #20

mathomp4 opened this issue Dec 2, 2024 · 0 comments

Comments

@mathomp4
Copy link

mathomp4 commented Dec 2, 2024

I am fairly certain I'm asking perhaps the most basic questions, but, well, I'm stuck. Namely, I sort of maintain some...not-pretty "use f2py in CMake" code that's from back in the day when f2py+CMake was a bit of a black art.

However @rgommers brought up this project and then, independently, another person did as well. So, okay then, this might be useful.

So what I'm trying to do is translate some f2py code I have in a project I manage:

      esma_add_f2py3_module(ShaveMantissa_
        SOURCES ShaveMantissa_py.F90 ShaveMantissa.c
        DESTINATION lib/Python/${this}
        INCLUDEDIRS ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_BINARY_DIR}/lib ${include_${this}}
        )

and translate into f2py-cmake. Now the "fun" part is that the Fortran code depends on some C code. I think our crazy homebuilt layer must be handling this (or I figured it out some other way), but I wasn't sure of the right way to do that here.

So, this is my first attempt to do this in a new fresh repo with:

find_package(
  Python
  COMPONENTS Interpreter Development.Module NumPy
  REQUIRED)

include(UseF2Py)

# make a libshavemantissa library from shavemanitssa.c
add_library(shavemantissa shavemantissa.c)

# Create the F2Py `numpyobject` library.
f2py_object_library(f2py_object OBJECT)

f2py_generate_module(ShaveMantissa_
  ShaveMantissa_py.F90
  OUTPUT_VARIABLE shave_mantissa_files
)

python_add_library(ShaveMantissa_ MODULE "${shave_mantissa_files}" WITH_SOABI)
target_link_libraries(ShaveMantissa_ PRIVATE f2py_object)
target_link_libraries(ShaveMantissa_ PRIVATE shavemantissa)

install(TARGETS  ShaveMantissa_   DESTINATION lib/Python/MAPL_cfio_r4)
install(PROGRAMS shavemantissa.py DESTINATION lib/Python/MAPL_cfio_r4)

# we also need to install the library
install(TARGETS shavemantissa DESTINATION lib)

And, with this, it actually does seem to work. At least, something is built. But I can't seem to actually import it. If I go to my install:

❯ python3 -c 'import ShaveMantissa_'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: dlopen(/Users/mathomp4/Temp/MAPLpy/install/lib/Python/MAPL_cfio_r4/ShaveMantissa_.cpython-312-darwin.so, 0x0002): symbol not found in flat namespace '_shave32_'

Do you see anything in my above CMake that might cause this? Maybe I need some F2PY_ARGS?

Again, sorry if these are dumb questions. f2py has always been my antagonist in CMake land. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant