diff --git a/pygmt/clib/loading.py b/pygmt/clib/loading.py index f1e07d8cac9..2de978534f3 100644 --- a/pygmt/clib/loading.py +++ b/pygmt/clib/loading.py @@ -71,7 +71,7 @@ def get_clib_path(env): The path to the libgmt shared library. """ - libname = ".".join(["libgmt", clib_extension()]) + libname = clib_name() if env is None: env = os.environ if "GMT_LIBRARY_PATH" in env: @@ -81,37 +81,44 @@ def get_clib_path(env): return libpath -def clib_extension(os_name=None): +def clib_name(os_name=None, is_64bit=None): """ - Return the extension for the shared library for the current OS. + Return the name of GMT's shared library for the current OS. - .. warning:: - - Currently only works for macOS and Linux. - - Returns - ------- + Parameters + ---------- os_name : str or None The operating system name as given by ``sys.platform`` (the default if None). + is_64bit : bool or None + Whether or not the OS is 64bit. Only used if the OS is ``win32``. If None, will + determine automatically. Returns ------- - ext : str - The extension ('.so', '.dylib', etc). + libname : str + The name of GMT's shared library. """ if os_name is None: os_name = sys.platform - # Set the shared library extension in a platform independent way + + if is_64bit is None: + is_64bit = sys.maxsize > 2 ** 32 + if os_name.startswith("linux"): - lib_ext = "so" + libname = "libgmt.so" elif os_name == "darwin": # Darwin is macOS - lib_ext = "dylib" + libname = "libgmt.dylib" + elif os_name == "win32": + if is_64bit: + libname = "gmt_w64.dll" + else: + libname = "gmt_w32.dll" else: raise GMTOSError('Operating system "{}" not supported.'.format(sys.platform)) - return lib_ext + return libname def check_libgmt(libgmt): diff --git a/pygmt/tests/test_clib.py b/pygmt/tests/test_clib.py index e663824e9df..f507f16d034 100644 --- a/pygmt/tests/test_clib.py +++ b/pygmt/tests/test_clib.py @@ -14,7 +14,7 @@ from .. import clib from ..clib.session import FAMILIES, VIAS -from ..clib.loading import clib_extension, load_libgmt, check_libgmt, get_clib_path +from ..clib.loading import clib_name, load_libgmt, check_libgmt, get_clib_path from ..clib.conversion import dataarray_to_matrix from ..exceptions import ( GMTCLibError, @@ -107,13 +107,15 @@ def test_check_libgmt(): check_libgmt(dict()) -def test_clib_extension(): - "Make sure we get the correct extension for different OS names" +def test_clib_name(): + "Make sure we get the correct library name for different OS names" for linux in ["linux", "linux2", "linux3"]: - assert clib_extension(linux) == "so" - assert clib_extension("darwin") == "dylib" + assert clib_name(linux) == "libgmt.so" + assert clib_name("darwin") == "libgmt.dylib" + assert clib_name("win32", is_64bit=True) == "gmt_w64.dll" + assert clib_name("win32", is_64bit=False) == "gmt_w32.dll" with pytest.raises(GMTOSError): - clib_extension("meh") + clib_name("meh") def test_getitem():