Skip to content

Commit 8698d43

Browse files
committed
Try except on dynamic linking error
1 parent 2da8d1b commit 8698d43

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

setup.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1818
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
# SOFTWARE.
20-
2120
import functools
2221
import os
2322
import shutil
2423
import subprocess
2524
import sys
2625
import tempfile
26+
from distutils.errors import CompileError
2727
from pathlib import Path
2828

2929
from setuptools import Extension, find_packages, setup
@@ -39,15 +39,31 @@ class IsalExtension(Extension):
3939

4040
class BuildIsalExt(build_ext):
4141
def build_extension(self, ext):
42-
if isinstance(ext, IsalExtension):
43-
_add_extension_options(ext)
42+
if not isinstance(ext, IsalExtension):
43+
super().build_extension(ext)
44+
return
4445
# Import cython here because it should be installed by setup requires.
4546
from Cython.Build import cythonize
4647
cythonized_module = cythonize(ext)[0]
4748
# _needs_stub is apparently not set elsewhere. It is not needed for
4849
# a functional isal extension.
4950
setattr(cythonized_module, "_needs_stub", False)
50-
super().build_extension(cythonized_module)
51+
possible_prefixes = [sys.exec_prefix, sys.base_exec_prefix]
52+
for prefix in possible_prefixes:
53+
if os.path.exists(os.path.join(prefix, "include", "isa-l")):
54+
ext.include_dirs = [os.path.join(prefix, "include")]
55+
ext.libraries = ["isal"]
56+
try:
57+
super().build_extension(cythonized_module)
58+
except CompileError: # Dynamic linking failed
59+
ext.libraries = [] # Make sure libraries are empty
60+
isa_l_prefix_dir = build_isa_l()
61+
ext.include_dirs = [os.path.join(isa_l_prefix_dir, "include")]
62+
# -fPIC needed for proper static linking
63+
ext.extra_compile_args = ["-fPIC"]
64+
ext.extra_objects = [
65+
os.path.join(isa_l_prefix_dir, "lib", "libisal.a")]
66+
super().build_extension(cythonized_module)
5167

5268

5369
# Use a cache to prevent isa-l from being build twice. According to the
@@ -78,25 +94,6 @@ def build_isa_l():
7894
return temp_prefix
7995

8096

81-
def _add_extension_options(ext: Extension):
82-
# Check current environment (conda or venv first) or base system install
83-
# (in case of venv) for isa-l include directories. Link dynamically.
84-
possible_prefixes = [sys.exec_prefix, sys.base_exec_prefix, "/usr"]
85-
for prefix in possible_prefixes:
86-
if os.path.exists(os.path.join(prefix, "include", "isa-l")):
87-
# Readthedocs uses a conda environment but does not activate it.
88-
ext.include_dirs = [os.path.join(prefix, "include")]
89-
ext.libraries = ["isal"]
90-
break
91-
else: # If not installed, build isa-l and link statically.
92-
isa_l_prefix_dir = build_isa_l()
93-
ext.include_dirs = [os.path.join(isa_l_prefix_dir, "include")]
94-
# -fPIC needed for proper static linking
95-
ext.extra_compile_args = ["-fPIC"]
96-
ext.extra_objects = [os.path.join(isa_l_prefix_dir, "lib", "libisal.a")
97-
]
98-
99-
10097
setup(
10198
name="isal",
10299
version="0.3.0-dev",

0 commit comments

Comments
 (0)