Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vovaf709 committed Dec 18, 2023
1 parent 8f6d996 commit 1845c8d
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions _build_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
from setuptools.command.build_py import build_py


class NumpyImport(dict):
class LazyImport(dict):
"""Hacky way to return Numpy's `include` path with lazy import."""

# Must be json-serializable due to
# https://github.com/cython/cython/blob/6ad6ca0e9e7d030354b7fe7d7b56c3f6e6a4bc23/Cython/Compiler/ModuleNode.py#L773
def __init__(self):
def __init__(self, module_name):
self.module_name = module_name
return super().__init__(self, description=self.__doc__)

# Must be hashable due to
Expand All @@ -20,9 +21,10 @@ def __hash__(self):
return id(self)

def __repr__(self):
import numpy as np
scope = {}
exec(f'import {self.module_name} as module', scope)

return np.get_include()
return scope['module'].get_include()

__fspath__ = __repr__

Expand Down Expand Up @@ -53,8 +55,6 @@ def initialize_options(self):


def get_ext_modules():
from pybind11.setup_helpers import Pybind11Extension

name = 'imops'
on_windows = platform.system() == 'Windows'
args = ['/openmp' if on_windows else '-fopenmp']
Expand All @@ -73,7 +73,7 @@ def get_ext_modules():
Extension(
f'{name}.src._{prefix}{module}',
[f'{name}/src/_{prefix}{module}.pyx'],
include_dirs=[NumpyImport()],
include_dirs=[LazyImport('numpy')],
library_dirs=[NumpyLibImport()] if module in modules_to_link_against_numpy_core_math_lib else [],
libraries=['npymath'] + ['m'] * (not on_windows)
if module in modules_to_link_against_numpy_core_math_lib
Expand All @@ -88,10 +88,12 @@ def get_ext_modules():
# for prefix, additional_args in zip(['', 'fast_'], [[], ['-ffast-math']])
for prefix, additional_args in zip(['', 'fast_'], [[], []])
] + [
Pybind11Extension(
Extension(
'cpp_modules',
[f'{name}/cpp/src/main.cpp'], # Sort source files for reproducibility
include_dirs=[LazyImport('pybind11')],
extra_compile_args=args + cpp_args,
extra_link_args=args + cpp_args,
language='c++',
)
]

0 comments on commit 1845c8d

Please sign in to comment.