I am looking for a solution to add pre-compiler logic in a cython file.
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.
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.
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.
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?
Some examples of plausible syntax (that may or may-not exist) that is similar to syntax found in c or python:
using an
#ifdef
or similar statement#ifdef __WINAPI def foo(): print('bar win') #else def foo(): print('bar linux') #endif
using a python-like
with
blockwith ifdef('__WINAPI'): def foo(): print('bar win')
ending a function with a cython-like line-ending
def foo() ifdef('__WINAPI'): print('bar win') def foo() ifndef('__WINAPI'): print('bar win')