Skip to content

Commit

Permalink
hooks: fix numpy/mkl in conda (#2579)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelotduarte authored Sep 22, 2024
1 parent 33066aa commit 012ab9c
Showing 1 changed file with 9 additions and 37 deletions.
46 changes: 9 additions & 37 deletions cx_Freeze/hooks/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import sys
from importlib.machinery import EXTENSION_SUFFIXES
from pathlib import Path
from textwrap import dedent
from typing import TYPE_CHECKING

from cx_Freeze._compat import IS_LINUX, IS_MACOS, IS_MINGW, IS_WINDOWS
Expand Down Expand Up @@ -151,20 +150,22 @@ def load_numpy__distributor_init(finder: ModuleFinder, module: Module) -> None:
packages = ["libblas", "libcblas", "liblapack", "llvm-openmp"]
blas_options = ["libopenblas", "mkl"]
packages += blas_options
files_to_copy: list[Path] = []
blas = None
for package in packages:
try:
pkg = next(conda_meta.glob(f"{package}-*.json"))
except StopIteration:
continue
files = json.loads(pkg.read_text(encoding="utf_8"))["files"]
# copy mkl/blas files to lib (issue #2574)
if IS_WINDOWS:
files_to_copy += [
prefix / file
for file in files
if file.lower().endswith(".dll")
]
for file in files:
source = prefix.joinpath(file).resolve()
if not source.match("*.dll"):
continue
target = f"lib/{source.name}"
finder.include_files(
source, target, copy_dependent_files=False
)
else:
extensions = tuple(
[ext for ext in EXTENSION_SUFFIXES if ext != ".so"]
Expand All @@ -179,35 +180,6 @@ def load_numpy__distributor_init(finder: ModuleFinder, module: Module) -> None:
finder.include_files(
source, target, copy_dependent_files=False
)
blas = package
if IS_WINDOWS:
for source in files_to_copy:
finder.include_files(
source,
f"lib/{blas}/{source.name}",
copy_dependent_files=False,
)
exclude_dependent_files = True
code_string += dedent(
f"""
def _init_numpy_blas():
import os
blas_path = os.path.join(
os.path.dirname(os.path.dirname(__file__)), "{blas}"
)
try:
os.add_dll_directory(blas_path)
except (OSError, AttributeError):
pass
env_path = os.environ.get("PATH", "").split(os.pathsep)
if blas_path not in env_path:
env_path.insert(0, blas_path)
os.environ["PATH"] = os.pathsep.join(env_path)
_init_numpy_blas()
"""
)

# do not check dependencies already handled
if exclude_dependent_files:
Expand Down

0 comments on commit 012ab9c

Please sign in to comment.