Skip to content

Commit

Permalink
Maturin: fixes Windows cross compilation (#311)
Browse files Browse the repository at this point in the history
Make sure to set a recent Python interpreter (the current project one) to be sure a too old one is not picked by Maturin
  • Loading branch information
Tpt authored Jan 29, 2025
1 parent c4a129a commit 34ae3e9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
6 changes: 6 additions & 0 deletions .changelog/_unreleased.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ id = "69495c76-ca33-4089-87df-ee64b280c939"
type = "improvement"
description = "publish from CI"
author = "@NiklasRosenstein"

[[entries]]
id = "a46ae7eb-bfd4-4a1c-b8b3-e6d2ebc5e3a9"
type = "fix"
description = "Maturin: set always the venv interpreter on all compilations - fixes Windows cross-compilations"
author = "[email protected]"
29 changes: 12 additions & 17 deletions kraken-build/src/kraken/std/python/buildsystem/maturin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import shutil
import subprocess as sp
from collections.abc import Callable, Collection
from collections.abc import Collection
from dataclasses import dataclass
from itertools import chain
from pathlib import Path
Expand All @@ -17,7 +17,7 @@
from ...cargo.manifest import CargoMetadata
from ..pyproject import PyprojectHandler
from ..settings import PythonSettings
from . import ManagedEnvironment
from . import ManagedEnvironment, PythonBuildSystem
from .pdm import PDMManagedEnvironment, PDMPythonBuildSystem
from .poetry import PoetryManagedEnvironment, PoetryPyprojectHandler, PoetryPythonBuildSystem
from .uv import UvPythonBuildSystem
Expand Down Expand Up @@ -56,15 +56,9 @@ class MaturinZigTarget:


class _MaturinBuilder:
def __init__(
self,
entry_point: Collection[str],
get_pyproject_reader: Callable[[TomlFile], PyprojectHandler],
project_directory: Path,
) -> None:
def __init__(self, entry_point: Collection[str], build_system: PythonBuildSystem) -> None:
self._entry_point = entry_point
self._get_pyproject_reader = get_pyproject_reader
self._project_directory = project_directory
self._build_system = build_system
self._default_build = True
self._zig_targets: Collection[MaturinZigTarget] = []
self._build_env: dict[str, str] = {}
Expand All @@ -83,7 +77,7 @@ def add_build_environment_variable(self, key: str, value: str) -> None:

def build(self, output_directory: Path) -> list[Path]:
# We clean up target dir
metadata = CargoMetadata.read(self._project_directory)
metadata = CargoMetadata.read(self._build_system.project_directory)
dist_dir = metadata.target_directory / "wheels"
if dist_dir.exists():
shutil.rmtree(dist_dir)
Expand All @@ -93,7 +87,7 @@ def build(self, output_directory: Path) -> list[Path]:
if self._default_build:
command = [*self._entry_point, "maturin", "build", "--release"]
logger.info("%s", command)
sp.check_call(command, cwd=self._project_directory, env=build_env)
sp.check_call(command, cwd=self._build_system.project_directory, env=build_env)
for target in self._zig_targets:
command = [
*self._entry_point,
Expand All @@ -105,6 +99,8 @@ def build(self, output_directory: Path) -> list[Path]:
target.target,
"--features",
",".join(target.zig_features),
"--interpreter",
str(self._build_system.get_managed_environment().get_path() / "bin" / "python"),
]
if not target.manylinux:
command.append("--manylinux")
Expand All @@ -120,7 +116,7 @@ def build(self, output_directory: Path) -> list[Path]:
target_build_env["RUSTFLAGS"] = target.rustflags
if target.ld_library_path is not None:
target_build_env["LD_LIBRARY_PATH"] = target.ld_library_path
sp.check_call(command, cwd=self._project_directory, env=target_build_env)
sp.check_call(command, cwd=self._build_system.project_directory, env=target_build_env)

# We get the output files
src_files = list(dist_dir.iterdir())
Expand Down Expand Up @@ -175,7 +171,7 @@ class MaturinPoetryPythonBuildSystem(PoetryPythonBuildSystem):

def __init__(self, project_directory: Path) -> None:
super().__init__(project_directory)
self._builder = _MaturinBuilder(["poetry", "run"], self.get_pyproject_reader, self.project_directory)
self._builder = _MaturinBuilder(["poetry", "run"], self)

def disable_default_build(self) -> None:
self._builder.disable_default_build()
Expand Down Expand Up @@ -234,7 +230,7 @@ class MaturinPdmPythonBuildSystem(PDMPythonBuildSystem):

def __init__(self, project_directory: Path) -> None:
super().__init__(project_directory)
self._builder = _MaturinBuilder(["pdm", "run"], self.get_pyproject_reader, self.project_directory)
self._builder = _MaturinBuilder(["pdm", "run"], self)

def disable_default_build(self) -> None:
self._builder.disable_default_build()
Expand Down Expand Up @@ -277,8 +273,7 @@ def __init__(self, project_directory: Path) -> None:
# We use the build requirement to do custom Maturin builds
self._builder = _MaturinBuilder(
["uv", "tool", "run", *chain.from_iterable(("--with", r) for r in self._get_build_requirements())],
self.get_pyproject_reader,
self.project_directory,
self,
)

def disable_default_build(self) -> None:
Expand Down

0 comments on commit 34ae3e9

Please sign in to comment.