Skip to content

BUG: f2py -c does not work on Windows with meson (Python 3.12) #25000

@jmrohwer

Description

@jmrohwer

Describe the issue:

I use f2py to compile various fortran modules. Testing this on Windows with Python 3.12 and the new meson-based build system, the *.pyd and *.dll binary files are not produced, instead the directory remains unchanged, no new files added. However, the output from the compile process (pasted below) does not show an obvious error.

When doing this on Python 3.11 using distutils and the same command, the following files are produced:

  • hi.cp311-win_amd64.pyd
  • hi\.libs\libhi.LM6TMX7H4VE3WXT3WJV3AXCNAQQAXT2U.gfortran-win_amd64.dll

After adding hi\.libs to the DLL path with os.add_dll_directory(os.path.join(os.getcwd(), 'hi/.libs')) I can then run

>>> import hi
>>> hi
<module 'hi' from 'C:\\Users\\jr\\Documents\\Python\\f2py_test\\hi.cp311-win_amd64.pyd'>
>>> hi.hello()
 Hello from fortran

Under Python 3.12 if I specify the build directory (this command also generates the output in "Error message" below),

python3.12 -m numpy.f2py -c hi.pyf hi.f --build-dir build-windows

then .\build-windows\bbdir\ contains the following files:

  • hi.cp312-win_amd64.pyd
  • hi.cp312-win_amd64.dll.a
    Note the library ends in *.dll.a not *.dll.

Also .\build-windows\bbdir\hi.cp312-win_amd64.pyd.p\ contains a bunch of *.obj files from the compile. However, the module can't be imported.

>>> os.chdir('build-windows/bbdir/')
>>> os.listdir(os.getcwd())
['.gitignore', '.hgignore', '.ninja_deps', '.ninja_log', 'build.ninja', 'compile_commands.json', 'hi.cp312-win_amd64.dll.a', 'hi.cp312-win_amd64.pyd', 'hi.cp312-win_amd64.pyd.p', 'meson-info', 'meson-logs', 'meson-private']
>>> import hi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing hi: The specified module could not be found.

Reproduce the code example:

**hi.f**

      SUBROUTINE HELLO()
      PRINT*,"Hello from fortran"
      END

**hi.pyf**

!    -*- f90 -*-
! Note: the context of this file is case sensitive.

python module hi ! in 
    interface  ! in :hi
        subroutine hello ! in :hi:hi.f
        end subroutine hello
    end interface 
end python module hi

! This file was auto-generated with f2py (version:1.23.2).
! See:
! https://web.archive.org/web/20140822061353/http://cens.ioc.ee/projects/f2py2e

Error message:

PS C:\Users\jr\Documents\Python\f2py_test> python3.12 -m numpy.f2py -c hi.pyf hi.f --build-dir build-windows
Cannot use distutils backend with Python 3.12, using meson backend instead.Using meson backend
Will pass --lower to f2py
See https://numpy.org/doc/stable/f2py/buildtools/meson.htmlReading fortran codes...
        Reading file 'hi.pyf' (format:free)
Post-processing...
        Block: hi
                        Block: hello
Applying post-processing hooks...
  character_backward_compatibility_hook
Post-processing (stage 2)...
Building modules...
    Building module "hi"...
    Generating possibly empty wrappers"
    Maybe empty "hi-f2pywrappers.f"
        Constructing wrapper function "hello"...
          hello()
    Wrote C/API module "hi" to file ".\himodule.c"
The Meson build system
Version: 1.2.3
Source dir: C:\Users\jr\Documents\Python\f2py_test\build-windows
Build dir: C:\Users\jr\Documents\Python\f2py_test\build-windows\bbdir
Build type: native build
Project name: hi
Project version: 0.1
Fortran compiler for the host machine: gfortran (gcc 12.2.0 "GNU Fortran (x86_64-posix-seh-rev2, Built by MinGW-W64 project) 12.2.0")
Fortran linker for the host machine: gfortran ld.bfd 2.39
C compiler for the host machine: gcc (gcc 12.2.0 "gcc.exe (x86_64-posix-seh-rev2, Built by MinGW-W64 project) 12.2.0")
C linker for the host machine: gcc ld.bfd 2.39
Host machine cpu family: x86_64
Host machine cpu: x86_64
Program python3 found: YES (C:\Python312\python.exe)
Run-time dependency python found: YES 3.12
Build targets in project: 1

Found ninja-1.11.1 at C:\ProgramData\chocolatey\bin\ninja.EXE
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: C:\ProgramData\chocolatey\bin\ninja.EXE -C C:/Users/jr/Documents/Python/f2py_test/build-windows/bbdir
ninja: Entering directory `C:/Users/jr/Documents/Python/f2py_test/build-windows/bbdir'
[3/6] Compiling C object hi.cp312-win_amd64.pyd.p/186a87aefc5816048ccf8f910da29dbb8286fc40_.._.._f2py_src_fortranobject.c.obj
C:/Python312/Lib/site-packages/numpy/f2py/src/fortranobject.c: In function 'fortran_doc':
C:/Python312/Lib/site-packages/numpy/f2py/src/fortranobject.c:343:52: warning: unknown conversion type character 'z' in format [-Wformat=]
  343 |             "fortranobject.c: fortran_doc: len(p)=%zd>%zd=size:"
      |                                                    ^
C:/Python312/Lib/site-packages/numpy/f2py/src/fortranobject.c:343:56: warning: unknown conversion type character 'z' in format [-Wformat=]
  343 |             "fortranobject.c: fortran_doc: len(p)=%zd>%zd=size:"
      |                                                        ^
C:/Python312/Lib/site-packages/numpy/f2py/src/fortranobject.c:343:13: warning: too many arguments for format [-Wformat-extra-args]
  343 |             "fortranobject.c: fortran_doc: len(p)=%zd>%zd=size:"
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[6/6] Linking target hi.cp312-win_amd64.pyd

Runtime information:

>>> print(numpy.__version__)
1.26.1
>>> print(sys.version)
3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)]
>>> print(numpy.show_runtime())
[{'numpy_version': '1.26.1',
  'python': '3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 '
            '64 bit (AMD64)]',
  'uname': uname_result(system='Windows', node='DESKTOP-Q4HRMEM', release='10', version='10.0.19045', machine='AMD64')},
 {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                      'found': ['SSSE3',
                                'SSE41',
                                'POPCNT',
                                'SSE42',
                                'AVX',
                                'AVX2'],
                      'not_found': ['F16C',
                                    'FMA3',
                                    'AVX512F',
                                    'AVX512CD',
                                    'AVX512_SKX',
                                    'AVX512_CLX',
                                    'AVX512_CNL',
                                    'AVX512_ICL']}},
 {'architecture': 'Haswell',
  'filepath': 'C:\\Python312\\Lib\\site-packages\\numpy.libs\\libopenblas64__v0.3.23-293-gc2f4bdbb-gcc_10_3_0-2bde3a66a51006b2b53eb373ff767a3f.dll',
  'internal_api': 'openblas',
  'num_threads': 2,
  'prefix': 'libopenblas',
  'threading_layer': 'pthreads',
  'user_api': 'blas',
  'version': '0.3.23.dev'}]
PS C:\Users\jr\Documents\Python\f2py_test> gfortran --version
GNU Fortran (x86_64-posix-seh-rev2, Built by MinGW-W64 project) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

PS C:\Users\jr\Documents\Python\f2py_test> meson --version
1.2.3
PS C:\Users\jr\Documents\Python\f2py_test> ninja --version
1.11.1

VS Build tools 2022 (version 17.3.1) but from the output above it seems gcc is detected as C compiler. Not sure if this has to do with the issue.

Context for the issue:

I need to compile Fortran modules across platforms, on linux and macOS the same command (f2py -c hi.pyf hi.f) works flawlessly:

Python 3.12.0 (main, Oct 21 2023, 12:40:24) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hi
>>> hi
<module 'hi' from '/home/jr/Documents/Python/f2py_testing/hi/hi.cpython-312-x86_64-linux-gnu.so'>
>>> hi.hello()
 Hello from fortran

Further context in #22211 and my comments in #22225.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions