micropython: fix cross

This commit is contained in:
Ben Siraphob
2025-09-30 15:19:30 +07:00
parent d31888f485
commit c1c39904a2
3 changed files with 89 additions and 1 deletions
@@ -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)
@@ -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"))
+45 -1
View File
@@ -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
'';