Pebaz / nimporter

Compile Nim Extensions for Python On Import!
MIT License
824 stars 33 forks source link

Don't assume vcc availability on Windows #37

Closed richboss closed 3 years ago

richboss commented 3 years ago

OS: Windows 10 Version 2004 (Build 19041.572) Python: Python 3.8.4 (tags/v3.8.4:dfa645a, Jul 13 2020, 16:46:45) [MSC v.1924 64 bit (AMD64)] Nim: 1.4.0 C Compiler: gcc version 8.1.0 (i686-posix-dwarf-rev0, Built by MinGW-W64 project)

On Windows, nimporter doesn't work if the Visual Studio C Compiler is not installed. See the following stacktrace:

c:\Temp>python test.py
Traceback (most recent call last):
  File "C:\Temp\test.py", line 2, in <module>
    import nimporter, nim_math
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 914, in _find_spec
  File "O:\app\python3\lib\site-packages\nimporter.py", line 1123, in find_spec
    return Nimporter.import_nim_code(fullname, path, library=False)
  File "O:\app\python3\lib\site-packages\nimporter.py", line 806, in import_nim_code
    NimCompiler.compile_nim_code(
  File "O:\app\python3\lib\site-packages\nimporter.py", line 566, in compile_nim_code
    raise NimCompileException(errors[0])
  File "O:\app\python3\lib\site-packages\nimporter.py", line 48, in __init__
    nim_module = nim_module.splitlines()[-1]
IndexError: list index out of range

Alas, this stacktrace is not very meaningful. But the root cause is quite simple. The class NimCompiler in the file nimporter.py defines a standard set of Nim cli args. On Windows (if sys.platform == 'win32') another option is added: --cc:vcc. This is where the problem arises. IMO it is wrong to unconditionally assume the availability of vcc on Windows boxes. Nim could well be using gcc or tcc instead.

Maybe you could check if vcc is at all installed before adding this cli argument?

Pebaz commented 3 years ago

Thank you for the bug report!

I will look into this soon and see if enumerating installed compilers is even possible on Windows. Any thoughts on how to do this would be appreciated!

retsyo commented 2 years ago

this is not a fix to fit every case. in case somebody use mingw, you can change line 133-146 in nimporter.py from

    NIM_CLI_ARGS = [
                       '--opt:speed',
                       '--parallelBuild:0',
                       '--gc:refc',
                       '--threads:on',
                       '--app:lib',
                       '-d:release',
                       '-d:strip',
                       '-d:ssl',

                       # https://github.com/Pebaz/nimporter/issues/41
                       '--warning[ProveInit]:off',

                   ] + (['--cc:vcc'] if 'MSC' in sys.version else [])

to

    NIM_CLI_ARGS = [
                       '--opt:speed',
                       '--parallelBuild:0',
                       '--gc:refc',
                       '--threads:on',
                       '--app:lib',
                       '-d:release',
                       '-d:strip',
                       '-d:ssl',
                       '--passL:-static',        # else, you need libgcc_s_seh-1.dll

                       # https://github.com/Pebaz/nimporter/issues/41
                       '--warning[ProveInit]:off',

                   ]