Skip to content

Commit

Permalink
hooks: add mkl (#2420)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelotduarte authored May 24, 2024
1 parent c814a2c commit 51c10e5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
38 changes: 38 additions & 0 deletions cx_Freeze/hooks/mkl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""A collection of functions which are triggered automatically by finder when
mkl package is included.
"""

from __future__ import annotations

from contextlib import suppress
from typing import TYPE_CHECKING

from cx_Freeze.exception import ModuleError
from cx_Freeze.module import DistributionCache

if TYPE_CHECKING:
from cx_Freeze.finder import ModuleFinder
from cx_Freeze.module import Module

# The sample/pandas is used to test using the following:
#
# - Ubuntu 22.04 w/ Python 3.10 and numpy+mkl
# pip install -i https://pypi.anaconda.org/intel/simple numpy
#
# - Windows w/ Python 3.9 to 3.12 using cgohlke/numpy-mkl-wheels
# Download: https://github.com/cgohlke/numpy-mkl-wheels/releases/


def load_mkl(finder: ModuleFinder, module: Module) -> None:
"""The mkl package."""
distribution = module.distribution
if distribution.installer == "pip":
for file in distribution.binary_files:
source = file.locate().resolve()
finder.include_files(source, f"lib/mkl/{source.name}")
for req_name in distribution.requires:
with suppress(ModuleError):
req_dist = DistributionCache(finder.cache_path, req_name)
for file in req_dist.binary_files:
source = file.locate().resolve()
finder.include_files(source, f"lib/mkl/{source.name}")
12 changes: 4 additions & 8 deletions cx_Freeze/hooks/numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from cx_Freeze._compat import IS_LINUX, IS_MACOS, IS_MINGW, IS_WINDOWS
from cx_Freeze.hooks._libs import replace_delvewheel_patch
from cx_Freeze.module import DistributionCache

if TYPE_CHECKING:
from cx_Freeze.finder import ModuleFinder
Expand Down Expand Up @@ -134,13 +133,10 @@ def load_numpy__distributor_init(finder: ModuleFinder, module: Module) -> None:
code_string = code_string.replace(
"path = ", "path = f'{sys.frozen_dir}\\lib\\mkl' # "
)
for req_name in distribution.requires:
req = DistributionCache(finder.cache_path, req_name)
for source in req.binary_files:
src = source.locate().resolve()
finder.include_files(
src, f"lib/mkl/{src.name}", copy_dependent_files=False
)
# create a fake module to activate mkl hook
mkl_path = finder.cache_path.joinpath("mkl")
mkl_path.touch()
finder.include_file_as_module(mkl_path)
exclude_dependent_files = True

elif distribution.installer == "conda":
Expand Down

0 comments on commit 51c10e5

Please sign in to comment.