mattkjames7 / PyGeopack

Wrapper for geopack-08 used for the Tsyganenko magnetic field models
GNU General Public License v3.0
11 stars 2 forks source link

Can't compile PyGeopack on Windows 10 #15

Closed mshumko closed 1 year ago

mshumko commented 1 year ago

Hello,

I can't compile the Fortran code. Looks like the gfortan compiler does not recognize the -fallow-argument-mismatch option I suspect is the culprit.

Code to reproduce the error

>>> import PyGeopack 

Error message

Importing libgeopack.dll failed, attempting to recompile
Compiling libgeopack...
Compiling FORTRAN code
gfortran: error: unrecognized command line option '-fallow-argument-mismatch'; did you mean '-Wno-argument-mismatch'?
Compilation error
Compiling libdatetime
Compiling matrix
Compiling modelparams
Compiling spline
Compiling tools
Compiling tracing

(A BUNCH OF WARNINGS)

collect2.exe: error: ld returned 1 exit status
Compilation error
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\anaconda3\envs\mms_conjunctions\Lib\site-packages\PyGeopack\_CFunctions.py", line 11, in <module>
    libgeopack = ctypes.CDLL(getLibFilename())
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\anaconda3\envs\mms_conjunctions\Lib\ctypes\__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: Could not find module 'C:\Users\User\AppData\Local\anaconda3\envs\mms_conjunctions\Lib\site-packages\PyGeopack\__data\libgeopackcpp\libgeopack.dll' (or one of its dependencies). Try using the full path with constructor syntax.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\User\AppData\Local\anaconda3\envs\mms_conjunctions\Lib\site-packages\PyGeopack\__init__.py", line 7, in <module>
    from . import Params
  File "C:\Users\User\AppData\Local\anaconda3\envs\mms_conjunctions\Lib\site-packages\PyGeopack\Params\__init__.py", line 1, in <module>
    from .FreeParams import FreeParams
  File "C:\Users\User\AppData\Local\anaconda3\envs\mms_conjunctions\Lib\site-packages\PyGeopack\Params\FreeParams.py", line 2, in <module>
    from ._CFunctions import _CFreeParams
  File "C:\Users\User\AppData\Local\anaconda3\envs\mms_conjunctions\Lib\site-packages\PyGeopack\Params\_CFunctions.py", line 2, in <module>
    from .._CFunctions import libgeopack
  File "C:\Users\User\AppData\Local\anaconda3\envs\mms_conjunctions\Lib\site-packages\PyGeopack\_CFunctions.py", line 14, in <module>
    compileSource()
  File "C:\Users\User\AppData\Local\anaconda3\envs\mms_conjunctions\Lib\site-packages\PyGeopack\_SourceCompilation.py", line 29, in compileSource    
    raise Exception("An error occurred during compilation.")
Exception: An error occurred during compilation.

Environment

> gfortran --version
GNU Fortran (x86_64-posix-seh, Built by strawberryperl.com project) 8.3.0
Copyright (C) 2018 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.
>>> import sys, platform; print(sys.version); print(platform.platform())
3.11.3 | packaged by Anaconda, Inc. | (main, Apr 19 2023, 23:46:34) [MSC v.1916 64 bit (AMD64)]
Windows-10-10.0.19044-SP0
mattkjames7 commented 1 year ago

Hi mshumko,

It looks like you have a slightly different compiler to what I use. Mine comes from here: https://jmeubank.github.io/tdm-gcc/

I haven't tried using strawberryperl - would I be correct in thinking that your gfortran and g++ compilers are installed simply by installing one of these packages: strawberryperl.com ?

One thing that might be worth trying is removing that -fallow-argument-mismatch option from the batch file (PyGeopack/__data/libgeopackcpp/fortran/compile.bat). I just tried doing that in my W10 VM and it fails to compile due to a mismatch in the argument types for one of Tsyganenko's model functions.

If that works for the strawberryperl compiler, then perhaps I will have to figure out a way to detect the compiler being used within the batch file.

Cheers, Matt.

mattkjames7 commented 1 year ago

So I have spent this evening looking into your problem...

I fixed the C++/Fortran code which required the fallow-argument-mismatch argument - compiling the geopack library does work now using the Strawberry Perl compilers. It also seems to work if I compile a C++ program and link to it.

I have built a new PyGeopack package using this fix: https://github.com/mattkjames7/PyGeopack/releases/tag/v1.2.1 This is probably somewhat different to the previous package you would have installed as it now does compilation during the installation, rather than when it fails to import the DLL, so errors will look a bit different.

The problem for me is, while all of the above works, I cannot import the module because ctypes complains of missing dependencies while loading the DLL (it is a FileNotFoundError, but the file is definitely there). I haven't figured out why this happens, because the OS seems to know where to find all of the dependencies when using C++!

It is entirely possibly that I have done something silly while installing Strawberry Perl, so you may have a little more success. Please do let me know if it works for you, or if you have any idea how to get it working correctly.

mshumko commented 1 year ago

Thanks for the quick fix @mattkjames7! I'm currently on vacation so I won't be able to test the new version until I return in a week.

As for your FileNotFoundError, I'm guessing that you need to add the directory containing that DLL to your Windows Path. I had a similar issue before and that fixed it.

mattkjames7 commented 1 year ago

Hello again, please do feel free to ignore this message while you're on vacation!

I have looked into this a bit more tonight and found that the problem was with ctypes. I think that newer versions of Python might not be scanning $PATH by default, so I added a function to scan for the C++ standard libs. It seems to work for me, hopefully it will for you too!

Latest release is here: https://github.com/mattkjames7/PyGeopack/releases/tag/v1.2.2

If, when you return from vacation, it doesn't work - please do reopen the issue.

mshumko commented 1 year ago

The latest version successfully built, thanks!