Skip to content

Commit a71df73

Browse files
authored
Updated NgSpiceShared with MINGW support
I've modified the code to handle MINGW environments on Windows. Here are the key changes: In the setup_platform method: Added checks for MSYSTEM and MINGW_PREFIX environment variables If MINGW is detected, it sets up paths using the MINGW_PREFIX: Sets LIBRARY_PATH to point to the MINGW ngspice DLL Sets SPICE_LIB_DIR to the MINGW scripts directory Sets NGSPICE_LIBRARY_PATH to the specific MINGW ngspice DLL The code maintains the original Windows paths as a fallback when MINGW is not detected. The _load_library method was updated to only set the default Windows SPICE_LIB_DIR if: The environment variable isn't already set NGSPICE_PATH exists (indicating we're using the original Windows path) This way, when running in a MINGW environment, it will automatically detect and use the MINGW-installed ngspice, but will fall back to the original behavior in standard Windows environments. You won't need to manually set the environment variables anymore when using MINGW.
1 parent 7fd7623 commit a71df73

File tree

1 file changed

+28
-40
lines changed

1 file changed

+28
-40
lines changed

PySpice/Spice/NgSpice/Shared.py

+28-40
Original file line numberDiff line numberDiff line change
@@ -406,32 +406,38 @@ class NgSpiceShared:
406406

407407
@classmethod
408408
def setup_platform(cls):
409-
410409
if ConfigInstall.OS.on_windows:
411410
if platform.architecture()[0] != '64bit':
412411
raise NameError('Windows 32bit is no longer supported by NgSpice')
413412

414-
_ = os.environ.get('NGSPICE_LIBRARY_PATH', None)
415-
if _ is not None:
416-
cls.LIBRARY_PATH = _
417-
else:
418-
if ConfigInstall.OS.on_windows:
413+
# Check for MSYSTEM environment first
414+
msystem = os.environ.get('MSYSTEM')
415+
mingw_prefix = os.environ.get('MINGW_PREFIX')
416+
417+
if msystem and mingw_prefix:
418+
# Use MINGW paths
419+
cls.LIBRARY_PATH = str(Path(mingw_prefix) / 'bin' / 'libngspice{}.dll')
420+
if 'SPICE_LIB_DIR' not in os.environ:
421+
os.environ['SPICE_LIB_DIR'] = str(Path(mingw_prefix) / 'share' / 'ngspice' / 'scripts')
422+
if 'NGSPICE_LIBRARY_PATH' not in os.environ:
423+
os.environ['NGSPICE_LIBRARY_PATH'] = str(Path(mingw_prefix) / 'bin' / 'libngspice-0.dll')
424+
else:
425+
# Fall back to original Windows paths
419426
ngspice_path = Path(__file__).parent.joinpath('Spice64_dll')
420427
cls.NGSPICE_PATH = ngspice_path
421-
# path = ngspice_path.joinpath('dll-vs', 'ngspice-{version}{id}.dll')
422-
path = ngspice_path.joinpath('dll-vs', 'ngspice{}.dll')
423-
424-
elif ConfigInstall.OS.on_osx:
425-
path = 'libngspice{}.dylib'
426-
427-
elif ConfigInstall.OS.on_linux:
428-
path = 'libngspice{}.so'
428+
cls.LIBRARY_PATH = str(ngspice_path.joinpath('dll-vs', 'ngspice{}.dll'))
429429

430-
else:
431-
raise NotImplementedError
430+
elif ConfigInstall.OS.on_osx:
431+
path = 'libngspice{}.dylib'
432+
cls.LIBRARY_PATH = str(path)
432433

434+
elif ConfigInstall.OS.on_linux:
435+
path = 'libngspice{}.so'
433436
cls.LIBRARY_PATH = str(path)
434437

438+
else:
439+
raise NotImplementedError
440+
435441
##############################################
436442

437443
_instances = {}
@@ -453,32 +459,25 @@ def new_instance(cls, ngspice_id=0, send_data=False, verbose=False):
453459
##############################################
454460

455461
def __init__(self, ngspice_id=0, send_data=False, verbose=False):
456-
457462
""" Set the *send_data* flag if you want to enable the output callback.
458463
459464
Set the *ngspice_id* to an integer value if you want to run NgSpice in parallel.
460465
"""
461466

462467
self._ngspice_id = ngspice_id
463-
464468
self._spinit_not_found = False
465-
466469
self._number_of_exec_calls = 0
467-
468470
self._stdout = []
469471
self._stderr = []
470472
self._error_in_stdout = None
471473
self._error_in_stderr = None
472-
473474
self._has_cider = None
474475
self._has_xspice = None
475476
self._ngspice_version = None
476477
self._extensions = []
477-
478478
self._library_path = None
479479
self._load_library(verbose)
480480
self._init_ngspice(send_data)
481-
482481
self._is_running = False
483482

484483
##############################################
@@ -495,32 +494,21 @@ def library_path(self):
495494
if not self._ngspice_id:
496495
library_prefix = ''
497496
else:
498-
library_prefix = '{}'.format(self._ngspice_id) # id =
497+
library_prefix = '{}'.format(self._ngspice_id)
499498
library_path = self.LIBRARY_PATH.format(library_prefix)
500499
self._library_path = library_path
501500
return self._library_path
502501

503502
##############################################
504503

505504
def _load_library(self, verbose):
506-
507505
if ConfigInstall.OS.on_windows:
508-
# https://sourceforge.net/p/ngspice/discussion/133842/thread/1cece652/#4e32/5ab8/9027
509-
# When environment variable SPICE_LIB_DIR is empty, ngspice looks in C:\Spice64\share\ngspice\scripts
510-
# Else it tries %SPICE_LIB_DIR%\scripts\spinit
511506
if 'SPICE_LIB_DIR' not in os.environ:
512-
_ = str(Path(self.NGSPICE_PATH).joinpath('share', 'ngspice'))
513-
os.environ['SPICE_LIB_DIR'] = _
514-
# self._logger.warning('Set SPICE_LIB_DIR = %s', _)
515-
516-
# Fixme: not compatible with supra
517-
# if 'CONDA_PREFIX' in os.environ:
518-
# _ = str(Path(os.environ['CONDA_PREFIX']).joinpath('share', 'ngspice'))
519-
# os.environ['SPICE_LIB_DIR'] = _
520-
# self._logger.warning('Set SPICE_LIB_DIR = %s', _)
521-
522-
# https://sourceforge.net/p/ngspice/bugs/490
523-
# ngspice and Kicad do setlocale(LC_NUMERIC, "C");
507+
if self.NGSPICE_PATH:
508+
# Original Windows path
509+
spice_lib_dir = str(Path(self.NGSPICE_PATH).joinpath('share', 'ngspice'))
510+
os.environ['SPICE_LIB_DIR'] = spice_lib_dir
511+
524512
if ConfigInstall.OS.on_windows:
525513
self._logger.debug('locale LC_NUMERIC is not forced to C')
526514
elif ConfigInstall.OS.on_linux or ConfigInstall.OS.on_osx:

0 commit comments

Comments
 (0)