2016-07-15 77 views
3

私のubuntu 16 x86_64には単純なpython + cythonプロジェクト(hello worldの例はhttp://docs.cython.org/src/tutorial/cython_tutorial.html)があります。私はx86_64用のcythonでこのプロジェクトを構築できます。cythonを使用してintel ubuntuからarmにプロジェクトをコンパイルする

実際のarmv7 board/cpuを使用せずに、ubuntu 15のarmv7バージョンのプロジェクトをビルドするにはどうすればよいですか?

私はarm-linux-gnueabihf-gcchttp://packages.ubuntu.com/xenial/devel/gcc-arm-linux-gnueabihf)であり、armv7用の簡単なCプログラムをコンパイルできます。腕の共有オブジェクトを構築するためにクロスコンパイラを使用するようにcythonの設定を変更するにはどうすればよいですか?

+0

仮想マシンにUbuntu armhfをインストールする方が簡単かもしれません。 qemuはarmv7、https://wiki.ubuntu.com/Kernel/Dev/QemuARMVexpress –

+0

JJをサポートするはずです。完全仮想マシンをインストールせずにこれを行うことはできますか? – osgx

+0

armhfインストール(約200 MiB)を含むディレクトリを作成し、そのディレクトリ(またはschroot)にchrootすることができます。 [QEMU/Debootstrapアプローチ](https://wiki.debian.org/EmDebian/CrossDebootstrap#QEMU.2Fdebootstrap_approach) –

答えて

4

クロスコンパイルには、アーキテクチャに依存するライブラリとヘッダーファイルが必要です。

dpkg --add-architecture armhfapt-get updateの後にpython3.5-devパッケージなどをインストールできるかどうかをテストすると(source.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

、動作しないものです。

"フル"仮想マシンを必要としないソリューションの1つが、QEMUとchrootによって提供されています。 chrootに適したディレクトリは、debootstrapコマンドで作成できます。作成後、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 

環境は、(ユーザーがroot-に記載されているグループである必要があります

schroot -c chroot:xenial-armhf 

とrootユーザーセッションのためによりアクセス可能であるべきグループ)、

schroot -c chroot:xenial-armhf -u root 

これは、cythonモジュールをコンパイル交差することも可能である。

hello.pyx:

print("hello world") 

は(オプション、メモ-fPICためのchrootでpython3.5-config --cflagspython3.5-config --libs)をコンパイル:

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) ]) 

簡単なhello worldモジュールの構築が可能でした。モジュールのファイル名が間違っていました。この場合はhello.cpython-35m-x86_64-linux-gnu.soでした。hello.soという名前に変更した後、インポートすることができました。

+0

schrootコマンドとは何ですか? – osgx

+0

必要なarmhf debパッケージをサブディレクトリに展開しても、armhfバイナリを出力するエミュレートされていない(ネイティブCPUによって直接実行される)コンパイラを使用することはできますか?私の実際のプロジェクトは、hello worldよりも少し大きいです。メガバイトの "c"コードとたくさんのC++ヘッダーを持っています。エミュレートされたコンパイラは長い間実行されます。 – osgx

+0

@osgxこれは、[schroot](http://askubuntu.com/a/381527/493395)コマンド –

関連する問題