Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions xobjects/context_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ def build_kernels(
if _forbid_compile:
raise RuntimeError("Compilation is forbidden")

if os.environ.get('XOBJECTS_FORBID_COMPILE'):
raise RuntimeError(
"Compilation is forbidden by the environment variable "
"XOBJECTS_FORBID_COMPILE"
)

so_file = self.compile_kernel(
module_name,
kernel_descriptions,
Expand Down Expand Up @@ -376,8 +382,9 @@ def kernels_from_file(
)
out_kernels = {}
for pyname, kernel_desc in kernel_descriptions.items():
c_name = kernel_desc.c_name or pyname
out_kernels[pyname] = KernelCpu(
function=getattr(module.lib, kernel_desc.c_name),
function=getattr(module.lib, c_name),
description=kernel_desc,
ffi_interface=module.ffi,
context=self,
Expand Down Expand Up @@ -459,14 +466,15 @@ def compile_kernel(
return Path(output_file)
finally:
# Clean temp files
files_to_remove = [
module_name + ".c",
module_name + ".o",
]

for ff in files_to_remove:
if os.path.exists(ff):
os.remove(ff)
if 'XOBJECTS_KEEP_BUILD_FILES' not in os.environ:
files_to_remove = [
module_name + ".c",
module_name + ".o",
]

for ff in files_to_remove:
if os.path.exists(ff):
os.remove(ff)

def _build_sources(
self,
Expand Down
26 changes: 26 additions & 0 deletions xobjects/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,32 @@ def compile_class_kernels(
extra_classes=(),
extra_compile_args=(),
):

if context.allow_prebuilt_kernels:
_print_state = Print.suppress
Print.suppress = True
try:
from xsuite import (
get_suitable_kernel,
XSK_PREBUILT_KERNELS_LOCATION,
)
kernel_info = get_suitable_kernel(
config={},
tracker_element_classes=[],
classes=list(extra_classes) + [cls],
)
except ImportError:
kernel_info = None

Print.suppress = _print_state
if kernel_info:
kernels = context.kernels_from_file(
module_name=kernel_info['module_name'],
containing_dir=XSK_PREBUILT_KERNELS_LOCATION,
kernel_descriptions=cls._kernels,
)
context.kernels.update(kernels)

if only_if_needed:
all_found = True
for kk, kernel_description in cls._kernels.items():
Expand Down
8 changes: 8 additions & 0 deletions xobjects/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from functools import wraps
from typing import Callable, Iterable, Union
import os

import pytest

Expand Down Expand Up @@ -105,3 +106,10 @@ def wrapper(*args, **kwargs):
return wrapper

return decorator

def skip_if_forbid_compile():
if os.environ.get('XOBJECTS_FORBID_COMPILE'):
pytest.skip(
"Compilation is forbidden by the environment variable "
"XOBJECTS_FORBID_COMPILE"
)
Loading