From c1c39904a2d678d7dccb71d2056d79025f1bcbfd Mon Sep 17 00:00:00 2001 From: Ben Siraphob Date: Wed, 14 May 2025 23:53:18 -0400 Subject: [PATCH] micropython: fix cross --- .../micropython/fix-cross-compilation.patch | 31 +++++++++++++ .../mi/micropython/fix-mpy-cross-path.patch | 13 ++++++ pkgs/by-name/mi/micropython/package.nix | 46 ++++++++++++++++++- 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 pkgs/by-name/mi/micropython/fix-cross-compilation.patch create mode 100644 pkgs/by-name/mi/micropython/fix-mpy-cross-path.patch diff --git a/pkgs/by-name/mi/micropython/fix-cross-compilation.patch b/pkgs/by-name/mi/micropython/fix-cross-compilation.patch new file mode 100644 index 000000000000..cfbae1de1c3c --- /dev/null +++ b/pkgs/by-name/mi/micropython/fix-cross-compilation.patch @@ -0,0 +1,31 @@ +--- a/ports/unix/Makefile ++++ b/ports/unix/Makefile +@@ -31,7 +31,7 @@ + QSTR_GLOBAL_DEPENDENCIES += $(VARIANT_DIR)/mpconfigvariant.h + + # OS name, for simple autoconfig +-UNAME_S := $(shell uname -s) ++UNAME_S := @UNAME_S@ + + # include py core make definitions + include $(TOP)/py/py.mk +@@ -151,7 +151,7 @@ + # If the variant enables it, enable modbluetooth. + ifeq ($(MICROPY_PY_BLUETOOTH),1) + ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1) +-HAVE_LIBUSB := $(shell (which pkg-config > /dev/null && pkg-config --exists libusb-1.0) 2>/dev/null && echo '1') ++HAVE_LIBUSB := $(shell (which @PKG_CONFIG@ > /dev/null && @PKG_CONFIG@ --exists libusb-1.0) 2>/dev/null && echo '1') + + # Figure out which BTstack transport to use. + ifeq ($(HAVE_LIBUSB),1) +@@ -180,8 +180,8 @@ + endif + else + # Use system version of libffi. +-LIBFFI_CFLAGS := $(shell pkg-config --cflags libffi) +-LIBFFI_LDFLAGS := $(shell pkg-config --libs libffi) ++LIBFFI_CFLAGS := $(shell @PKG_CONFIG@ --cflags libffi) ++LIBFFI_LDFLAGS := $(shell @PKG_CONFIG@ --libs libffi) + endif + + ifeq ($(UNAME_S),Linux) diff --git a/pkgs/by-name/mi/micropython/fix-mpy-cross-path.patch b/pkgs/by-name/mi/micropython/fix-mpy-cross-path.patch new file mode 100644 index 000000000000..a69b021c7062 --- /dev/null +++ b/pkgs/by-name/mi/micropython/fix-mpy-cross-path.patch @@ -0,0 +1,13 @@ +--- a/mpy-cross/mpy_cross/__init__.py ++++ b/mpy-cross/mpy_cross/__init__.py +@@ -61,6 +61,10 @@ + def _find_mpy_cross_binary(mpy_cross): + if mpy_cross: + return mpy_cross ++ # Check for MPY_CROSS environment variable first (for cross-compilation) ++ env_mpy_cross = os.environ.get("MPY_CROSS") ++ if env_mpy_cross: ++ return env_mpy_cross + return os.path.abspath(os.path.join(os.path.dirname(__file__), "../build/mpy-cross")) + + diff --git a/pkgs/by-name/mi/micropython/package.nix b/pkgs/by-name/mi/micropython/package.nix index efb64c675551..dadd165c0785 100644 --- a/pkgs/by-name/mi/micropython/package.nix +++ b/pkgs/by-name/mi/micropython/package.nix @@ -7,6 +7,7 @@ python3, libffi, readline, + buildPackages, }: stdenv.mkDerivation rec { @@ -43,6 +44,33 @@ stdenv.mkDerivation rec { extraPrefix = "lib/mbedtls/"; hash = "sha256-Sllp/iWWEhykMJ3HALw5KzR4ta22120Jcl51JZCkZE0="; }) + ./fix-cross-compilation.patch + ./fix-mpy-cross-path.patch + ]; + + postPatch = '' + # Fix cross-compilation by replacing uname and pkg-config + substituteInPlace ports/unix/Makefile \ + --subst-var-by UNAME_S "${ + { + "x86_64-linux" = "Linux"; + "i686-linux" = "Linux"; + "aarch64-linux" = "Linux"; + "armv7l-linux" = "Linux"; + "armv6l-linux" = "Linux"; + "riscv64-linux" = "Linux"; + "powerpc64le-linux" = "Linux"; + "x86_64-darwin" = "Darwin"; + "aarch64-darwin" = "Darwin"; + } + .${stdenv.hostPlatform.system} or stdenv.hostPlatform.parsed.kernel.name + }" \ + --subst-var-by PKG_CONFIG "${stdenv.cc.targetPrefix}pkg-config" + ''; + + depsBuildBuild = [ + buildPackages.stdenv.cc + buildPackages.python3 ]; nativeBuildInputs = [ @@ -58,8 +86,25 @@ stdenv.mkDerivation rec { makeFlags = [ "-C" "ports/unix" + "CROSS_COMPILE=${stdenv.cc.targetPrefix}" + ] + ++ lib.optionals (stdenv.hostPlatform.isAarch64 && stdenv.hostPlatform.isLinux) [ + # Workaround for false positive gcc warning in mbedtls on aarch64 + "CFLAGS_EXTRA=-Wno-array-bounds" ]; # also builds mpy-cross + # Build mpy-cross for the build platform first when cross-compiling + preBuild = '' + # Build mpy-cross for the build platform + make -C mpy-cross \ + CC="${buildPackages.stdenv.cc}/bin/${buildPackages.stdenv.cc.targetPrefix}cc" \ + CROSS_COMPILE="" + '' + + lib.optionalString (stdenv.buildPlatform != stdenv.hostPlatform) '' + # Set MPY_CROSS environment variable for cross-compilation + export MPY_CROSS="$PWD/mpy-cross/build/mpy-cross" + ''; + enableParallelBuilding = true; doCheck = true; @@ -87,7 +132,6 @@ stdenv.mkDerivation rec { runHook preInstall mkdir -p $out/bin install -Dm755 ports/unix/build-standard/micropython -t $out/bin - install -Dm755 mpy-cross/build/mpy-cross -t $out/bin runHook postInstall '';