cython_ifdef.py makes it possible to use preprocessor directives right in your .pyx source:
class X: #ifdef _WIN32 def windows_only(self): return 5 #else def unix_only(self): return 6 #endif
cython_ifdef.py test.pyx will produce a
test.c file that includes appropriate
#ifdef _WIN32 to produce a class that has
windows_only method when
_WIN32 is defined and
unix_only when it's not.
Here's an example of what
cython_ifdef.py can process: gevent/core_.pyx.
In addition to Cython, you need to have unifdef installed in order to run this script. On Debian/Ubuntu, do
apt-get install unifdef.
Download the script from github and put it somewhere on the run path.
How does it work
- It gets lists of all preprocessor symbols used in the source file using
unifdef -t -s
- It runs
unifdeffor all possible configurations on the original source.
- For each generated source, it runs
cythonand stores the result in memory.
- It then merges all the resulting .c files into one with the appropriate #ifdef in place.
- It only supports symbols that are either defined or undefined. Processing expressions such as
#if SYMBOL == 5is not implemented.
- It does not look into included .pxi.
- The amount of time it takes is exponential: 2^(Number of symbols), so it does not support arbitrary amount of preprocessor symbols.
It was written to support gevent's use case rather than a generic tool, so be warned.