scikit-build / scikit-build-sample-projects

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

Add a fortran and f2py example #29

Closed frank1010111 closed 1 year ago

frank1010111 commented 1 year ago

It would be awesome to have an example using scikit-build to build a Fortran project with f2py.

There is some scikit-build documentation and some numpy documentation for f2py, but a working example makes it easier to learn.

I would be willing to write such an example, or modify the extremely stale branch trying to do so.

itcarroll commented 1 year ago

Strongly support a version of this enhancement!

The pi example is detailed, but I would benefit more from an example that mirrors the hello-cython example and uses add_f2py_target. I just tried and failed 😣.

itcarroll commented 1 year ago

UPDATE: Disregard below. Will add an alternate PR as soon as I figure out how to test stdout from F2PY ...


In case anyone would like to make a suggestion, please check out this failing attempt to add a hello-f2py example.

Relevant chunck of cmake logs

      [1/9] Building C object CMakeFiles/_f2py_runtime_library.dir/tmp/pip-build-env-u1uollwm/overlay/lib/python3.10/site-packages/numpy/f2py/src/fortranobject.c.o
      [2/9] Linking C static library lib_f2py_runtime_library.a
      [3/9] Generating _hellomodule.c, _hello-f2pywrappers.f
      Reading fortran codes...
          Reading file '/home/icarroll/tmp/skb/projects/hello-f2py/hello/_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/9] Building Fortran preprocessed hello/CMakeFiles/_hello.dir/_hello-f2pywrappers.f-pp.f
      [5/9] Generating Fortran dyndep file hello/CMakeFiles/_hello.dir/Fortran.dd
      [6/9] Building Fortran object hello/CMakeFiles/_hello.dir/_hello-f2pywrappers.f.o
      [7/9] Building C object hello/CMakeFiles/_hello.dir/_hellomodule.c.o
      [8/9] Linking Fortran shared module hello/_hello.cpython-310-x86_64-linux-gnu.so
      FAILED: hello/_hello.cpython-310-x86_64-linux-gnu.so
      : && /usr/bin/gfortran -fPIC -O3 -DNDEBUG -O3  -Wl,--unresolved-symbols=ignore-all -Wl,--version-script="/home/icarroll/tmp/skb/projects/hello-f2py/_skbuild/linux-x86_64-3.10/cmake-build/hello/CMakeFiles/_hello-version-script.map" -shared  -o hello/_hello.cpython-310-x86_64-linux-gnu.so hello/CMakeFiles/_hello.dir/_hellomodule.c.o hello/CMakeFiles/_hello.dir/_hello-f2pywrappers.f.o  lib_f2py_runtime_library.a && :
      /usr/bin/ld: lib_f2py_runtime_library.a(fortranobject.c.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
      /usr/bin/ld: final link failed: Nonrepresentable section on output

I'm not sure what needs the -fPIC flag or how to apply it.