简体   繁体   中英

CrossCompilie python code for raspberry pi

I found issues on installing a cross compiler on my Ubuntu19.04 64 bit machine. I would like to cross compile python code into executable for my raspberry pi 3 model b+ running Debian Stretch. I followed many guides, none of them worked. I am actually following https://github.com/Yadoms/yadoms/wiki/Cross-compile-for-raspberry-PI

I followed step of the above written guide: - Setup Environment - Install cross compiler - Boost 1.64 - Python

On the last part (Python) it fails to execute the last instruction.

$ CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ AR=arm-linux-gnueabihf-ar RANLIB=arm-linux-gnueabihf-ranlib ./configure --host=arm-linux-gnueabihf --target=arm-linux-gnueabihf --build=x86_64-linux-gnu --prefix=$HOME/Desktop/rapsberry/depsBuild/python --disable-ipv6 ac_cv_file__dev_ptmx=no ac_cv_file__dev_ptc=no ac_cv_have_long_long_format=yes --enable-shared

output:

checking build system type... x86_64-pc-linux-gnu
checking host system type... arm-unknown-linux-gnueabihf
checking for python3.7... python3.7
checking for python interpreter for cross build... python3.7
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... checking for --without-gcc... no
checking for --with-icc... no
checking for arm-linux-gnueabihf-gcc... arm-linux-gnueabihf-gcc
checking whether the C compiler works... no
configure: error: in `/home/slr/Desktop/raspberry/boost_1_64_0/Python-3.7.5':
configure: error: C compiler cannot create executables
See `config.log' for more details

and then:

$ make HOSTPYTHON=$HOME/Desktop/raspberry/depsBuild/pythonhost/python HOSTPGEN=$HOME/Desktop/raspberry/depsBuild/pythonhost/Parser/pgen BLDSHARED="arm-linux-gnueabihf-gcc -shared" CROSS-COMPILE=arm-linux-gnueabihf- CROSS_COMPILE_TARGET=yes HOSTARCH=arm-linux BUILDARCH=arm-linux-gnueabihf 

output:

 make: *** No targets specified and no makefile found.  Stop.

I need python3 for my purpose.

I am really stuck in this problem, could someone have some idea? I tried also with QEMU and Docker ( https://raspberrypi.stackexchange.com/questions/109488/building-a-virtual-machine-with-the-img-file-of-the-raspberry-pi-stretch ) and both of them failed in compiling my target code: gcc: internal compiler error My code is pretty long ( Some thousand of lines ), while small codes successfully works. Thanks in advice.

There seems to be something wrong with the toolchain you're using, or with the way you're invoking the Python configure script.
Either way, it's impossible to debug without seeing your exact setup, so I'll start from scratch here.
I'm in the process of documenting a similar project here: Raspberry Pi C++ development .

Toolchain

The toolchains in the raspberrypi/tools repository are pretty old. I usually just build a new one using Crosstool-NG (which is what the raspberrypi/tools toolchains were built with as well, IIRC).
I used the armv8-rpi3-linux-gnueabihf sample .

You can of course build it yourself, but this can take quite some time, so you can also download the one I built from Docker Hub (see later).

You can find more information about how it was built here: Detailed information about configuring and building toolchains .

Compiling Python for the build system

In order to cross-compile a Python module, you need the same version of Python twice: once for your build system (the computer you're building on) and once for the host system (the Raspberry Pi you're building for).

Both will be compiled from source, to ensure that they are exactly the same version.

The build Python will just be used for cross-compiling the host Python, so we won't enable optimizations and optional modules.
This is the script I used: python-build.sh
You need OpenSSL, Zlib and libffi for pip to work. I built them from source as well, but you could of course install them using your package manager (you need the -dev versions).
Again, you can find the install scripts I used here .

Cross-compiling Python for the host system

Before you can cross-compile Python for the Raspberry Pi, you'll have to cross-compile its dependencies. A detailed explanation can be found here: Cross-compiling the dependencies .
You can find the scripts in the same folder on GitHub I linked to earlier, for instance, python.sh .
There are some caveats when cross-compiling, you need a pkg-config with the right prefix that looks for the required libraries in the sysroot of your cross-compilation, instead of in your build system's library folders. You also have to specify the include directories and library folders when calling the configure script.
All of this is handled by this Dockerfile and the scripts in the same folder.

Crossenv

The easiest way to cross-compile Python modules is to use Crossenv . The instructions can be found in the README of the GitHub page.

When everything is set up, you can run python setup.py bdist_wheel .

Example

As an example, you can follow these steps to compile a simple Python module using Cython:

1. Pull the toolchain and the cross-compiled Python from Docker hub

This is an image that contains the cross-compilation toolchain, native and cross-compiled Python, as well as Crossenv.

docker pull tttapa/rpi3-armv8-python-opencv-cross

2. Create the Python file to compile and setup.py to build it

Create these two files:

helloworld.py

print("Hello, World")

setup.py

from setuptools import setup
from Cython.Build import cythonize
from sys import version_info

setup(
    name='helloworld',
    version='0.0.1',
    ext_modules = cythonize("helloworld.py", language_level=version_info[0])
)

3. Create a script that builds the Python module

This script will be run inside of the Docker container.

build.sh

#!/usr/bin/env bash

set -e
cd /tmp/workdir
source "$HOME/crossenv/bin/activate"
build-pip install cython
cross-pip install wheel
python setup.py bdist_wheel

4. Start the Docker container and build the module

Run the following command in the same folder where you created the three files.

docker run --rm -v "$PWD:/tmp/workdir" \
    tttapa/rpi3-armv8-python-opencv-cross \
    bash "/tmp/workdir/build.sh"

When the build is complete, you'll find a file dist/helloworld-0.0.1-cp38-cp38-linux_arm.whl , which you can install using pip install helloworld-0.0.1-cp38-cp38-linux_arm.whl on the Raspberry Py. You'll also find the helloworld.c file generated by Cython.

To be able to run it, you'll probably have to install the cross-compiled libraries and Python itself to the RPi. You can do this by copying everything inside of ~/RPi-staging (inside of the Docker container) to the Pi's root folder / .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM