-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: f2py -c
does not work on Windows with meson (Python 3.12)
#25000
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 o 8000 ur terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for the report. The build process seems to have been completed correctly (no errors). I haven't tested meson on windows much but does it work if you rename the dll? Could you also add the meson.build file being used? Edit: on closer inspection this is because the wrong compiler stack is detected, could you try with the environment variables set for the right compilers? |
Any pointers on how to do this? Windows is not my main OS and I just need to be able to compile this to distribute my software across multiple platforms. As mentioned I have VS build tools installed but it does not seem to be used. The distutils based compile for Python 3.11 and earlier just "did the right thing", selecting gfortran for the Fortran but the MS compiler for the C. Is this new build process with meson documented somewhere for Windows? I have always found compiling on Window 8000 s very challenging... |
I'm attaching a ZIP of the build directory, this has the meson build file, all the logs, generated C code, etc. |
I likely won't be able to boot into a windows machine until the weekend but from the diff --git i/numpy/f2py/_backends/_meson.py w/numpy/f2py/_backends/_meson.py
index 3176a5e08..13eaf0569 100644
--- i/numpy/f2py/_backends/_meson.py
+++ w/numpy/f2py/_backends/_meson.py
@@ -92,6 +92,7 @@ def _get_build_command(self):
"meson",
"setup",
self.meson_build_dir,
+ "--vsenv", # gh-25000
]
def write_meson_build(self, build_dir: Path) -> None: Ought to work. |
There is progress, this now finds the VS compiler but it fails during the linking stage: The installed gfortran (from mingw) has Directory: C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\lib\gcc\x86_64-w64-mingw32\12.2.0
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 09/12/2022 01:37 finclude
d----- 09/12/2022 01:38 include
d----- 09/12/2022 01:15 include-fixed
d----- 09/12/2022 01:36 install-tools
-a---- 09/12/2022 01:37 690 crtbegin.o
-a---- 09/12/2022 01:37 891 crtend.o
-a---- 09/12/2022 01:37 2879 crtfastmath.o
-a---- 09/12/2022 01:38 61636 libatomic.a
-a---- 09/12/2022 01:38 128200 libatomic.dll.a
-a---- 09/12/2022 01:37 61120 libcaf_single.a
-a---- 09/12/2022 01:37 6074900 libgcc.a
-a---- 09/12/2022 01:37 56432 libgcc_eh.a
-a---- 09/12/2022 01:37 257802 libgcov.a
-a---- 09/12/2022 01:37 4667296 libgfortran.a
-a---- 09/12/2022 01:37 1332060 libgfortran.dll.a
-a---- 09/12/2022 01:37 269 libgfortran.spec
-a---- 09/12/2022 01:37 384208 libgomp.a
-a---- 09/12/2022 01:37 472688 libgomp.dll.a
-a---- 09/12/2022 01:37 164 libgomp.spec
-a---- 09/12/2022 01:37 503102 libquadmath.a
-a---- 09/12/2022 01:37 80196 libquadmath.dll.a
-a---- 09/12/2022 01:37 13854 libssp.a
-a---- 09/12/2022 01:37 12372 libssp.dll.a
-a---- 09/12/2022 01:37 824 libssp_nonshared.a
-a---- 09/12/2022 01:37 7032740 libstdc++.a
-a---- 09/12/2022 01:37 5578672 libstdc++.dll.a
-a---- 09/12/2022 01:37 2415 libstdc++.dll.a-gdb.py
-a---- 09/12/2022 01:37 620494 libstdc++fs.a
-a---- 09/12/2022 01:37 342108 libsupc++.a The complete traceback: 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
Activating VS 17.3.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: cl (msvc 19.33.31629 "Microsoft (R) C/C++ Optimizing Compiler Version 19.33.31629 for x64")
C linker for the host machine: link link 14.33.31629.0
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
hi 0.1
User defined options
vsenv: True
Found ninja-1.10.2 at "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\ninja.EXE"
Visual Studio environment is needed to run Ninja. It is recommended to use Meson wrapper:
C:\Python312\Scripts\meson compile -C bbdir
Activating VS 17.3.1
INFO: automatically activated MSVC compiler environment
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja\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'
[6/6] Linking target hi.cp312-win_amd64.pyd
FAILED: hi.cp312-win_amd64.pyd
"link" /MACHINE:x64 /OUT:hi.cp312-win_amd64.pyd hi.cp312-win_amd64.pyd.p/hi.f.obj hi.cp312-win_amd64.pyd.p/himodule.c.obj hi.cp312-win_amd64.pyd.p/hi-f2pywrappers.f.obj hi.cp312-win_amd64.pyd.p/186a87aefc5816048ccf8f910da29dbb8286fc40_.._.._f2py_src_fortranobject.c.obj "/LIBPATH:C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0" "/LIBPATH:C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc/x86_64-w64-mingw32/12.2.0" "/LIBPATH:C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc" "/LIBPATH:C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib/gcc" "/LIBPATH:C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/lib/../lib" "/LIBPATH:C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/x86_64-w64-mingw32/lib" "/LIBPATH:C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../lib" "/LIBPATH:C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/lib" "/LIBPATH:C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/lib" "/LIBPATH:C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../.." "/nologo" "/release" "/nologo" "/OPT:REF" "/DLL" "/IMPLIB:hi.cp312-win_amd64.lib" "C:\Python312\libs\python312.lib" "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "comdlg32.lib" "advapi32.lib" "gfortran.lib"
LINK : fatal error LNK1181: cannot open input file 'gfortran.lib'
ninja: build stopped: subcommand failed.
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "c:\python312\Lib\site-packages\numpy\f2py\__main__.py", line 5, in <module>
main()
File "c:\python312\Lib\site-packages\numpy\f2py\f2py2e.py", line 732, in main
run_compile()
File "c:\python312\Lib\site-packages\numpy\f2py\f2py2e.py", line 705, in run_compile
builder.compile()
File "c:\python312\Lib\site-packages\numpy\f2py\_backends\_meson.py", line 132, in compile
self.run_meson(self.build_dir)
File "c:\python312\Lib\site-packages\numpy\f2py\_backends\_meson.py", line 125, in run_meson
raise subprocess.CalledProcessError(
subprocess.CalledProcessError: Command '['meson', 'compile', '-C', 'bbdir']' returned non-zero exit status 1. |
@jmrohwer this is interesting enough that I've raised an issue over at |
TL;DR
Also more specifically:
Documentation can be tracked separately; this issue can be closed as soon as the ProcessI started out with a fresh windows machine and will document the steps I took. First steps
At this point, Program python3 found: YES (C:\Users\blah\AppData\Local\Programs\Python\Python312\python.exe)
Need python for x86, but found x86_64
Run-time dependency python found: NO (tried sysconfig) Back to the drawing board... Second attempt
At this point.. PS C:\Users\blah\build-windows\build-windows> meson setup bdir
The Meson build system
Version: 1.2.3
Source dir: C:\Users\blah\build-windows\build-windows
Build dir: C:\Users\blah\build-windows\build-windows\bdir
Build type: native build
Project name: hi
Project version: 0.1
Fortran compiler for the host machine: gfortran (gcc 13.2.0 "GNU Fortran (x86_64-win32-seh-rev1, Built by MinGW-Builds project) 13.2.0")
Fortran linker for the host machine: gfortran ld.bfd 2.39
C compiler for the host machine: gcc (gcc 13.2.0 "gcc (x86_64-win32-seh-rev1, Built by MinGW-Builds project) 13.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:\Users\blah\AppData\Local\Programs\Python\Python312\python.exe)
Run-time dependency python found: YES 3.12
Build targets in project: 1
Found ninja-1.11.1.git.kitware.jobserver-1 at "C:\Users\Rohit Goswami\AppData\Local\Programs\Python\Python312\Scripts\ninja.EXE"
PS C:\Users\blah\build-windows\build-windows> meson compile -C bdir
INFO: autodetecting backend as ninja
INFO: calculating backend command to run: "C:\Users\blah\AppData\Local\Programs\Python\Python312\Scripts\ninja.EXE" -C "C:/Users/blah/build-windows/build-windows/bdir"
ninja: Entering directory `C:/Users/blah/build-windows/build-windows/bdir'
[6/6] Linking target hi.cp312-win_amd64.pyd
PS C:\Users\blah\build-windows\build-windows> cd bdir
PS C:\Users\blah\build-windows\build-windows\bdir> python
Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct 2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 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. The files at this point are: 1646 hi.cp312-win_amd64.dll.a
100428 hi.cp312-win_amd64.pyd Reproducing what is noted in the issue :) RTools approach (SciPy)Used the SciPy documentation for setting up the 64-bit compilers, after removing the environment variable.
This doesn't go very far at all: Fortran compiler for the host machine: gfortran (gcc 8.3.0 "GNU Fortran (Built by Jeroen for the R-project) 8.3.0")
Fortran linker for the host machine: gfortran ld.bfd 2.33.1
C compiler for the host machine: cc (gcc 8.3.0 "cc (Built by Jeroen for the R-project) 8.3.0")
C linker for the host machine: cc ld.bfd 2.33.1
Host machine cpu family: x86_64
Host machine cpu: x86_64
Program python3 found: YES (C:\Users\blah\AppData\Local\Programs\Python\Python312\python.exe)
Run-time dependency python found: YES 3.12
...
[6/6] Linking target hi.cp312-win_amd64.pyd
FAILED: hi.cp312-win_amd64.pyd
"cc" -o hi.cp312-win_amd64.pyd hi.cp312-win_amd64.pyd.p/hi.f.obj hi.cp312-win_amd64.pyd.p/himodule.c.obj hi.cp312-win_amd64.pyd.p/hi-f2pywrappers.f.obj hi.cp312-win_amd64.pyd.p/7359b626462c6d5eeac3d0dfde0a8b196a4faf70_.._.._f2py_src_fortranobject.c.obj "-LC:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0" "-LC:/rtools40/mingw64/lib/gcc/x86_64-w64-mingw32/8.3.0" "-LC:/rtools40/mingw64/bin/../lib/gcc" "-LC:/rtools40/mingw64/lib/gcc" "-LC:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib" "-LC:/rtools40/mingw64/x86_64-w64-mingw32/lib" "-LC:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../lib" "-LC:/rtools40/mingw64/lib" "-LC:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib" "-LC:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../.." "-Wl,--allow-shlib-undefined" "-Wl,-O1" "-shared" "-Wl,--start-group" "-Wl,--out-implib=hi.cp312-win_amd64.dll.a" "C:\Users\Rohit Goswami\AppData\Local\Programs\Python\Python312\python312.dll" "-lkernel32" "-luser32" "-lgdi32" "-lwinspool" "-lshell32" "-lole32" "-loleaut32" "-luuid" "-lcomdlg32" "-ladvapi32" "-lgfortran" "-lm" "-Wl,--end-group"
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/libgfortran.a(write.o):(.text$get_precision+0x156): undefined reference to `quadmath_snprintf'
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/libgfortran.a(write.o):(.text$get_float_string+0x170): undefined reference to `quadmath_snprintf'
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/libgfortran.a(write.o):(.text$get_float_string+0xa62): undefined reference to `quadmath_snprintf'
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/libgfortran.a(write.o):(.text$get_float_string+0x165c): undefined reference to `quadmath_snprintf'
C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/libgfortran.a(write.o):(.text$get_float_string+0x173a): undefined reference to `quadmath_snprintf'
collect2.exe: error: ld returned 1 exit status Before fixing this, we can digress and obtain With
|
Thanks, I had figured out something similar but had not yet gotten around to documenting it here. I have also gotten the compilation to work in a similar way. |
Incidentally, in my own project (https://github.com/PySCeS/pysces) I use Do you know if it is possible to force cmake to use the MinGW compilers even when MSVC is installed? |
For compiler selection, you typically want to use an environment variable (e.g., |
One issue relating to |
It is fine to link to |
Makes sense, generally I'd think for generated code especially (like F2PY), it is a rather surprising default; atleast it should signal more than it currently does :) |
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:
After adding
hi\.libs
to the DLL path withos.add_dll_directory(os.path.join(os.getcwd(), 'hi/.libs'))
I can then runUnder Python 3.12 if I specify the build directory (this command also generates the output in "Error message" below),
then
.\build-windows\bbdir\
contains the following files: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.Reproduce the code example:
Error message:
Runtime information:
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:Further context in #22211 and my comments in #22225.
The text was updated successfully, but these errors were encountered: