Commit e6563038 authored by DavHau's avatar DavHau
Browse files

python3Packages.setuptools-rust: fix cross build for all `setuptools-rust` based packages

Prior to this change all python packages using `setuptools-rust` were broken when cross compiled as they built binaries for the build platform instead of for the host platform.

The setuptoolsRustBuildHook hook would have to be included as a native build input instead of the `setuptools-rust` package, but practically wasn't used anywhere.

This change makes it so that `setuptoolsRustBuildHook` becomes unnecessary, and instead `setuptools-rust` propagates a setup-hook directly setting up the build environment, similar to how cmake does it when included via nativeBuildInputs.

This change fixes cross on all packages using `setuptools-rust` as a build-system.
parent d08257ca
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@
  rustPlatform,
  cargo,
  rustc,
  setuptoolsRustBuildHook,
  setuptools-rust,
  openssl,
  Security ? null,
  isPyPy,
@@ -74,9 +74,12 @@ buildPythonPackage rec {

  cargoRoot = "src/rust";

  build-system = [
    setuptools-rust
  ];

  nativeBuildInputs = [
    rustPlatform.cargoSetupHook
    setuptoolsRustBuildHook
    cargo
    rustc
    pkg-config
+0 −14
Original line number Diff line number Diff line
@@ -441,20 +441,6 @@ in
    } ./setuptools-build-hook.sh
  ) { };

  setuptoolsRustBuildHook = callPackage (
    { makePythonHook, setuptools-rust }:
    makePythonHook {
      name = "setuptools-rust-setup-hook";
      propagatedBuildInputs = [ setuptools-rust ];
      substitutions = {
        pyLibDir = "${python}/lib/${python.libPrefix}";
        cargoBuildTarget = stdenv.hostPlatform.rust.rustcTargetSpec;
        cargoLinkerVar = stdenv.hostPlatform.rust.cargoEnvVarTarget;
        targetLinker = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
      };
    } ./setuptools-rust-hook.sh
  ) { };

  unittestCheckHook = callPackage (
    { makePythonHook }:
    makePythonHook {
+20 −1
Original line number Diff line number Diff line
{
  lib,
  stdenv,
  buildPythonPackage,
  fetchPypi,
  maturin,
@@ -11,8 +12,9 @@
  setuptools,
  setuptools-rust,
  setuptools-scm,
  replaceVars,
  targetPackages,
}:

buildPythonPackage rec {
  pname = "setuptools-rust";
  version = "1.12.0";
@@ -40,6 +42,23 @@ buildPythonPackage rec {

  doCheck = false;

  # integrate the setup hook to set up the build environment for cross compilation
  # this hook is automatically propagated to consumers using setuptools-rust as build-system
  #
  # Only include the setup hook if targetPackages.python3 is defined.
  # targetPackages.python3 is not always available, for example when including
  # setuptools-rust via buildInputs instead of nativeBuildInputs or building it directly.
  setupHook =
    if !(targetPackages ? python3) then
      null
    else
      replaceVars ./setuptools-rust-hook.sh {
        pyLibDir = "${targetPackages.python3}/lib/${targetPackages.python3.libPrefix}";
        cargoBuildTarget = stdenv.targetPlatform.rust.rustcTargetSpec;
        cargoLinkerVar = stdenv.targetPlatform.rust.cargoEnvVarTarget;
        targetLinker = "${targetPackages.stdenv.cc}/bin/${targetPackages.stdenv.cc.targetPrefix}cc";
      };

  passthru.tests = {
    pyo3 = maturin.tests.pyo3.override {
      format = "setuptools";
+1 −0
Original line number Diff line number Diff line
@@ -437,6 +437,7 @@ mapAliases {
  sentry-sdk_2 = throw "'sentry-sdk_2' has been renamed to/replaced by 'sentry-sdk'"; # Converted to throw 2025-10-29
  setuptools_dso = throw "'setuptools_dso' has been renamed to/replaced by 'setuptools-dso'"; # Converted to throw 2025-10-29
  setuptools_scm = throw "'setuptools_scm' has been renamed to/replaced by 'setuptools-scm'"; # Converted to throw 2025-10-29
  setuptoolsRustBuildHook = lib.warn "setuptoolsRustBuildHook is deprecated. Instead, include 'setuptools-rust' via 'build-system'" setuptools-rust; # added 2025-12-07
  setuptoolsTrial = throw "'setuptoolsTrial' has been renamed to/replaced by 'setuptools-trial'"; # Converted to throw 2025-10-29
  sharkiqpy = throw "'sharkiqpy' has been renamed to/replaced by 'sharkiq'"; # Converted to throw 2025-10-29
  shippai = throw "shippai has been removed because the upstream repository was archived in 2023"; # added 2025-07-09