From 537089dd153856bff4a5332c4e1eba7ccb94487a Mon Sep 17 00:00:00 2001 From: DavHau Date: Wed, 22 Oct 2025 11:14:51 +0700 Subject: [PATCH] python3Packages.orjson: fix build for riscv64 --- .../orjson/cross-arch-compat.patch | 44 +++++++++++++++++++ .../python-modules/orjson/default.nix | 5 +++ 2 files changed, 49 insertions(+) create mode 100644 pkgs/development/python-modules/orjson/cross-arch-compat.patch diff --git a/pkgs/development/python-modules/orjson/cross-arch-compat.patch b/pkgs/development/python-modules/orjson/cross-arch-compat.patch new file mode 100644 index 000000000000..9862a05bce76 --- /dev/null +++ b/pkgs/development/python-modules/orjson/cross-arch-compat.patch @@ -0,0 +1,44 @@ +From 8a6c16fd79e78aaaf1223090e673bdbe11451abc Mon Sep 17 00:00:00 2001 +From: DavHau +Date: Wed, 22 Oct 2025 10:40:03 +0700 +Subject: [PATCH] Fix cross-compilation by checking target arch in build.rs + +Previously, build.rs used #[cfg(target_arch)] attributes which check +the host architecture where the build script runs, not the target +being compiled for. This caused AVX-512 features to be incorrectly +enabled when cross-compiling from x86_64 to other architectures like +riscv64, leading to compilation errors. + +Now uses CARGO_CFG_TARGET_ARCH environment variable to correctly +detect the target architecture during cross-compilation. +--- + build.rs | 10 ++++++---- + 1 file changed, 6 insertions(+), 4 deletions(-) + +diff --git a/build.rs b/build.rs +index 42788623..28e006a9 100644 +--- a/build.rs ++++ b/build.rs +@@ -38,8 +38,11 @@ fn main() { + #[allow(unused_variables)] + let is_64_bit_python = matches!(python_config.pointer_width, Some(64)); + +- #[cfg(all(target_arch = "x86_64", not(target_os = "macos")))] +- if version_check::is_min_version("1.89.0").unwrap_or(false) && is_64_bit_python { ++ let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default(); ++ let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); ++ ++ if target_arch == "x86_64" && target_os != "macos" ++ && version_check::is_min_version("1.89.0").unwrap_or(false) && is_64_bit_python { + println!("cargo:rustc-cfg=feature=\"avx512\""); + } + +@@ -51,8 +54,7 @@ fn main() { + println!("cargo:rustc-cfg=feature=\"optimize\""); + } + +- #[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))] +- if is_64_bit_python { ++ if (target_arch == "x86_64" || target_arch == "aarch64") && is_64_bit_python { + println!("cargo:rustc-cfg=feature=\"inline_int\""); + } diff --git a/pkgs/development/python-modules/orjson/default.nix b/pkgs/development/python-modules/orjson/default.nix index 2525586c7f53..9111239fe8d6 100644 --- a/pkgs/development/python-modules/orjson/default.nix +++ b/pkgs/development/python-modules/orjson/default.nix @@ -42,6 +42,11 @@ buildPythonPackage rec { hash = "sha256-oTrmDYmUHXMKxgxzBIStw7nnWXcyH9ir0ohnbX4bdjU="; }; + patches = lib.optionals (stdenv.buildPlatform != stdenv.hostPlatform) [ + # fix architecture checks in build.rs to fix build for riscv + ./cross-arch-compat.patch + ]; + cargoDeps = rustPlatform.fetchCargoVendor { inherit pname version src; hash = "sha256-y6FmK1RR1DAswVoTlnl19CmoYXAco1dY7lpV/KTypzE=";