Skip to content

Commit

Permalink
Improve libpython detection (#394)
Browse files Browse the repository at this point in the history
See #392.
  • Loading branch information
ctrueden committed May 6, 2022
1 parent c5d55fb commit cfca63f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
38 changes: 29 additions & 9 deletions commands/python.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from distutils import sysconfig
from commands.util import is_osx
from commands.util import is_windows
from commands.util import is_bsd
import os
import sysconfig


def get_python_libs():
Expand Down Expand Up @@ -37,16 +37,36 @@ def get_python_lib_dir():
def get_libpython():
"""
Searches for the Python library, e.g. libpython<version>.so.
Primarily used for setting up LD_PRELOAD.
Used by setup.py to set PYTHON_LDLIBRARY, and by scripts to set up LD_PRELOAD.
"""
lib_python = os.path.join(sysconfig.get_config_var('LIBDIR'),
sysconfig.get_config_var('LDLIBRARY'))
libdir = sysconfig.get_config_var('LIBDIR')
ldlibrary = sysconfig.get_config_var('LDLIBRARY')
if libdir is None or ldlibrary is None:
return None

lib_python = os.path.join(libdir, ldlibrary)
if os.path.exists(lib_python):
return lib_python
else:
# x64 systems will tend to also have a MULTIARCH folder
lib_python = os.path.join(sysconfig.get_config_var('LIBDIR'),
sysconfig.get_config_var('MULTIARCH'),
sysconfig.get_config_var('LDLIBRARY'))

# x64 systems will tend to also have a MULTIARCH folder
multiarch = sysconfig.get_config_var('MULTIARCH')
if multiarch is not None:
lib_python = os.path.join(libdir, multiarch, ldlibrary)
if os.path.exists(lib_python):
return lib_python

# HACK: Non-existent static library is a known issue with conda-forge python;
# see: https://github.com/conda-forge/python-feedstock/issues/565
# Let's also look for a shared library in this case.
if ldlibrary.endswith('.a'):
ldshared = ldlibrary[:-1] + 'so'
lib_python = os.path.join(libdir, ldshared)
if os.path.exists(lib_python):
return lib_python
if multiarch is not None:
lib_python = os.path.join(libdir, multiarch, ldshared)
if os.path.exists(lib_python):
return lib_python

# give up
return None
10 changes: 8 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from commands.java import build_java, get_java_home, get_java_include,\
get_java_linker_args, build_jar, get_java_lib_folders, get_java_libraries, setup_java
from commands.javadoc import javadoc
from commands.python import get_python_libs, get_python_linker_args
from commands.python import get_libpython, get_python_libs, get_python_linker_args
from commands.scripts import build_scripts
from commands.test import test
from commands.util import is_windows
Expand Down Expand Up @@ -65,7 +65,13 @@ def read_file(name):
('JEP_NUMPY_ENABLED', numpy_found),
('VERSION', '"{0}"'.format(VERSION)),
]
ldlib = sysconfig.get_config_var('LDLIBRARY')
ldlib = get_libpython()
if ldlib:
# a libpython was found, so use the basename of the discovered path
ldlib = os.path.basename(ldlib)
else:
# no libpython was found, so use LDLIBRARY blindly
ldlib = sysconfig.get_config_var('LDLIBRARY')
if ldlib:
defines.append(('PYTHON_LDLIBRARY', '"' + ldlib + '"'))
if is_windows():
Expand Down

0 comments on commit cfca63f

Please sign in to comment.