首页 文章

使用cython将项目从intel ubuntu交叉到arm

提问于
浏览
5

我在我的ubuntu 16 x86_64上有简单的python cython项目(来自http://docs.cython.org/src/tutorial/cython_tutorial.html的hello world示例) . 我可以使用cython for x86_64构建这个项目 .

如何在不使用真正的armv7板/ cpu的情况下为armv7版本的ubuntu 15构建项目?

我有 arm-linux-gnueabihf-gcchttp://packages.ubuntu.com/xenial/devel/gcc-arm-linux-gnueabihf),它可以为armv7编译简单的C程序 . 如何更改cython的设置以使用交叉编译器为arm构建共享对象?

1 回答

  • 5

    交叉编译需要架构相关的库和头文件 .

    测试时是否可以在 dpkg --add-architecture armhfapt-get update 之后安装python3.5-dev软件包和其他软件包(在对sources.list进行一些修改之后),结果基本上是 .

    python3.5-dev:armhf : Depends: python3.5:armhf (= 3.5.1-10) but it is not going to be installed
    

    apt-get install python3.5:armhf 是不起作用的东西,see

    现有的提议允许为不同的体系结构共同安装库和头文件,但不允许(尚未)二进制文件 .

    QEMU和chroot提供了一种不需要"full"虚拟机的可能解决方案 . 可以通过 debootstrap 命令创建适合chroot的目录 . 创建后, schroot 可以授予对该环境的访问权限 .

    在以下命令中替换 <DIRECTORY><USER>

    apt-get install -y debootstrap qemu-user-static binfmt-support schroot
    debootstrap --arch=armhf --foreign --include=gcc,g++,python3.5-dev xenial <DIRECTORY>
    cp /usr/bin/qemu-arm-static <DIRECTORY>/usr/bin
    chroot <DIRECTORY>
    /debootstrap/debootstrap --second-stage
    echo "deb http://ports.ubuntu.com/ubuntu-ports xenial universe" >> /etc/apt/sources.list
    echo "deb http://ports.ubuntu.com/ubuntu-ports xenial multiverse" >> /etc/apt/sources.list
    apt-get update
    apt-get install -y cython cython3
    exit
    cat <<END > /etc/schroot/chroot.d/xenial-armhf
    [xenial-armhf]
    description=Ubuntu xenial armhf
    type=directory
    directory=/home/xenial-armhf
    groups=sbuild,root
    root-groups=sbuild,root
    users=root,<USER>
    END
    

    应该可以访问环境

    schroot -c chroot:xenial-armhf
    

    对于root用户会话(用户必须在根组中列出的组中),

    schroot -c chroot:xenial-armhf -u root
    

    在此之后,还可以交叉编译cython模块:

    hello.pyx:

    print("hello world")
    

    编译(chroot中的 python3.5-config --cflagspython3.5-config --libs 选项,注意 -fPIC ):

    cython hello.pyx
    arm-linux-gnueabihf-gcc --sysroot <DIRECTORY> -I/usr/include/python3.5m -I/usr/include/python3.5m  -Wno-unused-result -Wsign-compare -g -fstack-protector-strong -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -c hello.c
    arm-linux-gnueabihf-gcc --shared --sysroot <DIRECTORY> -lpython3.5m -lpthread -ldl  -lutil -lm hello.o -o hello.so
    

    然后可以测试该模块

    schroot -c chroot:xenial-armhf
    python3
    import hello
    

    交叉编译基于cython的python模块也可以工作 . 使用setup.py

    from distutils.core import setup
    from distutils.extension import Extension
    from Cython.Distutils import build_ext
    
    import os
    
    os.environ['CC'] = 'arm-linux-gnueabihf-gcc'
    os.environ['LDSHARED'] = 'arm-linux-gnueabihf-gcc -shared'
    sysroot_args=['--sysroot', '/path/to/xenial-armhf']
    
    setup(cmdclass = {'build_ext': build_ext},
          ext_modules= [ Extension("hello", ["hello.pyx"],
                                    extra_compile_args=sysroot_args,
                                    extra_link_args=sysroot_args) ])
    

    以这种方式 Build 一个简单的 hello world 模块是可能的 . 模块的文件名错误,在这种情况下它是 hello.cpython-35m-x86_64-linux-gnu.so . 将其重命名为 hello.so 后,可以导入它 .

相关问题