scikit-build / scikit-build-sample-projects

Sample projects demonstrating use of scikit-build
74 stars 27 forks source link

hello-f2py example #35

Open itcarroll opened 1 year ago

itcarroll commented 1 year ago

Adds a sample project in the style of hello-cython, using the add_f2py_target function provided by scikit-build.

The example also demonstrates the minimal setup.py content when porting package metadata to pyproject.toml.

This would close #29, but so would #32. My goal was to keep it very simple.

zerothi commented 1 year ago

Would be great if you could add that the method to create the signature on the fly, something like this:

# The module file for the signatures
set(hello_module "_hello")

# generate the signature file
set(hello_sig "${CMAKE_CURRENT_BINARY_DIR}/_hello_signature.pyf")
add_custom_command(
  OUTPUT ${hello_sig}
  COMMAND ${F2PY_EXECUTABLE} -m ${hello_module} hello.f90 -h ${siesta_sig} --overwrite-signature
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  DEPENDS hello.f90
  COMMENT "Generating signature file files"
  )

then the signature need not be present.

itcarroll commented 1 year ago

I can add @zerothi 's suggestion if a project maintainer agrees. But, because the F2PY docs suggest manual modifications to the signature after generation, I'm going to leave the example as-is for now.

I don't think the add_f2py_target command is capable of handling the "the quick and smart way", which would avoid signature files altogether.

zerothi commented 1 year ago

You are probably right. :) In the simple cases it is just nice ;)

itcarroll commented 1 year ago

Welp, this is now failing for me locally. Can't say what changed ... but now CMake claims to generate _hello-f2pywrappers2.f90 but does not, leading the subsequent build step to fail.

[3/13] Generating _hellomodule.c, _hello-f2pywrappers.f, _hello-f2pywrappers2.f90
      Reading fortran codes...
          Reading file '/home/icarroll/tmp/skb/projects/hello-f2py/hello_f2py/_hello.pyf' (format:free)
      Post-processing...
          Block: _hello
                          Block: hello
      Applying post-processing hooks...
        character_backward_compatibility_hook
      Post-processing (stage 2)...
      Building modules...
          Building module "_hello"...
          Generating possibly empty wrappers"
          Maybe empty "_hello-f2pywrappers.f"
              Constructing wrapper function "hello"...
                hello(a)
          Wrote C/API module "_hello" to file "./_hellomodule.c"
      [4/13] Building Fortran preprocessed hello_f2py/CMakeFiles/_hello.dir/_hello-f2pywrappers2.f90-pp.f90
      FAILED: hello_f2py/CMakeFiles/_hello.dir/_hello-f2pywrappers2.f90-pp.f90 hello_f2py/CMakeFiles/_hello.dir/_hello-f2pywrappers2.f90.o.ddi
      /usr/bin/gfortran -cpp -D_hello_EXPORTS -I../../../venv11/lib/python3.11/site-packages/numpy/f2py/src -I../../../venv11/lib/python3.11/site-packages/numpy/core/include -I/home/icarroll/miniconda3/include/python3.11 -O3 -DNDEBUG -O3 -Jhello_f2py -fPIC -E hello_f2py/_hello-f2pywrappers2.f90 -o hello_f2py/CMakeFiles/_hello.dir/_hello-f2pywrappers2.f90-pp.f90 && /usr/bin/cmake -E cmake_ninja_depends --tdi=hello_f2py/CMakeFiles/_hello.dir/FortranDependInfo.json --lang=Fortran --src=hello_f2py/_hello-f2pywrappers2.f90 --pp=hello_f2py/CMakeFiles/_hello.dir/_hello-f2pywrappers2.f90-pp.f90 --dep=hello_f2py/CMakeFiles/_hello.dir/_hello-f2pywrappers2.f90-pp.f90.d --obj=hello_f2py/CMakeFiles/_hello.dir/_hello-f2pywrappers2.f90.o --ddi=hello_f2py/CMakeFiles/_hello.dir/_hello-f2pywrappers2.f90.o.ddi
      gfortran: error: hello_f2py/_hello-f2pywrappers2.f90: No such file or directory
      gfortran: fatal error: no input files
      compilation terminated.