Cython预编译器决策 [英] Cython Precompiler decision making

查看:127
本文介绍了Cython预编译器决策的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在cython文件中添加预编译器逻辑的解决方案。

I am looking for a solution to add pre-compiler logic in a cython file.

我为C ++中的硬件设备API编写了cython包装器。这是一个cython项目,通常使用MSVC针对python 2.7和3.6进行编译。整个程序包都是用cython编写的,不需要外部c ++或头文件。

I have written a cython wrapper for a hardware device API that is in C++. This is a cython project that is typically compiled using MSVC for python 2.7 and 3.6. The entire package is written in cython without need for an external c++ or header file.

最初,我已经将此软件编写为在Windows机器上使用,所以我使用了许多基本的winapi函数可帮助访问内核事件循环和winapi错误消息。它工作得很好,但是我还想添加跨平台编译的功能。这要求我完全替换几个关键功能,以使其在Linux机器上工作。例如,取决于操作系统,硬件API甚至具有不同的事件处理功能。另外,需要替换winapi事件处理。

Initially, I have written this software for use on a windows machine, so I have used a number of base winapi functions to help access a kernel event loop and winapi error messages. It works very well, but I would like to also add in functionality for cross-platform compilation. This requires me to completely replace several key functions to make it work on a linux machine. For example, the hardware API even has different event handling functions depending on the OS. Also, the winapi event handling would need to be replaced.

现在,我将整个项目编译到一个模块中,以简化导入。所有代码都驻留在编译为 hwmodule.pyd 文件的pyx文件中。但是,为了实现跨平台编译的目标,我需要在设置时将几个小pyx文件中的文件修补在一起。该解决方案不是优雅的并且难以维护。更不用说,这对培训可能想要添加到项目中的其他人来说更加困难。

Right now, I compile the whole project together into a single module to simplify import. All code resides in the same pyx file that compiles into a hwmodule.pyd file. However, to accomplish the goal of cross-platform compilation, I need to patch the file together from several small pyx files at setup time. This solution is not elegant and difficult to maintain. Not to mention, this is more difficult to train others who may want to add to the project.

理想情况下,我可以编写cython到c编译时标记,这些标记根据标志或变量进行解释和编译。 cython中有什么解决方案可以实现我的目标?或者,是否有其他组织可以做到这一点并且易于维护?

Ideally, I would be able to write cython to c compile time flags that get interpretted and compiled depending on flags or variables. Is there any solution in cython that can accomplish my goal? Alternatively, is there a different organization that would be elegant and easy to maintain for this?

一些合理的语法示例(可能存在或可能不存在)相似到在c或python中找到的语法:

Some examples of plausible syntax (that may or may-not exist) that is similar to syntax found in c or python:


  • 使用 #ifdef 或类似的语句

#ifdef __WINAPI
def foo():
    print('bar win')
#else
def foo():
    print('bar linux')
#endif


  • 使用类似python的

    with ifdef('__WINAPI'):
        def foo():
            print('bar win')
    


  • 使用像cython的行尾结束函数

  • ending a function with a cython-like line-ending

    def foo() ifdef('__WINAPI'):
        print('bar win')
    
    def foo() ifndef('__WINAPI'):
        print('bar win')
    


  • 推荐答案

    简短评论由评论扩展: s为 Cython支持的功能。它允许您定义编译时间常数

    A brief answer expanded from a comment: this is a feature that Cython supports. It allows you to define compile time constants

    DEF a = 5
    

    并根据这些常量有条件地包含代码:

    and conditionally include code depending on those constants:

    IF a==5:
        cdef f():
            return 1
    ELSE:
        cdef f():
            return 2
    

    它还定义了一些有用的常量: UNAME_SYSNAME 可让您在Windows,OS X和Linux之间进行选择。

    It also defines some helpful constants: UNAME_SYSNAME lets you pick between Windows, OS X and Linux for example.

    这些表达式在Cython在.pyx文件上运行时进行评估-因此,Windows和Linux上生成的C代码是不同的,因此,如果要在其他平台上进行编译,则需要重新编译运行Cython,而不仅仅是重新编译C文件。

    These expressions are evaluated at the point Cython is run on your .pyx file - therefore the generated C code is different on Windows vs Linux, and so if you want to compile on another platform you'll need to re-run Cython, rather than just recompiling the C files.

    没有直接等效的C #ifdef / #ifndef -测试是否定义了编译时间常数不是支持的功能。

    There is no direct equivalent of C #ifdef/#ifndef in Cython - testing whether a compile time constant is defined is not a supported feature.

    这篇关于Cython预编译器决策的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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