Elegant cross-platform cython module compilation

Viewed 297

I am writing a library which has several modules written in Cython and makes heavy use of parallelism using the openmp module. The library can be used on any platform - Unix/Mac/Windows.

Because of this, I have to add compiler flags to the module definition, so it's compiled against openmp. For linux and mac only, I did something like this:

Extension('my.module', ['my/module.c'],
          extra_compile_args=['-fopenmp', '-O3'],
          extra_link_args=['-fopenmp', '-O3'],
          ),

However, this doesn't work on Windows, since msvcc has a different format for the directives i.e. -fopenmp is replaced by /openmp and -O3 is replaced by /O3. My current solution is the following:

IS_WINDOWS = 'win' in sys.platform
openmp_opt = '/openmp' if IS_WINDOWS else '-fopenmp'
optim_opt = '/O3' if IS_WINDOWS else '-O3'

Extension('my.module', ['my/module.c'],
          extra_compile_args=[openmp_opt, optim_opt],
          extra_link_args=[openmp_opt, optim_opt],
          ),

This works, as best as I'm able to test it. It has one problem: msvcc doesn't need extra_link_args, but gcc does. So when compiling on windows, I get linker warnings that /O3 and /openmp are recognized.

My question is this: Is there a better way of doing this, built into cython that I haven't found and also, is there a case I've missed i.e. are there other compilers that I need to worry about or is the windows check good enough?

0 Answers
Related