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

前端 未结 1 743
迷失自我
迷失自我 2021-01-06 08:43

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 c

相关标签:
1条回答
  • 2021-01-06 09:09

    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.

    0 讨论(0)
提交回复
热议问题