如何使用python distutils将扩展模块交叉编译为其他体系结构? [英] How can I use python distutils to cross compile an extension module to a different architecture?

查看:316
本文介绍了如何使用python distutils将扩展模块交叉编译为其他体系结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Cython为我拥有的几个python模块生成已编译的.so文件。如中概述的那样Cython文档,您可以创建如下的setup.py文件:

I'm using Cython to generate compiled .so files for a couple of python modules I have. As outlined in the Cython documentation, you can create a setup.py file as follows:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    ext_modules = cythonize([
        'MyModule1.py',
        'MyModule2.py',
        'MyModule3.py'
    ])
)

然后构建使用命令 python3 setup.py build_ext --inplace 的模块。

and then build the modules using the command python3 setup.py build_ext --inplace.

这很好,但是会创建二进制文件,匹配主机的体系结构(在我的情况下为x86_64)。我想针对已经具有交叉编译和环境的其他体系结构(armv7l)。可以使用python distutils吗?

This works fine, however it creates binaries that match the architecture of the host machine (in my case x86_64). I would like to target a different architecture (armv7l) whose cross compile and environment I already have. Is it possible to do so with python distutils?

推荐答案

传入替代的三月和相关标志通过扩展上的 extra_compile_args

Pass in an alternative march and related flags via extra_compile_args on the extension:

sources = ['MyModule1.py',
           'MyModule2.py',
           'MyModule3.py']

ext_modules=cythonize(sources,
                      extra_compile_args=['-march=armv7l'],
                      library_dirs=[<arm v7 libraries>],
                      include_path=[<arm v7 includes>])

需要有效的armv7l构建工具链。

Requires working build tool chain for armv7l.

用于基于armv7l的Linux的Docker容器可能会更容易使用,并且会自动进行手臂构建。

Docker container for an armv7l based linux would probably be easier to use, though, and would automate the arm build.

A可以在脚本中运行docker容器构建,并为所需的所有体系结构和OS生成本地包。

As in can run the docker container build in a script and generate native packages for all architectures and OS that you want.

这篇关于如何使用python distutils将扩展模块交叉编译为其他体系结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆