Content-Length: 298897 | pFad | http://github.com/numpy/numpy/issues/28861

77 BUG: Cannot cross-compile Numpy to OpenHarmony OS (AArch64) platform. · Issue #28861 · numpy/numpy · GitHub
Skip to content

BUG: Cannot cross-compile Numpy to OpenHarmony OS (AArch64) platform. #28861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 tasks done
SSRVodka opened this issue Apr 29, 2025 · 5 comments
Closed
2 tasks done

BUG: Cannot cross-compile Numpy to OpenHarmony OS (AArch64) platform. #28861

SSRVodka opened this issue Apr 29, 2025 · 5 comments

Comments

@SSRVodka
Copy link

Problem: Cannot cross-compile Numpy to OpenHarmony OS (AArch64) platform.

Background

Hello! I currently need to cross-compile the numpy library to the OpenHarmony AArch64 platform. It uses a modified linux kernel and musl libc, so I can't just download the officially compiled binary.

But I'm having a lot of problems compiling it.

Checkings

Before trying to cross-compile numpy, I checked the following:

  • Build Python has been compiled manually;
  • Host Python (On OpenHarmony AArch64) has been successfully compiled and is running properly on the target platform;

Try 1: Use Crossenv + pip

I then proceeded to prepare the environment as follows:

BUILD_PYTHON_DIR=...	# My directories here
HOST_PYTHON_DIR=...

BUILD_PYTHON=${BUILD_PYTHON_DIR}/bin/python3
BUILD_PIP=${BUILD_PYTHON_DIR}/bin/pip3
HOST_PYTHON=${HOST_PYTHON_DIR}/bin/python3

# initialize cross env
$BUILD_PIP install crossenv
$BUILD_PYTHON -m crossenv \
	$HOST_PYTHON \
	crossenv_aarch64

# source it
. crossenv_aarch64/bin/activate

The log file for the above command is here: Log 1: Paste Bin.

In the crossenv environment I executed the following command:

(cross) build-pip install meson-python
(cross) pip install -v --no-build-isolation --no-binary :all: numpy

And the log file: Log 2: Paste Bin;

Looks like it worked, right? The binary suffix of the output indicates that the architecture is correct.

But this is problematic: readelf outputs the x86_64 architecture instead:

Image

This method didn't work, so I tried another.

Try 2: Use meson + Cross Compile

I then quit the crossenv environment, downloaded the numpy source code and tried again:

# At the root of the numpy repo
LD_LIBRARY_PATH=$BUILD_PYTHON_DIR/lib $BUILD_PYTHON ./vendored-meson/meson/meson.py setup --cross-file ./ohos-build.meson.ini build-ohos
cd build-ohos
LD_LIBRARY_PATH=$BUILD_PYTHON_DIR/lib $BUILD_PYTHON ../vendored-meson/meson/meson.py compile

I defined my own ohos-build.meson.ini configuration file as follows:

[constants]
ohos_sdk = '/home/xhw/Desktop/OH/oh-cross-toolchain/ohos-sdk/14'
toolchain = ohos_sdk / 'native/llvm/bin'
sysroot = ohos_sdk / 'native/sysroot'
host_cpu = 'aarch64'
host_arch = 'aarch64'
dist_root = '/home/xhw/Desktop/OH/oh-edu-python/dist.aarch64'
common_flags = ['--sysroot=' + sysroot, '--target=' + host_cpu + '-linux-ohos']
common_c_flags = ['-fPIC', '-D__MUSL__=1', '-I' + dist_root/'include', '-I' + dist_root/'include/lzma', '-I' + dist_root/'include/ncursesw', '-I' + dist_root/'include/readline', '-I' + dist_root/'ssl/include']
common_ld_flags = ['-fuse-ld=lld', '-L' + dist_root/'lib', '-L' + dist_root/'ssl/lib64', '-lpython3.11']

[built-in options]
c_args = common_flags + common_c_flags
cpp_args = common_flags + common_c_flags
c_link_args = common_ld_flags
cpp_link_args = common_ld_flags

[properties]
cmake_toolchain_file = ohos_sdk / 'native/build/cmake/ohos.toolchain.cmake'
sizeof_long_double = 8
longdouble_format = 'IEEE_DOUBLE_LE'

[binaries]
c = toolchain / 'clang'
cpp = toolchain / 'clang++'
python = '/home/xhw/Desktop/OH/oh-edu-python/build-python.dist/bin/python3'
as = toolchain / 'llvm-as'
ld = toolchain / 'ld.lld'
c_ld = ld
cpp_ld = ld
lld = toolchain / 'ld.lld'
strip = toolchain / 'llvm-strip'
ranlib = toolchain / 'llvm-ranlib'
objdump = toolchain / 'llvm-objdump'
objcopy = toolchain / 'llvm-objcopy'
readelf = toolchain / 'llvm-readelf'
nm = toolchain / 'llvm-nm'
ar = toolchain / 'llvm-ar'
profdata = toolchain / 'llvm-profdata'

[build_machine]
system = 'linux'
kernel = 'linux'
cpu_family = 'x86_64'
cpu = 'x86_64'
endian = 'little'

[host_machine]
system = 'ohos'
kernel = 'linux'
cpu_family = host_cpu
cpu = host_cpu
endian = 'little'

It still failed, the logs are as follows (it seems that x86_64 was incorrectly used as the target platform):

Log 3: Paste Bin;

It seems that vendored-meson can't have failed to deal with the cross-compilation case?

So how should I cross-compile numpy correctly? Thanks a lot!

@rgommers
Copy link
Member

Log 3: Paste Bin;

The cross-file approach is preferred I think. The cross build is correctly configured:

Build type: cross build
C compiler for the host machine: /home/xhw/Desktop/OH/oh-cross-toolchain/ohos-sdk/14/native/llvm/bin/clang (clang 15.0.4 "OHOS (dev) clang version 15.0.4 (llvm-project 60204fb5771d075ecf06026e222ce3079f4b9528)")
C linker for the host machine: /home/xhw/Desktop/OH/oh-cross-toolchain/ohos-sdk/14/native/llvm/bin/clang ld.lld 15.0.4
C++ compiler for the host machine: /home/xhw/Desktop/OH/oh-cross-toolchain/ohos-sdk/14/native/llvm/bin/clang++ (clang 15.0.4 "OHOS (dev) clang version 15.0.4 (llvm-project 60204fb5771d075ecf06026e222ce3079f4b9528)")
C++ linker for the host machine: /home/xhw/Desktop/OH/oh-cross-toolchain/ohos-sdk/14/native/llvm/bin/clang++ ld.lld 15.0.4
Cython compiler for the host machine: cython (cython 3.0.12)
C compiler for the build machine: cc (gcc 11.4.0 "cc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0")
C linker for the build machine: cc ld.bfd 2.38
C++ compiler for the build machine: c++ (gcc 11.4.0 "c++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0")
C++ linker for the build machine: c++ ld.bfd 2.38
Cython compiler for the build machine: cython (cython 3.0.12)
Build machine cpu family: x86_64
Build machine cpu: x86_64
Host machine cpu family: aarch64
Host machine cpu: aarch64
Target machine cpu family: aarch64
Target machine cpu: aarch64

The first failure:

[96/330] Linking target numpy/_core/_operand_flag_tests.cpython-311-x86_64-linux-gnu.so
FAILED: numpy/_core/_operand_flag_tests.cpython-311-x86_64-linux-gnu.so 
/home/xhw/Desktop/OH/oh-cross-toolchain/ohos-sdk/14/native/llvm/bin/clang  -o numpy/_core/_operand_flag_tests.cpython-311-x86_64-linux-gnu.so numpy/_core/_operand_flag_tests.cpython-311-x86_64-linux-gnu.so.p/src_umath__operand_flag_tests.c.o -L/home/xhw/Desktop/OH/oh-edu-python/dist.aarch64/lib -L/home/xhw/Desktop/OH/oh-edu-python/dist.aarch64/ssl/lib64 -Wl,--as-needed -Wl,--allow-shlib-undefined -fuse-ld=/home/xhw/Desktop/OH/oh-cross-toolchain/ohos-sdk/14/native/llvm/bin/ld.lld -shared -fPIC -Wl,--start-group -lm -fuse-ld=lld -lpython3.11 -Wl,--end-group
ld.lld: error: numpy/_core/_operand_flag_tests.cpython-311-x86_64-linux-gnu.so.p/src_umath__operand_flag_tests.c.o is incompatible with elf64-x86-64
ld.lld: error: /home/xhw/Desktop/OH/oh-edu-python/dist.aarch64/lib/libpython3.11.so is incompatible with elf64-x86-64
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)

The file extension for Python extension modules is .cpython-311-x86_64-linux-gnu.so, indicating that the wrong Python interpreter is targeted. That's unfortunately a known issue, it is annoying to work around: mesonbuild/meson-python#321 (comment)

I think the fix is to add python = to the host section of your cross file and set a bunch of the environment variables like Void Linux and conda-forge do with or a la crossenv. This ideally wouldn't be necessary, but Meson isn't yet smart enough to get this right. Conda-forge needs this as well:
https://github.com/conda-forge/numpy-feedstock/blob/6d45a10c2c16f7ea2d465369adda4ad187ae68ca/recipe/build.sh#L12-L13

The more robust but slower way of doing it is running the build as a native build under QEMU rather than as a cross build.

@SSRVodka
Copy link
Author

SSRVodka commented May 1, 2025

@rgommers Really, really, really thank you! The problem that has been bothering me for almost a week is finally solved! Following your solution, I changed the meson cross file (specifying python as cross-python in crossenv, and changing the linking options that were problematic before):

[constants]
ohos_sdk = '/home/xhw/Desktop/OH/oh-cross-toolchain/ohos-sdk/14'
toolchain = ohos_sdk / 'native/llvm/bin'
sysroot = ohos_sdk / 'native/sysroot'
host_cpu = 'aarch64'
host_arch = 'aarch64'
dist_root = '/home/xhw/Desktop/OH/oh-edu-python/dist.aarch64'
common_flags = ['--sysroot=' + sysroot, '--target=' + host_cpu + '-linux-ohos']
common_c_flags = ['-fPIC', '-D__MUSL__=1', '-I' + dist_root/'include', '-I' + dist_root/'include/lzma', '-I' + dist_root/'include/ncursesw', '-I' + dist_root/'include/readline', '-I' + dist_root/'ssl/include']
common_ld_flags = ['-fuse-ld=lld', '-L' + dist_root/'lib', '-L' + dist_root/'ssl/lib64', '-lpython3.11']

[built-in options]
c_args = common_flags + common_c_flags
cpp_args = common_flags + common_c_flags
c_link_args = common_flags + common_ld_flags
cpp_link_args = common_flags + common_ld_flags

[properties]
cmake_toolchain_file = ohos_sdk / 'native/build/cmake/ohos.toolchain.cmake'
sizeof_long_double = 8
longdouble_format = 'IEEE_DOUBLE_LE'

[binaries]
c = toolchain / 'clang'
cpp = toolchain / 'clang++'
python = '/home/xhw/Desktop/OH/oh-python-pkgs/crossenv_aarch64/cross/bin/python3'
cython = ''
cython3 = cython
as = toolchain / 'llvm-as'
ld = toolchain / 'ld.lld'
c_ld = ld
cpp_ld = ld
lld = toolchain / 'ld.lld'
strip = toolchain / 'llvm-strip'
ranlib = toolchain / 'llvm-ranlib'
objdump = toolchain / 'llvm-objdump'
objcopy = toolchain / 'llvm-objcopy'
readelf = toolchain / 'llvm-readelf'
nm = toolchain / 'llvm-nm'
ar = toolchain / 'llvm-ar'
profdata = toolchain / 'llvm-profdata'

[host_machine]
system = 'ohos'
kernel = 'linux'
cpu_family = host_cpu
cpu = host_cpu
endian = 'little'

and compiled with crossenv. I ran the script as follows:

$BUILD_PIP install crossenv
$BUILD_PYTHON -m crossenv \
	$HOST_PYTHON \
	crossenv_aarch64
. crossenv_aarch64/bin/activate

cd numpy
VENDORED_MESON=${p_DIR}/numpy/vendored-meson/meson/meson.py
python ${VENDORED_MESON} setup --reconfigure --prefix=${p_DIR}/dist --cross-file ../ohos-build.meson.ini build-ohos
cd build-ohos
python ${VENDORED_MESON} compile
python ${VENDORED_MESON} install

the problem was solved!

Image

@SSRVodka SSRVodka closed this as completed May 1, 2025
@rgommers
Copy link
Member

rgommers commented May 1, 2025

Great, glad you solved it and thanks for sharing the solution!

@mattip
Copy link
Member

mattip commented May 1, 2025

Worth sticking into a wiki somewhere?

@rgommers
Copy link
Member

rgommers commented May 1, 2025

Worth sticking into a wiki somewhere?

We have docs on cross-compilation, I'll make a quick update to that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/numpy/numpy/issues/28861

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy