Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 22 additions & 15 deletions pygmt/clib/loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand Down
14 changes: 8 additions & 6 deletions pygmt/tests/test_clib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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():
Expand Down