Skip to content

Commit

Permalink
Updated NgSpiceShared with MINGW support
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kreijstal authored Nov 10, 2024
1 parent 7fd7623 commit a71df73
Showing 1 changed file with 28 additions and 40 deletions.
68 changes: 28 additions & 40 deletions PySpice/Spice/NgSpice/Shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,32 +406,38 @@ class NgSpiceShared:

@classmethod
def setup_platform(cls):

if ConfigInstall.OS.on_windows:
if platform.architecture()[0] != '64bit':
raise NameError('Windows 32bit is no longer supported by NgSpice')

_ = os.environ.get('NGSPICE_LIBRARY_PATH', None)
if _ is not None:
cls.LIBRARY_PATH = _
else:
if ConfigInstall.OS.on_windows:
# Check for MSYSTEM environment first
msystem = os.environ.get('MSYSTEM')
mingw_prefix = os.environ.get('MINGW_PREFIX')

if msystem and mingw_prefix:
# Use MINGW paths
cls.LIBRARY_PATH = str(Path(mingw_prefix) / 'bin' / 'libngspice{}.dll')
if 'SPICE_LIB_DIR' not in os.environ:
os.environ['SPICE_LIB_DIR'] = str(Path(mingw_prefix) / 'share' / 'ngspice' / 'scripts')
if 'NGSPICE_LIBRARY_PATH' not in os.environ:
os.environ['NGSPICE_LIBRARY_PATH'] = str(Path(mingw_prefix) / 'bin' / 'libngspice-0.dll')
else:
# Fall back to original Windows paths
ngspice_path = Path(__file__).parent.joinpath('Spice64_dll')
cls.NGSPICE_PATH = ngspice_path
# path = ngspice_path.joinpath('dll-vs', 'ngspice-{version}{id}.dll')
path = ngspice_path.joinpath('dll-vs', 'ngspice{}.dll')

elif ConfigInstall.OS.on_osx:
path = 'libngspice{}.dylib'

elif ConfigInstall.OS.on_linux:
path = 'libngspice{}.so'
cls.LIBRARY_PATH = str(ngspice_path.joinpath('dll-vs', 'ngspice{}.dll'))

else:
raise NotImplementedError
elif ConfigInstall.OS.on_osx:
path = 'libngspice{}.dylib'
cls.LIBRARY_PATH = str(path)

elif ConfigInstall.OS.on_linux:
path = 'libngspice{}.so'
cls.LIBRARY_PATH = str(path)

else:
raise NotImplementedError

##############################################

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

def __init__(self, ngspice_id=0, send_data=False, verbose=False):

""" Set the *send_data* flag if you want to enable the output callback.
Set the *ngspice_id* to an integer value if you want to run NgSpice in parallel.
"""

self._ngspice_id = ngspice_id

self._spinit_not_found = False

self._number_of_exec_calls = 0

self._stdout = []
self._stderr = []
self._error_in_stdout = None
self._error_in_stderr = None

self._has_cider = None
self._has_xspice = None
self._ngspice_version = None
self._extensions = []

self._library_path = None
self._load_library(verbose)
self._init_ngspice(send_data)

self._is_running = False

##############################################
Expand All @@ -495,32 +494,21 @@ def library_path(self):
if not self._ngspice_id:
library_prefix = ''
else:
library_prefix = '{}'.format(self._ngspice_id) # id =
library_prefix = '{}'.format(self._ngspice_id)
library_path = self.LIBRARY_PATH.format(library_prefix)
self._library_path = library_path
return self._library_path

##############################################

def _load_library(self, verbose):

if ConfigInstall.OS.on_windows:
# https://sourceforge.net/p/ngspice/discussion/133842/thread/1cece652/#4e32/5ab8/9027
# When environment variable SPICE_LIB_DIR is empty, ngspice looks in C:\Spice64\share\ngspice\scripts
# Else it tries %SPICE_LIB_DIR%\scripts\spinit
if 'SPICE_LIB_DIR' not in os.environ:
_ = str(Path(self.NGSPICE_PATH).joinpath('share', 'ngspice'))
os.environ['SPICE_LIB_DIR'] = _
# self._logger.warning('Set SPICE_LIB_DIR = %s', _)

# Fixme: not compatible with supra
# if 'CONDA_PREFIX' in os.environ:
# _ = str(Path(os.environ['CONDA_PREFIX']).joinpath('share', 'ngspice'))
# os.environ['SPICE_LIB_DIR'] = _
# self._logger.warning('Set SPICE_LIB_DIR = %s', _)

# https://sourceforge.net/p/ngspice/bugs/490
# ngspice and Kicad do setlocale(LC_NUMERIC, "C");
if self.NGSPICE_PATH:
# Original Windows path
spice_lib_dir = str(Path(self.NGSPICE_PATH).joinpath('share', 'ngspice'))
os.environ['SPICE_LIB_DIR'] = spice_lib_dir

if ConfigInstall.OS.on_windows:
self._logger.debug('locale LC_NUMERIC is not forced to C')
elif ConfigInstall.OS.on_linux or ConfigInstall.OS.on_osx:
Expand Down

0 comments on commit a71df73

Please sign in to comment.