scikit-build-core icon indicating copy to clipboard operation
scikit-build-core copied to clipboard

fortran cmake project -- complete functionality

Open zerothi opened this issue 1 year ago • 1 comments

Hi,

I have been transitioning to scikit-build-core which bypasses some of the problems from the older scikit-build procedure.

Bottom line is about the details for the f2py include, its fortranobject.c file and how the targets are created. This project has gotten pretty big, but it should be more flexible than the scikit-build procedure, and I think some of the ideas would be nice for adoption in case you wanted this being part of scikit-build-core.

It seems to be rather complex, but I think the usage is pretty clear.

Please see this cmake file: https://github.com/zerothi/sisl/blob/main/CMakeLists.txt

There are 2 functions:

  • create_f2py_signature
  • add_f2py_library

You can see how they are called here: https://github.com/zerothi/sisl/blob/main/src/sisl/io/siesta/CMakeLists.txt

The create_f2py_signature can automatically create a signature file and add some properties to it, these properties reflects its origin (sources) and some other required details when using it later. Of course, for users having their own signatures. One benefit is that one can use create_f2py_signature to create signatures for some files, then in add_f2py_library one can additionally add sources that are required for fortran to actually function (i.e. decoupling signature and library creation).

You will notice that the skip|only parts of the signature creation can be done in 2 ways:

  1. Direct:

    set(sources 
       file1.f90 "skip: hello_world :"
       file2.f90 "only: hello_world :"
       )
    

    which would skip the function/subroutine hello_world in file1 from being added to the signature file. It will also only add hello_world from file2 to the signature.

  2. Properties

    One can specify a set of properties to do the same:

    get_filename_component(name file1.f90 NAME)
    set_source_files_properties(${name}
    	PROPERTIES
     SISL_SIGNATURE_SKIP "hello_world"
     )
    

    which IMHO is a bit cleaner and more clear, but may require additional details. This could be wrapped in a simple function. The same property with ONLY is also allowed.

Of course this has the problem that it can hide too many things, but I kind of like it, for its simplicity and reduction of duplicated arguments.

In scikit-build there has been some confusion to when the wrappers should be added. And some OS's/CI's will not create empty files as output from the f2py command. Hence it is cleaner and simpler to make user-defined arguments that ensures which wrappers are added to the linker line. So for now the add_f2py_library has two options WITH_F_WRAPPER and WITH_F90_WRAPPER. Then it can easily be added if needed. Users will quickly realize which they need (check for non-empty files). I think this is the safer choice.

I would just let you know of this, more complicated build scheme, which could be useful in a template sample. Let me know if you have questions.

@rgommers I don't know if you are interested, but if some of these details may be useful to have in the f2py section of numpy, feel free to ping me.

zerothi avatar Apr 24 '23 08:04 zerothi