| 1 |
import os |
| 2 |
import re |
| 3 |
import sys |
| 4 |
|
| 5 |
try: |
| 6 |
from Cython.Distutils import build_ext |
| 7 |
except ImportError: |
| 8 |
from setuptools.command.build_ext import build_ext |
| 9 |
|
| 10 |
from distutils.extension import Extension |
| 11 |
from distutils.sysconfig import get_config_var, get_config_vars, get_python_lib, get_python_version |
| 12 |
from pkg_resources import Distribution |
| 13 |
|
| 14 |
|
| 15 |
if sys.platform == 'darwin': |
| 16 |
config_vars = get_config_vars() |
| 17 |
config_vars['LDSHARED'] = config_vars['LDSHARED'].replace('-bundle', '') |
| 18 |
config_vars['SHLIB_EXT'] = '.so' |
| 19 |
|
| 20 |
|
| 21 |
def is_pip_install(): |
| 22 |
if "_" in os.environ and os.environ["_"].endswith("pip"): |
| 23 |
return True |
| 24 |
if "pip-egg-info" in sys.argv: |
| 25 |
return True |
| 26 |
if re.search("/pip-.*-build/", __file__): |
| 27 |
return True |
| 28 |
return False |
| 29 |
|
| 30 |
|
| 31 |
class CyExtension(Extension): |
| 32 |
def __init__(self, *args, **kwargs): |
| 33 |
self._init_func = kwargs.pop("init_func", None) |
| 34 |
super().__init__(*args, **kwargs) |
| 35 |
|
| 36 |
def extend_includes(self, includes): |
| 37 |
self.include_dirs.extend(includes) |
| 38 |
|
| 39 |
def extend_macros(self, macros): |
| 40 |
self.define_macros.extend(macros) |
| 41 |
|
| 42 |
def extend_extra_objects(self, objs): |
| 43 |
self.extra_objects.extend(objs) |
| 44 |
|
| 45 |
|
| 46 |
class cy_build_ext(build_ext): |
| 47 |
|
| 48 |
def _get_egg_name(self): |
| 49 |
ei_cmd = self.get_finalized_command("egg_info") |
| 50 |
return Distribution( |
| 51 |
None, None, ei_cmd.egg_name, ei_cmd.egg_version, get_python_version(), |
| 52 |
self.distribution.has_ext_modules() and self.plat_name).egg_name() |
| 53 |
|
| 54 |
def build_extension(self, ext): |
| 55 |
|
| 56 |
if isinstance(ext, CyExtension) and ext._init_func: |
| 57 |
ext._init_func(ext) |
| 58 |
|
| 59 |
if not self.inplace: |
| 60 |
ext.library_dirs.append(os.path.join(self.build_lib, "pysam")) |
| 61 |
|
| 62 |
if sys.platform == 'darwin': |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
relative_module_path = ext.name.replace(".", os.sep) + get_config_var('EXT_SUFFIX') |
| 69 |
library_path = os.path.join( |
| 70 |
"@rpath", os.path.basename(relative_module_path) |
| 71 |
) |
| 72 |
|
| 73 |
if not ext.extra_link_args: |
| 74 |
ext.extra_link_args = [] |
| 75 |
ext.extra_link_args += ['-dynamiclib', |
| 76 |
'-rpath', '@loader_path', |
| 77 |
'-Wl,-headerpad_max_install_names', |
| 78 |
'-Wl,-install_name,%s' % library_path, |
| 79 |
'-Wl,-x'] |
| 80 |
else: |
| 81 |
if not ext.extra_link_args: |
| 82 |
ext.extra_link_args = [] |
| 83 |
|
| 84 |
ext.extra_link_args += ['-Wl,-rpath,$ORIGIN'] |
| 85 |
|
| 86 |
super().build_extension(ext) |