How can I use python distutils to cross compile an extension module to a different architecture?

那年仲夏 提交于 2019-12-25 09:12:49

问题


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

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

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?


回答1:


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

Requires working build tool chain for armv7l.

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

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



来源:https://stackoverflow.com/questions/45479617/how-can-i-use-python-distutils-to-cross-compile-an-extension-module-to-a-differe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!