Cython conditional compilation based on external value given via `setuptools`

浪尽此生 提交于 2019-12-19 04:05:09

问题


I try to conditionally generate C code from a Cython pyx file. I found in the Cython documentation that I can use DEF to define a value and IF to conditionally generate code based on a defined value, but how can I set the value from the setup.py via Extension from setuptools.

Thank You


回答1:


Thank you for the link.

The interesting flag in the setup.py is cython_compile_time_env. And to import the Extension from Cython.

from setuptools import setup
from Cython.Distutils.extension import Extension

ext = Extension(
    name,
    include_dirs=include_dirs,
    cython_compile_time_env=dict(OPENMP=True),
    sources=['test.pyx'])

setup(name=name,
      cmdclass=dict(build_ext=build_ext),
      ext_modules=[ext])

And in the test.pyx:

...
IF OPENMP:
#Do openmp
ELSE:
#No openmp
...

Cython conditional statements (IF...ELSE above) are documented here.



来源:https://stackoverflow.com/questions/27273302/cython-conditional-compilation-based-on-external-value-given-via-setuptools

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